Feedjit

Articles for you

Saturday, June 22, 2013

Easiest Implementation of Analog Clock in Opengl C++ Source Code. Computer Graphics



  1. // GLclock.c
  2. // By Saqib Khan(International Islamic University Islamabad)
  3. // A openGL example program, displays a 3D clock face
  4. //
  5. // Keyboard inputs: [ESC] = quit
  6. // 'L' = enables/disables lighting
  7. // 'V' = toggle ortho/prespective view

  8. #include <GL/glut.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <time.h>

  13. // define glu objects


  14. GLUquadricObj *Cylinder;
  15. GLUquadricObj *Disk;

  16. struct tm *newtime;
  17. time_t ltime;

  18. GLfloat rx, ry, rz, angle;

  19. // lighting
  20. GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
  21. GLfloat LightDiffuse[]= { 0.5f, 0.5f, 0.5f, 1.0f };
  22. GLfloat LightPosition[]= { 5.0f, 25.0f, 15.0f, 1.0f };
  23. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };

  24. static int light_state = 1; // light on = 1, light off = 0
  25. static int view_state = 1; // Ortho view = 1, Perspective = 0


  26. void Sprint( int x, int y, char *st)
  27. {
  28.     int l,i;

  29.     l=strlen( st );
  30.     glRasterPos3i( x, y, -1);
  31.     for( i=0; i < l; i++)
  32.         {
  33.         glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]);
  34.     }

  35. }

  36. static void TimeEvent(int te)
  37. {
  38.     int timent;
  39.     int i;

  40.     rx = 30 * cos( angle );
  41.     ry = 30 * sin( angle );
  42.     rz = 30 * cos( angle );
  43.     angle += 0.01;
  44.     if (angle > M_TWOPI) angle = 0;

  45.     glutPostRedisplay();
  46.     glutTimerFunc( 100, TimeEvent, 1);
  47. }

  48. void init(void)
  49. {


  50.    glClearColor (0.0, 0.0, 0.0, 0.0);
  51.    glShadeModel (GL_SMOOTH);
  52.    glEnable(GL_DEPTH_TEST);
  53.    // Lighting is added to scene
  54.    glLightfv(GL_LIGHT1 ,GL_AMBIENT, LightAmbient);
  55.    glLightfv(GL_LIGHT1 ,GL_DIFFUSE, LightDiffuse);
  56.    glLightfv(GL_LIGHT1 ,GL_POSITION, LightPosition);
  57.    glEnable(GL_LIGHTING);
  58.    glEnable(GL_LIGHT1);


  59.    Cylinder = gluNewQuadric();
  60.    gluQuadricDrawStyle( Cylinder, GLU_FILL);
  61.    gluQuadricNormals( Cylinder, GLU_SMOOTH);
  62.    gluQuadricOrientation( Cylinder, GLU_OUTSIDE);
  63.    gluQuadricTexture( Cylinder, GL_TRUE);

  64.    Disk = gluNewQuadric();
  65.    gluQuadricDrawStyle( Disk, GLU_FILL);
  66.    gluQuadricNormals( Disk, GLU_SMOOTH);
  67.    gluQuadricOrientation( Disk, GLU_OUTSIDE);
  68.    gluQuadricTexture( Disk, GL_TRUE);


  69. }

  70. void Draw_gear( void )
  71. {

  72.     int i;
  73.     glPushMatrix();
  74.     gluCylinder(Cylinder, 2.5, 2.5, 1, 16, 16);
  75.     gluDisk(Disk, 0, 2.5, 32, 16);
  76.     glTranslatef(0,0,1);
  77.     gluDisk(Disk, 0, 2.5, 32, 16);
  78.     glPopMatrix();
  79.     for( i = 0; i < 8; i++)
  80.         {
  81.         glPushMatrix();
  82.         glTranslatef( 0.0, 0.0, 0.50);
  83.         glRotatef( (360/8) * i, 0.0, 0.0, 1.0);
  84.         glTranslatef( 3.0, 0.0, 0.0);
  85.         glutSolidCube( 1.0 );
  86.         glPopMatrix();
  87.         }


  88. }

  89. void Draw_clock( GLfloat cx, GLfloat cy, GLfloat cz )
  90. {

  91.   int hour_ticks , sec_ticks;
  92.   glPushMatrix();

  93.   glTranslatef(cx,cy,cz);
  94.   glRotatef( 180, 1.0, 0.0, 0.0);


  95.   glPushMatrix(); // Draw large wire cube
  96.   glColor3f(1.0, 1.0, 1.0);
  97.   glTranslatef( 0.0, 0.0, 6.0);
  98.   glutWireCube(14.0);
  99.   glPopMatrix();

  100.   glPushMatrix(); // Draw clock face
  101.   glTranslatef( 0, 0, 1.0);
  102.   gluDisk(Disk, 0, 6.75, 32, 16);

  103.   glPopMatrix();

  104.   glPushMatrix();// Draw hour hand
  105.   glColor3f(1.0, 0.5, 0.5);
  106.   glTranslatef( 0, 0, 0.0);
  107.   glRotatef( (360/12) * newtime->tm_hour  + (360/60) * (60 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
  108.   glPushMatrix();
  109.   glTranslatef(0.0, 0.0, 2.0);
  110.   Draw_gear();
  111.   glPopMatrix();
  112.   glRotatef( 90, 1.0, 0.0, 0.0);
  113.   gluCylinder(Cylinder, 0.75, 0, 4, 16, 16);
  114.   glPopMatrix();

  115.   glPushMatrix();// Draw minute hand
  116.   glColor3f(1.0, 0.5, 1.0);
  117.   glTranslatef( 0, 0, 0.0);
  118.   glRotatef( (360/60) * newtime->tm_min, 0.0, 0.0, 1.0);
  119.   glPushMatrix();
  120.   glTranslatef(0.0, 0.0, 3.0);
  121.   glScalef(0.5, 0.5, 1.0);
  122.   Draw_gear();
  123.   glPopMatrix();
  124.   glRotatef( 90, 1.0, 0.0, 0.0);
  125.   gluCylinder(Cylinder, 0.5, 0, 6, 16, 16);
  126.   glPopMatrix();

  127.   glPushMatrix();// Draw second hand
  128.   glColor3f(1.0, 0.0, 0.5);
  129.   glTranslatef( 0, 0, -0.0);
  130.   glRotatef( (360/60) * newtime->tm_sec, 0.0, 0.0, 1.0);
  131.   glPushMatrix();
  132.   glTranslatef(0.0, 0.0, 4.0);
  133.   glScalef(0.25, 0.25, 1.0);
  134.   Draw_gear();
  135.   glPopMatrix();
  136.   glRotatef( 90, 1.0, 0.0, 0.0);
  137.   gluCylinder(Cylinder, 0.25, 0, 6, 16, 16);
  138.   glPopMatrix();


  139.   for(hour_ticks = 0; hour_ticks < 12; hour_ticks++)
  140.       {
  141.       glPushMatrix();// Draw next arm axis.
  142.       glColor3f(0.0, 1.0, 1.0); // give it a color
  143.       glTranslatef(0.0, 0.0, 0.0);
  144.       glRotatef( (360/12) * hour_ticks, 0.0, 0.0, 1.0);
  145.       glTranslatef( 6.0, 0.0, 0.0);
  146.       glutSolidCube(1.0);

  147.       glPopMatrix();
  148.   }
  149.   for(sec_ticks = 0; sec_ticks < 60; sec_ticks++)
  150.      {
  151.        glPushMatrix();
  152.     glTranslatef(0.0, 0.0, 0.0);
  153.     glRotatef( (360/60) * sec_ticks, 0.0, 0.0, 1.0);
  154.     glTranslatef(6.0, 0.0, 0.0);
  155.     glutSolidCube(0.25);
  156.     glPopMatrix();
  157.     }


  158.   glPopMatrix();

  159. }


  160. void display(void)
  161. {

  162.   time(&ltime); // Get time
  163.   newtime = localtime(&ltime); // Convert to local time

  164.   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  165.   // easy way to put text on the screen.
  166.   glMatrixMode (GL_PROJECTION);
  167.   glLoadIdentity();
  168.   glOrtho(-8.0, 8.0, -8.0, 8.0, 1.0, 60.0);
  169.   glMatrixMode(GL_MODELVIEW);
  170.   glLoadIdentity();
  171.   glDisable(GL_LIGHTING);
  172.   glDisable(GL_COLOR_MATERIAL);

  173.   // Put view state on screen
  174.   glColor3f( 1.0, 1.0, 1.0);
  175.   if (view_state == 0)
  176.       {
  177.       Sprint(-3, -4, "Perspective view");
  178.       }else Sprint(-2, -4, "Ortho view");


  179.       Sprint(-4,-8, asctime(newtime));

  180.   // Turn Perspective mode on/off
  181.   if (view_state == 0)
  182.      {
  183.      glMatrixMode (GL_PROJECTION);
  184.      glLoadIdentity();

  185.      gluPerspective(60.0, 1, 1.0, 60.0);
  186.      glMatrixMode(GL_MODELVIEW);
  187.      glLoadIdentity();
  188.      gluLookAt( rx, 0.0, rz, 0.0, 0.0, -14.0, 0, 1, 0);
  189.      }

  190.     if (light_state == 1)
  191.       {
  192.        glEnable(GL_LIGHTING);
  193.        glEnable(GL_COLOR_MATERIAL);  // Enable for lighing
  194.       }else
  195.       {
  196.       glDisable(GL_LIGHTING);
  197.       glDisable(GL_COLOR_MATERIAL);  // Disable for no lighing
  198.   }

  199. Draw_clock( 0.0, 0.0, -14.0);



  200. glutSwapBuffers();
  201. }

  202. void reshape (int w, int h)
  203. {
  204.    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  205.    glMatrixMode (GL_PROJECTION);
  206.    glLoadIdentity ();
  207. }

  208. void keyboard (unsigned char key, int x, int y)
  209. {
  210.    switch (key)
  211.    {
  212.          case 'L':
  213.          light_state = abs(light_state - 1);
  214.          break;
  215.       case 'V':
  216.          view_state = abs(view_state - 1);
  217.          break;
  218.       case 27:
  219.          exit(0); // exit program when [ESC] key presseed
  220.          break;
  221.       default:
  222.          break;
  223.    }


  224. }

  225. int main(int argc, char** argv)
  226. {
  227.    glutInit(&argc, argv);
  228.    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
  229.    glutInitWindowSize (500, 500);
  230.    glutInitWindowPosition (10, 10);
  231.    glutCreateWindow (argv[0]);
  232.    glutSetWindowTitle("GLclock");
  233.    init ();
  234.    glutDisplayFunc(display);
  235.    glutReshapeFunc(reshape);
  236.    glutKeyboardFunc(keyboard);
  237.    glutTimerFunc( 10, TimeEvent, 1);
  238.    glutMainLoop();
  239.    return 0;
  240. }

Read More

Articles for you