-
Notifications
You must be signed in to change notification settings - Fork 0
/
solar.cpp
443 lines (341 loc) · 12.5 KB
/
solar.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
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
/*
* texture2.cpp
*
* - light position is at the sun
* - the sun emits light (emission material)
*
*/
#include <unistd.h>
#include <stdio.h>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>
#include <iostream>
#include <stdlib.h>
#define ANIMATE 0
int day = 180;
int mode = ANIMATE;
int worldx=500;
int worldy=750;
int worldh=3000;
int floorh=-5;
// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
float deltaAngle = 0.0,ratio;
float deltaVAngle = 0.0;
float x=0.0f,y=1.75f,z=5.0f;
float lx=0.0f,ly=0.0f,lz=-1.0f;
//GLuint toTextures[2]; // Two texture objects
int iRenderMode = 3; // Sphere Mapped is default
GLUquadricObj* disk;
bool gp; // G Pressed? ( New )
GLuint filter; // Which Filter To Use
GLuint fogMode[]= { GL_EXP, GL_EXP2, GL_LINEAR }; // Storage For Three Types Of Fog
GLuint fogfilter= 0; // Which Fog To Use
GLfloat fogColor[4]= {0.2f, 0.2f, 0.2f, 0.4f}; // Fog Color
const int ww = 1000, hh = 670, numTextures = 10;
void keyboard(unsigned char key, int x, int y);
void init();
void display(void);
void reshape(GLsizei w, GLsizei h);
void idle();
void drawAxis();
void LoadGLTextures();
int LoadTextureRAW (const char * filename, int isWrap, int w, int h, int iTextures);
void FreeTexture();
void RenderScene(void);
void fog(float a);
GLuint texture[numTextures]; // Storage Textures
float angle = 0.0;
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (ww, hh);
glutCreateWindow ("The Planet");
init();
// glutDisplayFunc(RenderScene);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutKeyboardFunc(keyboard);
//RenderScene();
glutMainLoop();
FreeTexture();
return 0;
}
void init()
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 1.0 };
disk = gluNewQuadric();
gluQuadricDrawStyle(disk, GLU_FILL);
LoadGLTextures();
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, 10);
glLightfv(GL_LIGHT0, GL_DIFFUSE, mat_specular);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
}
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 't':
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,5,0, 0,0,0, -1,0,0); // top view
glutPostRedisplay(); break;
case 'f':
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // front view
glutPostRedisplay(); break;
case 'g': fog(0.1f);glutPostRedisplay(); break;
case 'p': fog(0.0f);glutPostRedisplay();break;
case 's':
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(5,0,0 , 0,0,0, 0,1,0); // side view
glutPostRedisplay(); break;
case 27: exit(0); break;
default: mode = 1 - mode; break;
}
}
void reshape (int w, int h)
{
/*
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
if (w <= h)
glFrustum(-1.0, 1.0, -1.0 * (GLfloat) h / (GLfloat) w,
1.0 * (GLfloat) h / (GLfloat) w, 1, 10.0);
else
glFrustum(-1.0 * (GLfloat) w / (GLfloat) h,
1.0 * (GLfloat) w / (GLfloat) h, -1.0, 1.0, 1, 10.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
*/
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(80.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void RenderScene(){
GLfloat brick_ambdiff[] = {1,1, 1, 1 };
// SKY
glPushMatrix();
// glRotatef(90, 0, 0, 1);
// glRotatef(90 ,0, 1, 0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[5]);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, brick_ambdiff);
glMaterialf(GL_FRONT, GL_SHININESS, 0);
glBegin(GL_QUADS);
glNormal3f(0, 0, 1);
glVertex3f(worldy*5, worldx*10+5, worldh);
glVertex3f(-worldy*5, worldx*10+5, worldh);
glVertex3f(-worldy*5, -worldx*10+5, worldh);
glVertex3f(worldy*5, -worldx*10+5, worldh);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
void display(void)
{
GLfloat light_position[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat mat_emission[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_white[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_noemis[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat mat_noemis_1[] = { 0.0, 1.0,1.0, 1.0};
//GLfloat mat_noemis_2[] = { 0.0, 0.5,0.5, 1.0};
static float rot = 0.0;
rot += 0.5;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
GLUquadricObj *pObj = gluNewQuadric(); // Get a new Quadric off the stack
gluQuadricTexture(pObj, true); // This turns on texture coordinates for our Quadrics
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glPushMatrix();
//***draw sun
glPushMatrix();
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glRotatef(angle*2,0,1,0); // rotate around itself
glRotatef(90,1,0,0);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glColor3f(5.0, 1.0, 1.0);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glBindTexture(GL_TEXTURE_2D, texture[0]);
gluSphere(pObj, 1.3f, 20, 20); // sun
glDisable(GL_TEXTURE_2D); // Disable Texture Mapping
glPopMatrix();
glPushMatrix();
glRotatef(rot/2, 0,1,0); // rotate around itself
glTranslatef(2,0,0);
// fog();
//***draw earth
glPushMatrix();
glRotatef(rot, 0,1,0); // rotate around itself
glTranslatef (-2.0, 0.0, 0.0);
glRotatef(90,1,0,0); // rotate so that the earth equation is along x-axis
glRotatef(90,0,0,1); // rotate so that the earth equation is along x-axis
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glColor3f(1, 1, 1);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_noemis);
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glBindTexture(GL_TEXTURE_2D, texture[1]);
gluSphere(pObj, 0.5, 20, 16);
glDisable(GL_TEXTURE_2D); // Disable Texture Mapping
glPopMatrix();
//***black ball
glPushMatrix();
glRotatef(rot, 0,1,0); // rotate around itself
glTranslatef (-5.0, 0.0, 0.0);
glRotatef(90*1.5,1,0,0); // rotate so that the earth equation is along x-axis
glRotatef(90,0,0,1); // rotate so that the earth equation is along x-axis
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glColor3f(1, 1, 1);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_noemis_1);
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glBindTexture(GL_TEXTURE_2D, texture[5]);
gluSphere(pObj, 0.5, 20, 16);
glDisable(GL_TEXTURE_2D); // Disable Texture Mapping
glPopMatrix();
//***draw 1st moon
glPushMatrix();
glRotatef(angle, -1,0,1);
glRotatef(-45,0,1,0);
glTranslatef(1,0,0);
glRotatef(angle, 1,0,1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[2]);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_noemis);
glColor3f(1, 1, 1);
gluSphere(pObj, 0.1, 20, 16);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
//draw 2nd moon
glPushMatrix();
glRotatef(angle, 1,0,1);
glRotatef(45,0,1,0);
glTranslatef(1,0,0);
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glBindTexture(GL_TEXTURE_2D, texture[3]);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_noemis);
glColor3f(1, 1, 1);
gluSphere(pObj, 0.2, 20, 16);
glDisable(GL_TEXTURE_2D); // Disable Texture Mapping
glPopMatrix();
glPopMatrix();
glPopMatrix();
// planet 2 : mars
glPushMatrix();
glRotatef ((GLfloat) day, 0.0, 1.0, 0.0); // rotate around the sun
glTranslatef (-3.0, 0.0, 0.0);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_noemis);
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glBindTexture(GL_TEXTURE_2D, texture[4]);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glColor3f(1, 1, 1);
gluSphere(pObj, 0.2, 20, 16);
glDisable(GL_TEXTURE_2D);
glRotatef (20, 1.0, 0.0, 0.0);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glColor3f (1.0, 0.62, 0.14);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
gluDisk(disk, 0.25, 0.35, 20, 4); // draw mars's ring
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
gluDeleteQuadric(pObj); // Free the Quadric
}
void fog( float a){
glClearColor(a,a,a,a*2); // We'll Clear To The Color Of The Fog ( Modified )
glFogi(GL_FOG_MODE, fogMode[fogfilter]); // Fog Mode
glFogfv(GL_FOG_COLOR, fogColor); // Set Fog Color
glFogf(GL_FOG_DENSITY, a*0.35f); // How Dense Will The Fog Be
glHint(GL_FOG_HINT, GL_NICEST); // Fog Hint Value
glFogf(GL_FOG_START, 1.0f); // Fog Start Depth
glFogf(GL_FOG_END, 5.0f); // Fog End Depth
glEnable(GL_FOG); // Enables GL_FOG
}
//********************************
// Load files for Texture Mapping
//********************************
void LoadGLTextures()
{
LoadTextureRAW("image/sunsun.raw", true, 640, 640, 0);
LoadTextureRAW("image/earthmap.raw", true, 1024, 512, 1);
LoadTextureRAW("image/neptune.raw", true, 128, 128, 2);
LoadTextureRAW("image/mercury.raw", true, 128, 256, 3);
LoadTextureRAW("image/jupiter.raw", true, 128, 128, 4);
LoadTextureRAW("image/milky.raw", true,1280, 1291, 5);
// LoadTextureRAW("galaxy.raw", true,900, 600, 5);
}
void idle()
{
angle += 0.2; // spin the earth 0.2 degree around y-axis
if (mode == ANIMATE) {
usleep(20000);
day++;
// if (day%720 == 0)
if (day==720)
day = 0;
}
glutPostRedisplay();
}
//*******************************************************
// Texture Mapping Function load .RAW file as a texture
//*******************************************************
int LoadTextureRAW (const char * filename, int isWrap, int width, int height, int iTextures)
{
char * data;
FILE * file;
file = fopen( filename, "rb" ); // open texture data
if ( file == NULL )
return 0;
data = (char *) malloc(width * height * 3); // allocate buffer
fread( data, width * height * 3, 1, file ); // read texture data
fclose(file);
glGenTextures( 1, &texture[iTextures] ); // allocate a texture name
// select our current texture
glBindTexture( GL_TEXTURE_2D, texture[iTextures] );
// select modulate to mix texture with the color of the object
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the first mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// if wrap is true, the texture wraps over at the edges (repeat)
// ... false, the texture ends at the edges (clamp)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
isWrap ? GL_REPEAT : GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
isWrap ? GL_REPEAT : GL_CLAMP );
// build our texture mipmaps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
// free buffer
free( data );
return iTextures;
}
void FreeTexture()
{
int i;
for (i=0; i<numTextures; i++)
glDeleteTextures( 1, &texture[i] );
}