-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
407 lines (353 loc) · 10.7 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*Published under The MIT License (MIT)
See LICENSE.TXT*/
// Ryan Pridgeon COM2032 rp00091
#include <Windows.h>
#include <cmath>
#include <cstdlib>
#ifdef _WIN32
#endif
#include <gl/gl.h>
#include <gl/glut.h>
#include <GL/glu.h>
#include "tga.h"
#include "solarsystem.h"
#include "camera.h"
#include "globals.h"
// the screen size
int screenWidth, screenHeight;
// The TGA texture containing the help dialogue and starfield and moon texture
TGA* help, *stars, * moon;
// toggles if the help dialogue is enabled
bool helpDialogue = true;
// toggles if orbits are drawn
bool showOrbits = true;
// holds the index of the last planet that was selected with the 1 to 9 number keys
int planetSelected = 1;
// The main instance of the solar system
SolarSystem solarSystem;
// The instance of the camera
Camera camera;
// These control the simulation of time
double time;
double timeSpeed;
// holds the state of the controls for the camera - when true, the key for that control is being pressed
struct ControlStates
{
bool forward, backward, left, right, yawLeft, yawRight, pitchUp,
pitchDown, rollLeft, rollRight;
} controls;
// timer function called every 10ms or more
void timer(int)
{
glutPostRedisplay(); // post for display func
glutTimerFunc(10, timer, 0); // limit frame drawing to 100fps
}
// creates a random number up to the max specified
float random(float max)
{
return (float)(std::rand() % 1000) * max * 0.001;
}
// adds a moon to the selected planet
void addMoon()
{
// make a moon using random values
solarSystem.addMoon(planetSelected,
(500 + random(1500)) * solarSystem.getRadiusOfPlanet(planetSelected),
10 + random(100), 0.5 + random(20),
solarSystem.getRadiusOfPlanet(planetSelected) * (0.05f + random(0.2f)), moon->getTextureHandle());
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_TEXTURE_2D);
// set up lighting
glEnable(GL_LIGHTING);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat matSpecular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat matAmbience[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat matShininess[] = { 20.0 };
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, matSpecular);
glMaterialfv(GL_FRONT, GL_SHININESS, matShininess);
glMaterialfv(GL_FRONT, GL_AMBIENT, matAmbience);
GLfloat lightAmbient[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat lightDiffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat lightSpecular[] = { 1.0, 1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDisable(GL_LIGHTING);
// Load all image data
help = new TGA("D:/solarsystemsourcecode/solar/images/help.tga");
stars = new TGA("images/stars.tga");
moon = new TGA("images/moon.tga");
TGA* sun = new TGA("images/sun.tga");
TGA* mercury = new TGA("images/mercury.tga");
TGA* venus = new TGA("images/venus.tga");
TGA* earth = new TGA("images/earth.tga");
TGA* mars = new TGA("images/mars.tga");
TGA* jupiter = new TGA("images/jupiter.tga");
TGA* saturn = new TGA("images/saturn.tga");
TGA* uranus = new TGA("images/uranus.tga");
TGA* neptune = new TGA("images/neptune.tga");
TGA* pluto = new TGA("images/pluto.tga");
// Add all the planets with accurate data. Distance measured in km, time measured in earth days.
solarSystem.addPlanet(0, 1, 500, 695500, sun->getTextureHandle()); // sun
solarSystem.addPlanet(57910000, 88, 58.6, 2440, mercury->getTextureHandle()); // mercury
solarSystem.addPlanet(108200000, 224.65, 243, 6052, venus->getTextureHandle()); // venus
solarSystem.addPlanet(149600000, 365, 1, 6371, earth->getTextureHandle()); // earth
solarSystem.addPlanet(227939100, 686, 1.03f, 3389, mars->getTextureHandle()); // mars
solarSystem.addPlanet(778500000, 4332, 0.4139, 69911, jupiter->getTextureHandle()); // jupiter
solarSystem.addPlanet(1433000000, 10759, 0.44375, 58232, saturn->getTextureHandle()); // saturn
solarSystem.addPlanet(2877000000, 30685, 0.718056, 25362, uranus->getTextureHandle()); // uranus
solarSystem.addPlanet(4503000000, 60188, 0.6713, 24622, neptune->getTextureHandle()); // neptune
solarSystem.addPlanet(5906380000, 90616, 6.39, 1137, pluto->getTextureHandle()); // pluto
solarSystem.addMoon(3, 7000000, 27.3, 27.3, 1738, moon->getTextureHandle()); // test moon for the earth
// set up time
time = 2.552f;
timeSpeed = 0.1f;
// reset controls
controls.forward = false;
controls.backward = false;
controls.left = false;
controls.right = false;
controls.rollRight = false;
controls.rollLeft = false;
controls.pitchDown = false;
controls.pitchUp = false;
controls.yawLeft = false;
controls.yawRight = false;
timer(0);
}
void drawCube(void);
void display(void)
{
// update the logic and simulation
time += timeSpeed;
solarSystem.calculatePositions(time);
if (controls.forward) camera.forward(); if (controls.backward) camera.backward();
if (controls.left) camera.left(); if (controls.right) camera.right();
if (controls.yawLeft) camera.yawLeft(); if (controls.yawRight) camera.yawRight();
if (controls.rollLeft) camera.rollLeft(); if (controls.rollRight) camera.rollRight();
if (controls.pitchUp) camera.pitchUp(); if (controls.pitchDown) camera.pitchDown();
// clear the buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
// set up the perspective matrix for rendering the 3d world
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.0f, (float)screenWidth / (float)screenHeight, 0.001f, 500.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// perform the camera orientation transform
camera.transformOrientation();
// draw the skybox
glBindTexture(GL_TEXTURE_2D, stars->getTextureHandle());
drawCube();
// perform the camera translation transform
camera.transformTranslation();
GLfloat lightPosition[] = { 0.0, 0.0, 0.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
// render the solar system
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
solarSystem.render();
glDisable(GL_LIGHTING);
// possibly render orbits
if (showOrbits)
solarSystem.renderOrbits();
glDisable(GL_DEPTH_TEST);
// set up ortho matrix for showing the UI (help dialogue)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble) screenWidth, (GLdouble) screenHeight, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// draw the help dialogue
if (helpDialogue)
{
glBindTexture(GL_TEXTURE_2D, help->getTextureHandle());
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(512.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(512.0f, 512.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, 512.0f);
glEnd();
}
glFlush();
glutSwapBuffers();
}
void keyDown(unsigned char key, int x, int y)
{
// check for numerical keys
if (key > '0' && key <= '9')
{
// point at the specified planet
float vec[3];
solarSystem.getPlanetPosition(key - '0', vec);
camera.pointAt(vec);
// select that planet
planetSelected = key - '0';
}
switch (key)
{
case '-':
timeSpeed /= 2.0f; // half the rate of time passing
break;
case '=':
timeSpeed *= 2.0f; // double the rate of time passing
break;
case 'h':
helpDialogue = !helpDialogue; // toggle the dialogue
break;
case '[':
planetSizeScale /= 1.2; // make planet scale smaller
break;
case ']':
planetSizeScale *= 1.2; // make planet scale bigger
break;
case 'o':
showOrbits = !showOrbits; // toggle show orbits
break;
case 'm':
addMoon(); // add a moon to the selected planet
break;
case 'r':
planetSizeScale = distanceScale;
break;
case ',':
camera.slowDown(); // slow down camera
break;
case '.':
camera.speedUp(); // speed up camera
break;
// these are all camera controls
case 'w':
controls.forward = true;
break;
case 's':
controls.backward = true;
break;
case 'a':
controls.left = true;
break;
case 'd':
controls.right = true;
break;
case 'l':
controls.rollRight = true;
break;
case 'j':
controls.rollLeft = true;
break;
case 'i':
controls.pitchDown = true;
break;
case 'k':
controls.pitchUp = true;
break;
case 'q':
controls.yawLeft = true;
break;
case 'e':
controls.yawRight = true;
break;
}
}
void keyUp(unsigned char key, int x, int y)
{
switch (key)
{
case 'w':
controls.forward = false;
break;
case 's':
controls.backward = false;
break;
case 'a':
controls.left = false;
break;
case 'd':
controls.right = false;
break;
case 'l':
controls.rollRight = false;
break;
case 'j':
controls.rollLeft = false;
break;
case 'i':
controls.pitchDown = false;
break;
case 'k':
controls.pitchUp = false;
break;
case 'q':
controls.yawLeft = false;
break;
case 'e':
controls.yawRight = false;
break;
}
}
void reshape(int w, int h)
{
screenWidth = w;
screenHeight = h;
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(1200, 700);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyDown);
glutKeyboardUpFunc(keyUp);
glutMainLoop();
return 0;
}
void drawCube(void)
{
glBegin(GL_QUADS);
// new face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// new face
glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f);
// new face
glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, 1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, -1.0f, -1.0f);
// new face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
// new face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// new face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd();
}