Guest User

Untitled

a guest
Jun 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h> //Needed for "exit" function
  3.  
  4. //Include OpenGL header files, so that we can use OpenGL
  5. #ifdef __APPLE__
  6. #include <OpenGL/OpenGL.h>
  7. #include <GLUT/glut.h>
  8. #else
  9. #include <GL/glut.h>
  10. #endif
  11.  
  12. using namespace std;
  13.  
  14. //VARIABLES//////////////////
  15.  
  16. float M_PI= 3.14159265358979323846;
  17. float rota = 0;
  18.  
  19. static bool keyStates[256];
  20.  
  21.  
  22.  
  23.  
  24. //FUNCTIONS/////////////////
  25.  
  26. void draw_missile_body();
  27.  
  28. //Called when a key is pressed
  29. void handleKeypress(unsigned char key,                                               //The key that was pressed
  30.                     int x, int y) {                                                  //The current mouse coordinates
  31.     switch (key) {
  32.         case 27: //Escape key
  33.             exit(0); //Exit the program
  34.        
  35.     }
  36. }
  37.  
  38. void keyPressed(unsigned char key, int x, int y);                                    //for smooth keyhandling
  39. void KeyOperations(void);
  40.  
  41. //Initializes 3D rendering
  42. void initRendering() {
  43.     //Makes 3D drawing work when something is in front of something else
  44.     glEnable(GL_DEPTH_TEST);
  45. }
  46.  
  47. //Called when the window is resized
  48. void handleResize(int w, int h) {
  49.     //Tell OpenGL how to convert from coordinates to pixel values
  50.     glViewport(0, 0, w, h);
  51.    
  52.     glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
  53.    
  54.     //Set the camera perspective
  55.     glLoadIdentity(); //Reset the camera
  56.     gluPerspective(45.0,                                                      //The camera angle
  57.                    (double)w / (double)h,                                     //The width-to-height ratio
  58.                    1.0,                                                       //The near z clipping coordinate
  59.                    200.0);                                                    //The far z clipping coordinate
  60. }
  61.  
  62. //Draws the 3D scene
  63. void drawScene() {
  64.     KeyOperations();
  65.     //Clear information from last draw
  66.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  67.  
  68.      glMatrixMode(GL_MODELVIEW);                                              //Switch to the drawing perspective
  69.     glLoadIdentity();                                                         //Reset the drawing perspective
  70.  
  71.    
  72.     glTranslatef(0,0,-10);
  73.     glRotatef(rota,0.5,0.5,0);
  74.    
  75.     draw_missile_body();
  76.  
  77.     rota += 0.05;
  78.  
  79.     if(rota > 360)
  80.         rota = 0;
  81.    
  82.    
  83.    
  84.  
  85.      glutSwapBuffers();                                                         //Send the 3D scene to the screen
  86. }
  87.  
  88. void keyPressed(unsigned char key, int x, int y)
  89. {
  90.     keyStates[key] = true;
  91.  
  92.    
  93.     glutPostRedisplay();
  94. }
  95.  
  96. void keyUp (unsigned char key, int x, int y)
  97. {
  98.     keyStates[key] = false;
  99.  
  100.    
  101.  
  102. }
  103.  
  104.  
  105. int main(int argc, char** argv) {
  106.     //Initialize GLUT
  107.     glutInit(&argc, argv);
  108.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  109.     glutInitWindowSize(800, 600); //Set the window size
  110.    
  111.     //Create the window
  112.    
  113.     glutCreateWindow("Missile_Project");
  114.     glClearColor(1,1,1,1);
  115.     initRendering(); //Initialize rendering
  116.  
  117.      //Set handler functions for drawing, keypresses, and window resizes
  118.     glutDisplayFunc(drawScene);
  119.     glutIdleFunc(drawScene);
  120.     glutKeyboardFunc(keyPressed);
  121.     glutKeyboardUpFunc(keyUp);
  122.     glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
  123.     glutReshapeFunc(handleResize);
  124.  
  125.      glutMainLoop();                                                           //Start the main loop.  glutMainLoop doesn't return.
  126.     return 0;                                                                  //This line is never reached
  127. }
  128.  
  129. void KeyOperations(void)
  130. {
  131.     radians = 2*PI*(vinkel/360);
  132.  
  133.     if (keyStates['w'])
  134.     {
  135.        
  136.        
  137.     }
  138.     if(timer > 0.7 && timer < 1.3f)
  139.     {
  140.         accel -= 0.001;
  141.     }
  142.  
  143.     if(timer >= 1.5f)
  144.     {
  145.        
  146.  
  147.         if(accel < 2.0f)
  148.         {
  149.           accel += 0.003;
  150.            
  151.         }
  152.     }
  153.  
  154.         boksx += ((accel*0.05)*cosf(radians +PI/2));
  155.         boksy += ((accel*0.05)*sinf(radians + PI/2));
  156.    
  157.     if(keyStates['w'] == false)
  158.     {
  159.         /*if(accel > 0)
  160.         {
  161.             accel -= 0.0005;}
  162.             */
  163.     }
  164.  
  165.     if (keyStates['s'])
  166.     {boksy -= 0.01;
  167.     acceleration_trigger = false;}
  168.  
  169.     if (keyStates['a'])
  170.     {vinkel += 0.10;}
  171.  
  172.     if (keyStates['d'])
  173.     {vinkel -= 0.10;}
  174.  
  175.    
  176. }
  177.  
  178. void draw_missile_body()
  179. {
  180.    
  181.  
  182.     glColor3f(1,0.5,0);
  183.     glBegin(GL_QUAD_STRIP);                      //Middle section
  184.     {
  185.         glVertex3f(0,0,0);
  186.         glVertex3f(1,0,0);
  187.         glVertex3f(0,1,0);
  188.         glVertex3f(1,1,0);
  189.  
  190.         glVertex3f(0,1,-3);
  191.         glVertex3f(1,1,-3);
  192.  
  193.         glVertex3f(0,0,-4);
  194.         glVertex3f(1,0,-4);
  195.  
  196.         glVertex3f(0,0,0);
  197.         glVertex3f(1,0,0);
  198.     }
  199.     glEnd();
  200.  
  201.     glColor3f(1,0,0);
  202.     glBegin(GL_QUADS);                              //Left panel
  203.     {
  204.         glVertex3f(0,0,0);
  205.         glVertex3f(0,0,-4);
  206.         glVertex3f(0,1,-3);
  207.         glVertex3f(0,1,0);
  208.     }
  209.  
  210.     glEnd();
  211.  
  212.     glBegin(GL_QUADS);                                //Right panel
  213.     {
  214.         glVertex3f(1,0,0);
  215.         glVertex3f(1,0,-4);
  216.         glVertex3f(1,1,-3);
  217.         glVertex3f(1,1,0);
  218.     }
  219.     glEnd();
  220.  
  221.     glColor3f(0,1,0);
  222.  
  223.     glTranslatef(0,0,1);
  224.     glBegin(GL_QUAD_STRIP);                        //right wing
  225.     {
  226.         glVertex3f(1,0.35,-1);
  227.         glVertex3f(2,0.35,-1);
  228.  
  229.         glVertex3f(1,0.70,-1);
  230.         glVertex3f(2,0.70,-1);
  231.  
  232.         glColor3f(0,0,1);
  233.         glVertex3f(1,0.70,-2.2);
  234.         glVertex3f(1.5,0.70,-1.9);
  235.  
  236.         glVertex3f(1.0,0.35,-2.2);
  237.         glVertex3f(1.5,0.35,-1.9);
  238.  
  239.        
  240.  
  241.         glVertex3f(1.0,0.35,-1);
  242.         glVertex3f(2.0,0.35,-1);
  243.  
  244.  
  245.  
  246.        
  247.     }
  248.     glEnd();
  249.  
  250.     glBegin(GL_QUADS);                             //outer wing-panel
  251.  
  252.     glVertex3f(2.0,0.35,-1);
  253.     glVertex3f(1.5,0.35,-1.9);
  254.     glVertex3f(1.5,0.70,-1.9);
  255.     glVertex3f(2.0,0.70,-1);
  256.  
  257.     glEnd();
  258.    
  259.     glPushMatrix();                                //left wing
  260.     {
  261.            
  262.             glRotatef(180,0,0,1);
  263.             glTranslatef(-1,-1,0);
  264.  
  265.         glBegin(GL_QUAD_STRIP);                        
  266.         {
  267.             glVertex3f(1,0.35,-1);
  268.             glVertex3f(2,0.35,-1);
  269.  
  270.             glVertex3f(1,0.70,-1);
  271.             glVertex3f(2,0.70,-1);
  272.  
  273.            
  274.             glVertex3f(1,0.70,-2.2);
  275.             glVertex3f(1.5,0.70,-1.9);
  276.  
  277.             glVertex3f(1.0,0.35,-2.2);
  278.             glVertex3f(1.5,0.35,-1.9);
  279.  
  280.        
  281.  
  282.             glVertex3f(1.0,0.35,-1);
  283.             glVertex3f(2.0,0.35,-1);
  284.  
  285.  
  286.  
  287.        
  288.         }
  289.         glEnd();
  290.  
  291.         glBegin(GL_QUADS);                              //outer wing-panel
  292.         {
  293.  
  294.             glVertex3f(2.0,0.35,-1);
  295.             glVertex3f(1.5,0.35,-1.9);
  296.             glVertex3f(1.5,0.70,-1.9);
  297.             glVertex3f(2.0,0.70,-1);
  298.  
  299.         }  
  300.         glEnd();
  301.  
  302.     }
  303.     glPopMatrix();
  304.  
  305.  
  306. }
Add Comment
Please, Sign In to add comment