The following code does not work:
self._program1 = glCreateProgram()
... attach shaders, link ...
glUseProgram(self._program1)
self._fooLoc = glGetAttribLocation(self._program1, 'foo')
glBindBuffer(GL_ARRAY_BUFFER, self._fooBuffer) #!!!!!
glVertexAttribPointer(self._fooLoc, 4, GL_FLOAT, False, 0, None) #!!!!!
glEnableVertexAttribArray(self._fooLoc)
glUseProgram(0)
... lots of stuff, but self._program1 is never used ...
glUseProgram(self._program1)
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)
glUseProgram(0)
However, if I modify the code as follows, then it does work:
self._program1 = glCreateProgram()
... attach shaders, link ...
glUseProgram(self._program1)
self._fooLoc = glGetAttribLocation(self._program1, 'foo')
glEnableVertexAttribArray(self._fooLoc)
glUseProgram(0)
... lots of stuff, but self._program1 is never used ...
glUseProgram(self._program1)
glBindBuffer(GL_ARRAY_BUFFER, self._fooBuffer) #!!!!!
glVertexAttribPointer(self._fooLoc, 4, GL_FLOAT, False, 0, None) #!!!!!
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)
glUseProgram(0)
Note that I have simply moved the two lines marked with exclamation marks.
What could the reason for this behavior be? I thought that if I associate an array buffer with a vertex attribute using glVertexAttribPointer, then that association becomes part of the state of the program and remains in force until it is changed explicitly?
Vertex Attribute and Uniform bindings are not associated with program in particular. The Attribute or Uniform location is just a number, and if a number of programs had identical location setups (as it can be explicitly done with later versions of GLSL), then the same attribute and uniform bindings can be reused with several programs. It also means, that glUseProgram does not restore bindings, that may have been altered somewhere else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With