OpenGLES 如何知道数组引用的是哪个属性
我正在看苹果如何使用顶点数组的示例:
typedef struct _vertexStruct
{
GLfloat position[2];
GLubyte color[4];
} vertexStruct;
enum {
ATTRIB_POSITION,
ATTRIB_COLOR,
NUM_ATTRIBUTES };
void DrawModel()
{
const vertexStruct vertices[] = {...};
const GLubyte indices[] = {...};
glVertexAttribPointer(ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE,
sizeof(vertexStruct), &vertices[0].position);
glEnableVertexAttribArray(ATTRIB_POSITION);
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE,
sizeof(vertexStruct), &vertices[0].color);
glEnableVertexAttribArray(ATTRIB_COLOR);
glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices)/sizeof(GLubyte),
GL_UNSIGNED_BYTE, indices);
}
(source)
OpenGL如何知道哪一个是颜色,哪一个是顶点? ATTRIB_POSITION
和 ATTRIB_COLOR
是用户定义的,所以 opengl 不应该知道它的含义。具体来说,我尝试使用恒定的颜色和顶点/纹理数组。如果我将 ATTRIB_COLOR
更改为 ATTRIB_TEXTURE
OpenGL 不会注意到任何事情,我该怎么做?
I'm looking at an example from apple how to use vertex arrays:
typedef struct _vertexStruct
{
GLfloat position[2];
GLubyte color[4];
} vertexStruct;
enum {
ATTRIB_POSITION,
ATTRIB_COLOR,
NUM_ATTRIBUTES };
void DrawModel()
{
const vertexStruct vertices[] = {...};
const GLubyte indices[] = {...};
glVertexAttribPointer(ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE,
sizeof(vertexStruct), &vertices[0].position);
glEnableVertexAttribArray(ATTRIB_POSITION);
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE,
sizeof(vertexStruct), &vertices[0].color);
glEnableVertexAttribArray(ATTRIB_COLOR);
glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices)/sizeof(GLubyte),
GL_UNSIGNED_BYTE, indices);
}
(source)
How does OpenGL know which one is color and which one is vertex? ATTRIB_POSITION
and ATTRIB_COLOR
are user defined, so opengl shouldn't know what it means. Specifically I try to use constant color and vertex/texture arrays. If I'll change ATTRIB_COLOR
to ATTRIB_TEXTURE
OpenGL won't notice a thing, how can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
注意:以下假设OpenGL ES 2.0。
OpenGL 既不知道也不关心任何特定顶点属性背后的含义是什么。它想要的只是一个数字:一个属性索引。
您的顶点着色器使用
attribute
关键字定义了许多作为输入的属性。您的工作是在链接程序之前使用glBindAttribLocation
将这些 GLSL 属性连接到 OpenGL 属性索引。因此,如果将着色器位置属性绑定到属性 0,则属性 0 只是位置。Note: the following assumes OpenGL ES 2.0.
OpenGL neither knows nor cares about what the meaning behind any particular vertex attribute is. All it wants is a number: an attribute index.
Your vertex shader defines a number of attributes that it takes as inputs, using the
attribute
keyword. It is your job to connect these GLSL attributes to OpenGL attribute indices, usingglBindAttribLocation
before linking the program. So attribute 0 is only the position if you bind the shader position attribute to attribute 0.