Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #include <math.h>
- // Lighting values
- GLfloat whiteLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
- GLfloat sourceLight[] = { 1.8f, 0.8f, 0.8f, 1.0f };
- GLfloat lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };
- // Called to draw scene
- void RenderScene(void)
- {
- // Earth and Moon angle of revolution********** start off by adding the two variables to keep track of the rotation for each object.
- static float fMoonRot =0.0f; // Angle For The
- static float fEarthRot = 0.0f;// Angle For The
- // Clear the window with current clearing color
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Save the matrix state and do the rotations****
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- // Set light position before viewing transformation******
- glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
- // Translate the whole scene out and into view *** angle viewr for me
- glTranslatef(0.0f, 0.0f, -300.0f);
- // Set material color, Red
- // Sun
- glColor3ub(255, 255, 0);
- glutSolidSphere(55.0f, 190, 190);//(size,redioes,reaioes)
- glPointSize(1);
- glBegin(GL_POINTS);
- glColor3f(1,1,1);
- glVertex3f(50,40,10);
- glVertex3f(67,80,8);
- glVertex3f(60,60,170);
- glVertex3f(80,40,70);
- glVertex3f(99,80,8);
- glVertex3f(77,60,154);
- glVertex3f(88,90,10);
- glVertex3f(100,70,16);
- glVertex3f(80,60,70);
- glVertex3f(123,50,128);
- glVertex3f(77,60,114);
- glVertex3f(100,40,10);
- glVertex3f(120,80,8);
- glVertex3f(119,90,10);
- glVertex3f(113,80,8);
- glVertex3f(87,60,170);
- glVertex3f(100,40,70);
- glVertex3f(129,80,8);
- glVertex3f(77,60,124);
- glVertex3f(100,90,90);
- glVertex3f(120,90,100);
- glVertex3f(130,70,90);
- glVertex3f(111,66,110);
- glVertex3f(141,55,84);
- glVertex3f(120,90,120);
- glVertex3f(133,124,55);
- glVertex3f(130,49,136);
- glVertex3f(133,96,82);
- glVertex3f(140,40,90);
- glVertex3f(150,50,88);
- glVertex3f(211,67,84);
- glVertex3f(200,80,8);
- glVertex3f(100,60,170);
- glVertex3f(60,50,130);
- glVertex3f(150,90,128);
- glVertex3f(177,160,154);
- glVertex3f(-50,-40,10);
- glVertex3f(-67,-80,8);
- glVertex3f(-60,-60,170);
- glVertex3f(-80,-40,70);
- glVertex3f(-99,-80,8);
- glVertex3f(-77,-60,154);
- glVertex3f(-88,-90,10);
- glVertex3f(-100,-99,3);
- glVertex3f(-100,-70,16);
- glVertex3f(-80,-60,70);
- glVertex3f(-123,-50,128);
- glVertex3f(-77,-60,114);
- glVertex3f(-100,-40,10);
- glVertex3f(-50,-90,10);
- glVertex3f(-119,-90,10);
- glVertex3f(-113,-80,8);
- glVertex3f(-87,-60,170);
- glVertex3f(-100,-40,70);
- glVertex3f(-129,-80,8);
- glVertex3f(-77,-60,124);
- glVertex3f(-100,-90,90);
- glVertex3f(-120,-90,100);
- glVertex3f(-130,-70,90);
- glVertex3f(-111,-66,110);
- glVertex3f(-141,-55,84);
- glVertex3f(-120,-90,120);
- glVertex3f(-130,-49,136);
- glVertex3f(-133,-96,82);
- glVertex3f(-140,-40,90);
- glVertex3f(-150,-50,88);
- glVertex3f(-211,-67,84);
- glVertex3f(-200,-80,8);
- glVertex3f(-100,-60,110);
- glVertex3f(-60,-50,130);
- glVertex3f(-150,-90,128);
- glVertex3f(-127,-100,114);
- glVertex3f(-150,140,-50);
- glVertex3f(-67,180,-80);
- glVertex3f(-60,160,-170);
- glVertex3f(-180,140,-70);
- glVertex3f(-130,188,-70);
- glVertex3f(-99,180,-60);
- glVertex3f(-77,60,-154);
- glVertex3f(-288,90,-66);
- glVertex3f(-100,99,-53);
- glVertex3f(-100,199,-90);
- glVertex3f(-123,155,-128);
- glVertex3f(-77,60,-114);
- glVertex3f(-109,174,-100);
- glVertex3f(-50,90,-63);
- glVertex3f(-120,123,-87);
- glVertex3f(-119,90,-10);
- glVertex3f(-87,210,-170);
- glVertex3f(-100,40,-70);
- glVertex3f(-129,180,-40);
- glVertex3f(-77,60,-124);
- glVertex3f(-215,190,-90);
- glVertex3f(-120,90,-100);
- glVertex3f(-130,70,-90);
- glVertex3f(-111,166,-110);
- glVertex3f(-141,55,-84);
- glVertex3f(-120,90,-120);
- glVertex3f(-133,124,-45);
- glVertex3f(-130,49,-136);
- glVertex3f(-133,96,-82);
- glVertex3f(-140,184,-90);
- glVertex3f(-150,50,-88);
- glVertex3f(-211,152,-84);
- glVertex3f(-200,142,-55);
- glVertex3f(-100,143,-110);
- glVertex3f(-60,165,-130);
- glVertex3f(-150,90,-128);
- glVertex3f(-127,100,-114);
- glVertex3f(-250,50,-158);
- glVertex3f(-217,196,-84);
- glVertex3f(-200,142,-55);
- glVertex3f(-188,60,-169);
- glVertex3f(-146,165,-130);
- glVertex3f(-124,90,-128);
- glVertex3f(-156,100,-178);
- glVertex3f(-150,50,-88);
- glVertex3f(-211,67,-84);
- glVertex3f(-200,142,-0);
- glVertex3f(-100,60,-0);
- glVertex3f(-60,165,-130);
- glVertex3f(-150,90,-128);
- glVertex3f(-127,100,-114);
- glVertex3f(150,-40,-50);
- glVertex3f(67,-80,-80);
- glVertex3f(60,-60,-170);
- glVertex3f(99,-80,-60);
- glVertex3f(77,-160,-154);
- glVertex3f(288,-90,-66);
- glVertex3f(100,-99,-3);
- glVertex3f(100,-170,-16);
- glVertex3f(130,-60,-70);
- glVertex3f(123,50,-128);
- glVertex3f(77,-60,-114);
- glVertex3f(109,-40,-100);
- glVertex3f(150,-90,-63);
- glVertex3f(120,-80,-87);
- glVertex3f(119,-90,-10);
- glVertex3f(113,-80,-99);
- glVertex3f(87,-60,-170);
- glVertex3f(100,-40,-70);
- glVertex3f(129,-80,-40);
- glVertex3f(77,-60,-124);
- glVertex3f(215,-90,-90);
- glVertex3f(120,-90,-100);
- glVertex3f(130,-70,-90);
- glVertex3f(141,-55,-84);
- glVertex3f(120,-90,-120);
- glVertex3f(208,-124,-45);
- glVertex3f(130,-149,-136);
- glVertex3f(140,-140,-90);
- glVertex3f(150,-99,-88);
- glVertex3f(211,-67,-84);
- glVertex3f(250,-121,-110);
- glVertex3f(222,-50,-130);
- glVertex3f(150,-90,-128);
- glVertex3f(244,-300,-114);
- glVertex3f(0,-90,-128);
- glVertex3f(0,66,114);
- glVertex3f(0,88,132);
- glVertex3f(10,80,114);
- glVertex3f(20,66,20);
- glVertex3f(50,90,114);
- glVertex3f(0,-90,114);
- glVertex3f(10,-90,123);
- glVertex3f(19,-90,12);
- glVertex3f(5,-88,115);
- glVertex3f(0,-70,131);
- glVertex3f(40,77,120);
- glVertex3f(61,66,55);
- glVertex3f(40,90,114);
- glVertex3f(0,-90,122);
- glVertex3f(44,-90,111);
- glVertex3f(-19,-90,12);
- glVertex3f(15,-88,115);
- glVertex3f(0,70,122);
- glVertex3f(-40,-77,-120);
- glVertex3f(-61,-66,-55);
- glVertex3f(-40,-90,-114);
- glVertex3f(0,-99,-122);
- glVertex3f(-55,-90,-111);
- glVertex3f(-80,-100,-12);
- glVertex3f(230,0,15);
- glVertex3f(250,10,20);
- glVertex3f(270,10,20);
- glVertex3f(-230,0,-15);
- glVertex3f(-250,10,-20);
- glVertex3f(230,0,-15);
- glVertex3f(-250,10,20);
- glColor3f(1,0,0);
- glVertex3f(100,99,3);
- glVertex3f(50,90,10);
- glVertex3f(260,14,25);
- glVertex3f(200,-80,-122);
- glVertex3f(180,-40,-70);
- glVertex3f(-113,280,-99);
- glVertex3f(-120,-80,8);
- glVertex3f(-133,-124,55);
- glVertex3f(111,-122,-110);
- glVertex3f(222,-96,-99);
- glEnd();
- glFlush();
- //(eyeX,eyeY,eyeZ,centerX,centerY,centerZ,upX,upY,upZ)
- gluLookAt(1, 0.25,0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- // Move the light after we draw the sun!
- glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
- //new
- // glRotatef(fEarthRot, 0.0f, 3.0f, 0.0f);
- // Rotate coordinate system*****(x,y,z)
- // Draw the Mercury
- glRotatef(fEarthRot, 0.0f, 3.0f, 0.0f);
- glColor3ub(114, 155, 145);
- glTranslatef(70.0f,0.0f,0.0f);//****position
- glutSolidSphere(10.0f, 190, 190);//***redios
- glColor3ub(114,0,0);
- glTranslatef(60.0f,0.0f,0.0f);//****position
- glutSolidSphere(20.0f, 190, 190);
- // Draw the Earth
- glColor3ub(0, 0, 145);
- glTranslatef(77.0f,0.0f,0.0f);//****position
- glutSolidSphere(25.0f, 190, 190);//***redios
- // Rotate from Earth based coordinates and draw Moon
- glColor3ub(255,255,255);
- glRotatef(fMoonRot,0.0f, 1.0f, 0.0f);
- glTranslatef(30.0f, 0.0f, 0.0f);
- /*glTranslatef(x, y, z) moves along the X, Y and Z axis, in that order.
- The line of code below moves left on the X axis 1.5 units.
- It does not move on the Y axis at all (0.0),
- and it moves into the screen 6.0 units.
- When you translate, you are not moving a set amount from the center of the screen,
- you are moving a set amount from wherever you currently were on the screen*/
- fMoonRot+= 8.0f;//rotate the moon
- if(fMoonRot > 360.0f)
- fMoonRot = 0.0f;
- glutSolidSphere(5.0f, 180, 180);
- // Restore the matrix state
- glPopMatrix(); // Modelview matrix
- // Step earth orbit 5 degrees
- fEarthRot += 2.0f;
- if(fEarthRot > 360.0f)
- fEarthRot = 0.0f;
- // Show the image**** it swaps the front buffer and back buffer
- glutSwapBuffers();
- }
- // This function does any needed initialization on the rendering
- // context.
- void SetupRC()
- {
- // Light values and coordinates
- glEnable(GL_DEPTH_TEST);
- // Hidden surface removal**/* */
- // glFrontFace(GL_CCW); // Counter clock-wise polygons face out******* no need
- // glEnable(GL_CULL_FACE);
- // Do not calculate inside of jet ******* no need
- // Enable lighting**
- glEnable(GL_LIGHTING);
- // Setup and enable light 0
- //glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);
- glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);
- //glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
- glEnable(GL_LIGHT0);//transparent--When it is enabled, light sources that are enabled contribute to the lighting calculation.-
- // Enable color tracking***************************************
- glEnable(GL_COLOR_MATERIAL);// specifies which material parameters track the current color All the time
- // Set Material properties to follow glColor values
- // glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
- // Black blue background
- // glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
- }
- //motion*********************************
- void TimerFunc(int value)
- {
- glutPostRedisplay();// Post repaint request to activate display
- glutTimerFunc(100, TimerFunc, 1);// // where millis is the delay in milliseconds, value will be passed to the timer function.
- }
- void ChangeSize(int w, int h)
- {
- GLfloat fAspect;
- // Prevent a divide by zero
- if(h == 0)
- h = 1;
- glViewport(0, 0, w, h); // Set Viewport to window dimensions
- /*Viewport: Viewport refers to the display area on the window (screen),
- which is measured in pixels in screen coordinates
- (excluding the title bar).
- */
- // Calculate aspect ratio of the window
- fAspect = (GLfloat)w/(GLfloat)h;
- // Set the perspective coordinate system
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();/*When you do a glLoadIdentity() what you are doing is moving back to the
- center of the screen with the X axis running left to right,
- the Y axis moving up and down, and the Z axis moving into,
- and out of the screen.
- The center of an OpenGL screen is 0.0f on the X and Y axis. To the left of center would be a negative number.
- To tand 425he right would be a positive number.
- Moving towards the top of the screen would be a positive number, moving to the bottom of the screen would be a negative number.
- Moving deeper into the screen is a negative number, moving towards the viewer would be a positive number.*/
- // field of view of 45 degrees, near and far planes 1.0
- gluPerspective(50.0f, fAspect, 1.0, 700.0);//determines the global coordinate system display.
- // Modelview matrix reset
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutCreateWindow("Solar System");
- glutInitWindowSize(500, 500);
- glutReshapeFunc(ChangeSize);
- glutDisplayFunc(RenderScene);
- glutTimerFunc(500, TimerFunc, 1);
- SetupRC();
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement