问题
so i am following a tutorial series on pyglet but i cant make shaders because it causes errors 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'
and the code i am using is 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)
pls help, the tutorial is a bit old. using python 3.7.5 and pyglet version 1.5.11. this is filler text because i dont know what else to say.this is filler text because i dont know what else to say.this is filler text because i dont know what else to say.
回答1:
Found this error also on Github. Apparently you need an environment variable to be set. Here is the solution by 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
Just run one of these commands in the terminal of your choice.
回答2:
The shader code has multiple syntax errors. See the corrected code:
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);
}
"""
# [...]
Furthermore there is a typo. GL_FLOAT
rather than 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)
来源:https://stackoverflow.com/questions/65310516/pyglet-shaders-not-working-keep-erroring