Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

python - glCreateShader stoped workig after irrelevant change

When I am running a pyopengl program, I get an error. I searhed the web but all it says is that it is a pyopengl version problem, but I am using the latest update.

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:UsersTheUserDesktopMyPytonDenThinMatrixOpenGl enderEngineMasterRenderer.py", line 10, in class MasterRendererClass: File "C:UsersTheUserDesktopMyPytonDenThinMatrixOpenGl enderEngineMasterRenderer.py", line 11, in MasterRendererClass shader = StaticShaderClass() File "C:UsersTheUserDesktopMyPytonDenThinMatrixOpenGlshadersstaticShader.py", line 22, in init super().init(self.VERTEX_FILE, self.FRAGMENT_FILE) File "C:UsersTheUserDesktopMyPytonDenThinMatrixOpenGlshadersshaderProgram.py", line 13, in init self.Vertex_Shader_Id = Load_Shader(vertex_file, GL_VERTEX_SHADER) File "C:UsersTheUserDesktopMyPytonDenThinMatrixOpenGlshadersshaderProgram.py", line 84, in Load_Shader Shader_Id = glCreateShader(type_of_shader) File "C:UsersTheUserAppDataLocalProgramsPythonPython38-32libsite-packagesOpenGLplatformaseplatform.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

I checked the OpenGL source code. Not that I meddle with it in the first place but its fine. For some reason, StaticShader refuses to initialize now. In my program, before doing some change, it was working just fine and it is still working in some other project. Despite I didn't even get close to shader codes it gave me this. What exactly is this and how can I handle it.

Btw while this poped up I was trying to update the render algorithm although it did not change much.

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;
}
question from:https://stackoverflow.com/questions/65859754/glcreateshader-stoped-workig-after-irrelevant-change

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Let me quote Python class attributes are evaluated on declaration:

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

A valid and current 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 and the class is defined or imported before the OpenGL window and context are created, the shader program cannot be generated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...