-
Notifications
You must be signed in to change notification settings - Fork 0
/
planets.c
486 lines (393 loc) · 10.6 KB
/
planets.c
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Simple OpenGL graphics program
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
#include <stdio.h>
GLfloat theta = 16280.0;
GLfloat isovert[12][3] = {0};
int isoTriangle[20][3] = {0};
GLfloat isoNetvert[22][3] = {0};
int isoNetTriangle[20][3] = {0};
int sec = 0;
double AU = 149.6; // in gigameters (million km)
double timescale = 360/60*0.5; // 60sec = one earth year
double Rad = 6.28/360;
double enlargement = 10;
// double enlargement = 5;
double shrinkdist = 1/10;
/*
Planetary Data:
=================
- Diameter (km),
- dist (Gm),
- day length (hours),
- number of moons
*/
double planets[10][4] =
{
{ 4879, 57.9, 4222.6, 0}, //mercury
{ 12104, 108.2, 2802, 0}, //venus
{ 12756, 149.6, 24.0, 1}, //earth
{ 6792, 227.9, 24.7, 2}, //mars
{ 473, 414.01, 9.07, 0}, //ceres
{142984, 778.6, 9.9, 4}, //jupiter
{120536, 1433.5, 10.7, 4}, //saturn
{ 51118, 2872.5, 17.2, 3}, //uranus
{ 49528, 4495.1, 16.1, 2}, //neptune
{1188.3, 5906.38, 10, 1}, //pluto
};
// planet colors (rgb)
double planetColors[10][3] =
{
{ .8, .8, .8 }, //mercury
{ 1, 1, 1 }, //venus
{ 0, 1, 0 }, //earth
{ 1, 1, 0 }, //mars
{ .5, .5, .5 }, //ceres
{ .8, .7, .3 }, //jupiter
{ .8, .8, .6 }, //saturn
{ .6, .7, 1 }, //uranus
{ 0, 0, 1 }, //neptune
{ 1, .7, .6 }, //pluto
};
double sunMass = 332946; //in earth masses
double planetMasses[10]=
{
0.0553,
0.815,
1.0,
0.107,
0.00015,
317.8,
95.2,
14.5,
17.1,
0.00218,
};
double moonStats[3]= {1737.1, .384399, 29.530589 };
/*===========
- Radius (km),
- dist (Gm),
- synodic period, (in earth days) ~ Month
*/
double monthPyear= 365.25/29.530589;
double MU = .384399;
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0,0,0,0);
glPointSize(4.0);
glLineWidth(1.5);
glPushMatrix();
configcamera();
glEnable(GL_CULL_FACE);
// superdivIsa(3);
// superdivIsaNet(3);
glPushMatrix();
glScalef(80,80,80);
cube();
glPopMatrix();
glRotatef(90,0,1,0);
solarSystem();
glPopMatrix();
glutSwapBuffers();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void sphere(double col[3]){
GLUquadricObj *mysphere;
mysphere = gluNewQuadric();
double r = col[0];
double g = col[1];
double b = col[2];
glPushMatrix();
glRotatef(90,1,0,0);
gluQuadricDrawStyle(mysphere, GLU_FILL);
glColor3f (r,g,b);
gluSphere(mysphere,1.0,15,9);
gluQuadricDrawStyle(mysphere, GLU_LINE);
glColor3f (r*0.7,g*0.7,b*0.7);
gluSphere(mysphere,1.005,15,9);
glPopMatrix();
}
void planet(int i){
double radius = planets[i][0]/2;
double size = radius/10000;
glPushMatrix();
glScalef(size,size,size);
// scaled so planet radius = 1.0 units
glRotatef(theta * timescale *60 * 365 * planets[i][3]/24.0 ,0,1,0);
sphere(planetColors[i]);
if(i==6){
ring2(1.9,0.2);
ring2(2.5,0.3);
}
// glTranslatef(0.5,0,0);
// sphere(planetColors[i]);
glPopMatrix();
}
void moon(int parent, int index){
double color[] = {1.2,1.2,1.2};
sphere(color);
}
void planetarySys(double param[4],int index){
glPushMatrix();
double dist = param[1];
double radius = param[0]/2;
double size = radius/10000;
double inAU = dist/AU;
double year = 1.0/(inAU*inAU); //years per year
if (index==9){
glRotatef(6,1,0,0);
}
// ORBIT SUN
glRotatef(theta *timescale* year,0,1,0);
//make distances smaller
glScalef(shrinkdist,shrinkdist,shrinkdist);
// DRAW PATH
ring(dist,size);
glPushMatrix();
glTranslatef(dist,0,0);
// position RELATIVE TO CENTER OF PLANET
//make planets bigger
glScalef(enlargement,enlargement,enlargement);
// Draw the planet
planet(index);
//moon
if(param[3] != 0){
double d1= moonStats[1] * pow(planets[index][0]/planets[2][0],0.7);
double r1= moonStats[0] * pow(planets[index][0]/planets[2][0],0.2);
if(index==3){
r1/=4;
}
for (int i =0; i<param[3]; i++){
moonSys(d1, r1, index, i);
d1 *= 1.2;
r1 *= 0.8;
}
}
glPopMatrix();
glPopMatrix();
}
void moonSys(double dist, double radius, int parent, int index){
glPushMatrix();
// double size = radius/6;
double size = radius/5000;
double p_mass = planetMasses[parent];
double grav_factor = p_mass;
// double grav_factor = 1;
double d_inMu = dist/MU;//distances in moon distances
double month = 1.0/(d_inMu*d_inMu); //months per month
double distScaled=dist*6;
// ORBIT PARENT
glRotatef( theta *timescale*monthPyear*month*grav_factor,0,1,0);
// DRAW PATH
// ring(distScaled,size);
glPushMatrix();
glTranslatef(distScaled,0,0);
// position RELATIVE TO CENTER OF MOON
glPushMatrix();
glScalef(size,size,size);
// scaled so moon radius = 1.0 units
moon(parent, index);
glPopMatrix();
glPopMatrix();
glPopMatrix();
}
void sun(){
double color[] = {1.1,.6,.0};
sphere(color);
}
void ring(double dist,double size){
GLUquadricObj *ring;
ring = gluNewQuadric();
double a = 0.15*size;
glDisable(GL_CULL_FACE);
glPushMatrix();
glRotatef(90,1,0,0);
gluQuadricDrawStyle(ring, GLU_FILL);
glColor3f (.2,.2,.2);
gluDisk(ring,dist-a,dist+a,32,1);
glLineWidth(1.0);
gluQuadricDrawStyle(ring, GLU_LINE);
// glColor3f (1,0.5,0);
gluDisk(ring,dist-a,dist+a,32,1);
glPopMatrix();
glEnable(GL_CULL_FACE);
}
void ring2(double dist,double size){
GLUquadricObj *ring;
ring = gluNewQuadric();
double a = size;
glDisable(GL_CULL_FACE);
glPushMatrix();
glRotatef(90,1,0,0);
gluQuadricDrawStyle(ring, GLU_FILL);
glColor3f(.6, .6, .4 );
gluDisk(ring,dist-a,dist+a,32,1);
glLineWidth(1.0);
// gluQuadricDrawStyle(ring, GLU_LINE);
// glColor3f(.3, .3, .2 );
// gluDisk(ring,dist-a,dist+a,32,1);
glPopMatrix();
glEnable(GL_CULL_FACE);
}
void solarSystem(){
sun();
for(int i =0; i<10; i++){
planetarySys(planets[i], i);
}
}
void configcamera(){
// lowOrbit();
// glTranslatef(0,0,-3);
// glTranslatef(0,0,-5);
// glTranslatef(0,0,-8);
// glTranslatef(0,0,-20);
// glTranslatef(0,0,-50);
// glTranslatef(0,0,-120);
glTranslatef(0,0,-
pow((1+cos(theta * Rad * 180/10))*4.5,3)-10);
// glRotatef( 5,1,0,0);
// glRotatef(5,1,0,0);
// glRotatef(15,1,0,0);
glRotatef(20,1,0,0);
// glRotatef(30,1,0,0);
// glRotatef(45,1,0,0);
// wobble();
swivel1(90/30);
// swivel2();
// glRotatef(10,1,0,0);
// swivel1();
}
void wobble(){
glRotatef(1*sin(theta * Rad * 90/10),1,0,0);
glRotatef(1*cos(theta * Rad * 90/10),0,1,0);
}
void swivel1(double speed){
// glRotatef(2,1,0,0);
glRotatef(theta * 90/10,0,1,0);
}
void lowOrbit(){
glRotatef(30,1,0,0);
glRotatef(-60,0,0,1);
glTranslatef(0,-1.1,-0.0);
glRotatef(-theta*90/10, 0,1,0);
glRotatef(-theta*90/10, 1,0,0);
}
void swivel2(){
glTranslatef(0,-1.1,-0.0);
glRotatef(-theta*90/10,0,1,0);
glRotatef(-theta*90/10,1,0,0);
}
void cube(){
glLineWidth(1.1);
glBegin( GL_LINES);
{
for(int q = -10; q<=10; q++){
glColor3f ( .2,.1,.1);
glVertex3f( 10, q, 10);
glVertex3f( 10, q,-10);
glVertex3f( 10, 10, q);
glVertex3f( 10,-10, q);
glVertex3f(-10, q, 10);
glVertex3f(-10, q,-10);
glVertex3f(-10, 10, q);
glVertex3f(-10,-10, q);
glColor3f ( .1,.2,.1);
glVertex3f( q, 10, 10);
glVertex3f( q, 10,-10);
glVertex3f( 10, 10, q);
glVertex3f(-10, 10, q);
glVertex3f( q,-10, 10);
glVertex3f( q,-10,-10);
glVertex3f( 10,-10, q);
glVertex3f(-10,-10, q);
glColor3f ( .1,.1,.2);
glVertex3f( q, 10, 10);
glVertex3f( q,-10, 10);
glVertex3f( 10, q, 10);
glVertex3f(-10, q, 10);
glVertex3f( q, 10,-10);
glVertex3f( q,-10,-10);
glVertex3f( 10, q,-10);
glVertex3f(-10, q,-10);
}
}
glEnd();
glLineWidth(2.0);
glBegin( GL_LINES);
{
glColor3f ( .5,.5,.5);
for(int q = -10; q<=10; q++){
glColor3f ( .1,.05,.05);
glVertex3f( 10, q, 10);
glVertex3f( 10, q,-10);
glVertex3f( 10, 10, q);
glVertex3f( 10,-10, q);
glVertex3f(-10, q, 10);
glVertex3f(-10, q,-10);
glVertex3f(-10, 10, q);
glVertex3f(-10,-10, q);
glColor3f ( .05,.1,.05);
glVertex3f( q, 10, 10);
glVertex3f( q, 10,-10);
glVertex3f( 10, 10, q);
glVertex3f(-10, 10, q);
glVertex3f( q,-10, 10);
glVertex3f( q,-10,-10);
glVertex3f( 10,-10, q);
glVertex3f(-10,-10, q);
glColor3f ( .05,.05,.1);
glVertex3f( q, 10, 10);
glVertex3f( q,-10, 10);
glVertex3f( 10, q, 10);
glVertex3f(-10, q, 10);
glVertex3f( q, 10,-10);
glVertex3f( q,-10,-10);
glVertex3f( 10, q,-10);
glVertex3f(-10, q,-10);
}
}
glEnd();
}
void mymotion() {
usleep(16667); // 1/60th of a sec
theta +=0.016667;
// theta +=0.016667/4.0;
glutPostRedisplay();
if (theta >=sec){
sec++;
// printf("tick sec");
}
// enlargement = 1+(sin(theta*Rad*360/10)+1)*10;
// enlargement = 1+(sin(theta*Rad*360/10)+1)*5 + 5;
enlargement = 12;
shrinkdist = 1.0/enlargement;
}
void resize(GLsizei w, GLsizei h){
GLsizei ww,hh;
glClearColor(0,0,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// gluOrtho2D(-2000,2000,-2000,2000);
GLfloat aspect = (GLfloat) w /(GLfloat) h;
GLfloat fit = 1.5;
gluPerspective(60,aspect,0.1,5000);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glViewport(0,0,w,h);
}
int main(int argc, char** argv){
glutInit( &argc, argv );
// glutInitDisplayMode(GLUT_DOUBLE)
glutInitWindowSize(800,480);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB| GLUT_DEPTH );
glutCreateWindow("Solar System (not to scale)");
glutDisplayFunc( display );
glutIdleFunc( mymotion );
glutReshapeFunc( resize );
glutMainLoop();
return 0;
}