Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <glut.h>
- float angle = 0.0f;
- bool pause = false;
- double zoom_in_out_x = 200.0f;
- double zoom_in_out_y = 200.0f;
- double zoom_in_out_z = 0.0f;
- double upvector_x = 1.0f;
- double upvector_y = 0.0f;
- double upvector_z = 1.0f;
- void renderScene(void);
- void changeSize(int width, int height);
- void processSpecialKeys(int key, int x, int y);
- void processNormalKeys(unsigned char key, int x, int y);
- int main(int argc, char **argv)
- {
- // initialize glut and create window
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowPosition(100, 100);
- glutInitWindowSize(320, 320);
- glutCreateWindow("Solar System");
- // register callback functions
- glutDisplayFunc(renderScene);
- glutReshapeFunc(changeSize);
- glutIdleFunc(renderScene);
- // register keyboard entry
- glutSpecialFunc(processSpecialKeys);
- glutKeyboardFunc(processNormalKeys);
- // enter glut event processing loop
- glutMainLoop();
- return 1;
- }
- void renderScene(void)
- {
- // clear color and depth buffers
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // reset transformations
- glLoadIdentity();
- // set camera initial position
- gluLookAt(0.0f, zoom_in_out_y, zoom_in_out_z,
- 0.0f, 0.0f, 0.0f,
- upvector_x, upvector_y, upvector_z);
- // draw earth
- glPushMatrix();
- glColor3f(0.0, 1.0, 0.0);
- glRotatef(angle, 0.0f, 1.0f, 0.0f);
- glTranslatef(0.0f, 0.0f, 50.0f);
- glutSolidSphere(10, 50, 50);
- //draw moon
- glPushMatrix();
- glColor3f(0.5, 0.5, 0.5);
- glRotatef(angle, 0.0f, 1.0f, 0.0f);
- glTranslatef(0.0f, 0.0f, 20.0f);
- glutSolidSphere(5, 50, 50);
- glPopMatrix();
- glPopMatrix();
- // draw sun
- glPushMatrix();
- glColor3f(1.0f, 1.0f, 0.0f);
- glTranslatef(0.0f, 0.0f, 0.0f);
- glutSolidSphere(20, 50, 50);
- glPopMatrix();
- //draw stars
- for (int i = 1; i <= 5; i++)
- {
- glPushMatrix();
- glColor3f(1.0f, 1.0f, 1.0f);
- glTranslatef(50.0f, 50.0f, 0.0f + i * 10);
- glutSolidSphere(2, 50, 50);
- glPopMatrix();
- glPushMatrix();
- glColor3f(1.0f, 1.0f, 1.0f);
- glTranslatef(-50.0f, 50.0f, 0.0f + i * 10);
- glutSolidSphere(2, 50, 50);
- glPopMatrix();
- glPushMatrix();
- glColor3f(1.0f, 1.0f, 1.0f);
- glTranslatef(50.0f, -50.0f, 0.0f + i * 10);
- glutSolidSphere(2, 50, 50);
- glPopMatrix();
- glPushMatrix();
- glColor3f(1.0f, 1.0f, 1.0f);
- glTranslatef(50.0f, 50.0f, 0.0f - i * 10);
- glutSolidSphere(2, 50, 50);
- glPopMatrix();
- }
- //rotate
- if(pause == false)
- angle += 0.1f;
- glutSwapBuffers();
- }
- void changeSize(int width, int height)
- {
- // prevent division by zero when window is too short
- if (height == 0) height = 1;
- float ratio = 1.0* width / height;
- // use the projection matrix
- glMatrixMode(GL_PROJECTION);
- // reset matrix
- glLoadIdentity();
- // set the viewport to be the entire window
- glViewport(0, 0, width, height);
- // set the correct perspective
- gluPerspective(45, ratio, 1, 1000);
- // Get Back to the Modelview
- glMatrixMode(GL_MODELVIEW);
- }
- void processNormalKeys(unsigned char key, int x, int y)
- {
- switch (key)
- {
- // press esc to exit
- case 27:
- exit(0);break;
- // press space to pause rotation
- case 32:
- pause = !pause; break;
- // press + to zoom in
- case 43:
- zoom_in_out_y -= 5; break;
- // press - to zoom out
- case 45:
- zoom_in_out_y += 5; break;
- default:break;
- }
- }
- void processSpecialKeys(int key, int x, int y)
- {
- switch (key)
- {
- // press left key to rotate camera angle left
- case GLUT_KEY_LEFT:
- upvector_x -= 0.1f;
- upvector_y += 0.1f;
- upvector_z += 0.1f;
- zoom_in_out_x += 20;
- zoom_in_out_z -= 20;
- break;
- // press right key to rotate camera angle right
- case GLUT_KEY_RIGHT:
- upvector_x += 0.1f;
- upvector_y -= 0.1f;
- upvector_z -= 0.1f;
- zoom_in_out_x -= 20;
- zoom_in_out_z += 20;
- break;
- // press up key to rotate camera angle up
- case GLUT_KEY_UP:
- upvector_x += 0.1f;
- upvector_y -= 0.1f;
- upvector_z += 0.1f;
- zoom_in_out_y += 20;
- zoom_in_out_z -= 20;
- break;
- // press down key to rotate camera angle down
- case GLUT_KEY_DOWN:
- upvector_x -= 0.1f;
- upvector_y += 0.1f;
- upvector_z -= 0.1f;
- zoom_in_out_y -= 20;
- zoom_in_out_z += 20;
- break;
- default:break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement