// ********************************************************************** // Hierarquia3D.cpp // ********************************************************************** // PUCRS/Escola Politécnica // COMPUTAÇÃO GRÁFICA // // Programa basico para criar aplicacoes 2D em OpenGL // // Marcio Sarroglia Pinho // pinho@pucrs.br // ********************************************************************** // Para uso no Xcode: // Abra o menu Product -> Scheme -> Edit Scheme -> Use custom working directory // Selecione a pasta onde voce descompactou o ZIP que continha este arquivo. #include #include #include #include using namespace std; #ifdef WIN32 #include #include #else #include #endif #ifdef __APPLE__ #include #endif #ifdef __linux__ #include #endif using namespace std; GLfloat AspectRatio; GLfloat AngY; bool fazCalculoPonto; GLfloat AnguloDeVisao; GLfloat Obs[3]; GLfloat Alvo[3]; // ********************************************************************** // void init(void) // Inicializa os parâmetros globais de OpenGL e do cenário // // ********************************************************************** void init(void) { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Fundo de tela preto glShadeModel(GL_FLAT); //glColorMaterial ( GL_FRONT, GL_AMBIENT_AND_DIFFUSE ); glEnable(GL_DEPTH_TEST); glEnable ( GL_CULL_FACE ); Obs[0] = 0; Obs[1] = 3; Obs[2] = 3; Alvo [0] = 0; Alvo [1] = 0; Alvo [2] = -1; AnguloDeVisao=70; AngY = 0; fazCalculoPonto = false; } // ********************************************************************** // void PosicUser() // Faz o posicinamento do observador no cenário. // // ********************************************************************** void PosicUser() { // Set the clipping volume glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(AnguloDeVisao,AspectRatio,0.01,200); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(Obs[0], Obs[1], Obs[2], Alvo[0], Alvo [1], Alvo[2], 0.0f,1.0f,0.0f); } // ********************************************************************** // void reshape( int w, int h ) // trata o redimensionamento da janela OpenGL // // ********************************************************************** void reshape( int w, int h ) { // Prevent a divide by zero, when window is too short // (you cant make a window of zero width). if(h == 0) h = 1; // Ajusta a relação entre largura e altura para evitar distorção na imagem. // Veja função "PosicUser". AspectRatio = 1.0f * w / h; // Reset the coordinate system before modifying glMatrixMode(GL_PROJECTION); //glLoadIdentity(); // Set the viewport to be the entire window glViewport(0, 0, w, h); PosicUser(); } // ********************************************************************** // void DesenhaCubo() // // // ********************************************************************** void DesenhaCubo() { glLineWidth(1); glutSolidCube(1); glLineWidth(1); glColor3f(1,1,0); glutWireCube(1); } // ********************************************************************** // void DesenhaCenario(bool desenha) // Por enquanto apenas exibe o cenário e não utiliza o parâmetro // 'desenha'. // ********************************************************************** void DesenhaCenario(bool desenha) { glPushMatrix(); glTranslatef ( -1.0f, 0.0f, -1.0f ); //glRotatef(AngY, 0,0,1); glColor3f(1, 0,0); DesenhaCubo(); glPopMatrix(); glPushMatrix(); glTranslatef ( 1.0f, 0.0f, -1.0f ); glColor3f(0,0,1); DesenhaCubo(); glPopMatrix(); } // ********************************************************************** // void display( void ) // // ********************************************************************** void display( void ) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); PosicUser(); DesenhaCenario(true); glutSwapBuffers(); } // ********************************************************************** // void keyboard ( unsigned char key, int x, int y ) // // // ********************************************************************** void keyboard ( unsigned char key, int x, int y ) { switch ( key ) { case 27: // Termina o programa qdo exit ( 0 ); // a tecla ESC for pressionada break; case ' ': fazCalculoPonto = true; break; case 'a': case 'A': AngY += 5; break; default: break; } } // ********************************************************************** // void arrow_keys ( int a_keys, int x, int y ) // ********************************************************************** void arrow_keys ( int a_keys, int x, int y ) { switch ( a_keys ) { case GLUT_KEY_UP: // When Up Arrow Is Pressed... glutFullScreen ( ); // Go Into Full Screen Mode break; case GLUT_KEY_DOWN: // When Down Arrow Is Pressed... glutInitWindowSize ( 700, 500 ); break; default: break; } } // ********************************************************************** // void main ( int argc, char** argv ) // ********************************************************************** int main ( int argc, char** argv ) { glutInit ( &argc, argv ); glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB );// | GLUT_STEREO);// | GLUT_DOUBLE | GLUT_RGBA ); //glutInitDisplayMode (GLUT_RGB | GLUT_DEPTH | GLUT_STEREO);// | GLUT_DOUBLE | GLUT_RGBA ); glutInitWindowPosition (0,0); glutInitWindowSize ( 700, 500 ); glutCreateWindow ( "Computação Gráfica - Transformações Hierárquicas 3D" ); init (); glutDisplayFunc ( display ); glutReshapeFunc ( reshape ); glutKeyboardFunc ( keyboard ); glutSpecialFunc ( arrow_keys ); glutIdleFunc ( display ); glutMainLoop ( ); return 0; }