-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.cpp
executable file
·325 lines (285 loc) · 9.61 KB
/
clock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#define _USE_MATH_DEFINES
#include <iostream>
#include <GL/glut.h>
#include <cmath>
using namespace std;
GLfloat second_angle = 30 * M_PI / 180;
GLfloat minute_angle = 120 * M_PI / 180;
GLfloat hour_angle = 0.0f; // rotational angle of the shapes
// OpenGL works as a state machine.
// The state variables remain same unless changed by the user.
// get the current time
time_t now = time(0);
tm *ltm = localtime(&now);
// extract the hour, minute and second
int hour = ltm->tm_hour;
int minute = ltm->tm_min;
int second = ltm->tm_sec;
// cout << hour << " : " << minute << " : " << second << endl;
// calculate the angle of the hour hand in radians
// GLfloat hour_angle = (((hour%12) * 30) + minute * 0.5) * M_PI / 180;
// // calculate the angle of the minute hand in radians
// GLfloat minute_angle = ((minute * 6) + second * 0.1) * M_PI / 180;
// // calculate the angle of the second hand in radians
// GLfloat second_angle = ((second * 6)) * M_PI / 180;
// cout << "Hour: " << hour_angle << endl;
// cout << "Minute: " << minute_angle << endl;
// cout << "Second: " << second_angle << endl;
int refreshMills = 1000; // refresh interval in milliseconds
GLfloat pendulum_angle = 0.0f;
GLfloat pendulum_length = 0.2f;
GLfloat pendulum_period = 2.0f;
GLfloat horizontal_distance_pendulum, vertical_distance_pendulum;
GLfloat pendulum_swing_width = 0.3f;
// display function
/* Handler for window-repaint event. Call back when the window first appears and
whenever the window needs to be re-painted. */
void display()
{
// set clear color to white
glClearColor(0.89f, 0.894f, 0.871f, 1.0f); // wall color
// clear previous screen (color buffer)
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); // To operate on Model-View matrix
glLoadIdentity(); // Reset the model-view matrix
// set line width
glLineWidth(10.0);
float radius, side, shift_y = 0.4;
// draw octagon
glBegin(GL_POLYGON);
// set line color
glColor3f(0.31f, 0.192f, 0.01f); // brown
radius = 0.45;
for (int i = 22.5; i < 382.5; i += 45)
{
float degInRad = i * M_PI / 180;
glVertex2f(cos(degInRad) * radius, shift_y + sin(degInRad) * radius);
}
glEnd();
side = 2 * radius * sin(22.5);
// cout << side << endl;
glBegin(GL_POLYGON);
glVertex2d((side / 2), 0.2f);
glVertex2d(side / 2, -0.4f);
glVertex2d(0.0f, -0.6f);
glVertex2d(-side / 2, -0.4f);
glVertex2d((-side / 2), 0.2f);
glEnd();
// draw circle
glBegin(GL_POLYGON);
// set line color
glColor3f(0.529f, 0.325f, 0.016f); // brown
radius = 0.3;
for (int i = 0; i < 360; i++)
{
float degInRad = i * M_PI / 180;
glVertex2f(cos(degInRad) * radius, shift_y + sin(degInRad) * radius);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0f, 0.7f, 0.035f);
radius = 0.27;
for (int i = 0; i < 360; i++)
{
float degInRad = i * M_PI / 180;
glVertex2f(cos(degInRad) * radius, shift_y + sin(degInRad) * radius);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 1.0f);
radius = 0.24;
for (int i = 0; i < 360; i++)
{
float degInRad = i * M_PI / 180;
glVertex2f(cos(degInRad) * radius, shift_y + sin(degInRad) * radius);
}
glEnd();
float small_tick = 0.02, large_tick = 0.04;
// draw the small ticks
glLineWidth(2.0);
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f); // black
radius = 0.24;
for (int i = 0; i < 360; i += 6)
{
float degInRad = i * M_PI / 180;
if (i % 30 != 0)
{
glVertex2f(cos(degInRad) * radius, shift_y + sin(degInRad) * radius);
glVertex2f(cos(degInRad) * (radius - small_tick), shift_y + sin(degInRad) * (radius - small_tick));
}
}
glEnd();
glLineWidth(5.0);
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f); // black
radius = 0.24;
// draw the twelve large ticks
for (int i = 0; i < 360; i += 30)
{
float degInRad = i * M_PI / 180;
if (i % 30 == 0)
{
glVertex2f(cos(degInRad) * radius, shift_y + sin(degInRad) * radius);
glVertex2f(cos(degInRad) * (radius - large_tick), shift_y + sin(degInRad) * (radius - large_tick));
}
}
glEnd();
// draw the point at the center of the clock
glPointSize(10.0);
glBegin(GL_POINTS);
glColor3f(0.0f, 0.0f, 0.0f); // black
glVertex2f(0.0f, shift_y);
glEnd();
glPushMatrix();
// draw the second hand from the center point above
glLineWidth(1.7);
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f); // black
radius = 0.18;
glRotatef(-second_angle, 0.0f, 0.0f, 1.0f); // rotation about positive z-axis
glVertex2f(0.0f, shift_y);
// glPushMatrix();
glVertex2f(cos(second_angle) * radius, shift_y + sin(second_angle) * radius);
glEnd();
glPopMatrix();
glPushMatrix();
// draw the minute hand from the center point above
glLineWidth(3.0);
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f); // black
radius = 0.16;
glRotatef(-minute_angle, 0.0f, 0.0f, 1.0f);
glVertex2f(0.0f, shift_y);
glVertex2f(cos(minute_angle) * radius, shift_y + sin(minute_angle) * radius);
glEnd();
glPopMatrix();
glPushMatrix();
// draw the hour hand from the center point above
glLineWidth(5.0);
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f); // black
radius = 0.12;
glRotatef(-hour_angle, 0.0f, 0.0f, 1.0f);
glVertex2f(0.0f, shift_y);
glVertex2f(cos(hour_angle) * radius, shift_y + sin(hour_angle) * radius);
glEnd();
glPopMatrix();
// draw a rectangle just below the clock, width = 0.3, height = 0.6
glBegin(GL_POLYGON);
glColor3f(0.188f, 0.133f, 0.012f);
glVertex2f(-pendulum_swing_width / 2, 0.0f);
glVertex2f(-pendulum_swing_width / 2, -0.4f);
glVertex2f(0.0f, -0.5f);
glVertex2f(pendulum_swing_width / 2, -0.4f);
glVertex2f(pendulum_swing_width / 2, 0.0f);
glEnd();
// draw a small circle at the top of the rectangle
glBegin(GL_POLYGON);
glColor3f(1.0f, 0.7f, 0.035f);
radius = 0.02;
for (int i = 0; i < 360; i++)
{
float degInRad = i * M_PI / 180;
glVertex2f(cos(degInRad) * radius, 0.0f - sin(degInRad) * radius);
}
glEnd();
// draw a line from the above circle, height = 0.4
horizontal_distance_pendulum = sin(pendulum_angle) * pendulum_length;
vertical_distance_pendulum = -cos(pendulum_angle) * pendulum_length;
glBegin(GL_LINES);
glColor3f(1.0f, 0.7f, 0.035f);
glLineWidth(20.0);
glVertex2f(0.0f, 0.0f);
glVertex2f(horizontal_distance_pendulum, vertical_distance_pendulum);
glEnd();
glRotatef(pendulum_angle, 0.0f, 0.0f, 1.0f);
glTranslatef(horizontal_distance_pendulum, vertical_distance_pendulum, 0.0f);
// draw a bigger circle at the bottom of the rectangle
glBegin(GL_POLYGON);
glColor3f(1.0f, 0.7f, 0.035f);
radius = 0.04;
for (int i = 0; i < 360; i++)
{
float degInRad = i * M_PI / 180;
glVertex2f(cos(degInRad) * radius, -sin(degInRad) * radius);
}
glEnd();
// // flush drawing routines to the window
// glFlush();
// // swap buffers to display, since we're double buffered
glutSwapBuffers();
}
// reshape callback function with proper aspect ratio
void reshape(int w, int h)
{
// compute the aspect ration of the new window
if (h == 0)
h = 1;
GLfloat aspect = (GLfloat)w / (GLfloat)h;
// set the viewport to cover the new window
glViewport(0, 0, w, h);
// set the aspect ratio of the clipping area to match the viewport
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // reset the projection matrix
if (w <= h)
{
// width is smaller, so stretch out the height
gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
}
else
{
// height is smaller, so stretch out the width
gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
}
}
// idle callback function
void idle()
{
GLfloat angular_velocity = 2.0f * M_PI / pendulum_period;
if (horizontal_distance_pendulum == pendulum_swing_width / 2)
{
angular_velocity = -angular_velocity;
}
else if (horizontal_distance_pendulum == -pendulum_swing_width / 2)
{
angular_velocity = -angular_velocity;
}
pendulum_angle = sin(angular_velocity * glutGet(GLUT_ELAPSED_TIME) / 1000.0f);
// force the redraw of the window
glutPostRedisplay();
}
void Timer(int value)
{
second_angle -= 6 * M_PI / 180;
minute_angle -= 6 * M_PI / (180 * 60);
hour_angle -= 6 * M_PI / (180 * 60 * 12);
glutPostRedisplay(); // Post re-paint request to activate display()
// recursive call
glutTimerFunc(refreshMills, Timer, 0); // next Timer call milliseconds later
}
// main function
int main(int argc, char **argv)
{
// initialize GLUT (must be called before any GLUT function)
glutInit(&argc, argv);
// enable double buffering
glutInitDisplayMode(GLUT_DOUBLE);
// set window size (width, height)
glutInitWindowSize(960, 960);
// set window position (0,0 is at the top left corner)
glutInitWindowPosition(500, 50);
// create window with title
glutCreateWindow("Analog Clock");
// register the display function (callback event handler)
glutDisplayFunc(display);
// register the reshape function (callback event handler)
glutReshapeFunc(reshape);
// idle function for animation
glutIdleFunc(idle);
// timer function for animation
glutTimerFunc(0, Timer, 0);
// event processing infinite loop
glutMainLoop();
return 0;
}