Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h> //Já inclui automaticamente os cabeçalhos do OpenGL: gl e glu #include <GL/gl.h> #include <GL/glu.h>
- #include <math.h>
- #define KEY_ESC 27
- #define KEY_ENTER 13
- /* Author: Jonathas Lopes Moreira */
- #include <iostream>
- using namespace std;
- void exibe(void); //ok
- void reshape (int x, int y); // ok
- void keyboard(unsigned char c, int x, int y); //falta
- void keyboardSpecial(int c, int x, int y); //falta
- void mouse(int button, int state, int x, int y); //ok
- void inicia(); //ok
- void polygon(int a, int b, int c , int d); // ok
- void cubo(); //ok
- void cilindro(); //ok
- void guindaste(); // falta
- GLfloat vertices[][3] =
- {
- {-1.0,-1.0,-1.0}, //v[0]
- {1.0,-1.0,-1.0}, //v[1]
- {1.0,1.0,-1.0}, //v[2]
- {-1.0,1.0,-1.0}, //v[3]
- {-1.0,-1.0,1.0}, //v[4]
- {1.0,-1.0,1.0}, //v[5]
- {1.0,1.0,1.0}, //v[6]
- {-1.0,1.0,1.0} //v[7]
- };
- // Observador
- GLfloat angObserv = 0.0; // ângulo de rotação do observador em torno de Y
- // Haste do guindaste
- GLfloat compHaste = 2.0, deslHaste = 1.5; // comprimento e deslocamento horizontal da haste em relacao ao centro da estrutura
- // Viga
- GLfloat compViga = 4.0, deslViga=0, altViga = 0.3, largViga = 0.15;
- // Coluna
- GLfloat largColuna = 0.5, altColuna = 3.0;
- // Angulos do guindaste
- GLfloat angGiro = 0,angInclinacao = 0, deslInclinacao=0;
- double pi = 4.0*atan(1.0); // numero pi
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv); //ok
- glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE); //ok
- glutInitWindowSize(500, 500); //ok
- glutCreateWindow("Guindaste"); //ok
- // funções callback importantes
- glutDisplayFunc(exibe); //ok
- glutReshapeFunc(reshape); //ok
- glutKeyboardFunc(keyboard); //ok
- glutSpecialFunc(keyboardSpecial); //ok
- glutMouseFunc(mouse); //ok
- inicia(); // configura a posicao do observador com glulookat
- glutMainLoop ( );
- return 0;
- }
- void exibe(void) //ok
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glRotatef(angObserv, 0,1,0);
- guindaste();
- glPopMatrix();
- glFlush();
- glutSwapBuffers();
- }
- void reshape (int x, int y) // ok funcao executada quando a janela muda
- {
- glViewport(0, 0, x, y);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(80.0, 1.0, 1.0, 30.0);
- glMatrixMode(GL_MODELVIEW); // seleciona a matriz modelview
- }
- void mouse(int button, int state, int x, int y)
- {
- if (button == GLUT_RIGHT_BUTTON)
- {
- exit(0);
- }
- }
- void keyboard(unsigned char c, int x, int y) //falta
- {
- if (c == KEY_ESC)
- exit(0);
- else if(c=='a')
- {
- angObserv += 1.0; // como mexe com o angulo observador, roda tudo pra esquerda
- }
- else if(c=='d')
- {
- angObserv -=1.0;
- }
- else if(c=='w')
- {
- if (angInclinacao<65)
- {
- angInclinacao += 0.4;
- deslInclinacao -=0.4;
- }
- }
- else if(c=='s')
- {
- if (angInclinacao>0.1)
- {
- angInclinacao -= 0.4;
- deslInclinacao +=0.4;
- }
- }
- glutPostRedisplay(); // ou exibe(); serve para atualizar a ação que acabou de ser executada
- }
- void keyboardSpecial(int c, int x, int y) //falta
- {
- if(c == GLUT_KEY_RIGHT)
- {
- if (compViga<4.0)
- {
- compViga += 0.05;
- deslViga-=0.025;
- }
- }
- else if(c == GLUT_KEY_LEFT)
- {
- if (compViga>2.0)
- {
- compViga -= 0.05;
- deslViga+=0.025;
- }
- }
- else if(c == GLUT_KEY_UP)
- angGiro += 1.0; // base fixa e roda o resto pra direita
- else if(c == GLUT_KEY_DOWN)
- angGiro -= 1.0; // base fixa e roda o resto pra esquerda
- glutPostRedisplay(); // ou exibe(); serve para atualizar a ação que acabou de ser executada
- }
- void inicia() //ok
- {
- // Posicionamento inicial do observador
- gluLookAt(-2.0,5.0,5.0, 0.0,2.0,0.0, 0.0,1.0,0.0);
- // estabelece parametros de iluminacao
- GLfloat mat_ambient[]={1.0, 1.0, 0.0, 1.0};
- GLfloat mat_diffuse[]={0.6, 0.6, 0.0, 1.0};
- GLfloat mat_specular[]={0.4, 0.4, 0.0, 1.0};
- GLfloat mat_shininess={50.0};
- GLfloat light_ambient[]={0.3, 0.3, 0.3, 1.0};
- GLfloat light_diffuse[]={0.6, 0.6, 0.6, 1.0};
- GLfloat light_specular[]={0.6, 0.6, 0.6, 1.0};
- GLfloat light_position[]={3.0,3.0,3.0,1.0};
- glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess);
- glEnable(GL_SMOOTH);
- glEnable(GL_LIGHTING);
- glEnable(GL_NORMALIZE);
- glEnable(GL_LIGHT0);
- glEnable(GL_DEPTH_TEST);
- glClearColor (0.7, 0.7, 1.0, 1.0);
- }
- void polygon(int a, int b, int c , int d) //ok
- {
- glBegin(GL_POLYGON);
- glVertex3fv(vertices[a]);
- glVertex3fv(vertices[b]);
- glVertex3fv(vertices[c]);
- glVertex3fv(vertices[d]);
- glEnd();
- }
- void cubo() //ok
- {
- /*
- polygon(0,1,2,3); //face atras
- polygon(5,1,2,6); //face do lado direito
- polygon(2,3,7,6); //face cima
- polygon(4,5,1,0); //face baixo
- polygon(3,7,4,0); //face esquerda
- polygon(4,5,6,7); //face frente
- {-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0},
- {1.0,1.0,-1.0}, {-1.0,1.0,-1.0},
- {-1.0,-1.0,1.0}, {1.0,-1.0,1.0},
- {1.0,1.0,1.0}, {-1.0,1.0,1.0}
- */
- glScalef(0.5,0.5,0.5);
- glNormal3f(0.0,0.0,-1.0);
- polygon(0,1,2,3); //face atras
- glNormal3f(0.0,1.0,0.0);
- polygon(2,3,7,6); //face cima
- glNormal3f(-1.0,0.0,0.0);
- polygon(3,7,4,0); //face esquerda
- glNormal3f(1.0,0.0,0.0);
- polygon(5,1,2,6); //face do lado direito
- glNormal3f(0.0,0.0,1.0);
- polygon(4,5,6,7); //face frente
- glNormal3f(0.0,-1.0,0.0);
- polygon(4,5,1,0); //face baixo
- }
- void cilindro() //ok
- {
- // Da linha 282 ate 286 cria um cilindro sem tampa
- float alt,ang,raio;
- static GLUquadric* quad;
- int i;
- alt=raio=1;
- quad = gluNewQuadric();
- gluCylinder(quad, raio, raio, alt, 100, 100);
- /* interface da função gluCylinder
- void gluCylinder(GLUquadric* quad,
- GLdouble base,
- GLdouble top,
- GLdouble height,
- GLint slices,
- GLint stacks);
- */
- // tampas para o cilindro
- glPushMatrix();
- glNormal3f(0.0,0.0,-1.0);
- glBegin(GL_POLYGON);
- for(i = 0; i < 100; i++)
- {
- ang = i*2*pi/100;
- glVertex2f(cos(ang)*raio, sin(ang)*raio);
- }
- glEnd();
- glTranslatef(0.0,0.0,1.0);
- glNormal3f(0.0,0.0,1.0);
- glBegin(GL_POLYGON);
- for(i = 0; i < 100; i++)
- {
- float ang = i*2*pi /100;
- glVertex2f(cos(ang) * raio, sin(ang) * raio);
- }
- glEnd();
- glPopMatrix();
- }
- void guindaste()
- {
- //Base do guindaste é fixa
- glPushMatrix();
- glScalef(2.0, 0.5, 1.0);
- cubo();
- glPopMatrix();
- glRotatef( angGiro, 0.0, 1.0, 0.0 );
- {
- //Base cilindrica de cima da base
- glPushMatrix();
- glRotatef(-90, 1.0, 0.0, 0.0);
- glScalef(0.5, 0.5, 0.5);
- cilindro();
- glPopMatrix();
- //Base vertical longa ou Haste vertical
- glPushMatrix();
- glTranslatef( 0.0, 1.5, 0.0 );
- glScalef( 0.5, 2.5, 0.5 );
- cubo();
- glPopMatrix();
- // inclinacao
- //Parte cilindrica na ponta de cima da base vertical longa
- glPushMatrix();
- glTranslatef( 0.0, 2.75, -0.15);
- glScalef( 0.4, 0.4, 0.3);
- cilindro();
- glPopMatrix();
- glRotatef( angInclinacao, 0, 0, 1 );
- {
- //Cubo horizontal longo ou Viga
- glPushMatrix();
- 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
- glScalef( compViga, 0.3, 0.15);
- cubo();
- glPopMatrix();
- //Parte cilindrica na ponta direita da base horizontal
- glPushMatrix();
- glTranslatef( compViga, 2.75, -0.0375);
- glScalef( 0.2, 0.2, 0.075);
- cilindro();
- glPopMatrix();
- //Parte vertical longa e fina no final da parte cilindrica direita
- glPushMatrix();
- glTranslatef( compViga, 1.75, 0.0 );
- glScalef( 0.05, 2, 0.075 );
- cubo();
- glPopMatrix();
- //Parte horizontal pequena
- glPushMatrix();
- glTranslatef( compViga, 0.95, 0.0 );
- glScalef( 0.25, 0.1, 0.1 );
- cubo();
- glPopMatrix();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement