Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h> //Needed for "exit" function
- //Include OpenGL header files, so that we can use OpenGL
- #ifdef __APPLE__
- #include <OpenGL/OpenGL.h>
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- using namespace std;
- //VARIABLES//////////////////
- float M_PI= 3.14159265358979323846;
- float rota = 0;
- static bool keyStates[256];
- //FUNCTIONS/////////////////
- void draw_missile_body();
- //Called when a key is pressed
- void handleKeypress(unsigned char key, //The key that was pressed
- int x, int y) { //The current mouse coordinates
- switch (key) {
- case 27: //Escape key
- exit(0); //Exit the program
- }
- }
- void keyPressed(unsigned char key, int x, int y); //for smooth keyhandling
- void KeyOperations(void);
- //Initializes 3D rendering
- void initRendering() {
- //Makes 3D drawing work when something is in front of something else
- glEnable(GL_DEPTH_TEST);
- }
- //Called when the window is resized
- void handleResize(int w, int h) {
- //Tell OpenGL how to convert from coordinates to pixel values
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
- //Set the camera perspective
- glLoadIdentity(); //Reset the camera
- gluPerspective(45.0, //The camera angle
- (double)w / (double)h, //The width-to-height ratio
- 1.0, //The near z clipping coordinate
- 200.0); //The far z clipping coordinate
- }
- //Draws the 3D scene
- void drawScene() {
- KeyOperations();
- //Clear information from last draw
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
- glLoadIdentity(); //Reset the drawing perspective
- glTranslatef(0,0,-10);
- glRotatef(rota,0.5,0.5,0);
- draw_missile_body();
- rota += 0.05;
- if(rota > 360)
- rota = 0;
- glutSwapBuffers(); //Send the 3D scene to the screen
- }
- void keyPressed(unsigned char key, int x, int y)
- {
- keyStates[key] = true;
- glutPostRedisplay();
- }
- void keyUp (unsigned char key, int x, int y)
- {
- keyStates[key] = false;
- }
- int main(int argc, char** argv) {
- //Initialize GLUT
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(800, 600); //Set the window size
- //Create the window
- glutCreateWindow("Missile_Project");
- glClearColor(1,1,1,1);
- initRendering(); //Initialize rendering
- //Set handler functions for drawing, keypresses, and window resizes
- glutDisplayFunc(drawScene);
- glutIdleFunc(drawScene);
- glutKeyboardFunc(keyPressed);
- glutKeyboardUpFunc(keyUp);
- glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
- glutReshapeFunc(handleResize);
- glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
- return 0; //This line is never reached
- }
- void KeyOperations(void)
- {
- radians = 2*PI*(vinkel/360);
- if (keyStates['w'])
- {
- }
- if(timer > 0.7 && timer < 1.3f)
- {
- accel -= 0.001;
- }
- if(timer >= 1.5f)
- {
- if(accel < 2.0f)
- {
- accel += 0.003;
- }
- }
- boksx += ((accel*0.05)*cosf(radians +PI/2));
- boksy += ((accel*0.05)*sinf(radians + PI/2));
- if(keyStates['w'] == false)
- {
- /*if(accel > 0)
- {
- accel -= 0.0005;}
- */
- }
- if (keyStates['s'])
- {boksy -= 0.01;
- acceleration_trigger = false;}
- if (keyStates['a'])
- {vinkel += 0.10;}
- if (keyStates['d'])
- {vinkel -= 0.10;}
- }
- void draw_missile_body()
- {
- glColor3f(1,0.5,0);
- glBegin(GL_QUAD_STRIP); //Middle section
- {
- glVertex3f(0,0,0);
- glVertex3f(1,0,0);
- glVertex3f(0,1,0);
- glVertex3f(1,1,0);
- glVertex3f(0,1,-3);
- glVertex3f(1,1,-3);
- glVertex3f(0,0,-4);
- glVertex3f(1,0,-4);
- glVertex3f(0,0,0);
- glVertex3f(1,0,0);
- }
- glEnd();
- glColor3f(1,0,0);
- glBegin(GL_QUADS); //Left panel
- {
- glVertex3f(0,0,0);
- glVertex3f(0,0,-4);
- glVertex3f(0,1,-3);
- glVertex3f(0,1,0);
- }
- glEnd();
- glBegin(GL_QUADS); //Right panel
- {
- glVertex3f(1,0,0);
- glVertex3f(1,0,-4);
- glVertex3f(1,1,-3);
- glVertex3f(1,1,0);
- }
- glEnd();
- glColor3f(0,1,0);
- glTranslatef(0,0,1);
- glBegin(GL_QUAD_STRIP); //right wing
- {
- glVertex3f(1,0.35,-1);
- glVertex3f(2,0.35,-1);
- glVertex3f(1,0.70,-1);
- glVertex3f(2,0.70,-1);
- glColor3f(0,0,1);
- glVertex3f(1,0.70,-2.2);
- glVertex3f(1.5,0.70,-1.9);
- glVertex3f(1.0,0.35,-2.2);
- glVertex3f(1.5,0.35,-1.9);
- glVertex3f(1.0,0.35,-1);
- glVertex3f(2.0,0.35,-1);
- }
- glEnd();
- glBegin(GL_QUADS); //outer wing-panel
- glVertex3f(2.0,0.35,-1);
- glVertex3f(1.5,0.35,-1.9);
- glVertex3f(1.5,0.70,-1.9);
- glVertex3f(2.0,0.70,-1);
- glEnd();
- glPushMatrix(); //left wing
- {
- glRotatef(180,0,0,1);
- glTranslatef(-1,-1,0);
- glBegin(GL_QUAD_STRIP);
- {
- glVertex3f(1,0.35,-1);
- glVertex3f(2,0.35,-1);
- glVertex3f(1,0.70,-1);
- glVertex3f(2,0.70,-1);
- glVertex3f(1,0.70,-2.2);
- glVertex3f(1.5,0.70,-1.9);
- glVertex3f(1.0,0.35,-2.2);
- glVertex3f(1.5,0.35,-1.9);
- glVertex3f(1.0,0.35,-1);
- glVertex3f(2.0,0.35,-1);
- }
- glEnd();
- glBegin(GL_QUADS); //outer wing-panel
- {
- glVertex3f(2.0,0.35,-1);
- glVertex3f(1.5,0.35,-1.9);
- glVertex3f(1.5,0.70,-1.9);
- glVertex3f(2.0,0.70,-1);
- }
- glEnd();
- }
- glPopMatrix();
- }
Add Comment
Please, Sign In to add comment