// ************************************************ // Navegacao3.cpp // Sample file for SmallVR // Authors: Marcio Serolli Pinho, André B. Trombetta, // Felipe Bacim, Régis Kopper // // Email: pinho@inf.pucrs.br // Homepages: // http://www.smallvr.org // http://grv.inf.pucrs.br // http://www.inf.pucrs.br/~pinho // // Porto Alegre, Brazil, 2002. // ************************************************ #ifdef WIN32 #include #endif #include // SmallVR header file #include "SmVR.h" float WindowRatio; // ****************************** // User stuff // see "PosicUser" function // on how to use these variables // ****************************** // User position SmVR_CPoint pt_user(0,1.6,2); // Target position SmVR_CPoint pt_target(0,1.0,0); // Root Object to represent the entire scene SmVR_CGeometricObject *RootObject; // Viewing angle in degrees float ViewAngle = 90; // ****************************** // Light stuff // ****************************** GLint especMaterial = 60; GLfloat posicaoLuz1 [4] = { 0.0, 1.0, 1.0, 1.0}; GLfloat luzAmbiente [4] = { 0.5, 0.5, 0.5, 1.0}; GLfloat luzDifusa [4] = { 0.7, 0.7, 0.7, 1.0}; // "cor" do objeto GLfloat luzEspecular [4] = { 0.5, 0.5, 0.5, 1.0}; // brilho do objeto GLfloat especularidade[4] = { 0.17, 0.17, 0.17, 1.0}; // capacidade de brilho do material // ********************************************************************** // void PosicUser() // // // ********************************************************************** void PosicUser() { // Set the clipping volume glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(ViewAngle,WindowRatio,0.01,200); // Set the user Position glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(pt_user.X,pt_user.Y, pt_user.Z, pt_target.X,pt_target.Y, pt_target.Z, 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; WindowRatio = 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 display( void ) // // // ********************************************************************** void display( void ) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); PosicUser(); glMatrixMode(GL_MODELVIEW); RootObject->Render(); glutSwapBuffers(); } // ********************************************************************** // void specialFunc (int key, int x, int y) // // ********************************************************************** void specialFunc (int key, int x, int y) { switch (key) { case GLUT_KEY_PAGE_UP: break; case GLUT_KEY_PAGE_DOWN: break; case GLUT_KEY_LEFT: break; case GLUT_KEY_RIGHT: break; case GLUT_KEY_UP: break; case GLUT_KEY_DOWN: break; } } // ********************************************************************** // void keyboard ( unsigned char key, int x, int y ) // // // ********************************************************************** void keyboard ( unsigned char key, int x, int y ) { switch ( key ) { case 27: // Encerra o programa exit(0); break; } glutPostRedisplay();// força o refresh da tela } // ********************************************************************** // void init(void) // Inicializa os parâmetros globais de OpenGL // // ********************************************************************** void init(void) { glShadeModel(GL_SMOOTH); // Habilita o modelo de tonalizacao de Gouraud glClearColor(1.0f, 1.0f, 1.0f, 0.0f); // Cor de fundo de tela glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); glMaterialfv(GL_FRONT,GL_SPECULAR, especularidade); // Define a refletancia do material glMateriali(GL_FRONT,GL_SHININESS,especMaterial); // Define a concentracao do brilho glLightModelfv(GL_LIGHT_MODEL_AMBIENT, luzAmbiente); // Ativa o uso da luz ambiente // Define os parametros da luz de numero 0 glLightfv(GL_LIGHT0, GL_AMBIENT , luzAmbiente ); glLightfv(GL_LIGHT0, GL_DIFFUSE , luzDifusa ); glLightfv(GL_LIGHT0, GL_SPECULAR, luzEspecular); glLightfv(GL_LIGHT0, GL_POSITION, posicaoLuz1 ); glEnable(GL_COLOR_MATERIAL); // Habilita a definicao da cor do material a partir da cor corrente glEnable(GL_LIGHTING); // Habilita o uso de iluminacao glEnable(GL_LIGHT0); // Habilita a luz de numero 0 glEnable(GL_DEPTH_TEST); // Habilita o depth-buffering glEnable(GL_NORMALIZE); // Habilita a normalizacao das normais } // ********************************************************************** // void CriaCenario() // Função de Criação do Cenários // // ********************************************************************** void CriaCenario() { // Cria o objeto SmallVR SmVR_CGeometricObject *TestObject; // Cria um objeto para carregar o modelo de arquivo SmVR_COBJLoader *obj1 = new SmVR_COBJLoader(); // Executa a carga do objeto obj1->Load("..//TestFiles//Objetos3D//Piso.obj"); // Associa o objeto ligo de arquivo com o objeto SmallVR // Isto é feito passando o objeto "obj1" na construtora da classe // do objeto SmallVR TestObject = new SmVR_CGeometricObject("Piso", obj1); // Adiciona o objeto recém criado ao grafo de cena RootObject->AddChild(TestObject); int v[3]; // vetor para armazenar os números dis vértices de uma face // Obtém um apontador para todos os vértices TVertices *Vert = obj1->GetVertices(); // Obtém o número de faces int nFaces = obj1->GetNFaces(); for (int f= 0; f< nFaces; f++) // para cada face { printf ("Face %d\n", f); // Obtém os números dos vértices da face "f" obj1->GetFace(f,v); for (int n=0; n<3; n++) // para cada vértice { printf ("\tVertice %d: x = %6.3f y = %6.3f z = %6.3f\n", n, Vert[v[n]].x, Vert[v[n]].y, Vert[v[n]].z); } printf("\n"); } // Cria um objeto para carregar o modelo de arquivo obj1 = new SmVR_COBJLoader(); // Executa a carga do objeto obj1->Load("..//TestFiles//Objetos3D//Couch.obj"); // Associa o objeto ligo de arquivo com o objeto SmallVR // Isto é feito passando o objeto "obj1" na construtora da classe // do objeto SmallVR TestObject = new SmVR_CGeometricObject("Sofa", obj1); // Reposiciona o sofá TestObject->TranslateBy(0,0.4,-2); TestObject->RotateBy(180,0,1,0); // Adiciona o objeto recém criado ao grafo de cena RootObject->AddChild(TestObject); } // ********************************************************************** // void main ( int argc, char** argv ) // // // ********************************************************************** int main ( int argc, char** argv ) { // GLUT initialization glutInit ( &argc, argv ); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);// | GLUT_DOUBLE | GLUT_RGBA ); glutInitWindowPosition (0,0); glutInitWindowSize ( 700, 500 ); glutCreateWindow ( "SmallVR Test - Second program" ); // initialize OpenGL parameters init (); // initialize the library RootObject = SmVR_Init(NULL); CriaCenario(); // GLUT stuff glutDisplayFunc ( display ); glutReshapeFunc ( reshape ); glutKeyboardFunc ( keyboard ); glutIdleFunc ( display ); glutMainLoop ( ); return 0; }