OpenGL绘制区域仅占据可用窗口的左下象限

kb5ga3dv  于 2022-11-04  发布在  其他
关注(0)|答案(2)|浏览(122)

我刚刚开始使用OpenGL和PyOpenGL,正在使用本页https://noobtuts.com/python/opengl-introduction中的教程代码。然而,我很快就遇到了以下问题:虽然代码成功地绘制了所需的内容,但绘制的内容不能超过窗口的左下象限。例如,在下面的代码中,我设置了矩形的大小和位置,使其占据整个窗口,正如您在下面的代码中所看到的,我将矩形的宽度和高度设置为窗口的宽度和高度,位置为0。0,所以我希望整个窗口变成蓝色,但这并没有发生,因为你可以看到下面。我在Mac OS Catalina 和运行Python 3上的PyOpenGL。
我在其他地方看到过这个地方和 Catalina 有关系:和这个地方https://github.com/ioquake/ioq3/issues/422#issuecomment-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

然而,这是行不通的。我肯定会得到一个窗口,其中蓝色矩形只占据了左下象限。

arknldoa

arknldoa1#

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
dffbzjpn

dffbzjpn2#

你可以安装修改过的glut http://iihm.imag.fr/blanch/software/glut-macosx/
或者你可以

glViewport(0, 0, width*2, height*2)

如果您不关心DPI

相关问题