opengl 当尝试使用glShaderSource()时,“'int'对象不可迭代”?

x759pob2  于 2023-10-18  发布在  其他
关注(0)|答案(2)|浏览(88)

我正在尝试OpenGL(PyOpenGL)3.3和Python 3.9:

vertexShader = glCreateShader(GL_VERTEX_SHADER)
glShaderSource(vertexShader, 1, open("assets\\vertexSrc.glsl", "r").read(), None)   << ERROR HERE
glCompileShader(vertexShader)

得到此错误:

Traceback (most recent call last):
  File "C:\Users\admin\Desktop\OpenGL\Python\Exploring\main.py", line 44, in <module>
    glShaderSource(vertexShader, 1, vertex_src, None)  # Defining the vertex shader source
  File "src/latebind.pyx", line 39, in OpenGL_accelerate.latebind.LateBind.__call__
  File "src/wrapper.pyx", line 299, in OpenGL_accelerate.wrapper.Wrapper.__call__
  File "src/wrapper.pyx", line 161, in OpenGL_accelerate.wrapper.PyArgCalculator.c_call
  File "src/wrapper.pyx", line 128, in OpenGL_accelerate.wrapper.PyArgCalculatorElement.c_call
  File "src/wrapper.pyx", line 122, in OpenGL_accelerate.wrapper.PyArgCalculatorElement.c_call
  File "C:\Users\admin\AppData\Local\Programs\Python\Python39\lib\site-packages\OpenGL\converters.py", line 305, in stringArray
    value = [as_8_bit(x) for x in arg]
TypeError: ("'int' object is not iterable", <bound method StringLengths.stringArray of <OpenGL.converters.StringLengths object at 0x0000015525B19430>>)

Process finished with exit code 1

我试图定义着色器源,但后来得到了这个错误。

7rtdyuoh

7rtdyuoh1#

open("assets\\vertexSrc.glsl", "r").read()返回python字符串。要从python字符串创建着色器,必须为python对象使用重载。查看glShaderSource的PyOpenGL签名:

glShaderSource( GLuint ( shader ) , GLsizei ( count ) , const GLchar **( string ) , const GLint *( length ) )-> void
glShaderSource( shaderObj , string )

变更:
glShaderSource(vertexShader, 1, open("assets\\vertexSrc.glsl", "r").read(), None)

glShaderSource(vertexShader, open("assets\\vertexSrc.glsl", "r").read())
clj7thdc

clj7thdc2#

您需要将着色器源代码作为字符串列表传递。

vertexShader = glCreateShader(GL_VERTEX_SHADER)

# Read the shader source code from the file
with open("assets\\vertexSrc.glsl", "r") as shader_file:
    vertex_src = shader_file.read()

# Convert the source code into a list of strings (lines)
vertex_src_lines = vertex_src.split('\n')

# Pass the list of strings to glShaderSource
glShaderSource(vertexShader, len(vertex_src_lines), vertex_src_lines, None)

glCompileShader(vertexShader)

相关问题