Search code examples
pythonopenglpyglet

Multiple viewports on one Pyglet window?


I need to make mutiple viewports of cube in python Pyglet, but i always just have one.

Look at def on_resize():

WINDOW = 1000 
INCREMENT = 5
transparant = False

class Window(pyglet.window.Window):
    xRotation = yRotation = zRotation = 30
    zoom = 1
    far = 100
    dist = 35
    x = y = z = 0

    def __init__(self, width, height, title = '') :
        super(Window, self).__init__( 1300,1000, title)
        pgl.glClearColor(0, 0, 0, 1)
        pgl.glEnable(pgl.GL_DEPTH_TEST)



    def on_draw(self) :

        self.clear()

cube drawing

        pgl.glPopMatrix()

    def on_resize(self, width, height) :
        pgl.glClear(pgl.GL_COLOR_BUFFER_BIT | pgl.GL_DEPTH_BUFFER_BIT)

        pgl.glViewport(0, 0, 650, 500)
        pgl.glLoadIdentity()
        pgl.glMatrixMode(ogl.GL_PROJECTION)
        pgl.glOrtho(-width / 8, width / 8, -height / 8, height / 8, 0, 500)
        pgl.glFlush()

        pgl.glViewport(500, 0, 650, 500)
        pgl.glLoadIdentity()
        pgl.glMatrixMode(ogl.GL_PROJECTION)
        Ratio = width/height
        # pgl.gluPerspective(self.dist, Ratio, 1, 1000)
        pgl.glOrtho(-width/8, width/8, -height/8, height/8, 0, 500)
        pgl.glFlush()
        pgl.glMatrixMode(ogl.GL_MODELVIEW)
        pgl.glTranslatef(0, 0, -100)

Window(WINDOW, WINDOW, 'Cube')
pyglet.app.run()

Can you help to solve this problem?


Solution

  • Setting the viewport and matrices will modify OpenGL state. You have to draw something right after that. Setting a new viewport will just override the previous setting. Something like:

    pgl.glClear(pgl.GL_COLOR_BUFFER_BIT | pgl.GL_DEPTH_BUFFER_BIT)
    
    pgl.glViewport(0, 0, 650, 500)    
    pgl.glMatrixMode(ogl.GL_PROJECTION)
    pgl.glLoadIdentity()
    pgl.glOrtho(-width / 8, width / 8, -height / 8, height / 8, 0, 500)
    pgl.glMatrixMode(ogl.GL_MODELVIEW)
    # set modelview matrix
    # draw cube
    
    pgl.glViewport(500, 0, 650, 500)
    pgl.glMatrixMode(ogl.GL_PROJECTION)
    pgl.glLoadIdentity()
    # this is actually the same as before, so you can just leave it
    pgl.glOrtho(-width/8, width/8, -height/8, height/8, 0, 500)
    pgl.glMatrixMode(ogl.GL_MODELVIEW)
    pgl.glTranslatef(0, 0, -100)
    # set modelview matrix
    # draw cube