How to use multiple viewports in OpenGL?

前端 未结 6 926
说谎
说谎 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 07:42

    Nehe has a good tutorial on how to do this, and his site is generally a good resource for OpenGL questions.

    0 讨论(0)
  • 2020-12-08 07:47

    yes,

    and you should also change the scissor settings to have a clean separation between the two views if they are in the same window.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-08 08:02

    Minimal runnable example

    Similar to this answer, but more direct and compilable. Output:

    main.c

    #include <stdlib.h>
    
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <GL/glut.h>
    
    static int width;
    static int height;
    
    static void display(void) {
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(1.0f, 0.0f, 0.0f);
    
        glViewport(0, 0, width/2, height/2);
        glLoadIdentity();
        gluLookAt(0.0, 0.0, -3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
        glutWireTeapot(1);
    
        glViewport(width/2, 0, width/2, height/2);
        glLoadIdentity();
        gluLookAt(0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
        glutWireTeapot(1);
    
        glViewport(0, height/2, width/2, height/2);
        glLoadIdentity();
        gluLookAt(0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0);
        glutWireTeapot(1);
    
        glViewport(width/2, height/2, width/2, height/2);
        glLoadIdentity();
        gluLookAt(0.0, -3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0);
        glutWireTeapot(1);
    
        glFlush();
    }
    
    static void reshape(int w, int h) {
        width = w;
        height = h;
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
        glMatrixMode(GL_MODELVIEW);
    }
    
    int main(int argc, char** argv) {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
        glutInitWindowSize(500, 500);
        glutInitWindowPosition(100, 100);
        glutCreateWindow(argv[0]);
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glShadeModel(GL_FLAT);
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
        glutMainLoop();
        return EXIT_SUCCESS;
    }
    

    Compile and run:

    gcc -o main.out main.c -lGL -lGLU -lglut
    ./main.out
    

    I think that in modern OpenGL 4 you should just render to textures, and then place those textures orthogonaly on the screen, see this as a starting point: http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/

    Tested on OpenGL 4.5.0 NVIDIA 352.63, Ubuntu 15.10.

    0 讨论(0)
  • 2020-12-08 08:07

    In GL 4 you can render to many viewports in one rendering pass. See ARB_viewport_array and related concepts.

    0 讨论(0)
  • 2020-12-08 08:09

    Think of OpenGL as being nothing more than commands which prepare you to output to the window you're currently working with.

    There's two commands with OpenGL that even NEHE's tutorials don't tell you the importance of:

    wglCreateContext - which takes a window device context DC, can be obtained from ANY window - whether it's a user control, a windows form, a GL window, or another application window (like notepad). This creates an OpenGL device context - they refer to as a resource context - which you later use with ...

    wglMakeCurrent - which takes two parameters, the Device Context you're dealing with (the parameter passed in for the Windows Device Context in wglCreateContext) - and the Resource Context that returns.

    Leveraging ONLY these two things - here's my advice:

    NEHE's tutorial provides a solution that leverages the existing window ONLY and segments the screen for drawing. Here's the tutorial: http://nehe.gamedev.net/tutorial/multiple_viewports/20002/

    Leveraging glViewport you'll need to re-draw on every update.

    That's one method.

    But there's another - less graphically and processor intense method:

    Create a window for each view by leveraging a user control.

    Each window has it's own hWnd.

    Get the DC, process the wglcreatecontext, and then, on a timer (mine is 30 frames a second), if you detect state change, then select wglMakeCurrent for that view and redraw. Otherwise, just skip the section entirely.

    This conserves valuable processing power, and also reduces the code from having to manage the window and viewport calculations manually.

    0 讨论(0)
提交回复
热议问题