opengl 无法在没有glfw上下文的情况下编译着色器

jckbn6z7  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(125)

我正在尝试使用Python绑定编译OpenGL着色器。如果不创建glfw上下文,我无法编译它们,这很奇怪。如果取消glfw行的注解,着色器将被编译。print(bool(glCreateShader))输出False。
错误:

Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\site-packages\OpenGL\latebind.py", line 43, in __call__
    return self._finalCall( *args, **named )
TypeError: 'NoneType' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/reas/Desktop/Programación/OpenGeEle/Moderno.py", line 44, in <module>
    shader = compileShader(vertex_src, GL_VERTEX_SHADER)
  File "C:\Program Files\Python38\lib\site-packages\OpenGL\GL\shaders.py", line 228, in compileShader
    shader = glCreateShader(shaderType)
  File "C:\Program Files\Python38\lib\site-packages\OpenGL\latebind.py", line 46, in __call__
    self._finalCall = self.finalise()
  File "C:\Program Files\Python38\lib\site-packages\OpenGL\extensions.py", line 242, in finalise
    raise error.NullFunctionError(
OpenGL.error.NullFunctionError: Attempt to call an undefined alternate function (glCreateShader, glCreateShaderObjectARB), check for bool(glCreateShader) before calling

Process finished with exit code 1

源代码:

import numpy as np
from numpy import array
from OpenGL.GL import (GL_ARRAY_BUFFER, GL_COLOR_BUFFER_BIT, GL_FRAGMENT_SHADER, GL_STATIC_DRAW,
                       GL_VERTEX_SHADER, glBindBuffer, glBufferData, glClear, glClearColor,
                       glGenBuffers)
from OpenGL.GL.shaders import compileShader, compileProgram
from PyQt5.QtWidgets import QApplication, QMainWindow, QOpenGLWidget

# IF I UNCOMMENT THIS, THE SHADERS WORK
"""
import glfw

glfw.init()
window = glfw.create_window(1280, 720, "My OpenGL window", None, None)
glfw.make_context_current(window)
"""

vertex_src = """
# version 330

in vec3 a_position;
in vec3 a_color;

out vec3 v_color;

void main()
{
    gl_Position = vec4(a_position, 1.0);
    v_color = a_color;
}
"""

fragment_src = """
# version 330

in vec3 v_color;
out vec4 out_color;

void main()
{
    out_color = vec4(v_color, 1.0);
}
"""

shader = compileShader(vertex_src, GL_VERTEX_SHADER)
shader2 = compileShader(fragment_src, GL_FRAGMENT_SHADER)
programa = compileProgram(shader, shader2)
uubf1zoe

uubf1zoe1#

对于任何OpenGL指令,都需要当前有效的OpenGL ContextQOpenGLWidget创建并提供OpenGL上下文,但只有在initializeGLpaintGLresizeGL回调之前,上下文才会变为当前上下文。
因此,必须在initializeGL事件回调方法中移动创建和编译着色器的代码。

相关问题