【问题标题】:How to set window hint for GLFW in Python如何在 Python 中为 GLFW 设置窗口提示
【发布时间】:2020-12-11 12:17:12
【问题描述】:

我编写了这段 Python 代码,它将在使用 GLFW 创建的窗口中绘制一个三角形:

import glfw
from OpenGL.GL import *
from OpenGL.GL.shaders import compileProgram, compileShader
import numpy as np

vertex_src = """
# version 330 core
in vec3 a_position;
void main() {
    gl_position = vec4(a_position, 1.0);
}
"""

fragment_src = """
# version 330 core
out vec4 out_color;
void main() {
    out_color = vec4(1.0, 0.0, 0.0, 1.0);
}
"""

if not glfw.init():
    print("Cannot initialize GLFW")
    exit()

window = glfw.create_window(320, 240, "OpenGL window", None, None)
if not window:
    glfw.terminate()
    print("GLFW window cannot be creted")
    exit()

glfw.set_window_pos(window, 100, 100)
glfw.make_context_current(window)

vertices = [-0.5, -0.5, 0.0,
            0.5, -0.5, 0.0,
            0.0, 0.5, 0.0]
colors = [1, 0, 0.0, 0.0,
          0.0, 1.0, 0.0,
          0.0, 0.0, 1.0]
vertices = np.array(vertices, dtype=np.float32)
colors = np.array(colors, dtype=np.float32)
shader = compileProgram(compileShader(
    vertex_src, GL_VERTEX_SHADER), compileShader(fragment_src, GL_FRAGMENT_SHADER))
buff_obj = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, buff_obj)
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)
position = glGetAttribLocation(shader, "a_position")
glEnableVertexAttribArray(position)
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
glUseProgram(shader)
glClearColor(0, 0.1, 0.1, 1)

while not glfw.window_should_close(window):
    glfw.poll_events()
    glfw.swap_buffers(window)

glfw.terminate()

在运行程序时,我得到了这个错误:

Traceback (most recent call last):
  File "opengl.py", line 43, in <module>
    shader = compileProgram(compileShader(
  File "/usr/local/lib/python3.8/dist-packages/OpenGL/GL/shaders.py", line 235, in compileShader
    raise ShaderCompilationError(
OpenGL.GL.shaders.ShaderCompilationError: ("Shader compile failure (0): b'0:2(10): error: GLSL 3.30 is not supported. Supported versions are: 1.10, 1.20, 1.30, 1.00 ES, and 3.00 ES\\n'", [b'\n# version 330 core\nin vec3 a_position;\nvoid main() {\n    gl_position = vec4(a_position, 1.0);\n}\n'], GL_VERTEX_SHADER)

明确表示不支持 GLSL 3.30。但是,通过设置窗口提示,这在 C 中确实有效:

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

如何在 Python 中设置这些窗口提示?

【问题讨论】:

    标签: python opengl glsl glfw pyopengl


    【解决方案1】:

    使用 Python 语法是

    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    

    请注意,您的片段着色器中有一个错字。 GLSL 区分大小写。它必须是gl_Position 而不是gl_position


    在核心配置文件OpenGL Context 中,您必须使用命名Vertex Array Object,因为默认的顶点数组对象 (0) 无效:

    vao = glGenVertexArrays(1) # <----
    glBindVertexArray(vao)     # <----
    
    position = glGetAttribLocation(shader, "a_position")
    glEnableVertexAttribArray(position)
    glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
    

    最后你错过了绘制几何图形。清除帧缓冲区并绘制数组:

    while not glfw.window_should_close(window):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glDrawArrays(GL_TRIANGLES, 0, 3)
        glfw.poll_events()
        glfw.swap_buffers(window)
    

    完整示例:

    import glfw
    from OpenGL.GL import *
    from OpenGL.GL.shaders import compileProgram, compileShader
    import numpy as np
    
    vertex_src = """
    # version 330 core
    in vec3 a_position;
    void main() {
        gl_Position = vec4(a_position, 1.0);
    }
    """
    
    fragment_src = """
    # version 330 core
    out vec4 out_color;
    void main() {
        out_color = vec4(1.0, 0.0, 0.0, 1.0);
    }
    """
    
    if not glfw.init():
        print("Cannot initialize GLFW")
        exit()
    
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
    window = glfw.create_window(320, 240, "OpenGL window", None, None)
    if not window:
        glfw.terminate()
        print("GLFW window cannot be creted")
        exit()
    
    glfw.set_window_pos(window, 100, 100)
    glfw.make_context_current(window)
    
    vertices = [-0.5, -0.5, 0.0,
                0.5, -0.5, 0.0,
                0.0, 0.5, 0.0]
    colors = [1, 0, 0.0, 0.0,
              0.0, 1.0, 0.0,
              0.0, 0.0, 1.0]
    vertices = np.array(vertices, dtype=np.float32)
    colors = np.array(colors, dtype=np.float32)
    shader = compileProgram(compileShader(
        vertex_src, GL_VERTEX_SHADER), compileShader(fragment_src, GL_FRAGMENT_SHADER))
    
    buff_obj = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, buff_obj)
    glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)
    
    vao = glGenVertexArrays(1)
    glBindVertexArray(vao)
    
    position = glGetAttribLocation(shader, "a_position")
    glEnableVertexAttribArray(position)
    glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
    
    glUseProgram(shader)
    glClearColor(0, 0.1, 0.1, 1)
    
    while not glfw.window_should_close(window):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glDrawArrays(GL_TRIANGLES, 0, 3)
        glfw.poll_events()
        glfw.swap_buffers(window)
    
    glfw.terminate()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-02
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2019-06-11
      相关资源
      最近更新 更多