// Geom.cpp : Defines the entry point for the console application. // #include #include class Chopper { private: float rotorDegreesToRotate; // The contionus amount of degrees that the rotor should roate to get a smooth animation. float rotorRotationSpeed; // The speed that the rotor should rotate at, directly affecting the above variable float chopperXAxisSpeed; // defines the positive, and negative speed that the chopper moves in the X- float chopperYAxisSpeed; // and Y-axis, float chopperRotation; // The current Rotation of the chopper, affecting steering and speed float chopperCurrentHeight; bool chopperEngineOn; // Determines wether the engine of the chopper is turned on or off. bool chopperInAir; // Determines wether the chopper is in the air, thus wether or not the player can control the aircraft. public: Chopper(); void chopperMainBody(float xAcceleration, float yAcceleration); void chopperRotor(float rotorSpeed, bool EngineOn); void chopperLiftoff(); float chopperLiftoffSequense(); int rotorDegreeCheck(); bool chopperIsFlying(); }; class ChopperMovement { private: bool accelerateForward; bool accelerateBackward; bool accelerateLeft; bool accelerateRight; public: ChopperMovement(); void accelerationInputKey(unsigned char keyPress); bool keyPressedControl(); }; int w = 800, h = 800;//Bredde og høyde på vindu //Egendefinerte funksjoner void initOpenGL(); //Setter parametre void display(); //Kalles av systemet. Kaller geom(). void reshape(int bredde, int hoyde); //Kalles av systemet void keyboard(unsigned char key, int x, int y); void idle(); //Kalles av systemet const float MAX_ROTOR_SPEED = 0.5f; const float MAX_FLYING_HEIGHT = 3.0f; const float CHOPPER_TAKEOFF_LIMIT = 0.1f; const float CHOPPER_TAKEOFF_HEIGHTGAIN_RATE = 0.00005f; const float ROTOR_ACCELERATION_RATE = 0.000025f; // CONSTANTS!!!!!!!!! const float FULL_ROTATION = 360.0f; const float CHOPPER_X_LENGTH = 0.5; const float CHOPPER_Y_LENGTH = 1.5; const float ROTOR_BLADE_LENGTH = 2; const float ROTOR_BLADE_WIDTH = 0.075; Chopper chopper1337; ChopperMovement chopper1337Movement; int main(int argc, char* argv[]) { //Disse 5 funksjonene må være tilstede glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(w, h); glutInitWindowPosition(100, 150); glutCreateWindow("Geometri-demo"); //Callback-funksjoner glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutIdleFunc(idle); //Setter startparametre initOpenGL(); //Denne funksjonen må stå til slutt i main() glutMainLoop(); return 0; } void initOpenGL() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f);//Hvit bakgrunn //Funksjonene under setter koordinatene til systemet vi vil tegne i glMatrixMode(GL_PROJECTION); glLoadIdentity(); //Koordinatsystem med -sizex <= x <= sizex, -sizey <= y <= sizey gluOrtho2D(-12.0f, 12.0f, -12.0f, 12.0f); //Gjøre klar for å transformere tegninger glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } //////////CALLBACKS////////////////////////////// float alpha = 0; void display() // This displays the objects to the screen { glClear(GL_COLOR_BUFFER_BIT);//Blanker skjermbuffer if (chopper1337.chopperIsFlying()) { glLoadIdentity(); glScalef(chopper1337.chopperLiftoffSequense(), chopper1337.chopperLiftoffSequense(), 0.0f); } if (chopper1337Movement.keyPressedControl()) glTranslatef(0.0f, 0.0005f, 0.0); chopper1337.chopperMainBody(0.0,0.0); glPushMatrix(); glRotatef(chopper1337.rotorDegreeCheck(), 0.0f, 0.0f, 1.0f); chopper1337.chopperRotor(0.0,true); glPopMatrix(); glutSwapBuffers();//Bytter buffer } void idle() { glutPostRedisplay(); } void reshape(int bredde, int hoyde) { //Kalles av systemet ved endring av vindu glViewport(0, hoyde - h, w, h); } void keyboard(unsigned char key, int x, int y) { switch(key) { case 27 : exit(0); break; case 87 : chopper1337Movement.accelerationInputKey(key); break; } glutPostRedisplay(); } /////////////////////////Graphics Objects//////////////////////// Chopper::Chopper() { rotorRotationSpeed = 0.0f; rotorDegreesToRotate = 0.0f; chopperCurrentHeight = 1.0f; } void Chopper :: chopperMainBody(float xAxisAcceleration, float yAxisAcceleration) { glColor3f(1.0f,1.0f,1.0f); glBegin(GL_TRIANGLES); glVertex2f(-CHOPPER_X_LENGTH/2, 0.0f); glVertex2f(CHOPPER_X_LENGTH/2, 0.0f); glVertex2f(0.0f, CHOPPER_Y_LENGTH/2); glVertex2f(-CHOPPER_X_LENGTH/2, 0.0f); glVertex2f(CHOPPER_X_LENGTH/2, 0.0f); glVertex2f(0.0f, -CHOPPER_Y_LENGTH/2); glEnd(); } void Chopper :: chopperRotor(float RotorSpeed, bool EngineStatus) { glColor3f(0.33f,0.33f,0.33f); glBegin(GL_QUADS); glVertex2f(-ROTOR_BLADE_LENGTH/2, -ROTOR_BLADE_WIDTH/2); glVertex2f(ROTOR_BLADE_LENGTH/2, -ROTOR_BLADE_WIDTH/2); // draws horisontal rotor-blade glVertex2f(ROTOR_BLADE_LENGTH/2, ROTOR_BLADE_WIDTH/2); glVertex2f(-ROTOR_BLADE_LENGTH/2, ROTOR_BLADE_WIDTH/2); glVertex2f(-ROTOR_BLADE_WIDTH/2, -ROTOR_BLADE_LENGTH/2); glVertex2f(ROTOR_BLADE_WIDTH/2, -ROTOR_BLADE_LENGTH/2); // draws vertical rotor-blade glVertex2f(ROTOR_BLADE_WIDTH/2, ROTOR_BLADE_LENGTH/2); glVertex2f(-ROTOR_BLADE_WIDTH/2, ROTOR_BLADE_LENGTH/2); glEnd(); } void Chopper :: chopperLiftoff() { chopperInAir = true; } float Chopper ::chopperLiftoffSequense() { if (chopperCurrentHeight < MAX_FLYING_HEIGHT) { chopperCurrentHeight += CHOPPER_TAKEOFF_HEIGHTGAIN_RATE; return chopperCurrentHeight; } else return chopperCurrentHeight; } bool Chopper :: chopperIsFlying() { return chopperInAir; // returns wether the chopper is in the air or not. } int Chopper ::rotorDegreeCheck() { rotorDegreesToRotate += rotorRotationSpeed; if (rotorRotationSpeed < MAX_ROTOR_SPEED) // the acceleration rate grows as the chopper engine starts up. rotorRotationSpeed += ROTOR_ACCELERATION_RATE; // peaking at the set max if (rotorDegreesToRotate >= FULL_ROTATION) // This ensures that the degrees total does not grow larger the longer the program runs rotorDegreesToRotate -= FULL_ROTATION; // ensuring that the variable does not become to large. if (rotorRotationSpeed > (MAX_ROTOR_SPEED - CHOPPER_TAKEOFF_LIMIT)) chopperLiftoff(); return rotorDegreesToRotate; } ChopperMovement::ChopperMovement() { accelerateForward = false; accelerateBackward = false; accelerateLeft= false; accelerateRight = false; } void ChopperMovement::accelerationInputKey(unsigned char accelerationKey) { accelerateForward = true; } bool ChopperMovement::keyPressedControl() { return accelerateForward; }