gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:24:52 27 4
gpt4 key购买 nike

所以我正在学习关于 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()

三角形.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。
这是填充文字,因为我不知道还能说什么。这是填充文字,因为我不知道还能说些什么。这是填充文字,因为我不知道还能说些什么。

最佳答案

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

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

关于python - Pyglet 着色器不工作,不断出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65310516/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com