How to use multiple viewports in OpenGL?

前端 未结 6 925
说谎
说谎 2020-12-08 07:12

I need to show the same object in OpenGL in two different viewports, for instance, one using ortographic projection and the other using perspective. In order to do this, do

6条回答
  •  有刺的猬
    2020-12-08 08:00

     // normal mode
      if(!divided_view_port)
        glViewport(0, 0, w, h);
    else
    {
        // right bottom
        glViewport(w/2, h/2, w, h);
        glLoadIdentity ();
        gluLookAt(5.0f, 5.0f, 5.0f,
                  0.0f, 0.0f, 0.0f,
                  0.0f, 1.0f, 0.0f);
    
        display();
    
        // left bottom
        glViewport(0, h/2, w/2, h);
        glLoadIdentity();
        gluLookAt (5.0f, 0.0f, 0.0f,
                  0.0f, 0.0f, 0.0f,
                  0.0f, 1.0f, 0.0f);
    
        display();
    
        // top right
        glViewport(w/2, 0, w, h/2);
        glLoadIdentity();
        gluLookAt(0.0f, 0.0f, 5.0f,
                  0.0f, 0.0f, 0.0f,
                  0.0f, 1.0f, 0.0f);
    
        display();
    
        // top left
        glViewport(0, 0, w/2, h/2);
        glLoadIdentity();
        gluLookAt(0.0f, 5.0f, 0.0f,
                  0.0f, 0.0f, 0.0f,
                  0.0f, 1.0f, 0.0f);
    
        display();
    }
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    if (w <= h)
        glOrtho(-2.0, 2.0, 
                -2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w, 
        -10.0, 100.0); 
    else
        glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, 
        -2.0, 2.0, 
        -10.0, 100.0);
    
    glMatrixMode(GL_MODELVIEW);
    

提交回复
热议问题