glCreateShader 在不相关的更改后停止工作

glCreateShader stoped workig after irrelevant change

当我运行一个pyopengl程序时,我得到一个错误。 我在网上搜索了一下,但它只说这是一个 pyopengl 版本问题,但我正在使用最新的更新。

Traceback (most recent call last): File "C:/Users/TheUser/Desktop/MyPytonDen/ThinMatrixOpenGl/engineTester/MainGameLoop.py", line 10, in from ThinMatrixOpenGl.renderEngine.MasterRenderer import MasterRendererClass File "C:\Users\TheUser\Desktop\MyPytonDen\ThinMatrixOpenGl\renderEngine\MasterRenderer.py", line 10, in class MasterRendererClass: File "C:\Users\TheUser\Desktop\MyPytonDen\ThinMatrixOpenGl\renderEngine\MasterRenderer.py", line 11, in MasterRendererClass shader = StaticShaderClass() File "C:\Users\TheUser\Desktop\MyPytonDen\ThinMatrixOpenGl\shaders\staticShader.py", line 22, in init super().init(self.VERTEX_FILE, self.FRAGMENT_FILE) File "C:\Users\TheUser\Desktop\MyPytonDen\ThinMatrixOpenGl\shaders\shaderProgram.py", line 13, in init self.Vertex_Shader_Id = Load_Shader(vertex_file, GL_VERTEX_SHADER) File "C:\Users\TheUser\Desktop\MyPytonDen\ThinMatrixOpenGl\shaders\shaderProgram.py", line 84, in Load_Shader Shader_Id = glCreateShader(type_of_shader) File "C:\Users\TheUser\AppData\Local\Programs\Python\Python38-32\lib\site-packages\OpenGL\platform\baseplatform.py", line 423, in call raise error.NullFunctionError( OpenGL.error.NullFunctionError: Attempt to call an undefined function glCreateShader, check for bool(glCreateShader) before calling Process finished with exit code 1

我检查了 OpenGL 源代码。并不是说我一开始就干涉它,但它很好。 出于某种原因,StaticShader 现在拒绝初始化。 在我的程序中,在进行一些更改之前,它工作得很好并且它仍在其他一些项目中工作。 尽管我什至没有接近着色器代码,但它给了我这个。 这到底是什么,我该如何处理。

顺便说一句,当它弹出时,我正在尝试更新渲染算法,尽管它并没有太大变化。

class StaticShaderClass(ShaderProgramClass):
    VERTEX_FILE = "../shaders/vertexShader.txt"
    FRAGMENT_FILE = "../shaders/fragmentShader.txt"
    location_transformation_matrix: int
    location_projection_matrix: int
    location_view_matrix: int
    location_light_position: int
    location_light_color: int
    location_shine_damper: int
    location_reflectivity: int

    def __init__(self):
        super().__init__(self.VERTEX_FILE, self.FRAGMENT_FILE)

    def Bind_Attributes(self):
        super().Bind_Attribute(0, "position")
        super().Bind_Attribute(1, "texture_coord")
        super().Bind_Attribute(2, "normal")

    def GetAllUniformLocation(self):
        self.location_transformation_matrix = super().GetUniformLocation("transformation_matrix")
        self.location_projection_matrix = super().GetUniformLocation("projection_matrix")
        self.location_view_matrix = super().GetUniformLocation("view_matrix")
        self.location_light_position = super().GetUniformLocation("light_position")
        self.location_light_color = super().GetUniformLocation("light_color")
        self.location_shine_damper = super().GetUniformLocation("shine_damper")
        self.location_reflectivity = super().GetUniformLocation("reflectivity")

    def Load_Shine_Variables(self, damper, reflectivity):
        Load_Float(self.location_shine_damper, damper)
        Load_Float(self.location_reflectivity, reflectivity)

    def Load_Transformation_Matrix(self, matrix: Matrix44):
        super().Load_Matrix(self.location_transformation_matrix, matrix)

    def Load_Projection_Matrix(self, projection: Matrix44):
        super().Load_Matrix(self.location_projection_matrix, projection)

    def Load_view_Matrix(self, camera: CameraClass):
        view_matrix = Maths.Create_view_Matrix(camera)
        super().Load_Matrix(self.location_view_matrix, view_matrix)

    def Load_Light(self, light: Light):
        Load_Vector(self.location_light_position, light.position)
        Load_Vector(self.location_light_color, light.color)
class ShaderProgramClass(ABC):
    Program_Id: int
    Vertex_Shader_Id: int
    Fragment_Shader_Id: int

    def __init__(self, vertex_file: str, fragment_file: str):
        self.Vertex_Shader_Id = Load_Shader(vertex_file, GL_VERTEX_SHADER)
        self.Fragment_Shader_Id = Load_Shader(fragment_file, GL_FRAGMENT_SHADER)
        self.Program_Id = glCreateProgram()
        glAttachShader(self.Program_Id, self.Vertex_Shader_Id)
        glAttachShader(self.Program_Id, self.Fragment_Shader_Id)
        self.Bind_Attributes()
        glLinkProgram(self.Program_Id)
        # glGetProgramInfoLog(self.Program_Id)
        glValidateProgram(self.Program_Id)
        self.GetAllUniformLocation()

    def Start(self):
        glUseProgram(self.Program_Id)

    def Clean_up(self):
        self.Stop()
        glDetachShader(self.Program_Id, self.Vertex_Shader_Id)
        glDetachShader(self.Program_Id, self.Fragment_Shader_Id)
        glDeleteShader(self.Vertex_Shader_Id)
        glDeleteShader(self.Fragment_Shader_Id)
        glDeleteProgram(self.Program_Id)

    @abstractmethod
    def Bind_Attributes(self):
        pass

    def Bind_Attribute(self, attribute: int, variable_name: str):
        glBindAttribLocation(self.Program_Id, attribute, variable_name)

    @staticmethod
    def Stop():
        glUseProgram(0)

    @abstractmethod
    def GetAllUniformLocation(self):
        pass

    def GetUniformLocation(self, uniform_name: str):
        return glGetUniformLocation(self.Program_Id, uniform_name)

    @staticmethod
    def Load_Matrix(location, matrix):
        matrix = np.array(matrix, dtype=np.float32)
        # it may require matrix s data type to change float later
        glUniformMatrix4fv(location, 1, False, matrix)


def Load_Float(location: int, value: float):
    glUniform1f(location, value)


def Load_Vector(location: int, vector: Vector3):
    glUniform3f(location, vector.x, vector.y, vector.z)


def Load_Boolean(location: int, value: bool):
    to_load = 0
    if value:
        to_load = 1
    glUniform1f(location, to_load)


def Load_Shader(file: str, type_of_shader: int):
    try:
        src = ""
        with open(file, "r") as f:
            text = f.readlines()
        for i in text:
            src += str(i)
    except ():
        raise Exception(FileNotFoundError, "file is not exist or could not be readied for some reason")
    Shader_Id = glCreateShader(type_of_shader)
    print(Shader_Id)
    glShaderSource(Shader_Id, src)
    glCompileShader(Shader_Id)
    if glGetShaderiv(Shader_Id, GL_COMPILE_STATUS) == GL_FALSE:
        print(glGetShaderInfoLog(Shader_Id))
        print("could not compile shader!")
    return Shader_Id
#version 400 core

in vec3 position;
in vec2 texture_coord;
in vec3 normal;

out vec2 pass_texture_coord;
out vec3 surface_normal;
out vec3 to_light_vector;
out vec3 to_camera_vector;

uniform mat4 transformation_matrix;
uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform vec3 light_position;

void main(){
    vec4 world_position = transformation_matrix * vec4(position, 1.0f);
    gl_Position = projection_matrix * view_matrix * world_position;
    pass_texture_coord = texture_coord;
    surface_normal = (transformation_matrix * vec4(normal,0.0)).xyz;
    to_light_vector = light_position - world_position.xyz;
    to_camera_vector = (inverse(view_matrix) * vec4(0.0,0.0,0.0,1.0)).xyz - world_position.xyz;
}

让我引用 Python class attributes are evaluated on declaration:

In Python, class attributes are evaluated and put into memory when the class is defined (or imported).

在创建 OpenGL window 和上下文之前定义或导入了有效且当前的 OpenGL context is required for each OpenGL instruction, such as for creating the shader program. Therefore, if the shader program is stored in a class attribute 和 class,无法生成着色器程序。