Search code examples
pythonopenglpygameopengl-3

Two OpenGL screens in one pygame window


I am using pygame with opengl, but I am having problems setting up many opengl screens (or "cameras") in the same pygame window. My code is something like

{initialize the screen using pygame.display.set_mode, glViewport, gluPersepctive etc}

while 1:
    {obtain pressed keys from the keyboard and do stuff}

    {draw opengl objects}

    pygame.display.flip()

Now, what I want is to do all this say four times, and draw the screen in four different places in the same pygame window. I tried to just put the opengl drawing in a for loop and changing the glViewport inside but did not make any progress. So which commands should I run four times to draw four different opengl screens?


Solution

  • Actually I found the problem with the code. The corrected (pseudo)code is below

    {initialize the screen using pygame.display.set_mode, glViewport, gluPersepctive etc}
    screen_params=[(0.,0.,0.5,0.5),(0.5,0.5,0.5,0.5),(0.,0.5,0.5,0.5),(0.5,0.,0.5,0.5)]
    while 1:
        {obtain pressed keys from the keyboard and do stuff}
        glClearColor(1.0, 1.0, 1.0, 0.0)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        for s in screen_params:
            glViewport(int(WIDTH*S[0]), int(HEIGHT*S[1]), int(WIDTH*S[2]), int(HEIGHT*S[3]))
            {draw opengl objects}
    pygame.display.flip()
    

    The key is to not run the Clear commands in every loop (duh), which I did before, but the code above will copy whatever you drew before onto four smaller screens, if WIDTH and HEIGHT are the sizes of your screen.