-
Notifications
You must be signed in to change notification settings - Fork 83
/
OpenGL-Clock.cpp
106 lines (83 loc) · 1.96 KB
/
OpenGL-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
#include <gl/glut.h>
#include <math.h>
struct Point {
GLint x;
GLint y;
};
void draw_dda(Point p1, Point p2) {
GLfloat dx = p2.x - p1.x;
GLfloat dy = p2.y - p1.y;
GLfloat x1 = p1.x;
GLfloat y1 = p1.y;
GLfloat step = 0;
if(abs(dx) > abs(dy)) {
step = abs(dx);
} else {
step = abs(dy);
}
GLfloat xInc = dx/step;
GLfloat yInc = dy/step;
for(float i = 1; i <= step; i++) {
glVertex2i(x1, y1);
x1 += xInc;
y1 += yInc;
}
}
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(1.0f);
gluOrtho2D(0.0f, 640.0f, 0.0f, 480.0f);
}
void draw_circle(Point pC, GLfloat radius) {
GLfloat step = 1/radius;
GLfloat x, y;
for(GLfloat theta = 0; theta <= 360; theta += step) {
x = pC.x + (radius * cos(theta));
y = pC.y + (radius * sin(theta));
glVertex2i(x, y);
}
}
void display(void) {
Point pC = {200, 200};
GLint radius = 150;
// radius of the needles from the center
GLint hRadius = 120;
GLint mRadius = 130;
GLint sRadius = 140;
// angles of the three needles
double hDegree = 0;
double mDegree = 12.3;
double sDegree = 3.0;
// Terminal Points for Needle
Point pHour, pMinute, pSecond;
pHour.y = pC.y + (hRadius * sin(hDegree));
pHour.x = pC.x + (hRadius * cos(hDegree));
pMinute.y = pC.y + (mRadius * sin(mDegree));
pMinute.x = pC.x + (mRadius * cos(mDegree));
pSecond.y = pC.y + (sRadius * sin(sDegree));
pSecond.x = pC.x + (sRadius * cos(sDegree));
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glColor3f(1.0, 0.0, 1.0);
draw_circle(pC, radius);
glColor3f(1.0, 0.0, 0.0);
draw_dda(pC, pHour);
glColor3f(0.0, 1.0, 0.0);
draw_dda(pC, pMinute);
glColor3f(0.0, 0.0, 1.0);
draw_dda(pC, pSecond);
glEnd();
glFlush();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(200, 200);
glutInitWindowSize(640, 480);
glutCreateWindow("Square");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}