Indexed Drawing in OpenGL not working
Posted
by
user2050846
on Game Development
See other posts from Game Development
or by user2050846
Published on 2014-07-25T06:52:12Z
Indexed on
2014/08/24
16:30 UTC
Read the original article
Hit count: 253
opengl
I am trying to render 2 types of primitives- - points ( a Point Cloud ) - triangles ( a Mesh )
I am rendering points simply without any index arrays and they are getting rendered fine. To render the meshes I am using indexed drawing with the face list array having the indices of the vertices to be rendered as Triangles. Vertices and their corresponding vertex colors are stored in their corresponding buffers. But the indexed drawing command do not draw anything.
The code is as follows-
Main Display Function:
void display()
{
simple->enable();
simple->bindUniform("MV",modelview);
simple->bindUniform("P", projection);
// rendering Point Cloud
glBindVertexArray(vao);
// Vertex buffer Point Cloud
glBindBuffer(GL_ARRAY_BUFFER,vertexbuffer);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
// Color Buffer point Cloud
glBindBuffer(GL_ARRAY_BUFFER,colorbuffer);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,0);
// Render Colored Point Cloud
//glDrawArrays(GL_POINTS,0,model->vertexCount);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
// ---------------- END---------------------//
//// Floor Rendering
glBindBuffer(GL_ARRAY_BUFFER,fl);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glVertexAttribPointer(1,4,GL_FLOAT,GL_FALSE,0,(void *)48);
glDrawArrays(GL_QUADS,0,4);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
// -----------------END---------------------//
//Rendering the Meshes
//////////// PART OF CODE THAT IS NOT DRAWING ANYTHING ////////////////////
glBindVertexArray(vid);
for(int i=0;i<NUM_MESHES;i++)
{
glBindBuffer(GL_ARRAY_BUFFER,mVertex[i]);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,(void *)(meshes[i]->vertexCount*sizeof(glm::vec3)));
//glDrawArrays(GL_TRIANGLES,0,meshes[i]->vertexCount);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,mFace[i]);
//cout<<gluErrorString(glGetError());
glDrawElements(GL_TRIANGLES,meshes[i]->faceCount*3,GL_FLOAT,(void *)0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
glUseProgram(0);
glutSwapBuffers();
glutPostRedisplay();
}
Point Cloud Buffer Allocation Initialization:
void initGLPointCloud()
{
glGenBuffers(1,&vertexbuffer);
glGenBuffers(1,&colorbuffer);
glGenBuffers(1,&fl);
//Populates the position buffer
glBindBuffer(GL_ARRAY_BUFFER,vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, model->vertexCount * sizeof (glm::vec3),
&model->positions[0], GL_STATIC_DRAW);
//Populates the color buffer
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glBufferData(GL_ARRAY_BUFFER, model->vertexCount * sizeof (glm::vec3),
&model->colors[0], GL_STATIC_DRAW);
model->FreeMemory(); // To free the not needed memory, as the data has been already
// copied on graphic card, and wont be used again.
glBindBuffer(GL_ARRAY_BUFFER,0);
}
Meshes Buffer Initialization:
void initGLMeshes(int i)
{
glBindBuffer(GL_ARRAY_BUFFER,mVertex[i]);
glBufferData(GL_ARRAY_BUFFER,meshes[i]->vertexCount*sizeof(glm::vec3)*2,NULL,GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER,0,meshes[i]->vertexCount*sizeof(glm::vec3),&meshes[i]->positions[0]);
glBufferSubData(GL_ARRAY_BUFFER,meshes[i]->vertexCount*sizeof(glm::vec3),meshes[i]->vertexCount*sizeof(glm::vec3),&meshes[i]->colors[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,mFace[i]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,meshes[i]->faceCount*sizeof(glm::vec3),
&meshes[i]->faces[0],GL_STATIC_DRAW);
meshes[i]->FreeMemory();
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
}
Initialize the Rendering, load and create shader and calls the mesh and PCD initializers.
void initRender()
{
simple= new GLSLShader("shaders/simple.vert","shaders/simple.frag");
//Point Cloud
//Sets up VAO
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
initGLPointCloud();
//floorData
glBindBuffer(GL_ARRAY_BUFFER, fl);
glBufferData(GL_ARRAY_BUFFER, sizeof(floorData),
&floorData[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArray(0);
//Meshes
for(int i=0;i<NUM_MESHES;i++)
{
if(i==0) // SET up the new vertex array state for indexed Drawing
{
glGenVertexArrays(1, &vid);
glBindVertexArray(vid);
glGenBuffers(NUM_MESHES,mVertex);
glGenBuffers(NUM_MESHES,mColor);
glGenBuffers(NUM_MESHES,mFace);
}
initGLMeshes(i);
}
glEnable(GL_DEPTH_TEST);
}
Any help would be much appreciated, I have been breaking my head on this problem since 3 days, and still it is unsolved.
© Game Development or respective owner