/*******************F******************************** * * RODRIGO LUIS DE SOUZA DA SILVA 28/04/2005 * * 08_projection.cpp * * 28/04/2005 - Reshape function added * 28/04/2005 - Cameras Settings Changed * 28/04/2005 - Keyboard and Special Keys added * 28/04/2005 - Light Control Added * 03/11/2006 - Fix GluLookAtPosition ***************************************************/ /* Programa simples que demonstra diferença entre a projeção ortogonal e perspectiva no OpenGL. */ #ifdef WIN32 #include #endif #include #include #include #include float angle = 0, raio = 0.8; int enableMenu = 0; int dist = 25; int proj = 0; int width = 600; int height = 600; float aux = 1; float luzX = -2, luzY = -2, luzZ = -5; char texto[30]; GLfloat light0_position[] = {luzX, luzY, luzZ, 0.0f}; float Ax =0.0, Ay=-8.0, Az=-8.0; float Bx=0.0, By=0.0, Bz=0.0; float Cx=0.0, Cy=1.0, Cz=0.0; float Posicao = Ay * 0.005; bool idleOn=true; //gluLookAt (0.0, -8.0, -8.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); float rotationX = 0.0, rotationY = 0.0; int last_x, last_y; void showMenu() { system("cls"); printf("\nPROJECTION\n==========\n\n"); printf("Mouse\n"); printf("* Clique com o botao esquerdo para mover camera ou objeto.\n"); printf("* Clique com o botao direito para alterar modo de projecao.\n"); printf("Teclado\n"); printf("* Direcionais + PgUp/PgDown movimentam a luz no plano (start: [0, 0, 5])\n"); printf("* ESC - Para sair\n"); } //Reshape Func void reshape( int w, int h) { width = w; height = h; glViewport(0,0,w,h); } void keyboard(unsigned char key, int x, int y) { switch (tolower(key)) { case 27 : exit(0); break; } glutPostRedisplay(); } void desenhaEixos() { glDisable(GL_LIGHTING); /* glColor3f (0.0, 1.0, 0.0); glBegin(GL_LINES); glColor3f(1.0, 0.0, 0.0); glVertex3f (-dist, 0.0, 0.0); glVertex3f ( dist, 0.0, 0.0); glColor3f(0.0, 1.0, 0.0); glVertex3f (0.0, -dist, 0.0); glVertex3f (0.0, dist, 0.0); glColor3f(0.0, 0.0, 1.0); glVertex3f (0.0, 0.0, -dist); glVertex3f (0.0, 0.0, dist); glEnd(); */ glBegin(GL_LINE_LOOP); glColor3f (1.0, 1.0, 1.0); glVertex3f (0.0, 0.0, 0.0); glVertex3f (5.0, 0.0, 0.0); glVertex3f (5.0, 5.0, 0.0); glVertex3f (0.0, 5.0, 0.0); glEnd(); glBegin(GL_LINES); int x; glColor3f (1.0, 1.0, 1.0); for (x = 1; x < 5; x++) { glVertex3f (x, 0.0, 0.0); glVertex3f (x, 5.0, 0.0); } for (x = 1; x < 5; x++) { glVertex3f (0.0, x, 0.0); glVertex3f (5.0, x, 0.0); } glEnd(); glEnable(GL_LIGHTING); } void idle() { Ay = Ay + 1 * 0.005; glutPostRedisplay(); } // Motion callback void motion(int x, int y ) { rotationY += (float) (y - last_y); rotationX += (float) (x - last_x); last_x = x; last_y = y; idleOn = !idleOn; if (idleOn) glutIdleFunc( NULL ); else glutIdleFunc( idle ); glutPostRedisplay(); } // Mouse callback void mouse(int button, int state, int x, int y) { if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) { last_x = x; last_y = y; } if ( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN ) { proj= !proj; if(!proj) printf("Projecao Ortogonal.\n"); else printf("Projecao Perspectiva.\n"); } } void special(int key, int x, int y) { switch (key) { case GLUT_KEY_LEFT: luzX+=1.0; break; case GLUT_KEY_RIGHT: luzX-=1.0; break; case GLUT_KEY_UP: luzY+= 1.0; break; case GLUT_KEY_DOWN: luzY-= 1.0; break; case GLUT_KEY_PAGE_UP: luzZ+=0.5; break; case GLUT_KEY_PAGE_DOWN: luzZ-=0.5; break; } light0_position[0] = luzX; light0_position[1] = luzY; light0_position[2] = luzZ; printf("New Light Position [%.1f,%.1f,%.1f]\n", luzX, luzY, luzZ); glLightfv(GL_LIGHT0, GL_POSITION, light0_position); glutPostRedisplay(); } void display(void) { // Limpar todos os pixels glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // inicializar sistema de projeção glMatrixMode(GL_PROJECTION); glLoadIdentity(); glLightfv(GL_LIGHT0, GL_POSITION, light0_position); // if(!proj) // glOrtho(0, 5, 0, 5, -200.0, 200.0); // //glOrtho(-orho, orho, -orho, orho, -200, 200); // else gluPerspective(40.0f,(GLfloat)width/(GLfloat)height,0.1f,200.0f); // Calculate The Aspect Ratio Of The Window glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //gluLookAt (0.0, -8.0, -8.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); gluLookAt (Ax, Ay, Az, Bx, By, Bz, Cx, Cy, Cz); // Movimenta a camera em torno do objeto // Luz fica fixa na posição (0, 0, 5) inicialmente glRotatef(rotationY, 1.0, 0.0, 0.0 ); glRotatef(rotationX, 0.0, 1.0, 0.0 ); glPushMatrix(); glColor3f (1.0, 1.0, 1.0); glDisable(GL_LIGHTING); // glutWireCube(10); glEnable(GL_LIGHTING); glTranslatef(-2.5f,-2.5f,0.0f); desenhaEixos(); glPopMatrix(); glPushMatrix(); glColor3f (1.0, 0.0, 0.0); glTranslatef(luzX,luzY,-0.5f); //glutSolidCube(1); glPopMatrix(); glutSwapBuffers (); glutPostRedisplay(); } void init (void) { // selecionar cor de fundo (preto) glClearColor (0.0, 0.0, 0.0, 0.0); glEnable(GL_LIGHT0); // habilita luz 0 glEnable(GL_COLOR_MATERIAL); // Utiliza cor do objeto como material glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); glLightfv(GL_LIGHT0, GL_POSITION, light0_position); glEnable(GL_LIGHTING); // Habilita luz glEnable(GL_DEPTH_TEST); // Habilita Z-buffer glEnable(GL_CULL_FACE); // Habilita Backface-Culling } int main(int argc, char** argv) { showMenu(); glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE|GLUT_DEPTH|GLUT_RGB); glutInitWindowSize (width, height); glutInitWindowPosition (100, 100); glutCreateWindow ("hello"); init (); glutMouseFunc( mouse ); glutKeyboardFunc(keyboard); glutSpecialFunc(special); glutMotionFunc( motion ); glutReshapeFunc( reshape ); glutDisplayFunc(display); glutIdleFunc(idle); glutMainLoop(); return 0; }