Advertisement
Guest User

GuindasteOpenGL

a guest
Dec 11th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.31 KB | None | 0 0
  1. #include <GL/glut.h> //Já inclui automaticamente os cabeçalhos do OpenGL: gl e glu #include <GL/gl.h> #include <GL/glu.h>
  2. #include <math.h>
  3. #define KEY_ESC 27
  4. #define KEY_ENTER 13
  5.  
  6.  
  7. /* Author: Jonathas Lopes Moreira */
  8. #include <iostream>
  9. using namespace std;
  10. void exibe(void); //ok
  11. void reshape (int x, int y); // ok
  12. void keyboard(unsigned char c, int x, int y); //falta
  13. void keyboardSpecial(int c, int x, int y); //falta
  14. void mouse(int button, int state, int x, int y); //ok
  15. void inicia(); //ok
  16. void polygon(int a, int b, int c , int d); // ok
  17. void cubo(); //ok
  18. void cilindro(); //ok
  19. void guindaste(); // falta
  20.  
  21. GLfloat vertices[][3] =
  22.     {
  23.         {-1.0,-1.0,-1.0},   //v[0]
  24.         {1.0,-1.0,-1.0},    //v[1]
  25.         {1.0,1.0,-1.0},     //v[2]
  26.         {-1.0,1.0,-1.0},    //v[3]
  27.         {-1.0,-1.0,1.0},    //v[4]
  28.         {1.0,-1.0,1.0},     //v[5]
  29.         {1.0,1.0,1.0},      //v[6]  
  30.         {-1.0,1.0,1.0}      //v[7]
  31.     };
  32.    
  33. // Observador
  34. GLfloat angObserv = 0.0;   // ângulo de rotação do observador em torno de Y
  35.  
  36. // Haste do guindaste
  37. GLfloat compHaste = 2.0, deslHaste = 1.5; // comprimento e deslocamento horizontal da haste em relacao ao centro da estrutura
  38.  
  39. // Viga
  40. GLfloat compViga = 4.0, deslViga=0, altViga = 0.3, largViga = 0.15;
  41.  
  42. // Coluna
  43. GLfloat largColuna = 0.5, altColuna = 3.0;
  44.  
  45. // Angulos do guindaste
  46. GLfloat angGiro = 0,angInclinacao = 0, deslInclinacao=0;
  47.  
  48.  
  49. double pi = 4.0*atan(1.0); // numero pi
  50.  
  51.  
  52. int main(int argc, char** argv)
  53. {
  54.     glutInit(&argc, argv); //ok
  55.     glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE); //ok
  56.     glutInitWindowSize(500, 500); //ok
  57.     glutCreateWindow("Guindaste"); //ok
  58.    
  59.    
  60.    
  61.  
  62.     // funções callback importantes
  63.     glutDisplayFunc(exibe);             //ok
  64.     glutReshapeFunc(reshape);           //ok
  65.     glutKeyboardFunc(keyboard);         //ok
  66.     glutSpecialFunc(keyboardSpecial);   //ok
  67.     glutMouseFunc(mouse);               //ok
  68.  
  69.    
  70.  
  71.     inicia(); // configura a posicao do observador com glulookat
  72.     glutMainLoop ( );
  73.     return 0;
  74.  
  75. }
  76. void exibe(void) //ok
  77. {
  78.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  79.     glPushMatrix();
  80.         glRotatef(angObserv, 0,1,0);
  81.         guindaste();
  82.    glPopMatrix();
  83.    glFlush();
  84.    glutSwapBuffers();
  85. }
  86. void reshape (int x, int y) // ok funcao executada quando a janela muda
  87. {
  88.     glViewport(0, 0, x, y);
  89.     glMatrixMode(GL_PROJECTION);
  90.     glLoadIdentity();
  91.     gluPerspective(80.0, 1.0, 1.0, 30.0);
  92.     glMatrixMode(GL_MODELVIEW); // seleciona a matriz modelview
  93.    
  94. }
  95. void mouse(int button, int state, int x, int y)
  96. {
  97.     if (button == GLUT_RIGHT_BUTTON)
  98.     {
  99.         exit(0);
  100.     }
  101. }
  102. void keyboard(unsigned char c, int x, int y) //falta
  103. {
  104.     if (c == KEY_ESC)
  105.         exit(0);
  106.     else if(c=='a')
  107.     {
  108.         angObserv += 1.0; // como mexe com o angulo observador, roda tudo pra esquerda         
  109.     }
  110.     else if(c=='d')
  111.     {
  112.         angObserv -=1.0;
  113.     }
  114.     else if(c=='w')
  115.     {
  116.         if (angInclinacao<65)
  117.         {
  118.             angInclinacao += 0.4;
  119.             deslInclinacao -=0.4;
  120.         }  
  121.            
  122.     }
  123.     else if(c=='s')
  124.     {
  125.         if (angInclinacao>0.1)
  126.         {
  127.             angInclinacao -= 0.4;
  128.             deslInclinacao +=0.4;
  129.         }          
  130.            
  131.     }
  132.     glutPostRedisplay();  // ou exibe(); serve para atualizar a ação que acabou de ser executada
  133. }
  134. void keyboardSpecial(int c, int x, int y) //falta
  135. {
  136.     if(c == GLUT_KEY_RIGHT)
  137.     {
  138.         if (compViga<4.0)  
  139.         {
  140.             compViga += 0.05;
  141.             deslViga-=0.025;
  142.         }
  143.            
  144.     }
  145.     else if(c == GLUT_KEY_LEFT)
  146.     {
  147.         if (compViga>2.0)
  148.         {
  149.             compViga -= 0.05;
  150.             deslViga+=0.025;
  151.         }
  152.     }      
  153.     else if(c == GLUT_KEY_UP)
  154.         angGiro += 1.0; // base fixa e roda o resto pra direita
  155.     else if(c == GLUT_KEY_DOWN)
  156.         angGiro -= 1.0;  // base fixa e roda o resto pra esquerda
  157.     glutPostRedisplay();  // ou exibe(); serve para atualizar a ação que acabou de ser executada
  158. }
  159.  
  160. void inicia() //ok
  161. {
  162.     // Posicionamento inicial do observador
  163.     gluLookAt(-2.0,5.0,5.0, 0.0,2.0,0.0, 0.0,1.0,0.0);
  164.  
  165.     // estabelece parametros de iluminacao
  166.     GLfloat mat_ambient[]={1.0, 1.0, 0.0, 1.0};
  167.     GLfloat mat_diffuse[]={0.6, 0.6, 0.0, 1.0};
  168.     GLfloat mat_specular[]={0.4, 0.4, 0.0, 1.0};
  169.     GLfloat mat_shininess={50.0};
  170.     GLfloat light_ambient[]={0.3, 0.3, 0.3, 1.0};
  171.     GLfloat light_diffuse[]={0.6, 0.6, 0.6, 1.0};
  172.     GLfloat light_specular[]={0.6, 0.6, 0.6, 1.0};
  173.  
  174.     GLfloat light_position[]={3.0,3.0,3.0,1.0};
  175.  
  176.     glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  177.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  178.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  179.  
  180.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  181.  
  182.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  183.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  184.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  185.     glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess);
  186.  
  187.     glEnable(GL_SMOOTH);
  188.     glEnable(GL_LIGHTING);
  189.     glEnable(GL_NORMALIZE);
  190.     glEnable(GL_LIGHT0);
  191.     glEnable(GL_DEPTH_TEST);
  192.  
  193.     glClearColor (0.7, 0.7, 1.0, 1.0);
  194. }
  195.  
  196.  
  197.  
  198.  
  199. void polygon(int a, int b, int c , int d) //ok
  200. {
  201.     glBegin(GL_POLYGON);
  202.         glVertex3fv(vertices[a]);
  203.         glVertex3fv(vertices[b]);
  204.         glVertex3fv(vertices[c]);
  205.         glVertex3fv(vertices[d]);
  206.     glEnd();
  207. }
  208. void cubo() //ok
  209. {
  210.     /*
  211.         polygon(0,1,2,3); //face atras
  212.         polygon(5,1,2,6); //face do lado direito
  213.         polygon(2,3,7,6); //face cima
  214.         polygon(4,5,1,0); //face baixo
  215.         polygon(3,7,4,0); //face esquerda
  216.         polygon(4,5,6,7); //face frente
  217.         {-1.0,-1.0,-1.0},   {1.0,-1.0,-1.0},
  218.         {1.0,1.0,-1.0},     {-1.0,1.0,-1.0},
  219.         {-1.0,-1.0,1.0},    {1.0,-1.0,1.0},
  220.         {1.0,1.0,1.0},      {-1.0,1.0,1.0}
  221.     */
  222.     glScalef(0.5,0.5,0.5);
  223.     glNormal3f(0.0,0.0,-1.0);
  224.     polygon(0,1,2,3); //face atras
  225.  
  226.    
  227.     glNormal3f(0.0,1.0,0.0);
  228.     polygon(2,3,7,6); //face cima
  229.  
  230.     glNormal3f(-1.0,0.0,0.0);
  231.     polygon(3,7,4,0); //face esquerda
  232.  
  233.     glNormal3f(1.0,0.0,0.0);
  234.     polygon(5,1,2,6); //face do lado direito
  235.  
  236.     glNormal3f(0.0,0.0,1.0);
  237.     polygon(4,5,6,7); //face frente
  238.  
  239.     glNormal3f(0.0,-1.0,0.0);
  240.     polygon(4,5,1,0); //face baixo
  241.    
  242. }
  243.  
  244. void cilindro() //ok
  245. {
  246.     // Da linha 282 ate 286 cria um cilindro sem tampa
  247.     float alt,ang,raio;
  248.     static GLUquadric* quad;
  249.     int i;
  250.     alt=raio=1;
  251.     quad = gluNewQuadric();
  252.     gluCylinder(quad, raio, raio, alt, 100, 100);
  253.    
  254.     /*  interface da função gluCylinder
  255.         void gluCylinder(GLUquadric* quad,
  256.         GLdouble  base,
  257.         GLdouble top,
  258.         GLdouble height,
  259.         GLint slices,
  260.         GLint stacks);
  261.     */
  262.    
  263.     // tampas para o cilindro
  264.     glPushMatrix();
  265.         glNormal3f(0.0,0.0,-1.0);
  266.         glBegin(GL_POLYGON);
  267.             for(i = 0; i < 100; i++)
  268.             {
  269.                 ang = i*2*pi/100;
  270.                 glVertex2f(cos(ang)*raio, sin(ang)*raio);
  271.             }
  272.         glEnd();
  273.        
  274.         glTranslatef(0.0,0.0,1.0);
  275.         glNormal3f(0.0,0.0,1.0);
  276.        
  277.         glBegin(GL_POLYGON);
  278.             for(i = 0; i < 100; i++)
  279.             {
  280.                 float ang = i*2*pi /100;
  281.                 glVertex2f(cos(ang) * raio, sin(ang) * raio);
  282.             }
  283.         glEnd();
  284.     glPopMatrix();
  285. }
  286. void guindaste()
  287. {
  288.      //Base do guindaste é fixa
  289.      glPushMatrix();
  290.         glScalef(2.0, 0.5, 1.0);
  291.         cubo();
  292.      glPopMatrix();
  293.     glRotatef( angGiro, 0.0, 1.0, 0.0 );
  294.     {
  295.         //Base cilindrica de cima da base
  296.         glPushMatrix();
  297.             glRotatef(-90, 1.0, 0.0, 0.0);
  298.             glScalef(0.5, 0.5, 0.5);
  299.             cilindro();
  300.         glPopMatrix();
  301.        
  302.         //Base vertical longa ou Haste vertical
  303.         glPushMatrix();    
  304.             glTranslatef( 0.0, 1.5, 0.0 );
  305.             glScalef( 0.5, 2.5, 0.5 );
  306.             cubo();
  307.         glPopMatrix();
  308.        
  309.         // inclinacao
  310.             //Parte cilindrica na ponta de cima da base vertical longa
  311.             glPushMatrix();
  312.                 glTranslatef( 0.0, 2.75, -0.15);
  313.                 glScalef( 0.4, 0.4, 0.3);
  314.                 cilindro();
  315.             glPopMatrix();
  316.         glRotatef( angInclinacao, 0, 0, 1 );
  317.         {
  318.             //Cubo horizontal longo ou Viga
  319.             glPushMatrix();
  320.                 glTranslatef(2-deslViga, 2.75, 0); // deslViga é o deslocamento da viga e diminui metade do que e aumenta/diminui a metade do que o comprimento da viga para poder fazer a relacao de translacao e escala ficarem corretos
  321.                 glScalef( compViga, 0.3, 0.15);
  322.                 cubo();
  323.             glPopMatrix();
  324.            
  325.             //Parte cilindrica na ponta direita da base horizontal
  326.             glPushMatrix();
  327.                 glTranslatef( compViga, 2.75, -0.0375);
  328.                 glScalef( 0.2, 0.2, 0.075);
  329.                 cilindro();
  330.             glPopMatrix();
  331.            
  332.             //Parte vertical longa e fina no final da parte cilindrica direita
  333.             glPushMatrix();    
  334.                 glTranslatef( compViga, 1.75, 0.0 );
  335.                 glScalef( 0.05, 2, 0.075 );
  336.                 cubo();
  337.             glPopMatrix();
  338.            
  339.             //Parte horizontal pequena
  340.             glPushMatrix();    
  341.                 glTranslatef( compViga, 0.95, 0.0 );
  342.                 glScalef( 0.25, 0.1, 0.1 );
  343.                 cubo();
  344.             glPopMatrix(); 
  345.         }
  346.     }
  347.    
  348.  
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement