Search code examples
pythonpython-3.xshaderpyopengl

What does the syntax [] mean in this case?


I'm learning how to use shader in pyopengl through a sample project on GitHubGist. I came across "[]" syntax and I don't know what it means in this case.

I only know one use of "[]" which is indexing an array.

    def initShader(self, vertex_shader_source, fragment_shader_source):
        # create program
        self.program=glCreateProgram()
        print('create program')
        printOpenGLError()

        # vertex shader
        print('compile vertex shader...')
        self.vs = glCreateShader(GL_VERTEX_SHADER)
        glShaderSource(self.vs, [vertex_shader_source])
        glCompileShader(self.vs)
        glAttachShader(self.program, self.vs)
        printOpenGLError()

I don't understand the "[]" syntax used in glShaderSource()


Solution

  • The function glShaderSource accepts a list of code snippets.
    See PyOpenGL documentation of glShaderSource:

    glShaderSource( GLhandle(shaderObj),[bytes(string),...]) -> None
    

    [vertex_shader_source] is the list of strings, where each string contains glsl source code ([] generates a List).
    In this case the list contains only one element. The elements of the list are concatenated and compiled.

    See also the C specification of glShaderSource