glVertexAttribPointer

glVertexAttribPointer的详细用法,参考 http://docs.gl/gl4/glVertexAttribPointer

这篇文章做一下简单的记录:

先按照我自己的理解,做一下说明:

attribute:  position, UV坐标,Color等属性。
vertex:     一组 attribute

函数声明:

void glVertexAttribPointer(GLuint index,          // typedef unsigned int GLuint;
                GLint size,      // typedef int GLint;
                GLenum type,       // typedef unsigned int GLenum;
                GLboolean normalized, // typedef unsigned char GLboolean;
                GLsizei stride,      // typedef int GLsizei;
                const GLvoid * pointer); // typedef void GLvoid;

index: 对应顶点着色器的 layout(localtion = 0)

size:  attribute 的大小, 例如,GL_BGRA是4。 position(x, y) 是2。

type: 初始值是 GL_FLOAT

normalized: GL_TRUE时保存的数据 int会映射到[-1,1],unsigned int会映射到[0, 1]。(byte好像会映射到[0, 255]) 。 GL_FLOAT时会直接使用float的数据。

stride: 一组vertex的大小(连续的attribute组之间的间隔)

pointer: 这个参数的类型是void*, 要获取数据的偏移(attribute的偏移)。例如 vertex的内容为: position, color,UV. 这个时候如果想获取color, 则需要跳过 position的大小。

相关的程序代码:

// position,          color,       uv坐标
float
data[] = { -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }; unsigned int buffer; glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
//position glEnableVertexAttribArray(
0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 7, 0);

//color
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 7, (GLvoid*) 2 * sizeof(float));

//uv
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 7, (GLvoid*) 5 * sizeof(float));
原文地址:https://www.cnblogs.com/weishuan/p/12927198.html