我刚开始使用OpenGL和PyOpenGL,正在使用本页的教程代码https://noobtuts.com/python/opengl-introduction.然而,我很快就遇到了以下问题:虽然代码成功地绘制了预期的内容,但绘制的内容不能超过窗口的左下象限。例如,在下面的代码中,我设置了矩形的大小和位置,使其占据整个窗口,正如你在下面代码中看到的那样,我将矩形的宽度和高度设置为窗口的宽度和宽度,位置为0,0,所以我希望整个窗口变为蓝色,但这并没有发生,正如你下面看到的那样。我在Mac操作系统Catalina上,在Python 3上运行PyOpenGL。
我在其他地方看到,这个地方与卡塔琳娜有关:https://github.com/redeclipse/base/issues/920还有这个地方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
然而,这不起作用。我肯定得到了一个蓝色矩形只占据左下象限的窗口。
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
您可以安装修改后的供过于求http://iihm.imag.fr/blanch/software/glut-macosx/
或者你可以做
glViewport(0, 0, width*2, height*2)
如果你不关心DPI