PDA

View Full Version : individual mesh buffer doesnt work.



xenophobic
12-24-2016, 02:50 PM
Hello, I'm new to openGL platform.

I'm trying to implement a VBO based program to render multiple meshes, given a common vertex list, and individual index lists for each mesh.

In order to simplify things, I deactivated all other meshes and I want to draw only 1 quad, which consists of 2 big triangles.

in init() function I created vertex, normal and index buffers like this:


vertexData = (GLfloat*)malloc(sizeof(GLfloat)*totalNumVertices* 3);

...
<parse and create buffers named vertexData and normalData>

glGenBuffers(1, &vboVertex); // buffer for both normals and positions.
glGenBuffers(1, &vboNormal);

glBindBuffer(GL_ARRAY_BUFFER, vboVertex); // activate buffer.
glBufferData(GL_ARRAY_BUFFER, totalNumVertices * 3 * sizeof(GLfloat), vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vboNormal);
glBufferData(GL_ARRAY_BUFFER, totalNumVertices * 3 * sizeof(GLfloat), normalData, GL_STATIC_DRAW);


// here, for every Mesh structure I create their own index buffer. Then, in display() I plan to call their own index buffers with global vertex and normal buffers.
for(vector<Mesh>::iterator it_mesh = CurrentScene->_meshes.begin(); it_mesh!=CurrentScene->_meshes.end(); it_mesh++)
{
glGenBuffers(1, &(*it_mesh).buffer_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (*it_mesh).buffer_id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, numberoftriangles*3*sizeof(uint), (*it_mesh).indexBuffer, GL_STATIC_DRAW);
}


This code above is roughly the init function.
In Display() function I tried to render scene like this:


for every mesh, do:
<pushmatrix, set material, set modeling transforms>
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

glBindBuffer(GL_ARRAY_BUFFER, vboVertex); // activate buffer.
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, vboNormal); // activate buffer.
glNormalPointer(GL_FLOAT, 0, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (*it_mesh).buffer_id);

glDrawElements(GL_TRIANGLES, triangle_count, GL_UNSIGNED_INT, 0);
<pop matrix>



However, I can't see any output. The screen is black. What might be the problem?

Thanks in advance.

Dark Photon
12-25-2016, 01:40 PM
You might post a short stand-alone GLUT test program with your full code that folks can read and compile. At first glance, I don't see anything wrong with what you're doing above except that you're passing glDrawElements the number of triangles rather than the number of indices.

xenophobic
12-28-2016, 01:58 PM
You might post a short stand-alone GLUT test program with your full code that folks can read and compile. At first glance, I don't see anything wrong with what you're doing above except that you're passing glDrawElements the number of triangles rather than the number of indices.

yeah, im new to forum and i couldnt figure a proper way to express my problem tbh. that was the best i could for the first try.

I fixed my code, by the way. It turned out that I was doing 2 things wrong.

First thing is what you said. I should've put number of triangles to second parameter rather than number of indices.

The second thing was to create multiple index arrays. Maybe this is possible in the hands of an opengl pro, but for me it didnt work. Instead, I have concatenated all indices from all meshes into one single element array buffer, and I have set the index offset and numberoftriangles for every particular mesh as private member, so that I could apply separate drawing for each mesh.

Hope it helps someone.