-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
324 lines (248 loc) · 7.5 KB
/
main.cpp
File metadata and controls
324 lines (248 loc) · 7.5 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
315
316
317
318
319
320
321
322
323
324
// C++ libraries
#include <iostream>
#include "Camera.h"
#include "SolarSystem.h"
/// Defines
#define FPS 17
/// Global Variables
int mainWindow, windowWidth, windowHeight;
int mouseX, mouseY;
double currentAspect;
// States
int axisOn = 1, gridOn = 1, detailsOn = 1, mode, projOn;
// Main Objects
SolarSystem *solarSystem;
Camera *camera;
using namespace std;
///////////////////////////////////////////////////////////////////////////////
/** Auxiliar functions **/
// Draw a string in the screen
void drawBitmapString(char *string, float x, float y, float z)
{
char *c;
glRasterPos3f(x, y, z);
for (c = string; *c != '\0'; c++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *c);
}
}
void displayGridAxis()
{
if(axisOn) // Then, display the axis
{
// Axis
glDisable(GL_LIGHTING);
glColor3f(0.85, 0.85, 0.85);
glBegin(GL_LINES);
// X
glVertex3f(-20.0, 0.0, 0.0);
glVertex3f(20.0, 0.0, 0.0);
// Y
glVertex3f(0.0, -20.0, 0.0);
glVertex3f(0.0, 20.0, 0.0);
// Z
glVertex3f(0.0, 0.0, -20.0);
glVertex3f(0.0, 0.0, 20.0);
glLineWidth(0.5);
glEnd();
glEnable(GL_LIGHTING);
}
if(gridOn) // Then, display the grid
{
glDisable(GL_LIGHTING);
glColor3f(0.4, 0.4, 0.4);
glBegin(GL_LINES);
for(float d = -20.0; d <= 20.0; d += 2)
{
glVertex3f(-20.0, 0.0, d);
glVertex3f(20.0, 0.0, d);
glVertex3f(d, 0.0, -20.0);
glVertex3f(d, 0.0, 20.0);
}
glEnd();
glEnable(GL_LIGHTING);
}
}
//////////////////////////////////////////////////////////////////////////
/** GLUT setup and initialization **/
void setupGlut(int *argc, char **argv)
{
windowHeight = windowWidth = 600;
glutInit(argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH); // Context initialization
glutInitWindowSize(600, 600); // Size setting
mainWindow = glutCreateWindow("SolarSystemSimulator"); // Window creation
glutSetCursor(GLUT_CURSOR_CROSSHAIR);
}
void setLights()
{
GLfloat light_position[] = {0.0, 5.0, 10.0, 0.0};
GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light2_position[] = {0.0, -5.0, 10.0, 0.0};
GLfloat light2_specular[] = {1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT1, GL_POSITION, light2_position);
glLightfv(GL_LIGHT1, GL_SPECULAR, light2_specular);
}
void setGL()
{
printf("-> Setting shades, lights and anti-aliasing.\n\n");
// Color and shades
glClearColor(0.0f, 0.0f, 0.04f, 0.0f); // Define the color to clear the screen
glShadeModel(GL_SMOOTH);
// Enables and disables
glEnable(GL_DEPTH_TEST);
glEnable(GL_LINE_SMOOTH);
glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE); // Anti aliasing
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
setLights();
}
/** Deinitilization Function (if ESC was pressed)**/
void glutDeinit()
{
glutDestroyWindow(mainWindow);
exit(EXIT_SUCCESS);
}
///////////////////////////////////////////////////////////////////////////////
/** CALLBACK for Display Function **/
void display()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
// Calculate the perspective projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, currentAspect, 0.1, 100.0);
// Now, calculate and transform the model view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
camera->positioning();
solarSystem->display(detailsOn, projOn, mode);
displayGridAxis();
// Send to GPU vertex data and graphics commands
glFlush();
}
/** CALLBACK for Resize Function **/
void reshape(GLint width, GLint height)
{
// Save the new dimensions
windowWidth = width;
windowHeight = height;
// Calculate the new aspect of screen
currentAspect = ((GLdouble)windowWidth)/windowHeight;
glViewport(0, 0, width, height);
}
///////////////////////////////////////////////////////////////////////////////
/** CALLBACK for Timer Function **/
void timerDisplay(GLint value)
{
glutPostRedisplay();
glutTimerFunc(16, timerDisplay, 0);
}
void physicsTimer(GLint value)
{
// Rotate camera
camera->rotateOnMouse(mouseX, mouseY, windowWidth, windowHeight);
solarSystem->interactBodies();
glutTimerFunc(20, physicsTimer, 0);
}
/** CALLBACK for Keyboard keys and mouse**/
void keyPressed(unsigned char key, int x, int y)
{
// Analyse the key and realize the desired action
if(key == 27) // ESC
{
printf("ESC pressed.\n");
glutDeinit();
}
else if(key == '1') // Toggle show axis
{
axisOn = !axisOn;
printf("-> Toggle show axis.\n");
}
else if(key == '2') // Toggle show grid
{
gridOn = !gridOn;
printf("-> Toggle show grid.\n");
}
else if(key == '3') // Toggle show details of each body
{
detailsOn = !detailsOn;
printf("-> Toggle show details of each body.\n");
}
else if(key == '4')
{
if(mode < 2)
mode++;
else
mode = 0;
printf("-> Change vector type to: %d.\n", mode);
}
else if(key == '5') // Toggle show projections in XZ plan
{
projOn = !projOn;
printf("-> Toggle show projections in XZ plan.\n");
}
else
camera->translateOnKeyboard(key);
}
void specialKeyPressed(GLint key, GLint x, GLint y)
{
switch(key)
{
// case GLUT_KEY_DOWN:
// rotX -= 2;
// break;
// case GLUT_KEY_UP:
// rotX += 2;
// break;
// case GLUT_KEY_LEFT:
// rotY += 2;
// break;
// case GLUT_KEY_RIGHT:
// rotY -= 2;
// break;
default:
printf("< Nothing to do. >\n");
}
}
void passiveMouse(int x, int y)
{
// Save the mouse coordinates
mouseX = x;
mouseY = y;
}
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
char *directory = argv[1];
if(directory != 0)
{
// Initialization of objects
printf("-> Loading the solar system at %s; \n", directory);
solarSystem = new SolarSystem(directory);
solarSystem->loadSystemFromFile();
camera = new Camera();
printf("-> Setting up the Graphics Window; \n");
setupGlut(&argc, argv); // GLUT initialization
setGL();
/* Set the callback functions */
// Graphics
glutDisplayFunc(display);
glutReshapeFunc(reshape);
// Keyboard, mouse and timers
glutKeyboardFunc(keyPressed); // Character keys
glutSpecialFunc(specialKeyPressed); // Special keys pressed
glutPassiveMotionFunc(passiveMouse);
glutTimerFunc(16, timerDisplay, 0);
glutTimerFunc(20, physicsTimer, 0);
glutMainLoop();
}
else
{
printf("> No input file passed as argument.\n");
}
return 0;
}