PDA

View Full Version : Making hour hand and minute hand rotate(Clock)



nadeera
03-16-2017, 01:30 AM
I have created a triangle fan for the base of the clock and two quads for the hour hand and minute hand respectively. However, I am unable to make the quad rotate even after using translate, rotate and even timer. I don't get what is wrong. Can someone help me?


#include<windows.h>
#include<GL/glut.h>
#include<math.h>

#define PI 3.1415926535897932384626433832795

void drawFilledCircle(GLfloat x, GLfloat y, GLfloat radius)
{
int i;
int triangleAmount = 20; // # of triangles used to draw the circle

//GLfloat radius = 0.5f; //radius
GLfloat twicePi = 2.0f * PI;

glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0f,0.0f,0.0f); //Red
glVertex2f(x,y); //center of circle
for(i=0; i<= triangleAmount; i++)
{
glVertex2f(x+(radius* cos(i * twicePi/triangleAmount)), y+(radius*sin(i* twicePi/triangleAmount)));
}
glEnd();


glFlush(); //Render now - without it only empty screen comes out
}

void display(){

GLfloat hourAngle =0.0f;
glClearColor(0.0f,0.0f,0.0f,1.0f); //Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); //Clear the color buffer(background)

drawFilledCircle(0.0f,0.0f,0.8f);

//float hourAngle = -45;
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(250.0f, 250.0f, 0.0f);
glRotatef(hourAngle,1.0f,0.0f,0.0f);
glTranslatef(-250.0f,-250.0f, 0.0f);

glBegin(GL_QUADS); //hour hand
glColor3f(0.0f,1.0f,0.0f); //Green
glVertex2f(-0.1f,-0.1f); //x,y
glVertex2f(0.1f,-0.1f);
glVertex2f(0.1f,0.7f);
glVertex2f(-0.1f,0.7f);
glEnd();
glPopMatrix();

glMatrixMode(GL_MODELVIEW); //minute hand
glPushMatrix();
glTranslatef(250.0f, 250.0f, 0.0f);
glRotatef(hourAngle,1.0f,0.0f,0.0f);
glTranslatef(-250.0f,-250.0f, 0.0f);
glBegin(GL_QUADS); //Each set of 4 vertices form a quad
glColor3f(0.0f,0.0f,1.0f); //Blue
glVertex2f(-0.05f,-0.05f); //x,y
glVertex2f(0.05f,-0.05f);
glVertex2f(0.05f,0.6f);
glVertex2f(-0.05f,0.6f);
glEnd();
glPopMatrix();

glFlush(); //Render now - without it only empty screen comes out
}

void timerFunction(int value){
glutPostRedisplay();
glutTimerFunc(33, timerFunction,1);
}

/*Main function: GLUT runs as a console application starting at main() */
int main(int argc, char**argv){

glMatrixMode(GL_PROJECTION);//No effect
glOrtho(-1, 1, -1, 1, 1, -1);//on position of circle
glutInit(&argc, argv); //Initialize GLUT - without it ERROR: Function <glutCreateWindow> called first without calling 'glutInit'
glutCreateWindow("My Clock"); //Create a window with the given title
glutInitWindowSize(320,320); //Set the initial width & height
glutInitWindowPosition(250,250); //Position the window's initial width & height
glutDisplayFunc(display); //Register display callback handler for window re-paint
glutTimerFunc(33, timerFunction, 1);
glutMainLoop(); //Enter the event processing loop
return 0;
}