halfo

Graphics Lab Assignment 1

Dec 9th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. // #pragma comment (lib,"glut32.lib")
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6.  
  7. #include <GL/glut.h>
  8.  
  9. #define BLACK 1, 1, 1
  10.  
  11. double angle_sun, angle_moon, angle_earth;
  12.  
  13. void display () {
  14.     //clear the display
  15.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  16.     glClearColor (BLACK, 0);
  17.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  18.  
  19.     glMatrixMode (GL_MODELVIEW);
  20.     glLoadIdentity ();
  21.  
  22.     // defines a viewing transformation
  23.     gluLookAt (.1, 0, 300, // eye
  24.                 0, 0, 0,   // lookat
  25.                 0, 0, 1);  // up
  26.  
  27.  
  28.     // again select MODEL-VIEW
  29.     glMatrixMode (GL_MODELVIEW);
  30.  
  31.     glColor3f (1, 0, 0);
  32.     glPushMatrix (); {
  33.         glTranslated (0, 0, 0);
  34.         glRotatef (angle_sun, 1, 0, 0);
  35.  
  36.         glRotatef (90, 0, 1, 0);
  37.         glutWireSphere (70,  // GLdouble radius
  38.                         30,  // GLint slices
  39.                         30); // GLint stacks
  40.  
  41.         glColor3f (0, 0, 1);
  42.         glPushMatrix (); {
  43.             glTranslated (0, 150, 0);
  44.             glRotatef (angle_earth, 0, 0, 1);
  45.  
  46.             glRotatef (0, 1, 0, 0);
  47.             glutWireSphere (40,  // GLdouble radius
  48.                             18,  // GLint slices
  49.                             18); // GLint stacks
  50.  
  51.             glColor3f (0, 0, 0);
  52.             glPushMatrix (); {
  53.                 glTranslated (0, 100, 0);
  54.                 glRotatef (angle_moon, 0, 0, 1);
  55.  
  56.                 glutWireSphere (25,  // GLdouble radius
  57.                                 18,  // GLint slices
  58.                                 18); // GLint stacks
  59.             } glPopMatrix ();
  60.         } glPopMatrix ();
  61.     } glPopMatrix ();
  62.  
  63.     // ADD this line in the end --- if you use double buffer (i.e. GL_DOUBLE)
  64.     glutSwapBuffers ();
  65. }
  66.  
  67. void animate(){
  68.     angle_sun += .1;
  69.     angle_earth += .5;
  70.     angle_moon += 2;
  71.     glutPostRedisplay();
  72. }
  73.  
  74. void init () {
  75.     glClearColor (BLACK, 0);
  76.  
  77.     glMatrixMode (GL_PROJECTION); // load the projection matrix
  78.     glLoadIdentity (); // initialize the matrix
  79.     gluPerspective (70, 1, 0.1, 10000.0); // perspective parameters
  80. }
  81.  
  82. int main (int argc, char **argv) {
  83.     glutInit (&argc,argv);
  84.     glutInitWindowSize (900, 700);
  85.     glutInitWindowPosition (300, 200);
  86.     glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB); // depth, double buffer, RGB color
  87.  
  88.     glutCreateWindow ("ID");
  89.     init ();
  90.  
  91.     glEnable (GL_DEPTH_TEST); // enable depth testing
  92.     glutDisplayFunc (display); // display callback function
  93.     glutIdleFunc (animate); //what you want to do in the idle time (when no drawing is occuring)
  94.     glutMainLoop (); // main loop of OpenGL
  95.  
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment