python - 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
但是,这是行不通的。我肯定会得到一个蓝色矩形仅占据左下象限的窗口。
解决方案
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
推荐阅读
- android - Gradle sync failed: 系统找不到指定的路径
- perl - 在 Perl 中转换时间戳
- pop3 - 在 POP3 中,RETR 命令的预期第一响应行是什么?
- javascript - 从网页下载多个 JSON 时,AJAX 出错
- ios - 我可以从 IOS 收集 firebase 动态链接的 utm 参数吗?
- python - 如何知道 pygame.Rect 与其他 Rect 碰撞的一侧?
- visual-studio-2019 - 保存时 Gulp.js 手表在 VS 2019 中不起作用
- vue.js - Vue.js 从列表中选择单选选项
- autohotkey - Autohotkey:将 Ultrawide 21:9 纵横比更改为 16:9
- mysql - 在 SQL 中选择日期之间的平均差异