-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
150 lines (113 loc) · 3.38 KB
/
main.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
#include "main.h"
#include "compass.cpp"
#include "gps.cpp"
//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
}
}
//Initializes 3D rendering
void initRendering() {
glEnable(GL_DEPTH_TEST);
}
//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
void plotPoint(Coord point, Coord offset) {
glVertex3f(point.x + offset.x, point.y + offset.y, point.z + offset.z);
}
/*
* We rotate the point clockwise by angle in the following plane
* ------->x
* |
* |
* |
* |
* z
*/
Coord rotateAlongY(Coord point, float angle) {
angle = angle * M_PI / 180;
Coord result;
result.y = point.y;
result.z = point.z * cos(angle) + point.x * sin(angle);
result.x = point.x * cos(angle) - point.z * sin(angle);
return result;
}
/*
* Given the destination angle (clockwise to North) and the current angle
* (clockwise to North), find the angle of destination relative to
* the current angle (clockwise)
*/
float dest_relative_to_current(float dest, float current) {
return 360 - dest - current;
}
//Draws the 3D scene
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
_destination_angle = read_direction(_DIRECTION_FILE);
_dest_to_current_angle = dest_relative_to_current(_destination_angle, _current_angle);
printf("Curr: %6f Dest: %6f Dest to curr: %6f ", _current_angle, _destination_angle, _dest_to_current_angle);
Coord head = rotateAlongY(_head, _dest_to_current_angle);
Coord tail1 = rotateAlongY(_tail1, _dest_to_current_angle);
Coord tail2 = rotateAlongY(_tail2, _dest_to_current_angle);
Coord tail3 = rotateAlongY(_tail3, _dest_to_current_angle);
Coord tail4 = rotateAlongY(_tail4, _dest_to_current_angle);
Coord offset = _offset;
glBegin(GL_TRIANGLES); // Four sides
//Triangle
glColor4f(0,0,1,1);
plotPoint(head, offset);
plotPoint(tail1, offset);
plotPoint(tail3, offset);
glColor4f(0,1,0,1);
plotPoint(head, offset);
plotPoint(tail1, offset);
plotPoint(tail2, offset);
glColor4f(0,1,1,1);
plotPoint(head, offset);
plotPoint(tail2, offset);
plotPoint(tail4, offset);
glColor4f(1,0,0,1);
plotPoint(head, offset);
plotPoint(tail3, offset);
plotPoint(tail4, offset);
glEnd();
glBegin(GL_QUADS); // Bottom face
glColor4f(1,0,1,1);
plotPoint(tail1, offset);
plotPoint(tail2, offset);
plotPoint(tail3, offset);
plotPoint(tail4, offset);
glEnd();
glutSwapBuffers();
}
void update(int value) {
glutPostRedisplay(); //Tell GLUT that the display has changed
//Tell GLUT to call update again in DISPLAY_INTERVAL milliseconds
glutTimerFunc(DISPLAY_INTERVAL, update, 0);
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
//Create the window
glutCreateWindow("Transformations and Timers - videotutorialsrock.com");
initRendering();
//Set handler functions
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutTimerFunc(DISPLAY_INTERVAL, update, 0); //Add a timer
spatial_simple();
glutMainLoop();
return 0;
}