2010-10-22 4 views
5

J'essaie donc d'apprendre à utiliser glViewport(), "MULTIPLE TIMES". Ci-dessous est mon code, j'ai essayé de suivre d'autres exemples, mais ils sont si compliqués avec d'autres choses qui ne sont pas pertinentes à ce que je fais ou, je ne comprends pas ce qu'ils font. En passant, j'utilise glut pour gérer mon système de fenêtrage. Donc, la meilleure façon pour moi d'apprendre est d'obtenir un exemple simple en cours d'exécution pour moi-même, puis extrapoler à partir de là. Ci-dessous est mon code pour un programme simple qui divise l'écran en deux et dessine deux sphères identiques, je ne peux pas comprendre pourquoi la sphère sur la droite est étirée lorsque gluPerspective() est identique pour les deux ports de vue. S'il vous plaît, si vous pouviez simplement m'expliquer ce que je faisais de mal dans mon code, cela aiderait grandement. Les ressources externes sont excellentes, mais j'ai besoin d'exemples simples et simples (pas d'exemples de Nate Robinson).OpenGL Viewports multiples et gluPerspective()

GLfloat width = 800, height = 600, x_0 = 470, y_0 = 0; 
int mainWindow; 


    void resize(int w, int h){ 
     width=w; 
     height=h; 
    } 

    void draw(void){ 
     glEnable(GL_SCISSOR_TEST); 
     glClearDepth(1.0); 
     glClearColor(0.0, 0.0, 0.0, 1.0); 
     glEnable(GL_DEPTH_TEST); 
     glEnable(GL_LIGHT0); 
     glEnable(GL_LIGHTING); 
     GLfloat color[4] = {1.0, 0.0, 0.0, 1.0}; 

     /* 
      * LEFT VIEW PORT 
      */ 
     glMatrixMode(GL_PROJECTION); 
     glLoadIdentity(); 
     glViewport(0, 0, (float)width/2 , height); 
     gluPerspective(45, ((float)width/2)/(float)height, 1, 2000); 
     glMatrixMode(GL_MODELVIEW); 
     glScissor(0, 0, (float)width , height); 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     glLoadIdentity(); 
     gluLookAt(0.0, 0.0, 50.0, 
        0.0, 0.0, 0.0, 
        0.0, 1.0, 0.0); 

     glPushAttrib(GL_LIGHTING_BIT | GL_CURRENT_BIT); 
     glPushMatrix(); 
     glMaterialfv(GL_FRONT, GL_DIFFUSE, color); 
     glutSolidSphere(10.0, 20, 40); 
     glPopMatrix(); 
     glPopAttrib(); 

     /* 
      * THE SECOND VIEW PORT 
      */ 
     glMatrixMode(GL_PROJECTION); 
     glLoadIdentity(); 
     glViewport((float)width/2, 0, width , height); 
     gluPerspective(45, ((float)width)/2/((float)height), 1, 2000); 
     glMatrixMode(GL_MODELVIEW); 
     glScissor(((float)width/2), 0, width , height); 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     glLoadIdentity(); 
     gluLookAt(0.0, 0.0, 50.0, 
        0.0, 0.0, 0.0, 
        0.0, 1.0, 0.0); 
     glPushAttrib(GL_LIGHTING_BIT | GL_CURRENT_BIT); 
     glPushMatrix(); 
     glMaterialfv(GL_FRONT, GL_DIFFUSE, color); 
     glutSolidSphere(10.0, 20, 40); 
     glPopMatrix(); 
     glPopAttrib(); 

     glDisable(GL_SCISSOR_TEST); 
     glFlush(); 
     glutSwapBuffers(); 
    } 

    void glutAppInitialize(void){ 
     GLfloat light_ambient[] = {0.2, 0.2, 0.2, 1.0}; 
     GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; 
     GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0}; 
     GLfloat light_position[] = {1.0, 1.0, 0.0, 0.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); 
    } 

    int main(int argc, char* argv[]){ 
     glutInit(&argc, argv); 
     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); 
     glutInitWindowPosition(x_0, y_0); 
     glutInitWindowSize(width ,height); 
     glutAppInitialize(); 
     mainWindow = glutCreateWindow("Tori App"); 
     glutDisplayFunc(draw); 
     glutReshapeFunc(resize); 
     glutMainLoop(); 
     return(0); 
    } 

Notez que je joue autour pendant que j'attends une réponse, je l'ai eu à faire ce que je voulais, mais la solution ne fait pas puisque, s'il vous plaît voir les lignes de code, essentiellement je me suis déplacé les ports de vue commencent position à 1/4 de l'écran au lieu de sur la moitié et je ne suis plus diviser par deux sur gluperspective.

/* 
* THE SECOND VIEW PORT 
*/ 

    glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
glViewport(width/4, 0, width , height); 
gluPerspective(45, ((float)width)/(((float)height)), 1, 2000); 

Répondre

5

La deuxième paire d'arguments pour glViewport sont largeur et hauteur, pas la fin x/y de position. Votre deuxième glViewport doit être glViewport(width/2, 0, width/2, height)