-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolarSystem.h
More file actions
422 lines (370 loc) · 11 KB
/
Copy pathSolarSystem.h
File metadata and controls
422 lines (370 loc) · 11 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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#pragma once
#include <limits>
typedef float EarthRatio;
typedef GLuint DisplayList, Texture;
float fmod360(float f) {
return (float)fmod( f, 360 );
}
float degToRad(float f) {
return f * (float)M_PI / 180.0f;
}
float radToDeg(float f) {
return f * 180.0f / (float)M_PI;
}
class Planet {
private:
// create the texture
void initTexture(char* texturePath) {
int TexWidth, TexHeight;
unsigned char* Pixels;
Pixels = BmpToTexture(texturePath, &TexWidth, &TexHeight);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &(this->texture));
glBindTexture(GL_TEXTURE_2D, this->texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TexWidth, TexHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, Pixels);
}
// create display lists
void initDisplayLists() {
int resolution = (int)(40 * SCALE);
// default (to scale)
this->defaultDL = glGenLists( 1 );
glNewList(this->defaultDL, GL_COMPILE);
glEnable(GL_TEXTURE_2D);
OsuSphere(this->defaultRadius, resolution, resolution);
glDisable(GL_TEXTURE_2D);
glEndList();
// adjusted
this->adjustedDL = glGenLists( 1 );
glNewList(this->adjustedDL, GL_COMPILE);
glEnable(GL_TEXTURE_2D);
OsuSphere(this->adjustedRadius, resolution, resolution);
glDisable(GL_TEXTURE_2D);
glEndList();
// the current display list to use
this->nowDL = this->adjustedDL;
}
public:
char* name;
float axisTilt;
EarthRatio orbitalPeriod, orbitalRadius, defaultRadius, adjustedRadius, nowRadius, rotationalPeriod;
DisplayList nowDL, defaultDL, adjustedDL;
Texture texture;
// Default constructor (needed for SolarSystem)
Planet() {
this->name = "Unnamed";
this->axisTilt = 0.0f;
this->orbitalPeriod = 0.0f;
this->orbitalRadius = 0.0f;
this->defaultRadius = 0.0f;
this->adjustedRadius = 0.0f;
this->nowRadius = 0.0f;
this->rotationalPeriod = 0.0f;
this->texture = 0;
this->defaultDL = 0;
this->adjustedDL = 0;
this->nowDL = 0;
}
// Primary constructor, initialize with parameters
Planet(
char* name,
float axisTilt,
EarthRatio defaultRadius,
EarthRatio adjustedRadius,
EarthRatio orbitalRadius,
EarthRatio rotationalPeriod,
char* texturePath
) {
this->name = name;
this->axisTilt = axisTilt;
this->orbitalPeriod = (float)pow(orbitalRadius, 3.0 / 2.0);
this->orbitalRadius = orbitalRadius * SCALE;
this->defaultRadius = defaultRadius * SCALE;
this->adjustedRadius = adjustedRadius * SCALE;
this->nowRadius = this->adjustedRadius;
this->rotationalPeriod = rotationalPeriod;
this->initTexture(texturePath);
this->initDisplayLists();
}
// change scale
void updatePlanetScale( bool adjusted ) {
this->nowDL = adjusted ? this->adjustedDL : this->defaultDL;
this->nowRadius = adjusted ? this->adjustedRadius : this->defaultRadius;
}
};
class SolarSystem {
private:
static const int NUM_BODIES = 9;
static const int ANIMATION_STEP = 5000000;
static const int ANIMATION_SPEED_MIN = 100000;
static const int ANIMATION_SPEED_MAX = 100000000;
static const int ANIMATION_SPEED_DEFAULT = 10000000;
static constexpr float SEC_PER_YEAR = 31536000;
static constexpr float EARTH_ROTATIONS_PER_YEAR = 365.2422f;
static constexpr float SPACING = 5.0f * SCALE;
// SUN, MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE
static constexpr EarthRatio RADIUS[NUM_BODIES] = {
25.0f,
0.1915f,
0.4745f,
1.0f,
0.266f,
5.605f,
4.725f,
2.005f,
1.94f
};
static constexpr EarthRatio RADIUS_ADJUSTED[NUM_BODIES] = {
RADIUS[0],
RADIUS[1] * 10.0f,
RADIUS[2] * 6.0f,
RADIUS[3] * 3.0f,
RADIUS[4] * 8.0f,
RADIUS[5],
RADIUS[6],
RADIUS[7] * 1.5f,
RADIUS[8] * 1.5f
};
static constexpr EarthRatio ORBITAL_RADIUS[NUM_BODIES] = {
std::numeric_limits<float>::infinity(),
0.387f,
0.723f,
1.0f,
1.52f,
5.20f,
9.58f,
19.20f,
30.05f
};
static constexpr EarthRatio ROTATIONAL_PERIOD[NUM_BODIES] = {
std::numeric_limits<float>::infinity(),
58.8f,
-244.0f,
1.0f,
1.03f,
0.415f,
0.445f,
-0.720f,
0.673f
};
static constexpr float AXIS_TILT[NUM_BODIES] = {
7.25f,
0.01f,
177.40f,
23.44f,
25.19f,
3.13f,
26.73f,
97.77f,
28.32f
};
static constexpr char* BMP_PATHS[NUM_BODIES] = {
"images/2k_sun.bmp",
"images/2k_mercury.bmp",
"images/2k_venus.bmp",
"images/2k_earth_daymap.bmp",
"images/2k_mars.bmp",
"images/2k_jupiter.bmp",
"images/2k_saturn.bmp",
"images/2k_uranus.bmp",
"images/2k_neptune.bmp"
};
static constexpr char* NAME[NUM_BODIES] = {
"Sol",
"Mercury",
"Venus",
"Earth",
"Mars",
"Jupiter",
"Saturn",
"Uranus",
"Neptune"
};
DisplayList animationSpeedButtonsDL;
Planet bodies[NUM_BODIES];
int animationSpeed; // 2 means twice as fast, 3 is 3x as fast, etc.
float earthYearCount, earthDayCount, earthOrbitDuration;
// create display lists
void initButtonDisplayList() {
float x = 18;
float y = 99;
float width = 2;
float height = 4;
this->animationSpeedButtonsDL = glGenLists( 1 );
glNewList(this->animationSpeedButtonsDL, GL_COMPILE);
glColor3f(0.5, 0.5, 0.5);
glBegin(GL_QUADS);
glVertex3f(x, y, 0);
glVertex3f(x, y - height, 0);
glVertex3f(x + width, y - height, 0);
glVertex3f(x + width, y, 0);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_LINE_STRIP);
glVertex3f(x, y, 0);
glVertex3f(x, y - height, 0);
glVertex3f(x + width, y - height, 0);
glVertex3f(x + width, y, 0);
glVertex3f(x, y, 0);
glVertex3f(x, y - height / 2, 0);
glVertex3f(x + width, y - height / 2, 0);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3f(x + width / 4, y - 2 * height / 6, 0);
glVertex3f(x + width / 2, y - height / 6, 0);
glVertex3f(x + 3 * width / 4, y - 2 * height / 6, 0);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3f(x + width / 4, y - 4 * height / 6, 0);
glVertex3f(x + width / 2, y - 5 * height / 6, 0);
glVertex3f(x + 3 * width / 4, y - 4 * height / 6, 0);
glEnd();
glEndList();
}
// creates the sun and planets object instances
// NOTE*: to be called after the graphics are initialized (so textures
// can be applied to bodies) and after Reset() is called
void initBodies() {
for (int i = 0; i < NUM_BODIES; i++)
this->bodies[i] = Planet(
NAME[i],
AXIS_TILT[i],
RADIUS[i],
RADIUS_ADJUSTED[i],
ORBITAL_RADIUS[i],
ROTATIONAL_PERIOD[i],
BMP_PATHS[i]
);
}
public:
// default constructor
SolarSystem() {
animationSpeed = ANIMATION_SPEED_DEFAULT;
earthYearCount = 0.0f;
earthDayCount = 0.0f;
earthOrbitDuration = SEC_PER_YEAR / (float)ANIMATION_SPEED_DEFAULT;
animationSpeedButtonsDL = 0;
}
// getters
DisplayList getAnimationSpeedButtonsDL() {
return this->animationSpeedButtonsDL;
}
Planet getPlanet(int index) {
return this->bodies[index];
}
int getAnimationSpeed() {
return this->animationSpeed;
}
float getEarthYearCount() {
return this->earthYearCount;
}
float getEarthDayCount() {
return this->earthDayCount;
}
float getEarthOrbitDuration() {
return this->earthOrbitDuration;
}
// call init functions
void init() {
this->initButtonDisplayList();
this->initBodies();
}
// changes the scale ratios used without reloading textures
void updatePlanetScales( bool adjusted ) {
for (int i = 1; i < NUM_BODIES; i++) {
this->bodies[i].updatePlanetScale( adjusted );
}
}
// increments the animation speed by ANIMATION_STEP (5,000,000).
// handles special case: ANIMATION_MIN (100,000).
void increaseAnimationSpeed() {
animationSpeed = ( animationSpeed == ANIMATION_SPEED_MIN )
? ANIMATION_STEP
: ( animationSpeed == ANIMATION_SPEED_MAX )
? ANIMATION_SPEED_MAX
: animationSpeed + ANIMATION_STEP;
// update
earthOrbitDuration = (float)SEC_PER_YEAR / (float)animationSpeed;
}
// decrements the animation speed by ANIMATION_STEP (5,000,000).
// handles special case: ANIMATION_MIN (100,000).
void decreaseAnimationSpeed() {
animationSpeed = ( animationSpeed == ANIMATION_SPEED_MIN || animationSpeed == ANIMATION_STEP )
? ANIMATION_SPEED_MIN
: animationSpeed - ANIMATION_STEP;
// update
earthOrbitDuration = (float)SEC_PER_YEAR / (float)animationSpeed;
}
// draws the solar system
void draw(float Time) {
float dx = -this->getPlanet(0).nowRadius;
earthYearCount = Time / earthOrbitDuration;
earthDayCount = earthYearCount * EARTH_ROTATIONS_PER_YEAR;
// orbit paths
if ( OrbitPathsOn ) {
glDisable(GL_LIGHTING);
glColor3fv(MulArray3(0.2f, (float*)White));
for (int i = 0; i < NUM_BODIES; i++) {
glBegin(GL_LINE_STRIP);
Planet currentBody = this->bodies[i];
float distanceFromSun = dx + currentBody.nowRadius;
// use distance to draw circle
float numSegs = 99;
for (int j = 0; j <= numSegs; j++) {
float p = (float)j / numSegs * 2 * (float)M_PI;
float x = distanceFromSun * (float)cos(p);
float z = distanceFromSun * (float)sin(p);
glVertex3f(x, 0, z);
}
glEnd();
dx += (2 * currentBody.nowRadius) + SPACING;
}
glColor3fv((float*)White);
glEnable(GL_LIGHTING);
}
// draw each body
dx = -this->getPlanet(0).nowRadius;
for (int i = 0; i < NUM_BODIES; i++) {
Planet currentBody = this->bodies[i];
float orbitAngleRad = degToRad( fmod360( earthYearCount * 360 / currentBody.orbitalPeriod ) );
float rotationAngleDeg = fmod360( earthDayCount * 360 / currentBody.rotationalPeriod );
// bind current body's texture
glBindTexture(GL_TEXTURE_2D, currentBody.texture);
// draw current planet
glPushMatrix();
float distanceFromSun = dx + currentBody.nowRadius;
float x = distanceFromSun * (float)cos(orbitAngleRad);
float z = distanceFromSun * (float)sin(orbitAngleRad);
glTranslatef(x, 0, -z); // orbit
glRotatef(currentBody.axisTilt, 1, 0, 0); // axis tilt
glRotatef(rotationAngleDeg, 0, 1, 0); // rotation
// draw sun
if (i == 0) {
SetPointLight(GL_LIGHT0, 0, 0, 0, 1, 0.75, 0.5); // sun light
glDisable(GL_LIGHTING);
glCallList(currentBody.nowDL);
glEnable(GL_LIGHTING);
}
// draw planet
else {
glCallList(currentBody.nowDL);
// rotational axis line
if ( AxisOfRotationLineOn ) {
glDisable(GL_LIGHTING);
glColor3fv((float*)White);
float axisLineRadius = currentBody.nowRadius + (3 * SCALE);
glBegin(GL_LINES);
glVertex3f(0, -axisLineRadius, 0);
glVertex3f(0, axisLineRadius, 0);
glEnd();
glEnable(GL_LIGHTING);
}
}
glPopMatrix();
dx += (2 * currentBody.nowRadius) + SPACING;
}
}
};