以下代码不起作用:
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)
但是,如果我按如下方式修改代码,那么它确实有效:
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)
请注意,我只是移动了标有感叹号的两行。
这种行为的原因是什么?我认为如果我使用glVertexAttribPointer
将数组缓冲区与顶点属性相关联,那么该关联将成为程序状态的一部分并保持有效直到它被显式更改?