void init_cube(void){
makeCube(cbr_points,cbr_normals);
glGenVertexArrays(1,&VAID_cube);
glBindVertexArray(VAID_cube);
glGenBuffers(1,&VBID_cube);
glBindBuffer(GL_ARRAY_BUFFER,VBID_cube);
glBufferData(GL_ARRAY_BUFFER,sizeof(vec4)*cbr_points.size()+sizeof(vec3)*cbr_normals.size(),0,GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER,0,cbr_points.size()*sizeof(vec4),cbr_points.data());
glBufferSubData(GL_ARRAY_BUFFER,cbr_points.size()*sizeof(vec4),cbr_normals.size()*sizeof(vec3),cbr_normals.data());
GLuint vPosition = glGetAttribLocation(g_IDShader,"vPosition");
glEnableVertexAttribArray(vPosition);
glVertexAttribPointer(vPosition,4,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(0));
GLuint vNormal = glGetAttribLocation(g_IDShader,"vNormal");
glEnableVertexAttribArray(vNormal);
glVertexAttribPointer(vNormal,3,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(cbr_points.size()*sizeof(vec4)));
}
void setVAVBObject( void )
{
// Set VAO
glGenVertexArrays ( 1, &g_VAID );
glBindVertexArray ( g_VAID );
// Set vertex position, normal and elements - VBO
glGenBuffers ( 1, &g_VBIDvertices );
glBindBuffer ( GL_ARRAY_BUFFER, g_VBIDvertices );
glBufferData ( GL_ARRAY_BUFFER, g_bunny.vertices.size() * sizeof(g_bunny.vertices[0]), g_bunny.vertices.data(), GL_STATIC_DRAW );
glGenBuffers ( 1, &g_VBIDnormals );
glBindBuffer ( GL_ARRAY_BUFFER, g_VBIDnormals );
glBufferData ( GL_ARRAY_BUFFER, g_bunny.normals.size() * sizeof(g_bunny.normals[0]), g_bunny.normals.data(), GL_STATIC_DRAW );
// Ex-TODO: Computing normal
// Set up the indices buffer - flat shading does not use indices
glGenBuffers ( 1, &g_IBIDelements );
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, g_IBIDelements );
glBufferData ( GL_ELEMENT_ARRAY_BUFFER,
g_bunny.elements.size() * sizeof(g_bunny.elements[0]),
g_bunny.elements.data(), GL_STATIC_DRAW );
GLuint vPosition = glGetAttribLocation( g_IDShader, "vPosition" );
glEnableVertexAttribArray( vPosition );
glBindBuffer( GL_ARRAY_BUFFER, g_VBIDvertices);
glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );
GLuint vNormal = glGetAttribLocation( g_IDShader, "vNormal" );
glEnableVertexAttribArray( vNormal );
glBindBuffer( GL_ARRAY_BUFFER, g_VBIDnormals);
glVertexAttribPointer( vNormal, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );
}
void displayBunny( void ) {
int size;
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, g_IBIDelements );
glGetBufferParameteriv ( GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size );
glDrawElements ( GL_TRIANGLES, size/sizeof(GLushort), GL_UNSIGNED_SHORT, 0 );
}
void displayCube( void ) {
glBindVertexArray(VAID_cube);
glBindBuffer(GL_ARRAY_BUFFER,VBID_cube);
glDrawArrays(GL_TRIANGLES,0,cbr_points.size());
}
我正在使用obj文件和顶点数组,所以我希望在一个场景中用不同的方法显示。
当我离散地使用这个功能时,它表现得很好,但是当我使用像
时displayBunny();
displayCube();
它出错了。我必须只选择一种方法来显示基元?
答案 0 :(得分:3)
glBindVertexArray(VAID_cube);
displayBunny
中似乎忽略了其中一个。
此外,GL_ELEMENT_ARRAY_BUFFER
是VAO状态的部分。每次渲染对象时都不应该重新绑定它。只需绑定VAO。