Advertisement
Guest User

asdfas

a guest
Nov 28th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | None | 0 0
  1.  
  2. #include <GL/glut.h> //for all OpenGL functionality
  3.  
  4. //#include "stdafx.h"
  5. // Define initial values for components
  6. static int body = 0, right = 0,left=0,rudder=0;
  7.  
  8. int font=(int)GLUT_BITMAP_8_BY_13;
  9.  
  10.  
  11. void init(void)
  12. {
  13.   glClearColor (0.0, 0.0, 0.0, 0.0);
  14. }
  15.  
  16. // convenience function for displaying strings
  17. void renderBitmapString(float x, float y, void *font,char *string)
  18. {  
  19.   char *c;
  20.  
  21.   glRasterPos3f(x, y,0.0);
  22.  
  23.   for (c=string; *c != '\0'; c++) {
  24.     glutBitmapCharacter(font, *c);
  25.   }
  26.  
  27. }
  28.  
  29. void display(void)
  30. {
  31.    glClear (GL_COLOR_BUFFER_BIT);
  32.     glColor3f (1.0, 1.0, 0.0);    
  33.  
  34.  
  35.     // Directions
  36.     renderBitmapString(-3,5.3,(void *)font,"Satellite Animation v1.0");
  37.     renderBitmapString(-3,5,(void *)font,"press b to rotate body");
  38.     renderBitmapString(-3,4.7,(void *)font,"press r - rotate flaps");
  39.     renderBitmapString(-3,4.4,(void *)font,"press l - rotate left flaps");
  40.     renderBitmapString(-3,4.1,(void *)font,"press m - rotate left rudder");
  41.     renderBitmapString(-3,3.8,(void *)font,"press a - rotate all components");
  42.    
  43.      glColor3f (1.0, 1.0, 1.0);
  44.    // Create the body
  45.    // Push and Pop are used to nest levels of control for the geometric transformation
  46.    // This nesting allows for controlling transformation on the entire object and subcomponents
  47.  
  48.    glPushMatrix();
  49.  
  50.        // The Body
  51.        glPushMatrix();  
  52.        // Rotate about the x-axis
  53.        glRotatef ((GLfloat) body, 1.0, 0.0, 0.0);      
  54.        glutWireSphere (1.5,10, 10);  
  55.  
  56.        // Create a Right Wing  
  57.        glPushMatrix();
  58.        glRotatef ((GLfloat) right, 1.0, 0.0, 0.0);
  59.        glPushMatrix();
  60.        glTranslatef (3.0, 0.0, 0.0);  
  61.        glScalef (3.0, 0.1, 0.7);
  62.        glutWireCube (1.0);
  63.        glPopMatrix();
  64.  
  65.        // Create a Left Wing  
  66.        glPushMatrix();
  67.        glRotatef ((GLfloat) left, 1.0, 0.0, 0.0);  // X-axis rotation
  68.        glTranslatef (-3.0, 0.0, 0.0);  
  69.        glPushMatrix();
  70.        glScalef (3.0, 0.1, 0.7);
  71.        glutWireCube (1.0);
  72.        glPopMatrix();
  73.  
  74.        // Create rudder on Left wing  
  75.        glPushMatrix();
  76.        glRotatef ((GLfloat) rudder, 0.0, 0.0, 1.0);  // Z-axis rotation
  77.        glPushMatrix();    
  78.        glScalef (0.1, 2.0, 0.7);
  79.        glutWireCube (1.0);
  80.        glPopMatrix();
  81.  
  82.        // For each Push we must have a pop
  83.       glPopMatrix();
  84.       glPopMatrix();
  85.       glPopMatrix();  
  86.       glPopMatrix();
  87.       glPopMatrix();
  88.      
  89.  
  90.    glutSwapBuffers();
  91. }
  92.  
  93. void reshape (int w, int h)
  94. {
  95.    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  96.    glMatrixMode (GL_PROJECTION);
  97.    glLoadIdentity ();  
  98.    gluPerspective(100.0, (GLfloat) w/(GLfloat) h, 2.0, 80.0);
  99.    glMatrixMode(GL_MODELVIEW);
  100.    glLoadIdentity();
  101.    glTranslatef (0.0, 0.0, -5.0);
  102. }
  103.  
  104. void keyboard (unsigned char key, int x, int y)
  105. {
  106.    switch (key) {
  107.       case 'b':   /*  b key rotates body  */
  108.          body = (body + 5) % 360;
  109.          glutPostRedisplay();
  110.          break;    
  111.       case 'r':  /*  r key rotates right wing  */
  112.          right = (right + 5) % 360;
  113.          glutPostRedisplay();
  114.          break;    
  115.       case 'l':  /*  l key rotates left wing  */
  116.          left = (left - 5) % 360;
  117.          glutPostRedisplay();
  118.          break;
  119.        case 'm': /* m key rotates rudder on left wing  */
  120.          rudder = (rudder - 5) % 360;
  121.          glutPostRedisplay();
  122.          break;
  123.        case 'a': /* a key performs all movements  */
  124.          body = (body - 5) % 360;
  125.          right = (right - 5) % 360;
  126.          left = (left - 5) % 360;
  127.          rudder = (rudder - 5) % 360;
  128.          glutPostRedisplay();
  129.          break;
  130.        default:
  131.          break;
  132.    }
  133. }
  134.  
  135.  
  136. int main(int argc, char** argv)
  137. {
  138.    glutInit(&argc, argv);
  139.    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
  140.    glutInitWindowSize (500, 500);
  141.    glutInitWindowPosition (100, 100);
  142.    glutCreateWindow (argv[0]);
  143.    init ();  
  144.    glutDisplayFunc(display);    
  145.    glutReshapeFunc(reshape);
  146.    glutKeyboardFunc(keyboard);
  147.    
  148.    glutMainLoop();
  149.    return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement