opengl-es - OpenGL ES 中顶点属性指针是否持久?

标签 opengl-es webgl

想象一下有两个 GLSL 程序 AB 依次调用的场景。 如果设置程序Auniform变量,它的值保持不变,并且在程序的每次绘制调用之前不需要初始化。所以它可以被认为是程序的“成员”(就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.enableVertexAttribArraygl.disableVertexAttribArraygl.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.enableVertexAttribArraygl.disableVertexAttribArraygl.vertexAtrribPointer 会影响 currentVAO 的属性上面的例子。调用 createVertexArrayOES 会创建一个新的 VAO。调用 bindVertexArrayOES 将上述伪代码中的 currentVAO 设置为新的 VAO。使用 null 调用 bindVertexArrayOEScurrentVAO 设置回默认值。

在 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/

相关文章:

objective-c - cocos2d 与 View Controller

opengl - 根据不同的 texcoord 计算 mip 级别选择

opengl-es - 对顶点使用GLshort而不是GLfloat

javascript - WebGL:INVALID_VALUE:attachShader:没有对象或对象被删除。这暗地里有帮助吗?

java - 3D 对象相遇处的不平整边缘和故障

opengl - LibGdx Shader ("no uniform with name ' u_texture' in shader")

javascript - THREE.js 忽略 THREE.Line 中 BufferGeometry + LineBasicMaterial 的线宽?

opengl - 如何使用 WebGL 渲染 2D Sprite?

javascript - webgl 如何绘制多个立方体

javascript - 使用 WebGL 和 glMatrix 转换 3D 对象