opengl的glVertexAttribPointer应该怎么用,其第一个参数索引一直没有搞明白

//VAO[0]
glGenVertexArrays(2, vertex_array);

glBindVertexArray(vertex_array[0]);

//顶点数据
GLuint vbo_vertex = 0;
glGenBuffers(1, &vbo_vertex);
assert(vbo_vertex != 0);

glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*3*8, vertexs, GL_STATIC_DRAW);
//Bind VAO and VBO 索引0一定是顶点
//将顶点数据输入到VAO,用来建立VAO和VBO的数据传递,上传数据完成之后可以删除
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0); //leave out this sentence

//顶点颜色
GLuint vbo_color = 0;
glGenBuffers(1, &vbo_color);
assert(vbo_color);

glBindBuffer(GL_ARRAY_BUFFER, vbo_color);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*24, color, GL_STATIC_DRAW);
glVertexAttribPointer(1,3,GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
最新回答
仰天长啸我也要穿越

2024-07-08 01:07:44

glVertexAttribPointer第一个参数对应vertexshader里哪个in attribute,是vertexshader中attribute的索引
追问
你能给个详细一点的源码例子吗,谢谢啦,发到我邮箱也行,davidsu33@qq.com
追答
//vertex shader
layout(location = 0) in vec3 position; //position的location设定为0

glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*3*8, vertexs, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); //指定position的数据格式,第一个参数0就是在顶点着色器中设定的索引