代码之家  ›  专栏  ›  技术社区  ›  bin4ry

GLError 1281 glGetUniformLocation中的值无效

  •  0
  • bin4ry  · 技术社区  · 2 年前

    我写了一个着色器类,由于某种原因,我得到了一个错误:

    OpenGL.error.GLError: GLError(
        err = 1281,
        description = b'invalid value',
        baseOperation = glGetUniformLocation,
        cArguments = (0, b'projection\x00'),
        result = -1
    )
    

    这是我的代码:

    # shader.py
    
    from rengine.debugging import Debug
    from rengine.math import REMath
    from rengine.gfx.gl import *
    
    class Shader():
        def __init__(self, vertex_src:str = None, fragment_src:str = None,
                           vertex_path:str = None, fragment_path:str = None):
            self.__uniform_locations = {}
            if vertex_src and fragment_src:
                self.load_from_src(vertex_src, fragment_src)
            elif vertex_path and fragment_path:
                self.load_from_file(vertex_path, fragment_path)
            else: self.__program_id = 0
    
        def use(self):
            glUseProgram(self.__program_id)
        
        def unbind(self):
            glUseProgram(0)
    
        def destroy(self):
            glDeleteShader(self.__vertex_shader)
            glDeleteShader(self.__fragment_shader)
    
        def set_uniform_location(self, uniform_name:str):
            if uniform_name not in self.__uniform_locations:
                self.__uniform_locations[uniform_name] = glGetUniformLocation(self.__program_id, uniform_name)
                # OpenGL.error.GLError: GLError(
                #     err = 1281,
                #     description = b'invalid value',
                #     baseOperation = glGetUniformLocation,
                #     cArguments = (0, b'projection\x00'),
                #     result = -1
                # )
    
    
        def set_uniform_mat4(self, uniform_name:str, matrix:REMath.mat4):
            self.set_uniform_location(uniform_name)
            glUniformMatrix4fv(self.__uniform_locations[uniform_name], 1, GL_FALSE, REMath.value_ptr(matrix))
    
        def set_uniform_float(self, uniform_name:str, value:float):
            self.set_uniform_location(uniform_name)
            glUniform1f(self.__uniform_locations[uniform_name], value)
    
        def set_uniform_int(self, uniform_name:str, value:int):
            self.set_uniform_location(uniform_name)
            glUniform1i(self.__uniform_locations[uniform_name], value)
    
        def set_uniform_vec2(self, uniform_name:str, vector:REMath.vec2):
            self.set_uniform_location(uniform_name)
            glUniform2fv(self.__uniform_locations[uniform_name], 1, REMath.value_ptr(vector))
    
        def set_uniform_vec3(self, uniform_name:str, vector:REMath.vec3):
            self.set_uniform_location(uniform_name)
            glUniform3fv(self.__uniform_locations[uniform_name], 1, REMath.value_ptr(vector))
    
        def set_uniform_vec4(self, uniform_name:str, vector:REMath.vec4):
            self.set_uniform_location(uniform_name)
            glUniform4fv(self.__uniform_locations[uniform_name], 1, REMath.value_ptr(vector))
    
        def compile_shader(self, source:str, shader_type:GL_SHADER_TYPE):
            shader_id = glCreateShader(shader_type)
            glShaderSource(shader_id, source)
            glCompileShader(shader_id)
    
            if not glGetShaderiv(shader_id, GL_COMPILE_STATUS):
                error = glGetShaderInfoLog(shader_id).decode()
                Debug.print_err(f"Shader Compilation Failed: {error}")
                glDeleteShader(shader_id)
                return
            
            Debug.print_succ(f"Shader Compiled Successfully: {shader_id}")
    
            return shader_id
        
        def compile_program(self, vertex_shader:GL_SHADER, fragment_shader:GL_SHADER):
            program_id = glCreateProgram()
            glAttachShader(program_id, vertex_shader)
            glAttachShader(program_id, fragment_shader)
            glLinkProgram(program_id)
            return program_id
        
        def load_from_src(self, vertex_src:str, fragment_src:str):
            self.__vertex_shader = self.compile_shader(vertex_src, GL_VERTEX_SHADER)
            self.__fragment_shader = self.compile_shader(fragment_src, GL_FRAGMENT_SHADER)
            self.__program_id = self.compile_program(self.__vertex_shader, self.__fragment_shader)
        
        def load_from_file(self, vertex_path:str, fragment_path:str):
            with open(vertex_path, "r") as vf:
                self.__vertex_shader = self.compile_shader(vf.read(), GL_VERTEX_SHADER)
            with open(fragment_path, "r") as ff:
                self.__fragment_shader = self.compile_shader(ff.read(), GL_FRAGMENT_SHADER)
            self.__program_id = self.compile_program(self.__vertex_shader, self.__fragment_shader)
    

    这是我的着色器: 顶点着色器:

    #version 330 core
    
    layout (location = 0) in vec3 aPos;
    layout (location = 1) in vec3 aNormal;
    layout (location = 2) in vec2 aTexCoord;
    
    uniform mat4 model;
    uniform mat4 view;
    uniform mat4 projection;
    
    void main()
    {
        gl_Position = projection * view * model * vec4(aPos, 1.0);
    }
    

    片段着色器:

    #version 330 core
    
    out vec4 fragColor;
    
    void main()
    {
        fragColor = vec4(1.0, 1.0, 1.0, 1.0);
    }
    

    我试着调试代码,注意到当我创建着色器时,它们具有相同的id(0),我认为这就是问题所在,但我不知道为什么会发生这种情况

    看起来你的帖子大多是代码;请添加更多详细信息。 看起来你的帖子大多是代码;请添加更多详细信息。 看起来你的帖子大多是代码;请添加更多详细信息。 看起来你的帖子大多是代码;请添加更多详细信息。 看起来你的帖子大多是代码;请添加更多详细信息。 看起来你的帖子大多是代码;请添加更多详细信息。 看起来你的帖子大多是代码;请添加更多详细信息。 看起来你的帖子大多是代码;请添加更多详细信息。 看起来你的帖子大多是代码;请添加更多详细信息。 看起来你的帖子大多是代码;请添加更多详细信息。

    0 回复  |  直到 2 年前
        1
  •  0
  •   bin4ry    2 年前

    错误在于我在创建OpenGL上下文之前创建了着色器