Search code examples
pythonopenglpyopenglopengl-compat

How to use multiple glViewport() and glOrtho()


I am trying to use pygame and pyopengl, in the main window i have 2 viewports 1 big map and 1 minimap (both presenting the same frame). i need both maps to rotate around a center who isnt 0,0,0 (lets say i need the center of rotation to be -130,0,60 which needs to be a constant point)

also i need 1 view to view a distance of glTranslatef(0, 0, -1000) and the 2nd view to be glTranslatef(1, 1, -200) both distances are constant

i tried to use

gluLookAt()
glOrtho()

but it doesnt change the rotation.... around 0,0,0 or i might be using it wrong.

the code looks like this:

pygame.init()
display = (1700, 1000)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
gluPerspective(50, (display[0] / display[1]), 0.1, 5000)
glTranslatef(0, 0, -1000) # this is the view distance i want from map 1
while True:

   ##### i use this function to zoom in and out with mouse Wheel
   ##### also the zoom in/out zooms to 0,0,0 and i need (-130,0,60)
   if move_camera_distance:
        if zoom_in:
            glScalef(0.8,0.8,0.8)
        elif zoom_out:
            glScalef(1.2, 1.2, 1.2)
        move_camera_distance = False
        zoom_in = False
        zoom_out = False        


    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    ###### Map 1 
    ###### Need to rotate around (-130,0,60)
    ###### distance from camera need to be (0,0,-1000)
    glViewport(1, 1, display[0], display[1])  # splits the screen
    glCallList(obj.gl_list)
    DrawBuffer(bufferObj, noPoints, noCirclePoints, noCrossPoints) 


    ###### Map 2
    ###### Need to rotate around (-130,0,60)
    ###### distance from camera need to be (0,0,-300)
    glViewport(1300, 650, 400, 400)  # splits the screen
    glCallList(obj.gl_list)
    DrawBuffer(bufferObj, noPoints, noCirclePoints, noCrossPoints)

    pygame.display.flip()
    pygame.time.wait(10)

The output i get is 2 maps, both rotate around 0,0,0 both are from a distance of (0,0,-1000) and both change together if i change anything in the While loop. thanks for help.


Solution

  • Note that the current matrix can be stored to the matrix stack by glPushMatrix and restored from the matrix stack by glPopMatrix. There are different matrix modes and matrix stacks (e.g. GL_MODELVIEW, GL_PROJECTION). See (glMatrixMode).

    Set the projection matrix at initialization and set different modelview matrices for the different views:

    glMatrxMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(50, (display[0] / display[1]), 0.1, 5000)
    
    glMatrxMode(GL_MODELVIEW)
    glLoadIdentity()
    # [...]
    
    while True:
    
        ###### Map 1 
        glViewport(1, 1, display[0], display[1])  # splits the screen
    
        glPushMatrix()
        glTranslatef(0, 0, -1000) # this is the view distance i want from map 1
        # [...]
        glPopMatrix()
    
    
        ###### Map 2
        glViewport(1300, 650, 400, 400) # splits the screen
    
        glPushMatrix()
        glTranslatef(0, 0, -300) # this is the view distance i want from map 1
        # [...]
        glPopMatrix()
    
    

    To rotate the model around a point, you've to:

    • Translate the model in that way that way, that the pivot is in the origin of the world. This is a translation by the inverse pivot vector.
    • Rotate the model.
    • Translate the rectangle in that way that the pivot is back at its original position.

    In the program, this operations have t o be applied in revers order, because operations like glTranslate and glRotate define a matrix and multiply the matrix to the current matrix:

    glTranslatef(-130, 0, 60);
    glRotate( ... )
    glTranslatef(130, 0, -60);
    

    Do this immediately before you draw the object.