OpenGL中的"glEnableVertexAttribArray(GLuint index)"的目的是什么? [英] What is the purpose of `glEnableVertexAttribArray(GLuint index)` in OpenGL?

查看:566
本文介绍了OpenGL中的"glEnableVertexAttribArray(GLuint index)"的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用glVertexAttribPointer(GLuint index, ...)后,默认情况下禁用顶点属性,因为文档

After calling glVertexAttribPointer(GLuint index, ...) the vertex attribute is disabled by default as the docs say

默认情况下,所有客户端功能都被禁用,包括所有通用顶点属性数组.

By default, all client-side capabilities are disabled, including all generic vertex attribute arrays.

为什么我们必须使用附加功能将其启用?有人可以说出一个有用的案例吗?

Why must we enable it using an extra function? Can someone name a case, where this is useful?

研究时,我学到了以下内容:

When researching I learned the following:

  • 通过在GLSL或glBindAttribLocation中使用layout(location = x)限定符,我们可以显式设置位置,而不是让OpenGL生成该位置.但这不是我的问题.

  • By using the layout(location = x) qualifier in GLSL or glBindAttribLocation we can set the location explicitly rather then letting OpenGL generate it. But this is not the point of my question.

glEnableVertexAttribArray不能用于绘制带有多个着色器的VAO.当使用程序对象查询属性位置时,将假定位置是特定于着色器的.那么人们会期望,我们可以在运行正确的着色器之前启用正确的属性位置.但是,我注意到,对此进行测试后发现,一个位置值在不同的着色器中可能会出现多次.此外,输出看起来不正确.如果您想查看代码,请询问.

glEnableVertexAttribArray can not be used to draw one VAO with multiple shaders. As attribute locations are queried using a program object, one would assume that locations are shader-specific; then one would expect, we can enable the right attributes locations before running the right shader. But testing this, I noticed, that one location value can occur more than one time in different shaders; furthermore the output looked wrong. If you wish to see the code, just ask.

属性位置存储在VAO中.

Attribute locations are stored in the VAO.

推荐答案

该设置很有意义.启用和禁用它都有非常有效的用例.

The setting makes complete sense. There are very valid use cases for both having it enabled and disabled.

入口点的名称已经给出了一个强有力的提示,说明为什么会这样.请注意glEnableVertexAttribArray()中的Array部分.此调用不会启用属性".它允许使用来自数组的顶点属性值,即:

The name of the entry point already gives a strong hint why that is. Note the Array part in glEnableVertexAttribArray(). This call does not "enable the attribute". It enables using vertex attribute values from an array, meaning:

  • 如果启用,则对每个顶点使用数组中的单独值.
  • 如果禁用,则该属性的当前值将用于所有顶点.

属性的当前值是通过glVertexAttrib[1234]f()系列的调用来设置的.您希望在下一个绘制调用中为所有顶点使用相同的属性值的用例的典型代码序列是:

The current value of an attribute is set with calls of the glVertexAttrib[1234]f() family. A typical code sequence for the use case where you want to use the same attribute value for all vertices in the next draw call is:

glDisableVertexAttribArray(loc);
glVertexAttrib4f(loc, colR, colG, colB, colA);

与每个顶点从数组中获得自己的属性值的情况相比:

Compared to the case where each vertex gets its own attribute value from an array:

glEnableVertexAttribArray(loc);
glVertexAttribPointer(loc, ...);

现在,从数组中获取属性无疑更为常见.因此,您可能会争辩说,默认值对于现代OpenGL使用是不幸的.但是该设置以及对其进行更改的调用肯定仍然非常有用.

Now, it is certainly much more common to source attributes from an array. So you could argue that the default is unfortunate for modern OpenGL use. But the setting, and the calls to change it, are definitely still very useful.

这篇关于OpenGL中的"glEnableVertexAttribArray(GLuint index)"的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆