-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenGLClock.cpp
More file actions
314 lines (250 loc) · 8.03 KB
/
OpenGLClock.cpp
File metadata and controls
314 lines (250 loc) · 8.03 KB
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
// assignment1.cpp : Defines the entry point for the console application.
/*
George Villaume
gvilla3@lsu edu
CSC 4356: Interactive Computer Graphics
Professor: Jinwei Ye
*/
#include <iostream>
#include <GL/glut.h> //use #include <GLUT/glut.h> if using Xcode in MacOS
//#include <GLUT/glut.h>
#include <math.h>
#include <time.h>
int win_H, win_W;
time_t timer;
struct tm curr_time;
void reshape(int w, int h)
{
glViewport(0, 0, w, h); /* Establish viewing area to cover entire window. */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, 0, h, -1, 1);
glScalef(1, -1, 1);
glTranslatef(0, -h, 0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
/* Draw top watch band */
glColor3f(0.85, 0.78, .09); // Sets color to gold
glBegin(GL_QUADS); // Draws base band first
glVertex2f(140.0, 132.0);
glVertex2f(150.0, 72.0);
glVertex2f(362.0, 72.0);
glVertex2f(372.0, 132.0);
glEnd();
glColor3f(0.65, 0.6, .07); // Sets color to a darker gold for depth
glBegin(GL_QUADS); // Draws middle band
glVertex2f(150.8, 67.0);
glVertex2f(155.0, 37.0);
glVertex2f(357.0, 37.0);
glVertex2f(361.2, 67.0);
glEnd();
glColor3f(0.49, 0.45, .04); // Darker gold
glBegin(GL_QUADS); // Draws top watch band
glVertex2f(155.8, 32.0);
glVertex2f(160.0, 2.0);
glVertex2f(352.0, 2.0);
glVertex2f(356.2, 32.0);
glEnd();
/* Draw bottom watch band */
glPushMatrix(); // Push new matrix on to the stack
// Transformations act as a stack
glTranslatef(256.0, 256.0, 0.0); // 3. Translate back to desired location
glRotatef(180.0, 0.0, 0.0, 1.0); // 2. Rotate around origin
glTranslatef(-256.0, -256.0, 0.0); // 1. Translate to origin
glColor3f(0.85, 0.78, .09); // Same gold as top base
glBegin(GL_QUADS); // Bottom base band
glVertex2f(140.0, 132.0);
glVertex2f(150.0, 72.0);
glVertex2f(362.0, 72.0);
glVertex2f(372.0, 132.0);
glEnd();
glColor3f(0.65, 0.6, .07); // Darker gold
glBegin(GL_QUADS); // Darker gold
glVertex2f(150.8, 67.0);
glVertex2f(155.0, 37.0);
glVertex2f(357.0, 37.0);
glVertex2f(361.2, 67.0);
glEnd();
glColor3f(0.49, 0.45, .04); //Darkest gold
glBegin(GL_QUADS); //Bottom bottom band
glVertex2f(155.8, 32.0);
glVertex2f(160.0, 2.0);
glVertex2f(352.0, 2.0);
glVertex2f(356.2, 32.0);
glEnd();
glPopMatrix(); // Pop matrix off the stack
/* Draw rim of watch */
glColor3f(0.969, 0.898, .125); // Set rim of watch to lightest gold
glBegin(GL_TRIANGLE_FAN); //Begin a triangle fan object to draw a circle
glVertex2f(256.0, 256.0); // Center point
glVertex2f(256.0, 86.0); // Next point that sets radius
float lastX = 256.0; // Save the points from the last vertex added
float lastY = 86.0;
float currentX, currentY;
for (int i = 1; i <= 360; i++) // For loop to draw the rest of the vertices
{
currentX = ((cos(1.0) * (lastX - 256.0)) - (sin(1.0) * (lastY - 256.0))) + 256.0; // Calculate next vertex to be placed by rotating
currentY = ((sin(1.0) * (lastX - 256.0)) + (cos(1.0) * (lastY - 256.0))) + 256.0; // the last points 1 degree using a 2D rotation equation
glVertex2f(currentX, currentY); // Place vertex
lastX = currentX; // Save last points
lastY = currentY;
}
glEnd(); // Close triangle fan object
/* Draw glare on watch rim */
glColor3f(1.0, 1.0, 1.0); // White for watch glare
for (int k = 300; k < 359; k++) // For loop to rotate the glare around the rim of the watch
{
if (k < 304 || k > 308) // Add the gap in the glare
{
glPushMatrix();
glTranslatef(256.0, 256.0, 0.0);
glRotatef((float)k, 0.0, 0.0, 1.0); // Rotates the glare by k degrees so that it coveres the range I picked
glTranslatef(-256.0, -256.0, 0.0);
glBegin(GL_QUADS); // Draw glare
glVertex2f(254.0, 93.0);
glVertex2f(254.0, 99.0);
glVertex2f(258.0, 99.0);
glVertex2f(258.0, 93.0);
glEnd();
glPopMatrix();
}
}
/* Draw clock face */
// White color carried over from glare
glBegin(GL_TRIANGLE_FAN); // Begin drawing clock face
glVertex2f(256.0, 256.0); // Center point
glVertex2f(256.0, 106.0); // First vertex sets radius
lastX = 256.0; // Save points
lastY = 106.0;
for (int i = 1; i <= 360; i++) // Iterated 359 times to draw vertices 1 degree apart
{
currentX = ((cos(1.0) * (lastX - 256.0)) - (sin(1.0) * (lastY - 256.0))) + 256.0; // 2D rotation equation
currentY = ((sin(1.0) * (lastX - 256.0)) + (cos(1.0) * (lastY - 256.0))) + 256.0;
glVertex2f(currentX, currentY); // Place vertex
lastX = currentX; // Save points
lastY = currentY;
}
glEnd(); // Finish drawing triangle fan
/* Draw tick marks and watch dial */
for (int j = 0; j < 360; j += 6) // For loop to draw tick marks every 30 degrees
{
glColor3f(0.0, 0.0, 0.0); // Sets color to black in the for loop to avoid improper color after drawing watch dial
if (j % 30 == 0) // Every 30 degrees for hour marks
{
glPushMatrix();
glTranslatef(256.0, 256.0, 0.0);
glRotatef((float)j, 0.0, 0.0, 1.0); // Rotate by j degrees
glTranslatef(-256.0, -256.0, 0.0);
glBegin(GL_QUADS); // Draw thick tick mark
glVertex2d(254.0, 106.0);
glVertex2d(258.0, 106.0);
glVertex2d(258.0, 120.0);
glVertex2d(254.0, 120.0);
glEnd();
if (j == 90) // At 90 degrees, draws the dial on the side
{
glColor3f(0.65, 0.6, .07);
glBegin(GL_QUADS);
glVertex2d(240.0, 87.0);
glVertex2d(272.0, 87.0);
glVertex2d(272.0, 74.0);
glVertex2d(240.0, 74.0);
glEnd();
}
glPopMatrix();
}
else // At all other intervals, draw the small tick mark
{
glPushMatrix();
glTranslatef(256.0, 256.0, 0.0);
glRotatef((float)j, 0.0, 0.0, 1.0);
glTranslatef(-256.0, -256.0, 0.0);
glBegin(GL_LINES);
glVertex2d(256.0, 106.0);
glVertex2d(256.0, 116.0);
glEnd();
glPopMatrix();
}
}
/* Draw hour hand */
glPushMatrix();
glTranslatef(256.0, 256.0, 0.0);
glRotatef((((float)curr_time.tm_hour) * 30.0 + ((float)curr_time.tm_min) / 2.0 + ((float)curr_time.tm_sec) / 120.0), 0.0, 0.0, 1.0);
glTranslatef(-256.0, -256.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_TRIANGLE_STRIP);
glVertex2f(266.0, 256.0);
glVertex2f(246.0, 256.0);
glVertex2f(261.0, 156.0);
glVertex2f(251.0, 156.0);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glVertex2f(266.0, 256.0);
glVertex2f(246.0, 256.0);
glVertex2f(261.0, 286.0);
glVertex2f(251.0, 286.0);
glEnd();
glPopMatrix();
/* Draw minute hand */
glPushMatrix();
glTranslatef(256.0, 256.0, 0.0);
glRotatef((((float)curr_time.tm_min) * 6.0 + ((float)curr_time.tm_sec) / 10.0), 0.0, 0.0, 1.0);
glTranslatef(-256.0, -256.0, 0.0);
glColor3f(0.5, 0.5, 0.5);
glBegin(GL_TRIANGLES);
glVertex2f(262.0, 256.0);
glVertex2f(250.0, 256.0);
glVertex2f(256.0, 126.0);
glEnd();
glBegin(GL_TRIANGLES);
glVertex2f(262.0, 256.0);
glVertex2f(250.0, 256.0);
glVertex2f(256.0, 286.0);
glEnd();
glPopMatrix();
/* Draw second hand */
glPushMatrix();
glTranslatef(256.0, 256.0, 0.0);
glRotatef((((float)curr_time.tm_sec) * 6.0), 0.0, 0.0, 1.0);
glTranslatef(-256.0, -256.0, 0);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2f(256.0, 256.0);
glVertex2f(256.0, 121.0);
glEnd();
glPopMatrix();
glutSwapBuffers(); // swap the back buffer to front
}
void TimeEvent(int time_val)
{
time(&timer); // get the current date and time from system
localtime_s(&curr_time, &timer); // use localtime_r(&timer, &curr_time); if using Xcode in MacOS
//localtime_r(&timer, &curr_time);
glutPostRedisplay();
glutTimerFunc(30, TimeEvent, 1);// By using a timed event, your application should run at the same speed on any machine.
}
int main(int argc, char** argv)
{
GLenum type;
glutInit(&argc, argv);
type = GLUT_DEPTH;
type |= GLUT_RGB;
type |= GLUT_DOUBLE;
glutInitDisplayMode(type);
time(&timer); // get current date and time
localtime_s(&curr_time, &timer); // use localtime_r(&timer, &curr_time); if using Xcode in MacOS
//localtime_r(&timer, &curr_time);
// set window size and create a window for rendering
win_W = 512;
win_H = 512;
glutInitWindowSize(win_H, win_W);
glutInitWindowPosition(100, 100);
glutCreateWindow("My clock");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(30, TimeEvent, 1);
glutMainLoop();
return 0;
}