Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.31 KB | None | 0 0
  1. #ifdef __APPLE__
  2. #include <GLUT/glut.h>
  3. #else
  4. #include <GL/glut.h>
  5. #endif
  6.  
  7. #include<iostream>
  8. #include <math.h>
  9. GLUquadric* gluquadric;
  10. const double PI = 3.14159265359;
  11. double hangle;
  12. double vangle;
  13. int distance;
  14. double cx, cy, cz;
  15. int fovy;
  16. int wheight;
  17. int wwidth;
  18. bool wireFrame;
  19. bool orthoView;
  20. int ii;
  21. double sx;
  22. double sz;
  23. double sy;
  24. void changeSize(int w, int h)
  25. {
  26.     wheight = h;
  27.     wwidth = w;
  28.     // Prevent a divide by zero, when window is too short
  29.     // (you cant make a window of zero width).
  30.     if (h == 0)
  31.         h = 1;
  32.  
  33.     float ratio =  w * 1.0 / h;
  34. }
  35.  
  36. void makeHelicopter(double x, double y, double z)
  37. {
  38.     //left skid
  39.     glPushMatrix();
  40.     glColor3f(0,0.8,0.4);
  41.     glTranslatef(x -1, y + 0,z -1.5);
  42.     //glRotatef(30,1,0,0);
  43.     gluCylinder(gluquadric, 0.05, 0.05, 3.0, 10, 10);
  44.     glPopMatrix();
  45.     //Left skid connector
  46.     glPushMatrix();
  47.     glColor3f(0,0.8,0.4);
  48.     glTranslated(x -1, y + 0, z + 0);
  49.     glRotatef(135,0,0,1);
  50.     glRotatef(90,1,0,0);
  51.     gluCylinder(gluquadric, 0.05, 0.05, 0.5, 10, 10);
  52.     glPopMatrix();
  53.  
  54.     //right skid
  55.     glPushMatrix();
  56.     glColor3f(0,0.8,0.4);
  57.     glTranslatef(x + 1, y + 0, z -1.5);
  58.     //glRotatef(30,1,0,0);
  59.     gluCylinder(gluquadric, 0.05, 0.05, 3.0, 10, 10);
  60.     glPopMatrix();
  61.     //Right skid connector
  62.     glPushMatrix();
  63.     glColor3f(0,0.8,0.4);
  64.     glTranslated(x + 1, y + 0, z + 0);
  65.     glRotatef(225,0,0,1);
  66.     glRotatef(90,1,0,0);
  67.     gluCylinder(gluquadric, 0.05, 0.05, 0.5, 10, 10);
  68.     glPopMatrix();
  69.     //Main body
  70.     glPushMatrix();
  71.     glColor3f(0.5,0.8,0.4);
  72.     glTranslated(x + 0, y + 1, z + 0);
  73.     glScaled(1.0, 1.0, 2.0);
  74.     gluSphere(gluquadric, 1, 10, 10);
  75.     glPopMatrix();
  76.  
  77.     //Main rotor shaft
  78.     glPushMatrix();
  79.     glColor3f(0.5,0,0.4);
  80.     glTranslated(x + 0, y + 2.3, z + 0);
  81.     glRotatef(90,1,0,0);
  82.     gluCylinder(gluquadric, 0.15, 0.15, 0.4, 10, 10);
  83.     glPopMatrix();
  84.  
  85.     //Rotor 1
  86.     glPushMatrix();
  87.     glColor3f(0.5,0,0.4);
  88.     glTranslated(x + 0, y + 0, z + 1.25);
  89.     glTranslated(0, 2.3, 0);
  90.     glScaled(5, 1, 50);
  91.     glutSolidCube(0.05);
  92.     glPopMatrix();
  93.     //Start problem
  94.     //Rotor 2
  95.     glPushMatrix();
  96.     glColor3f(0.5,0,0.4);
  97.     glRotated(120, 0, 1, 0);
  98.     glTranslated(x + 0, y + 0, z + 1.25);
  99.     glTranslated(0, 2.3, 0);
  100.     glScalef(5, 1, 50);
  101.     glutSolidCube(0.05);
  102.     glPopMatrix();
  103.  
  104.     //Rotor 3
  105.     glPushMatrix();
  106.     glColor3f(0.5,0,0.4);
  107.     glRotated(240, 0, 1, 0);
  108.     glTranslated(x + 0, y + 0, z + 1.25);
  109.     glTranslated(0, 2.3, 0);
  110.     glScalef(5, 1, 50);
  111.     glutSolidCube(0.05);
  112.     glPopMatrix();
  113.     //End problem
  114.     //Tail
  115.     glPushMatrix();
  116.     glColor3f(0.2,1,0.4);
  117.     glTranslated(x + 0, y + 1, z + 1.5);
  118.     gluCylinder(gluquadric, 0.5, 0.2, 2, 10, 10);
  119.     glPopMatrix();
  120.  
  121.     //tail rotor shaft
  122.     glPushMatrix();
  123.     glColor3f(0.2,0.8,0.4);
  124.     glTranslated(x + 0.2, y + 1, z + 3.2);
  125.     glRotatef(90,0,1,0);
  126.     gluCylinder(gluquadric, 0.1, 0.1, 0.2, 10, 10);
  127.     glPopMatrix();
  128.  
  129.     //Tail Rotor 1
  130.     glPushMatrix();
  131.     glColor3f(0.2,0.8,0.4);
  132.     glTranslated(x + 0.4, y + 1, z + 3.45);
  133.     glRotated(90, 0, 0, 1);
  134.     glRotated(180, 0, 1, 0);
  135.     glScalef(5, 1, 10);
  136.     glutSolidCube(0.05);
  137.     glPopMatrix();
  138.  
  139.     //Tail Rotor 2
  140.     glPushMatrix();
  141.     glColor3f(0.2,0.8,0.4);
  142.     glTranslated(x + 0.4, y + 0.7, z + 3.1);
  143.     glRotated(480, 1, 0, 0);
  144.     glRotated(90, 0, 0, 1);
  145.     glRotated(180, 0, 1, 0);
  146.     glScalef(5, 1, 10);
  147.     glutSolidCube(0.05);
  148.     glPopMatrix();
  149.  
  150.     //Tail Rotor 3
  151.     glPushMatrix();
  152.     glColor3f(0.2,0.8,0.4);
  153.     glTranslated(x + 0.4, y + 1.2, z + 3.05);
  154.     glRotated(240, 1, 0, 0);
  155.     glRotated(90, 0, 0, 1);
  156.     glRotated(180, 0, 1, 0);
  157.     glScalef(5, 1, 10);
  158.     glutSolidCube(0.05);
  159.     glPopMatrix();
  160.  
  161.     //Tail fin
  162.     glPushMatrix();
  163.     glColor3f(0.5,0.8,0);
  164.     glTranslated(x + 0, y + 1.5, z + 3.2);
  165.     glScalef(1, 15, 10);
  166.     glutSolidCube(0.05);
  167.     glPopMatrix();
  168.  
  169.     //Left side wing
  170.     glPushMatrix();
  171.     glColor3f(0.5,0.5,0.8);
  172.     glTranslated(x -1.5, y + 0.7, z + 0);
  173.     glRotated(30, 0, 0, 1);
  174.     glScalef(7, 1, 5);
  175.     glutSolidCube(0.2);
  176.     glPopMatrix();
  177.  
  178.     //Left missile launcher
  179.     glPushMatrix();
  180.     glColor3f(0.5,0.8,0.8);
  181.     glTranslated(x -1.5, y + 0.5, z -1);
  182.     gluCylinder(gluquadric, 0.2, 0.2, 1.5, 10, 10);
  183.     glPushMatrix();
  184.     //glTranslated(x + 0, y + 0, z + 0);
  185.     gluDisk(gluquadric, 0, 0.2, 10, 10);
  186.     glPopMatrix();
  187.     glPushMatrix();
  188.     glTranslated(0, 0, 1.5);
  189.     gluDisk(gluquadric, 0, 0.2, 10, 10);
  190.     glPopMatrix();
  191.     glPopMatrix();
  192.  
  193.     //Right side wing
  194.     glPushMatrix();
  195.     glColor3f(0.5,0.5,0.8);
  196.     glTranslated(x + 1.5, y + 0.7, z + 0);
  197.     glRotated(330, 0, 0, 1);
  198.     glScalef(7, 1, 5);
  199.     glutSolidCube(0.2);
  200.     glPopMatrix();
  201.  
  202.     //Right missile launcher
  203.     glPushMatrix();
  204.     glColor3f(0.5,0.8,0.8);
  205.     glTranslated(x + 1.5, y + 0.5, z -1);
  206.     gluCylinder(gluquadric, 0.2, 0.2, 1.5, 10, 10);
  207.     gluDisk(gluquadric, 0, 0.2, 10, 10);
  208.     glPushMatrix();
  209.     glTranslated(0, 0, 1.5);
  210.     gluDisk(gluquadric, 0, 0.2, 10, 10);
  211.     glPopMatrix();
  212.     glPopMatrix();
  213.  
  214.     //Machine gun
  215.     glPushMatrix();
  216.     glColor3f(0.8,0.5,0.4);
  217.     glTranslated(x + 0, y + 0.3, z -1.5);
  218.     glRotated(180, 0, 1, 0);
  219.     gluCylinder(gluquadric, 0.1, 0.1, 1, 10, 10);
  220.     gluSphere(gluquadric, 0.3, 10, 10);
  221.     glPopMatrix();
  222.  
  223.     //Cockpit window
  224.     glPushMatrix();
  225.     glColor3f(0.5,0.5,0.5);
  226.     glTranslated(x + 0, y + 1.3, z -1.2);
  227.     gluSphere(gluquadric, 0.7, 10, 10);
  228.     glPopMatrix();
  229. }
  230.  
  231. void renderScene(void)
  232. {
  233.     glutPostRedisplay();
  234.     if(wireFrame)
  235.     {
  236.         glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
  237.     }
  238.     else
  239.     {
  240.         glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
  241.     }
  242.     GLUquadricObj *quadratic;
  243.     quadratic = gluNewQuadric();
  244.  
  245.     // Clear Color and Depth Buffers
  246.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  247.  
  248.     // Reset transformations
  249.     glLoadIdentity();
  250.     cx = distance*sin(vangle)*cos(hangle);
  251.     cy = distance*cos(vangle);
  252.     cz = distance*sin(vangle)*sin(hangle);
  253.     // Set the camera
  254.     gluLookAt(  cx, cy, cz,//camX, camY, camZ,
  255.                 0,0,0,//targetX, targetY,  targetZ,
  256.                 0,1,0);//upX, upY,  upZ);
  257.  
  258.     glEnable(GL_DEPTH_TEST);
  259.  
  260.     makeHelicopter(sx,sy,sz);
  261.  
  262.     glutSwapBuffers();
  263. }
  264.  
  265. void initialize()
  266. {
  267.     hangle = PI/2;
  268.     vangle = PI/2;
  269.     distance = 20;
  270.     fovy = 45;
  271.     wireFrame = false;
  272.     orthoView = false;
  273.     ii = 0;
  274.     sz = 0;
  275.     sy = 0;
  276.     sx = 0;
  277. }
  278.  
  279. void processSpecialKeyboard(int key, int x, int y)
  280. {
  281.     switch(key)
  282.     {
  283.     case GLUT_KEY_UP:
  284.         if(vangle > 0.2)
  285.         {
  286.             vangle-=0.1;
  287.         }
  288.             break;
  289.     case GLUT_KEY_DOWN:
  290.         if(vangle < PI - 0.2)
  291.         {
  292.             vangle+=0.1;
  293.         }
  294.             break;
  295.     case GLUT_KEY_LEFT:
  296.         if(hangle < 2 * PI)
  297.         {
  298.             hangle+=0.1;
  299.         }
  300.             break;
  301.     case GLUT_KEY_RIGHT:
  302.         if(hangle > 0)
  303.         {
  304.             hangle-=0.1;
  305.         }
  306.             break;
  307.     }
  308.     glutPostRedisplay();
  309. }
  310.  
  311. void processKeyboard(unsigned char key, int x, int y)
  312. {
  313.     switch(key)
  314.     {
  315.         case 'f':
  316.             if(distance > 5)
  317.             {
  318.                 --distance;
  319.             }
  320.             break;
  321.         case 'b':
  322.             ++distance;
  323.             break;
  324.         case 'z':
  325.             if(fovy > 12)
  326.             {
  327.                 fovy-=10;
  328.             }
  329.             break;
  330.         case 'Z':
  331.             if(fovy < 180)
  332.             {
  333.                 fovy+=10;
  334.             }
  335.             break;
  336.         case 'w':
  337.             wireFrame = true;
  338.             break;
  339.         case 'W':
  340.             wireFrame = false;
  341.             break;
  342.         case 'c':
  343.         case 'C':
  344.             initialize();
  345.             break;
  346.         case 'o':
  347.         case 'O':
  348.             orthoView = true;
  349.             break;
  350.         case 'p':
  351.         case 'P':
  352.             orthoView = false;
  353.             break;
  354.         case 27:
  355.             exit(0);
  356.             break;
  357.     }
  358.     glutPostRedisplay();
  359. }
  360.  
  361. void idling()
  362. {
  363.     ii+=2;
  364.     sx = /*0.08*/2 * cosf(ii * 3.141592 /180 );//calculate the x component
  365.     sz = /*0.08*/2 * sinf(ii * 3.141592 /180);//calculate the y component
  366.     // Use the Projection Matrix
  367.     glMatrixMode(GL_PROJECTION);
  368.  
  369.     // Reset Matrix
  370.     glLoadIdentity();
  371.  
  372.     // Set the viewport to be the entire window
  373.     glViewport(0, 0, wwidth, wheight);
  374.  
  375.     // Set the correct perspective.
  376.     double aspectRatio =(double)wwidth/(double)wheight;
  377.     if(orthoView)
  378.     {
  379.         glOrtho(-(float)wwidth/100, (float)wwidth/100, -(float)wheight/100, (float)wheight/100, 0.1f, 1000.0f);
  380.     }
  381.     else
  382.     {
  383.         gluPerspective((float)fovy, (float)wwidth/(float)wheight, 0.1f, 1000.0f);
  384.     }
  385.  
  386.     // Get Back to the Modelview
  387.     glMatrixMode(GL_MODELVIEW);
  388. }
  389.  
  390. int main(int argc, char **argv)
  391. {
  392.     initialize();
  393.     gluquadric = gluNewQuadric();
  394.     // init GLUT and create window
  395.     glutInit(&argc, argv);
  396.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  397.     glutInitWindowPosition(100,100);
  398.     glutInitWindowSize(800,600);
  399.     glutCreateWindow("Lab Demo");
  400.  
  401.     // register callbacks
  402.     glutDisplayFunc(renderScene);
  403.  
  404.     glutKeyboardFunc(processKeyboard);
  405.     glutSpecialFunc(processSpecialKeyboard);
  406.  
  407.     glutReshapeFunc(changeSize);
  408.     glutIdleFunc(idling);//idling);
  409.  
  410.     // enter GLUT event processing cycle
  411.     glutMainLoop();
  412.  
  413.     return 1;
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement