Pyglet 着色器不工作,不断出错

Pyglet shaders not working, keep erroring

所以我正在学习有关 pyglet 的教程系列,但我无法制作着色器,因为它会导致错误 v

Traceback (most recent call last):
  File "/home/awesomenoob/Coding/Python/window_with_shade.py", line 20, in <module>
    window=window1(1280, 720,"tutorial", resizable=True)
  File "/home/awesomenoob/Coding/Python/window_with_shade.py", line 9, in __init__
    self.triangle=Triangle()
  File "/home/awesomenoob/Coding/Python/Triangle.py", line 52, in __init__
    glUseProgram(shader)
  File "/home/awesomenoob/.local/lib/python3.7/site-packages/pyglet/gl/lib.py", line 107, in errcheck
    raise GLException(msg)
pyglet.gl.lib.GLException: b'invalid operation'

我使用的代码是 window_with_shade.py v

from pyglet.gl import *
from Triangle import Triangle
class window1(pyglet.window.Window):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_minimum_size(400,300)
        glClearColor(0.2,0.3,0.2,1.0)

        self.triangle=Triangle()
        
    def on_draw(self):
        self.clear()
        glDrawArrays(GL_TRIANGLES, 0, 3)
        
    def on_resize(self,width,height):
        glViewport(0,0,width,height)


if __name__=="__main__":
    window=window1(1280, 720,"tutorial", resizable=True)
    pyglet.app.run()
    

triangle.py v

from pyglet.gl import *
import ctypes
class Triangle:
    def __init__(self):

        self.triangle = [-0.5, -0.5, 0.0, 1.0, 0.0 ,0.0,
                          0.5, -0.5, 0.0, 0.0, 1.0, 0.0,
                          0.0,  0.5, 0.0, 0.0, 0.0, 1.0]

        self.vertex_shader_source= b"""
        #version 330
        in layout(location = 0) vec3 positions
        in layout(location = 1) vec3 colors

        out vec3 newColor:
        void main()
        (
            gl_Position = vec4(position, 1.0f):
            newColor= color:
        )
        """

        self.fragment_shader_source= b"""
        #version 330
        in vec3 newColor:

        in vec4 outColor:
        void main()
        (
            outColor = vec4(newColor, 1.0f):
        )
        """

        vertex_buff= ctypes.create_string_buffer(self.vertex_shader_source)
        c_vertex=ctypes.cast(ctypes.pointer(ctypes.pointer(vertex_buff)), ctypes.POINTER(ctypes.POINTER(GLchar)))
        vertex_shader= glCreateShader(GL_VERTEX_SHADER)
        glShaderSource(vertex_shader, 1, c_vertex, None)
        glCompileShader(vertex_shader)

        
        fragment_buff= ctypes.create_string_buffer(self.fragment_shader_source)
        c_fragment=ctypes.cast(ctypes.pointer(ctypes.pointer(fragment_buff)), ctypes.POINTER(ctypes.POINTER(GLchar)))
        fragment_shader= glCreateShader(GL_FRAGMENT_SHADER)
        glShaderSource(fragment_shader, 1, c_fragment, None)
        glCompileShader(fragment_shader)

        shader= glCreateProgram()
        glAttachShader(shader, vertex_shader)
        glAttachShader(shader, fragment_shader)
        glLinkProgram(shader)
        
        glUseProgram(shader)

        vbo= GLuint(0)
        glGenBuffers(1, vbo)

        glBindBuffer(GL_ARRAY_BUFFER, vbo)
        glBufferData(GL_ARRAY_BUFFER, 72, (GLfloat * len(self.triangle))(*self.triangle),GL_STATIC_DRAW)

        #positions
        glVertexAttribPointer(0, 3, GLFLOAT, GL_FALSE, 24, ctypes.c_void_p(0))
        glEnableVertexAttribArray(0)
        #colors
        glVertexAttribPointer(1, 3, GLFLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
        glEnableVertexAttribArray(1)

请帮忙,教程有点旧。使用 python 3.7.5 和 pyglet 版本 1.5.11。 这是填充文字,因为我不知道 say.this 还有什么是填充文字,因为我不知道 say.this 还有什么是填充文字,因为我不知道还能说什么。

也在 Github 上发现了这个错误。 显然你需要设置一个环境变量。这是 crramirez 的解决方案:

The right command compatible to most mesa versions is:

export -n LIBGL_ALWAYS_INDIRECT

or

env -u LIBGL_ALWAYS_INDIRECT python3 your-program.py

只需 运行 您选择的终端中的这些命令之一。

着色器代码有多个语法错误。查看更正后的代码:

class Triangle:
    def __init__(self):

        self.triangle = [-0.5, -0.5, 0.0, 1.0, 0.0 ,0.0,
                          0.5, -0.5, 0.0, 0.0, 1.0, 0.0,
                          0.0,  0.5, 0.0, 0.0, 0.0, 1.0]

        self.vertex_shader_source= b"""
        #version 330
        layout(location = 0) in vec3 position;
        layout(location = 1) in vec3 color;

        out vec3 newColor;

        void main()
        {
            gl_Position = vec4(position, 1.0f);
            newColor = color;
        }
        """

        self.fragment_shader_source= b"""
        #version 330
        in vec3 newColor;

        out vec4 outColor;

        void main()
        {
            outColor = vec4(newColor, 1.0f);
        }
        """

        # [...]

还有一个错别字。 GL_FLOAT 而不是 GLFLOAT:

#positions
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0))
glEnableVertexAttribArray(0)
#colors
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
glEnableVertexAttribArray(1)

我发现问题出在 Opengl 版本上