如何制作可调整大小的窗口?

问题描述 投票:0回答:1

我正在遵循 OpenGL Superbible 中的示例程序,它们听起来像是使用 glutCreateWindow 创建的窗口将可以调整大小。我正在使用下面的清单 2.2 的我自己的 python 版本,并且我的窗口永远无法调整大小。我在用: macOS Ventura 上的 PyOpenGL 3.1.7(和“freeglut”(我认为在我安装 freeglut 之前也是一样的)。

这是清单:

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

def change_size(w, h):
    glViewport(0, 0, w, h)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    aspect_ratio = w/h
    if w <= h:
        glOrtho(-100, 100, -100/aspect_ratio, 100/aspect_ratio, 1, -1)
    else:
        glOrtho(-100*aspect_ratio, 100*aspect_ratio, -100, 100, 1, -1)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    
    
def render_scene():
    glClear(GL_COLOR_BUFFER_BIT)
    glColor(1,0,0,0)
    glRectf(-25,25,25,-25)
    glFlush()
        
def setup_rc():
    glClearColor(0, 0, 1, 1)
    
def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA)
    glutCreateWindow("Simple")
    glutDisplayFunc(render_scene)
    glutReshapeFunc(change_size)
    setup_rc()
    glutMainLoop()
    
main()

有没有一种简单的方法来创建可调整大小的主窗口?

python macos pyopengl freeglut glutcreatewindow
1个回答
0
投票

glut 是一个旧库,在各种系统上都存在一些已知问题,例如 Mac 上的调整大小错误。如果您的系统上存在 glut 问题,我建议您使用更新的 glfw

安装glfw

pip3 install glfw

只需进行一些更改即可使用 _glfw 而不是 glut 重写代码:

from OpenGL.GL import *
from glfw.GLFW import *

def change_size(window, _w, _h):
    w, h = glfwGetFramebufferSize(window)
    glViewport(0, 0, w, h)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    aspect_ratio = w/h
    if w <= h:
        glOrtho(-100, 100, -100/aspect_ratio, 100/aspect_ratio, 1, -1)
    else:
        glOrtho(-100*aspect_ratio, 100*aspect_ratio, -100, 100, 1, -1)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    
def render_scene():
    glClear(GL_COLOR_BUFFER_BIT)
    glColor(1,0,0,0)
    glRectf(-25,25,25,-25)
    glFlush()
        
def setup_rc():
    glClearColor(0, 0, 1, 1)
    
def main():
    if glfwInit() == GLFW_FALSE:
        raise Exception("error: init glfw")

    glfwWindowHint(GLFW_SAMPLES, 8)
    window = glfwCreateWindow(640, 480, "Simple", None, None)
    glfwMakeContextCurrent(window)

    setup_rc()
    glfwSetWindowSizeCallback(window, change_size)  
    change_size(window, 640, 480)
    while not glfwWindowShouldClose(window):
        glfwPollEvents()
        render_scene()
        glfwSwapBuffers(window)
    
main()
© www.soinside.com 2019 - 2024. All rights reserved.