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

查看:22
本文介绍了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天全站免登陆