想象一下有两个 GLSL 程序 A
和 B
依次调用的场景。
如果设置程序A
的uniform
变量,它的值保持不变,并且在程序的每次绘制调用之前不需要初始化。所以它可以被认为是程序的“成员”(就OOP而言)。
但是属性
值或者当顶点属性设置为指针时怎么样?
最佳答案
您将问题标记为 OpenGL ES 和 WebGL。这两个属性都是全局的。它们的状态未连接到任何 GLSL 程序。
你可以这样想
glState = {
attributes: [
{ enabled: false, type: gl.FLOAT, size: 4, stride:0, offset: 0, buffer, null, },
{ enabled: false, type: gl.FLOAT, size: 4, stride:0, offset: 0, buffer, null, },
{ enabled: false, type: gl.FLOAT, size: 4, stride:0, offset: 0, buffer, null, },
{ enabled: false, type: gl.FLOAT, size: 4, stride:0, offset: 0, buffer, null, },
...
]
};
gl.enableVertexAttribArray
、gl.disableVertexAttribArray
和 gl.vertexAtrribPointer
影响全局属性状态。 More details here .
ES 2.0 和 WebGL 有一个扩展对于顶点数组对象。该扩展允许设置所有属性的组合状态并将其存储在顶点数组对象或“VAO”上。仍然存在全局状态,在很大程度上可以将其视为默认的 VAO。
你可以考虑这样工作
glState = {
defaultVAO = {
attributes: [
{ enabled: false, type: gl.FLOAT, size: 4, stride:0, offset: 0, buffer, null, },
{ enabled: false, type: gl.FLOAT, size: 4, stride:0, offset: 0, buffer, null, },
{ enabled: false, type: gl.FLOAT, size: 4, stride:0, offset: 0, buffer, null, },
{ enabled: false, type: gl.FLOAT, size: 4, stride:0, offset: 0, buffer, null, },
...
]
},
currentVAO = defaultVAO
};
现在,gl.enableVertexAttribArray
、gl.disableVertexAttribArray
和 gl.vertexAtrribPointer
会影响 currentVAO
的属性上面的例子。调用 createVertexArrayOES 会创建一个新的 VAO。调用 bindVertexArrayOES
将上述伪代码中的 currentVAO
设置为新的 VAO。使用 null
调用 bindVertexArrayOES
将 currentVAO
设置回默认值。
在 ES 3.0 中,VAO 始终可用(换句话说,它们不再是扩展,而是基本功能集的一部分。)
注意:VAO 很容易模拟。 There's an emulation library here因此您可以在 WebGL 中的任何位置使用 VAO。如果支持,仿真库将使用 native 库。如果不是,它将效仿它们。
关于opengl-es - OpenGL ES 中顶点属性指针是否持久?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27316605/