PDA

View Full Version : I have designed a analog clock. How to run this clock using system time.



waqar ahmad
06-24-2013, 01:34 PM
How to implement timing function in it:



#include<iostream>
#include<windows.h>
#include<gl/GLU.h>
#include<gl/GL.h>
#include<glut.h>
#include<math.h>
#define PI 3.141592653589793

GLfloat secAngle=0.0;
GLfloat minAngle=0.0;
GLfloat hourAngle=0.0;
GLfloat hour=0.0;
GLfloat x1=0.0;
GLfloat y2=0.0;
GLfloat z1=0.0;
GLfloat radius = 10.0;
GLfloat smallradius = 3.0;
int angle;

void smallLines(GLfloat,GLfloat,GLfloat,GLfloat,GLfloat ,int);
void secondsLine(void);
void minutesLines(void);
void hourLine(void);
void centerpoint(void);
void cirlce(void);

void myInit(void)
{
glColor3f(1.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}

void DrawClock()
{
char buff[100];
glLoadIdentity();
glClearColor(1.0, 1.0, 1.0, 1.0);
glOrtho(0.0, 20.0, 0.0, 20.0, -10.0, 10.0);
glClear(GL_COLOR_BUFFER_BIT);
}

// ------------------------------- Circle -------------------------------------------

void cirlce()
{
glClearColor(1.0, 1.0, 1.0, 1.0);

glPushMatrix();
glTranslatef(10.0, 10.0, 10.0);
glColor3f(0.9,0.9,0.9);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(x1, y2, z1);
for(angle=0; angle <= 360; angle +=1)
glVertex3f(x1 + cos(angle * PI/180.0f)* radius, y2 + sin(angle * PI/180.0f) * radius, z1);
glEnd();
glPopMatrix();
}

// ------------------------------- Small Lines ---------------------------------------

void smallLines(GLfloat x1,GLfloat y2,GLfloat z1,GLfloat radius,GLfloat smallradiu,int angle)
{
glPushMatrix();
glTranslatef(10.0, 10.0, 0.0);
glColor3f(0.2,0.2,0.2);
glLineWidth(2);
for(angle=0; angle <= 360; angle +=30)
{
glBegin(GL_LINES);
if(angle == 0.0 || angle==90.0 || angle == 180.0 || angle == 270.0 || angle == 360.0 )
{
glColor3f(1.0,0.2,0.2);
glVertex3f(x1 + cos(angle * PI/180.0f)* (radius-1.2), y2 + sin(angle * PI/180.0f) * (radius-1.2), z1+0.1);
glVertex3f(x1 + cos(angle * PI/180.0f)* radius, y2 + sin(angle * PI/180.0f) * radius, z1+0.1);
}
else
{
glColor3f(0.2,0.2,0.2);
glVertex3f(x1 + cos(angle * PI/180.0f)* (radius-0.5), y2 + sin(angle * PI/180.0f) * (radius-0.5), z1);
glVertex3f(x1 + cos(angle * PI/180.0f)* radius, y2 + sin(angle * PI/180.0f) * radius, z1);
}
glEnd();
}
glPopMatrix();
}

// ------------------------------- Center Point ---------------------------------------

void centerpoint()
{
glColor3f(0.0,0.0,0.0);
glPointSize(8.0);

glPushMatrix();
glTranslatef(10.0, 10.0, 0.0);
glBegin(GL_POINTS);
glVertex3f(0.0,0.0,0.0);
glEnd();
glPopMatrix();
//glFlush();

}

// ------------------------------- Seconds Line ---------------------------------------

void secondsLine()
{
glColor3f(1.0,1.0,0.0);
glLineWidth(2.0);
glPushMatrix();
glTranslatef(10.0, 10.0, 0.0);
glRotatef(-secAngle,0.0,0.0,1.0);
glBegin(GL_LINES);
glVertex2f(0,0);
glVertex2f(7,7);
glEnd();
glPopMatrix();
}

// ------------------------------- Minutes Line ---------------------------------------

void minutesLines()
{
glColor3f(1.0,0.0,0.0);
glLineWidth(2.0);
glPushMatrix();

glTranslatef(10.0, 10.0, 0.0);
glRotatef(-minAngle, 0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 7.0, 1.0);
glEnd();
glPopMatrix();
}

// ------------------------------- Hour Line ------------------------------------------

void hourLine()
{
glColor3f(0.0,1.0,0.0);
glLineWidth(3.0);
glPushMatrix();

glTranslatef(10.0, 10.0, 0.0);
glRotatef(-hourAngle, 0.0, 0.0, 1.0);

glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 6.5, 0.0);
glEnd();
glPopMatrix();
}

// ------------------------------- Idel Function ---------------------------------------

void Idle(void)
{
secAngle += 0.1;
hourAngle += 0.01;
minAngle += 0.2;

cirlce();
smallLines(x1,y2,z1,radius,smallradius,angle);
centerpoint();
secondsLine();
minutesLines();
hourLine();
glFlush();
}

void main(int argc , char **argv)
{
glutInit(&argc , argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(200,300);
glutCreateWindow("Analog Clock");

glutDisplayFunc(DrawClock);
glutIdleFunc(Idle);
myInit();
glutMainLoop();
}

GClements
06-24-2013, 02:43 PM
How to implement timing function in it
Use glutTimerFunc() to register a timer callback. Have the timer callback call glutPostRedisplay(). Have the display callback read the current time using e.g. time() and localtime() and use the results to position the hands.

waqar ahmad
06-25-2013, 01:49 PM
if i will not use ( _CRT_SECURE_NO_WARNINGS) it gives error




#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include<iostream>
#include<windows.h>
#include<gl/GLU.h>
#include<gl/GL.h>
#include<glut.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#define PI 3.141592653589793

GLfloat secAngle=0.0;
GLfloat minAngle=0.0;
GLfloat hourAngle=0.0;
GLfloat hour=0.0;
GLfloat x1=0.0;
GLfloat y2=0.0;
GLfloat z1=0.0;
GLfloat radius = 10.0;
GLfloat smallradius = 3.0;
//int angle;

GLfloat rx, ry, rz, angle;

struct tm *newtime;
time_t ltime;

void smallLines(GLfloat,GLfloat,GLfloat,GLfloat,GLfloat ,int);
void secondsLine(void);
void minutesLines(void);
void hourLine(void);
void centerpoint(void);
void cirlce(void);

static void TimeEvent(int te)
{
//int timent;
//int i;

rx = 30 * cos( angle );
ry = 30 * sin( angle );
rz = 30 * cos( angle );
angle += 0.01;
//if (angle > M_TWOPI) angle = 0;

glutPostRedisplay();
glutTimerFunc( 100, TimeEvent, 1);
}

void myInit(void)
{
glColor3f(1.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}

void DrawClock()
{
time(&ltime); // Get time
newtime = localtime(&ltime); // Convert to local time

char buff[100];
glLoadIdentity();
glClearColor(1.0, 1.0, 1.0, 1.0);
glOrtho(0.0, 20.0, 0.0, 20.0, -10.0, 10.0);
glClear(GL_COLOR_BUFFER_BIT);
}

// ------------------------------- Circle -------------------------------------------

void cirlce()
{
glClearColor(1.0, 1.0, 1.0, 1.0);

glPushMatrix();
glTranslatef(10.0, 10.0, 10.0);
glColor3f(0.9,0.9,0.9);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(x1, y2, z1);
for(angle=0; angle <= 360; angle +=1)
glVertex3f(x1 + cos(angle * PI/180.0f)* radius, y2 + sin(angle * PI/180.0f) * radius, z1);
glEnd();
glPopMatrix();
//secondsLine();
}

// ------------------------------- Small Lines ---------------------------------------

void smallLines(GLfloat x1,GLfloat y2,GLfloat z1,GLfloat radius,GLfloat smallradiu,int angle)
{
glPushMatrix();
glTranslatef(10.0, 10.0, 0.0);
glColor3f(0.2,0.2,0.2);
glLineWidth(2);
for(angle=0; angle <= 360; angle +=30)
{
glBegin(GL_LINES);
if(angle == 0.0 || angle==90.0 || angle == 180.0 || angle == 270.0 || angle == 360.0 )
{
glColor3f(1.0,0.2,0.2);
glVertex3f(x1 + cos(angle * PI/180.0f)* (radius-1.2), y2 + sin(angle * PI/180.0f) * (radius-1.2), z1+0.1);
glVertex3f(x1 + cos(angle * PI/180.0f)* radius, y2 + sin(angle * PI/180.0f) * radius, z1+0.1);
}
else
{
glColor3f(0.2,0.2,0.2);
glVertex3f(x1 + cos(angle * PI/180.0f)* (radius-0.5), y2 + sin(angle * PI/180.0f) * (radius-0.5), z1);
glVertex3f(x1 + cos(angle * PI/180.0f)* radius, y2 + sin(angle * PI/180.0f) * radius, z1);
}
glEnd();
}
glPopMatrix();
}

// ------------------------------- Center Point ---------------------------------------

void centerpoint()
{
glColor3f(0.0,0.0,0.0);
glPointSize(8.0);

glPushMatrix();
glTranslatef(10.0, 10.0, 0.0);
glBegin(GL_POINTS);
glVertex3f(0.0,0.0,0.0);
glEnd();
glPopMatrix();
//glFlush();

}

// ------------------------------- Seconds Line ---------------------------------------

void secondsLine()
{
int sec_ticks;
secAngle += 0.1;
glColor3f(1.0,1.0,0.0);
glLineWidth(2.0);
glPushMatrix();
glTranslatef(10.0, 10.0, 0.0);
glRotatef((360/60) * newtime->tm_sec,0.0,0.0,1.0);
glBegin(GL_LINES);
glVertex2f(0,0);
glVertex2f(7,7);
glEnd();

for(sec_ticks = 0; sec_ticks < 60; sec_ticks++)
{
glPushMatrix();
glTranslatef(0.0, 0.0, 0.0);
glRotatef( (360/60) * sec_ticks, 0.0, 0.0, 1.0);
glTranslatef(6.0, 0.0, 0.0);
glutSolidCube(0.25);
glPopMatrix();
}

glPopMatrix();
glutPostRedisplay();
//glutTimerFunc(10000,secondsLine,0);

}
// ------------------------------- Minutes Line ---------------------------------------

void minutesLines()
{
glColor3f(1.0,0.0,0.0);
glLineWidth(2.0);
glPushMatrix();

glTranslatef(10.0, 10.0, 0.0);
glRotatef((360/60) * newtime->tm_min, 0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 7.0, 1.0);
glEnd();
glPopMatrix();
}

// ------------------------------- Hour Line ------------------------------------------

void hourLine()
{
int hour_ticks;

glColor3f(0.0,1.0,0.0);
glLineWidth(3.0);
glPushMatrix();

glTranslatef(10.0, 10.0, 0.0);
glRotatef((360/12) * newtime->tm_hour + (360/60) * (60 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);

glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 6.5, 0.0);
glEnd();


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

glPopMatrix();
}

glPopMatrix();
}

// ------------------------------- Idel Function ---------------------------------------

void Idle(void)
{
//secAngle += 0.1;
hourAngle += 0.01;
minAngle += 0.2;

cirlce();
smallLines(x1,y2,z1,radius,smallradius,angle);
centerpoint();
secondsLine();
minutesLines();
hourLine();
glFlush();
}
void main(int argc , char **argv)
{
glutInit(&argc , argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(200,300);
glutCreateWindow("Analog Clock");

glutDisplayFunc(DrawClock);
glutIdleFunc(Idle);
// glutTimerFunc( 10, TimeEvent, 1);
myInit();
glutMainLoop();
}