如何在Python中为GLFW设置窗口提示

Pus*_*pam 2 python opengl glsl pyopengl glfw

我编写了以下 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()
Run Code Online (Sandbox Code Playgroud)

运行程序时,我收到此错误:

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)
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

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

Rab*_*d76 5

使用 Python 语法是

glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
Run Code Online (Sandbox Code Playgroud)

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


在核心配置文件 OpenGL 上下文中,您必须使用命名的顶点数组对象,因为默认的顶点数组对象 (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))
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

完整示例:

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()
Run Code Online (Sandbox Code Playgroud)