我能够渲染一个带有顶点的 VBO,但是当我尝试添加颜色(使用着色器属性)时,什么也没有出现。我在这里做错了什么?我可以找到很多在线示例显示这适用于交错的 VBO,但我宁愿使用这种方法(序列化数组)。有任何想法吗?
缓冲区生成和分配
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
glGenBuffers( 1, &vbo );
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, ( vertices.size() + colors.size() ) * sizeof(float), NULL, GL_STATIC_DRAW );
glBufferSubData( GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(vertices[0]), vertices.data() );
glBufferSubData( GL_ARRAY_BUFFER, vertices.size() * sizeof(vertices[0]), colors.size() * sizeof(colors[0]), colors.data() );
/* HERE if I replace the lower block with this top portion, things show up fine
glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 3, GL_FLOAT, 0, (void*)0);
*/
glEnableVertexAttribArray( 0 );
glEnableVertexAttribArray( 1 );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, 0 );
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0, (void*)(vertices.size() * sizeof(GLfloat)) );
glGenBuffers( 1, &ibo );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ibo );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, faces.size() * sizeof(GLushort), faces.data(), GL_STATIC_DRAW );
在这里画画
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClearDepth( 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glUseProgram( gl );
glBindVertexArray( vao );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ibo );
glDrawElements( GL_TRIANGLE_FAN, faces.size(), GL_UNSIGNED_SHORT, (void*)0 );
glutSwapBuffers();
glutPostRedisplay();
垂直着色器
#version 130
layout (location = 0 ) in vec3 in_Position;
layout (location = 1 ) in vec3 in_Color;
out vec3 out_Color;
void main()
{
out_Color = in_Color;
gl_Position = vec4(in_Position,1.0);
}
碎片着色器
#version 130
in vec3 out_Color;
void main() {
gl_FragColor = vec4(out_Color, 1.0);
}