首页 > 解决方案 > OpenGL绘制区域仅占据可用窗口的左下象限

问题描述

我刚刚开始使用 OpenGL 和 PyOpenGL,并且正在使用此页面 https://noobtuts.com/python/opengl-introduction中的教程代码。但是我很快遇到了以下问题:虽然代码成功绘制了预期的内容,但绘图不能占据我窗口的左下象限。例如,在下面我设置矩形的大小和位置,使其占据整个窗口,正如您在下面的代码中看到的那样,我将矩形的宽度和高度设置为窗口的宽度和高度,位置为 0,0所以我希望整个窗口变成蓝色,但这并没有发生,如下所示。我在 Mac OS Catalina 上并在 Python 3 上运行 PyOpenGL。

我在其他地方看到这个地方与 Catalina 有关:https ://github.com/redeclipse/base/issues/920 和这个地方https://github.com/ioquake/ioq3/issues/422#问题comment-541193050

然而,这对我来说太高级了,我无法理解。

有人知道如何解决吗?

提前感谢您的帮助

from OpenGL import *
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

window = 0  # glut window number
width, height = 500, 400  # window size

def refresh2d(width, height):
    glViewport(0, 0, width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0.0, width, 0.0, height, 0.0, 1.0)
    glMatrixMode (GL_MODELVIEW)
    glLoadIdentity()

def draw_rect(x, y, width, height):
    glBegin(GL_QUADS)                                  # start drawing a rectangle
    glVertex2f(x, y)                                   # bottom left point
    glVertex2f(x + width, y)                           # bottom right point
    glVertex2f(x + width, y + height)                  # top right point
    glVertex2f(x, y + height)                          # top left point
    glEnd()                                            # done drawing a rectangle

def draw():  # ondraw is called all the time
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)  # clear the screen
    glLoadIdentity()  # reset position
    refresh2d(width, height)  # set mode to 2d

    glColor3f(0.0, 0.0, 1.0)  # set color to blue
    draw_rect(0, 0, 500, 400)  # rect at (0, 0) with width 500, height 400

    glutSwapBuffers()  # important for double buffering


# initialization
glutInit()  # initialize glut
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowSize(width, height)  # set window size
glutInitWindowPosition(0, 0)  # set window position
window = glutCreateWindow("my first attempt")  # create window with title
glutDisplayFunc(draw)  # set draw function callback
glutIdleFunc(draw)  # draw all the time
glutMainLoop()  # start everything

但是,这是行不通的。我肯定会得到一个蓝色矩形仅占据左下象限的窗口。

标签: pythonmacosopenglmacos-catalinapyopengl

解决方案


FWIW,使用glfw我能够解决这个问题:

width = 1280
height = 1024
win = glfw.CreateWindow(width, height, "window title")
fb_width, fb_height = glfw.GetFramebufferSize(win)
glViewport(0, 0, fb_width, fb_height) # <--- this is the key line

推荐阅读