如何使用多个glViewport()和glOrtho()

时间:2019-07-07 16:04:38

标签: python opengl pyopengl opengl-compat

我正在尝试使用pygame和pyopengl,在主窗口中我有2个视口 1张大地图和1张小地图(都呈现相同的框架)。我需要两个地图都绕着一个不为0,0,0的中心旋转(假设我需要将旋转中心为-130,0,60,这需要是一个恒定点)

我还需要1个视图才能查看glTranslatef(0, 0, -1000)的距离 且第二个视图均为glTranslatef(1, 1, -200),两个距离都是恒定

我尝试使用

gluLookAt()
glOrtho()

但是它不会改变旋转。...大约0,0,0 否则我可能用错了。

代码如下:

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)

我得到的输出是2个贴图,都绕着0,0,0旋转,两者都距(0,0,-1000)的距离,并且如果我在While循环中进行任何更改,它们都一起改变。 感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

尝试在绘制缓冲区之前调用glLoadIdentity

答案 1 :(得分:0)

请注意,当前矩阵可以由glPushMatrix存储到矩阵堆栈,并可以由glPopMatrix从矩阵堆栈恢复。存在不同的矩阵模式和矩阵堆栈(例如GL_MODELVIEWGL_PROJECTION)。请参阅(glMatrixMode)。

在初始化时设置投影矩阵,并为不同的视图设置不同的modelview矩阵:

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()


要围绕某个点旋转模型,您必须:

  • 以这种方式转换模型,使枢轴位于世界的起源中。这是反枢轴向量的平移。
  • 旋转模型。
  • 以枢轴返回其原始位置的方式平移矩形。

在程序中,此操作不能以相反的顺序应用,因为glTranslateglRotate之类的操作定义了一个矩阵并将该矩阵乘以当前矩阵:

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

在绘制对象之前立即执行此操作。

相关问题