Creating an Animated Clock Using OpenGL in C++ Link to this heading

Introduction Link to this heading

Let’s take a look at how we can code a simple animated clock in OpenGL.

Initialization Link to this heading

In this clock program, we start by including the necessary libraries and setting up essential variables.

c++
 1#include <windows.h>
 2#include <GL/glut.h>
 3#include <math.h>
 4
 5float PI = 3.14;
 6
 7int animationFactor = 0;
 8int clockMinute=0;
 9
10double minuteHandRotation=0;
11double hourHandRotation =0;
12
13
14void init() {
15    glClearColor(0.2, 0.95, 0.6, 1.0);
16    glClear(GL_COLOR_BUFFER_BIT); //clearing color buffer
17}

Drawing Functions Link to this heading

Creating a visual representation of the clock involves several drawing functions. Let’s briefly cover the main components:

  • Drawing the Clock Disk: This function is responsible for rendering the circular face of the clock.
c++
 1void drawDisk(double radius, int n) {
 2    double angle = 0;
 3    glPushMatrix();
 4    glColor3f(1, 1, 1);
 5    glBegin(GL_POLYGON);
 6    for (int c = 0; c <= n; c++) {
 7        double x = radius * cos(angle);
 8        double y = radius * sin(angle);
 9        glVertex2d(x, y);
10        angle = angle + ((2 * PI) / n);
11    }
12    glEnd();
13    glPopMatrix();
14}
  • Drawing Clock Marks: To mark the hours, we draw lines that signify each hour on the clock face.
c++
 1void drawMarks(double radius){
 2    glPushMatrix();
 3    glColor3f(0, 0, 0);
 4    glBegin(GL_LINES);
 5    double angle=0;
 6    for (int c = 0; c <= 12; c++) {
 7        double x = radius * cos(angle);
 8        double y = radius * sin(angle);
 9
10        glVertex2d(0, 0);
11        glVertex2d(x, y);
12        angle = angle + ((2 * PI) / 12);
13    }
14    glEnd();
15    glPopMatrix();
16}
  • Drawing the Hour Hand: The hour hand is drawn and rotated according to the current time, moving every five minutes.
c++
 1void drawHourHand(double radius){
 2
 3    if(clockMinute==5){
 4        hourHandRotation+= (360 /  12);
 5        clockMinute=0;
 6    }
 7
 8    glPushMatrix();
 9    glColor3f(1, 0, 0);
10    glRotated(-1*hourHandRotation,0,0,1);
11    glBegin(GL_POLYGON);
12    glVertex2d(0, radius);
13    glVertex2d(radius*0.1, 0);
14    glVertex2d(-radius*0.1, 0);
15
16    glEnd();
17    glPopMatrix();
18
19}
  • Drawing the Minute Hand: Similarly, the minute hand is drawn and rotated, advancing every minute.
c++
 1
 2void drawMinuteHand(double radius){
 3    glPushMatrix();
 4    glColor3f(0.8, 0.5, 0);
 5    glRotated(-1*minuteHandRotation,0,0,1);
 6    glBegin(GL_POLYGON);
 7    glVertex2d(0, radius*0.5);
 8    glVertex2d(radius*0.05, 0);
 9    glVertex2d(-radius*0.05, 0);
10    glEnd();
11    glPopMatrix();
12
13    minuteHandRotation += (360 /  60);
14}
  • Drawing the Complete Clock: The final clock is assembled by combining the disk, marks, hour hand, and minute hand.
c++
1void drawClock(int radius){
2    drawDisk(radius,32);
3    drawMarks(radius);
4    drawHourHand(radius);
5    drawMinuteHand(radius);
6}

Reshaping and Display Functions Link to this heading

These important utility functions handle the window resizing and actual rendering of the clock. The reshaping function ensures that the clock maintains its aspect ratio, while the display function takes care of rendering the clock and axes.

c++
 1void reshape(GLsizei w, GLsizei h) {
 2    glViewport(0, 0, w, h);
 3    //calculate aspect ratio
 4    if (h == 0)
 5        h = 1;
 6    GLfloat aspect = (GLfloat)w / (GLfloat)h;
 7
 8    glMatrixMode(GL_PROJECTION);
 9    glLoadIdentity();
10    if (w >= h)
11        gluOrtho2D(-10 * aspect, 10 * aspect, -10, 10);
12    else
13        gluOrtho2D(-10, 10, -10 / aspect, 10 / aspect);
14}
15
16void display() {
17    glClear(GL_COLOR_BUFFER_BIT); // to avoid tracing 
18    glLineWidth(2.0); //Setting the line width
19
20    //y-axis
21    glColor3f(1.0, 0.0, 0.0);
22    glBegin(GL_LINES);
23    glVertex2f(0.0, 100.0);
24    glVertex2f(0.0, -100.0);
25    glEnd();
26
27    //x-axis
28    glColor3f(1.0, 0.0, 0.0);
29    glBegin(GL_LINES);
30    glVertex2f(100.0, 0.0);
31    glVertex2f(-100.0, 0.0);
32    glEnd();
33
34    drawClock();
35
36    glutSwapBuffers(); //swapping the buffers
37
38}

Animation Link to this heading

The animation function is a crucial part of this project. It updates the clock’s minute and hour hands in real-time, creating the illusion of a working clock.

Conclusion Link to this heading

Creating an animated clock using OpenGL in C++ is not only a fun project but also a great way to explore the principles of computer graphics and animation.