Guest User

Untitled

a guest
May 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.79 KB | None | 0 0
  1. // Geom.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <Gl/glut.h>
  5. #include <math.h>
  6.  
  7. class Chopper
  8. {
  9. private:
  10.  
  11.  
  12.     float rotorDegreesToRotate;                     // The contionus amount of degrees that the rotor should roate to  get a smooth animation.
  13.     float rotorRotationSpeed;                       // The speed that the rotor should rotate at, directly affecting the above variable
  14.     float chopperXAxisSpeed;                        // defines the positive, and negative speed that the chopper moves in the X-
  15.     float chopperYAxisSpeed;                        // and Y-axis,
  16.     float chopperRotation;                          // The current Rotation of the chopper, affecting steering and speed
  17.     float chopperCurrentHeight;
  18.  
  19.     bool chopperEngineOn;                           // Determines wether the engine of the chopper is turned on or off.
  20.     bool chopperInAir;                              // Determines wether the chopper is in the air, thus wether or not the player can control the aircraft.
  21.  
  22. public:
  23.     Chopper();
  24.  
  25.     void chopperMainBody(float xAcceleration, float yAcceleration);
  26.     void chopperRotor(float rotorSpeed, bool EngineOn);
  27.     void chopperLiftoff();
  28.  
  29.     float chopperLiftoffSequense();
  30.  
  31.     int rotorDegreeCheck();
  32.     bool chopperIsFlying();
  33.  
  34. };
  35.  
  36. class ChopperMovement
  37. {
  38. private:
  39.     bool accelerateForward;
  40.     bool accelerateBackward;
  41.     bool accelerateLeft;
  42.     bool accelerateRight;
  43.  
  44. public:
  45.     ChopperMovement();
  46.  
  47.     void accelerationInputKey(unsigned char keyPress);
  48.  
  49.     bool keyPressedControl();
  50.  
  51. };
  52.  
  53. int w = 800, h = 800;//Bredde og høyde på vindu
  54.  
  55. //Egendefinerte funksjoner
  56.  
  57.  
  58.  
  59. void initOpenGL(); //Setter parametre
  60.  
  61. void display(); //Kalles av systemet. Kaller geom().
  62. void reshape(int bredde, int hoyde); //Kalles av systemet
  63. void keyboard(unsigned char key, int x, int y);
  64. void idle(); //Kalles av systemet
  65.  
  66. const float MAX_ROTOR_SPEED = 0.5f;
  67. const float MAX_FLYING_HEIGHT = 3.0f;
  68. const float CHOPPER_TAKEOFF_LIMIT = 0.1f;
  69. const float CHOPPER_TAKEOFF_HEIGHTGAIN_RATE = 0.00005f;
  70. const float ROTOR_ACCELERATION_RATE = 0.000025f;                                                    // CONSTANTS!!!!!!!!!
  71. const float FULL_ROTATION = 360.0f;
  72.  
  73.  
  74. const float CHOPPER_X_LENGTH = 0.5;
  75. const float CHOPPER_Y_LENGTH = 1.5;
  76.  
  77. const float ROTOR_BLADE_LENGTH = 2;
  78. const float ROTOR_BLADE_WIDTH = 0.075;
  79.  
  80.  
  81. Chopper chopper1337;
  82. ChopperMovement chopper1337Movement;
  83.  
  84. int main(int argc, char* argv[])
  85. {
  86.     //Disse 5 funksjonene må være tilstede
  87.     glutInit(&argc, argv);
  88.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  89.     glutInitWindowSize(w, h);
  90.     glutInitWindowPosition(100, 150);
  91.     glutCreateWindow("Geometri-demo");
  92.  
  93.     //Callback-funksjoner
  94.     glutDisplayFunc(display);
  95.     glutReshapeFunc(reshape);
  96.     glutKeyboardFunc(keyboard);
  97.     glutIdleFunc(idle);
  98.     //Setter startparametre
  99.     initOpenGL();
  100.     //Denne funksjonen må stå til slutt i main()
  101.     glutMainLoop();
  102.     return 0;
  103. }
  104.  
  105. void initOpenGL()
  106. {
  107.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);//Hvit bakgrunn
  108.     //Funksjonene under setter koordinatene til systemet vi vil tegne i
  109.     glMatrixMode(GL_PROJECTION);
  110.     glLoadIdentity();
  111.     //Koordinatsystem med -sizex <= x <= sizex, -sizey <= y <= sizey
  112.     gluOrtho2D(-12.0f, 12.0f, -12.0f, 12.0f);
  113.     //Gjøre klar for å transformere tegninger
  114.     glMatrixMode(GL_MODELVIEW);
  115.     glLoadIdentity();
  116. }
  117.  
  118. //////////CALLBACKS//////////////////////////////
  119.  
  120. float alpha = 0;
  121. void display()          // This displays the objects to the screen
  122. {
  123.     glClear(GL_COLOR_BUFFER_BIT);//Blanker skjermbuffer
  124.  
  125.    
  126.  
  127.  
  128.     if (chopper1337.chopperIsFlying())
  129.     {
  130.         glLoadIdentity();
  131.         glScalef(chopper1337.chopperLiftoffSequense(), chopper1337.chopperLiftoffSequense(), 0.0f);
  132.     }
  133.  
  134.     if (chopper1337Movement.keyPressedControl())
  135.         glTranslatef(0.0f, 0.0005f, 0.0);
  136.  
  137.     chopper1337.chopperMainBody(0.0,0.0);
  138.  
  139.     glPushMatrix();
  140.         glRotatef(chopper1337.rotorDegreeCheck(), 0.0f, 0.0f, 1.0f);
  141.         chopper1337.chopperRotor(0.0,true);
  142.     glPopMatrix();
  143.  
  144.  
  145.  
  146.     glutSwapBuffers();//Bytter buffer
  147. }
  148.  
  149. void idle()
  150. {
  151.     glutPostRedisplay();
  152. }
  153.  
  154. void reshape(int bredde, int hoyde)
  155. {
  156.     //Kalles av systemet ved endring av vindu
  157.     glViewport(0, hoyde - h, w, h);
  158. }
  159.  
  160. void keyboard(unsigned char key, int x, int y)
  161. {
  162.     switch(key) {
  163.    
  164.  
  165.         case 27 : exit(0); break;
  166.         case 87 : chopper1337Movement.accelerationInputKey(key); break;
  167.     }
  168.     glutPostRedisplay();
  169. }
  170.  
  171. /////////////////////////Graphics Objects////////////////////////
  172.  
  173. Chopper::Chopper()
  174. {
  175.  
  176.     rotorRotationSpeed = 0.0f;
  177.     rotorDegreesToRotate = 0.0f;
  178.     chopperCurrentHeight = 1.0f;
  179. }
  180.  
  181. void Chopper :: chopperMainBody(float xAxisAcceleration, float yAxisAcceleration)
  182. {
  183.     glColor3f(1.0f,1.0f,1.0f);
  184.     glBegin(GL_TRIANGLES);
  185.         glVertex2f(-CHOPPER_X_LENGTH/2, 0.0f);
  186.         glVertex2f(CHOPPER_X_LENGTH/2, 0.0f);
  187.         glVertex2f(0.0f, CHOPPER_Y_LENGTH/2);
  188.         glVertex2f(-CHOPPER_X_LENGTH/2, 0.0f);
  189.         glVertex2f(CHOPPER_X_LENGTH/2, 0.0f);
  190.         glVertex2f(0.0f, -CHOPPER_Y_LENGTH/2);
  191.  
  192.     glEnd();
  193.  
  194. }
  195.  
  196. void Chopper :: chopperRotor(float RotorSpeed, bool EngineStatus)
  197. {
  198.     glColor3f(0.33f,0.33f,0.33f);
  199.     glBegin(GL_QUADS);
  200.         glVertex2f(-ROTOR_BLADE_LENGTH/2, -ROTOR_BLADE_WIDTH/2);
  201.         glVertex2f(ROTOR_BLADE_LENGTH/2, -ROTOR_BLADE_WIDTH/2); // draws horisontal rotor-blade
  202.         glVertex2f(ROTOR_BLADE_LENGTH/2, ROTOR_BLADE_WIDTH/2);
  203.         glVertex2f(-ROTOR_BLADE_LENGTH/2, ROTOR_BLADE_WIDTH/2);
  204.  
  205.         glVertex2f(-ROTOR_BLADE_WIDTH/2, -ROTOR_BLADE_LENGTH/2);
  206.         glVertex2f(ROTOR_BLADE_WIDTH/2, -ROTOR_BLADE_LENGTH/2); // draws vertical rotor-blade
  207.         glVertex2f(ROTOR_BLADE_WIDTH/2, ROTOR_BLADE_LENGTH/2);
  208.         glVertex2f(-ROTOR_BLADE_WIDTH/2, ROTOR_BLADE_LENGTH/2);
  209.     glEnd();
  210. }
  211.  
  212. void Chopper :: chopperLiftoff()
  213. {
  214.     chopperInAir = true;
  215. }
  216.  
  217. float Chopper ::chopperLiftoffSequense()
  218. {
  219.     if (chopperCurrentHeight < MAX_FLYING_HEIGHT)
  220.     {
  221.         chopperCurrentHeight += CHOPPER_TAKEOFF_HEIGHTGAIN_RATE;
  222.         return chopperCurrentHeight;
  223.     }
  224.     else
  225.         return chopperCurrentHeight;
  226.  
  227. }
  228.  
  229. bool Chopper :: chopperIsFlying()
  230. {
  231.     return chopperInAir;                                    // returns wether the chopper is in the air or not.
  232. }
  233.  
  234. int Chopper ::rotorDegreeCheck()
  235. {
  236.     rotorDegreesToRotate += rotorRotationSpeed;
  237.  
  238.     if (rotorRotationSpeed < MAX_ROTOR_SPEED)               // the acceleration rate grows as the chopper engine starts up.
  239.         rotorRotationSpeed += ROTOR_ACCELERATION_RATE;      // peaking at the set max
  240.  
  241.     if (rotorDegreesToRotate >= FULL_ROTATION)  // This ensures that the degrees total does not grow larger the longer the program runs
  242.         rotorDegreesToRotate -= FULL_ROTATION;  // ensuring that the variable does not become to large.
  243.  
  244.     if (rotorRotationSpeed > (MAX_ROTOR_SPEED - CHOPPER_TAKEOFF_LIMIT))
  245.         chopperLiftoff();
  246.  
  247.     return rotorDegreesToRotate;
  248. }
  249.  
  250.  
  251.  
  252. ChopperMovement::ChopperMovement()
  253. {
  254.     accelerateForward = false;
  255.     accelerateBackward = false;
  256.     accelerateLeft= false;
  257.     accelerateRight = false;
  258.  
  259. }
  260.  
  261. void ChopperMovement::accelerationInputKey(unsigned char accelerationKey)
  262. {
  263.    
  264.     accelerateForward = true;
  265.    
  266. }
  267.  
  268. bool ChopperMovement::keyPressedControl()
  269. {
  270.     return accelerateForward;
  271. }
Add Comment
Please, Sign In to add comment