OpenGL Sample Code - A 3D Analog Clock Luke Yulu Chen

Download Report

Transcript OpenGL Sample Code - A 3D Analog Clock Luke Yulu Chen

OpenGL Sample Code - A 3D Analog Clock

Luke Yulu Chen

How to Program a 3D Analog Clock

Get System Time

Convert Time to Rotation Angle

Draw Hands for Hour, Minute and Second

Make Hands Rotate with Time

Get System Time

Commands

 •

time_t time ( time_t *timer ); The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, GMT, according to the system clock.

 • •

struct tm *localtime ( time_t *timer ); Converts a time value and corrects for the local time zone. Returns a pointer to the structure result.

Get System Time

Structure tm struct tm { int tm_sec; // seconds after the minute [0,59] int tm_min; // minutes after the hour [0,59] int tm_hour; // hours since midnight [0,23] int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; }

Convert Time to Rotation Angle

Rotation Angle for a Second Hand rot_sec = - (float)curtime->tm_sec * (360.0/60.0); second degree per second

What is the angle for a hour hand?

Draw Hands for Hour, Minute and Second

Composed by Sphere, Cylinder, Disk and Cone Y Z X Translate

Draw Hands for Hour, Minute and Second

Rotate around X-Axis with -90

 

Z-Axis points out of screen

• • • • • •

PushMatrix ( ); Rotatef (-90.0, 0.0, 0.0, 1.0); glBegin ( … ); … glEnd ( … ); PopMatrix ( ); Scre en Rotate Y Z X

Make Hands Rotate with Time

Scre en Y Z X