所以我正在学习pyglet的一系列教程,但我不能制作着色器,因为它会导致错误
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'
我使用的代码是 带遮阳帘的窗户
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()
三角
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)
请帮忙,教程有点旧了。使用Python3.7.5和pyglet版本1.5.11。 这是填充文字,因为我不知道还能说什么。这是填充文字,因为我不知道还能说什么。这是填充文字,因为我不知道还能说什么
我发现问题出在错误的Opengl版本上
着色器代码有多个语法错误。请参阅更正的代码:
此外,还有一个拼写错误}:
GL_FLOAT
而不是{在Github上也发现此错误。 显然,您需要设置一个环境变量。这是 通过crramirez的解决方案:
只需在您选择的终端中运行其中一个命令
相关问题 更多 >
编程相关推荐