Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- #include<iostream>
- #include <math.h>
- GLUquadric* gluquadric;
- const double PI = 3.14159265359;
- double hangle;
- double vangle;
- int distance;
- double cx, cy, cz;
- int fovy;
- int wheight;
- int wwidth;
- bool wireFrame;
- bool orthoView;
- int ii;
- double sx;
- double sz;
- double sy;
- void changeSize(int w, int h)
- {
- wheight = h;
- wwidth = w;
- // Prevent a divide by zero, when window is too short
- // (you cant make a window of zero width).
- if (h == 0)
- h = 1;
- float ratio = w * 1.0 / h;
- }
- void makeHelicopter(double x, double y, double z)
- {
- //left skid
- glPushMatrix();
- glColor3f(0,0.8,0.4);
- glTranslatef(x -1, y + 0,z -1.5);
- //glRotatef(30,1,0,0);
- gluCylinder(gluquadric, 0.05, 0.05, 3.0, 10, 10);
- glPopMatrix();
- //Left skid connector
- glPushMatrix();
- glColor3f(0,0.8,0.4);
- glTranslated(x -1, y + 0, z + 0);
- glRotatef(135,0,0,1);
- glRotatef(90,1,0,0);
- gluCylinder(gluquadric, 0.05, 0.05, 0.5, 10, 10);
- glPopMatrix();
- //right skid
- glPushMatrix();
- glColor3f(0,0.8,0.4);
- glTranslatef(x + 1, y + 0, z -1.5);
- //glRotatef(30,1,0,0);
- gluCylinder(gluquadric, 0.05, 0.05, 3.0, 10, 10);
- glPopMatrix();
- //Right skid connector
- glPushMatrix();
- glColor3f(0,0.8,0.4);
- glTranslated(x + 1, y + 0, z + 0);
- glRotatef(225,0,0,1);
- glRotatef(90,1,0,0);
- gluCylinder(gluquadric, 0.05, 0.05, 0.5, 10, 10);
- glPopMatrix();
- //Main body
- glPushMatrix();
- glColor3f(0.5,0.8,0.4);
- glTranslated(x + 0, y + 1, z + 0);
- glScaled(1.0, 1.0, 2.0);
- gluSphere(gluquadric, 1, 10, 10);
- glPopMatrix();
- //Main rotor shaft
- glPushMatrix();
- glColor3f(0.5,0,0.4);
- glTranslated(x + 0, y + 2.3, z + 0);
- glRotatef(90,1,0,0);
- gluCylinder(gluquadric, 0.15, 0.15, 0.4, 10, 10);
- glPopMatrix();
- //Rotor 1
- glPushMatrix();
- glColor3f(0.5,0,0.4);
- glTranslated(x + 0, y + 0, z + 1.25);
- glTranslated(0, 2.3, 0);
- glScaled(5, 1, 50);
- glutSolidCube(0.05);
- glPopMatrix();
- //Start problem
- //Rotor 2
- glPushMatrix();
- glColor3f(0.5,0,0.4);
- glRotated(120, 0, 1, 0);
- glTranslated(x + 0, y + 0, z + 1.25);
- glTranslated(0, 2.3, 0);
- glScalef(5, 1, 50);
- glutSolidCube(0.05);
- glPopMatrix();
- //Rotor 3
- glPushMatrix();
- glColor3f(0.5,0,0.4);
- glRotated(240, 0, 1, 0);
- glTranslated(x + 0, y + 0, z + 1.25);
- glTranslated(0, 2.3, 0);
- glScalef(5, 1, 50);
- glutSolidCube(0.05);
- glPopMatrix();
- //End problem
- //Tail
- glPushMatrix();
- glColor3f(0.2,1,0.4);
- glTranslated(x + 0, y + 1, z + 1.5);
- gluCylinder(gluquadric, 0.5, 0.2, 2, 10, 10);
- glPopMatrix();
- //tail rotor shaft
- glPushMatrix();
- glColor3f(0.2,0.8,0.4);
- glTranslated(x + 0.2, y + 1, z + 3.2);
- glRotatef(90,0,1,0);
- gluCylinder(gluquadric, 0.1, 0.1, 0.2, 10, 10);
- glPopMatrix();
- //Tail Rotor 1
- glPushMatrix();
- glColor3f(0.2,0.8,0.4);
- glTranslated(x + 0.4, y + 1, z + 3.45);
- glRotated(90, 0, 0, 1);
- glRotated(180, 0, 1, 0);
- glScalef(5, 1, 10);
- glutSolidCube(0.05);
- glPopMatrix();
- //Tail Rotor 2
- glPushMatrix();
- glColor3f(0.2,0.8,0.4);
- glTranslated(x + 0.4, y + 0.7, z + 3.1);
- glRotated(480, 1, 0, 0);
- glRotated(90, 0, 0, 1);
- glRotated(180, 0, 1, 0);
- glScalef(5, 1, 10);
- glutSolidCube(0.05);
- glPopMatrix();
- //Tail Rotor 3
- glPushMatrix();
- glColor3f(0.2,0.8,0.4);
- glTranslated(x + 0.4, y + 1.2, z + 3.05);
- glRotated(240, 1, 0, 0);
- glRotated(90, 0, 0, 1);
- glRotated(180, 0, 1, 0);
- glScalef(5, 1, 10);
- glutSolidCube(0.05);
- glPopMatrix();
- //Tail fin
- glPushMatrix();
- glColor3f(0.5,0.8,0);
- glTranslated(x + 0, y + 1.5, z + 3.2);
- glScalef(1, 15, 10);
- glutSolidCube(0.05);
- glPopMatrix();
- //Left side wing
- glPushMatrix();
- glColor3f(0.5,0.5,0.8);
- glTranslated(x -1.5, y + 0.7, z + 0);
- glRotated(30, 0, 0, 1);
- glScalef(7, 1, 5);
- glutSolidCube(0.2);
- glPopMatrix();
- //Left missile launcher
- glPushMatrix();
- glColor3f(0.5,0.8,0.8);
- glTranslated(x -1.5, y + 0.5, z -1);
- gluCylinder(gluquadric, 0.2, 0.2, 1.5, 10, 10);
- glPushMatrix();
- //glTranslated(x + 0, y + 0, z + 0);
- gluDisk(gluquadric, 0, 0.2, 10, 10);
- glPopMatrix();
- glPushMatrix();
- glTranslated(0, 0, 1.5);
- gluDisk(gluquadric, 0, 0.2, 10, 10);
- glPopMatrix();
- glPopMatrix();
- //Right side wing
- glPushMatrix();
- glColor3f(0.5,0.5,0.8);
- glTranslated(x + 1.5, y + 0.7, z + 0);
- glRotated(330, 0, 0, 1);
- glScalef(7, 1, 5);
- glutSolidCube(0.2);
- glPopMatrix();
- //Right missile launcher
- glPushMatrix();
- glColor3f(0.5,0.8,0.8);
- glTranslated(x + 1.5, y + 0.5, z -1);
- gluCylinder(gluquadric, 0.2, 0.2, 1.5, 10, 10);
- gluDisk(gluquadric, 0, 0.2, 10, 10);
- glPushMatrix();
- glTranslated(0, 0, 1.5);
- gluDisk(gluquadric, 0, 0.2, 10, 10);
- glPopMatrix();
- glPopMatrix();
- //Machine gun
- glPushMatrix();
- glColor3f(0.8,0.5,0.4);
- glTranslated(x + 0, y + 0.3, z -1.5);
- glRotated(180, 0, 1, 0);
- gluCylinder(gluquadric, 0.1, 0.1, 1, 10, 10);
- gluSphere(gluquadric, 0.3, 10, 10);
- glPopMatrix();
- //Cockpit window
- glPushMatrix();
- glColor3f(0.5,0.5,0.5);
- glTranslated(x + 0, y + 1.3, z -1.2);
- gluSphere(gluquadric, 0.7, 10, 10);
- glPopMatrix();
- }
- void renderScene(void)
- {
- glutPostRedisplay();
- if(wireFrame)
- {
- glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
- }
- else
- {
- glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
- }
- GLUquadricObj *quadratic;
- quadratic = gluNewQuadric();
- // Clear Color and Depth Buffers
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- // Reset transformations
- glLoadIdentity();
- cx = distance*sin(vangle)*cos(hangle);
- cy = distance*cos(vangle);
- cz = distance*sin(vangle)*sin(hangle);
- // Set the camera
- gluLookAt( cx, cy, cz,//camX, camY, camZ,
- 0,0,0,//targetX, targetY, targetZ,
- 0,1,0);//upX, upY, upZ);
- glEnable(GL_DEPTH_TEST);
- makeHelicopter(sx,sy,sz);
- glutSwapBuffers();
- }
- void initialize()
- {
- hangle = PI/2;
- vangle = PI/2;
- distance = 20;
- fovy = 45;
- wireFrame = false;
- orthoView = false;
- ii = 0;
- sz = 0;
- sy = 0;
- sx = 0;
- }
- void processSpecialKeyboard(int key, int x, int y)
- {
- switch(key)
- {
- case GLUT_KEY_UP:
- if(vangle > 0.2)
- {
- vangle-=0.1;
- }
- break;
- case GLUT_KEY_DOWN:
- if(vangle < PI - 0.2)
- {
- vangle+=0.1;
- }
- break;
- case GLUT_KEY_LEFT:
- if(hangle < 2 * PI)
- {
- hangle+=0.1;
- }
- break;
- case GLUT_KEY_RIGHT:
- if(hangle > 0)
- {
- hangle-=0.1;
- }
- break;
- }
- glutPostRedisplay();
- }
- void processKeyboard(unsigned char key, int x, int y)
- {
- switch(key)
- {
- case 'f':
- if(distance > 5)
- {
- --distance;
- }
- break;
- case 'b':
- ++distance;
- break;
- case 'z':
- if(fovy > 12)
- {
- fovy-=10;
- }
- break;
- case 'Z':
- if(fovy < 180)
- {
- fovy+=10;
- }
- break;
- case 'w':
- wireFrame = true;
- break;
- case 'W':
- wireFrame = false;
- break;
- case 'c':
- case 'C':
- initialize();
- break;
- case 'o':
- case 'O':
- orthoView = true;
- break;
- case 'p':
- case 'P':
- orthoView = false;
- break;
- case 27:
- exit(0);
- break;
- }
- glutPostRedisplay();
- }
- void idling()
- {
- ii+=2;
- sx = /*0.08*/2 * cosf(ii * 3.141592 /180 );//calculate the x component
- sz = /*0.08*/2 * sinf(ii * 3.141592 /180);//calculate the y component
- // Use the Projection Matrix
- glMatrixMode(GL_PROJECTION);
- // Reset Matrix
- glLoadIdentity();
- // Set the viewport to be the entire window
- glViewport(0, 0, wwidth, wheight);
- // Set the correct perspective.
- double aspectRatio =(double)wwidth/(double)wheight;
- if(orthoView)
- {
- glOrtho(-(float)wwidth/100, (float)wwidth/100, -(float)wheight/100, (float)wheight/100, 0.1f, 1000.0f);
- }
- else
- {
- gluPerspective((float)fovy, (float)wwidth/(float)wheight, 0.1f, 1000.0f);
- }
- // Get Back to the Modelview
- glMatrixMode(GL_MODELVIEW);
- }
- int main(int argc, char **argv)
- {
- initialize();
- gluquadric = gluNewQuadric();
- // init GLUT and create window
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowPosition(100,100);
- glutInitWindowSize(800,600);
- glutCreateWindow("Lab Demo");
- // register callbacks
- glutDisplayFunc(renderScene);
- glutKeyboardFunc(processKeyboard);
- glutSpecialFunc(processSpecialKeyboard);
- glutReshapeFunc(changeSize);
- glutIdleFunc(idling);//idling);
- // enter GLUT event processing cycle
- glutMainLoop();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement