VBO Trouble

Started by
3 comments, last by WhiskyJoe 12 years, 2 months ago
Hey guys,

After searching all night and morning on the web, I didn't seem to find the answer to my problem. So I hope you guys can help me on this.
The problem is that I try to load an .obj file and draw it using VBOs. The problem is.. A simple cube ends up like this:

ilhum1.png

Now this is the front view, not the side.. obviously, this is not correct.

This is the way I try to do it:

Rld::Mesh::Mesh( String a_FileName, String a_Name )
{
glGenBuffers(1, &m_VertexBuffer);
glGenBuffers(1, &m_UVBuffer);
glGenBuffers(1, &m_NormalBuffer);
glGenBuffers(1, &m_IndexBuffer);

m_Name = a_Name;
Load(a_FileName);

glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float3) * m_Vertices.size(), &m_Vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, m_NormalBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float3) * m_Normals.size(), &m_Normals[0], GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, m_UVBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float2) * m_UVs.size(), &m_UVs[0], GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IndexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * m_Indices.size(), &m_Indices[0], GL_STATIC_DRAW);

glBindBuffer( GL_ARRAY_BUFFER, 0 );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
}

void Rld::Mesh::Render()
{
glColor3f(1.0f, 0.0f, 0.0f);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glEnableVertexAttribArray(1);
glBindBuffer( GL_ARRAY_BUFFER, m_NormalBuffer );
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glEnableVertexAttribArray(2);
glBindBuffer( GL_ARRAY_BUFFER, m_UVBuffer );
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_IndexBuffer );
glDrawElements( GL_TRIANGLES, m_Indices.size(), GL_UNSIGNED_INT, 0 );
//glDrawArrays( GL_TRIANGLES, 0, m_Vertices.size());

glDisableVertexAttribArray(2);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
glBindBuffer( GL_ARRAY_BUFFER, 0 );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
}


From what I know, this should work. I also already checked the data and everything (vertex and index data) seems to be alright.
Any pointers in the good direction would be much appreciated smile.png

Edit:
Perhaps handy to mention that m_***Buffer are unsigned ints, the containers that hold the data are std::vector containers and float2/float3 hold nothing more than 2/3 float (x, y and x, y, z)
Advertisement
You are setting up all the streams at 0
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);


I suggest using 1 and 2 as well
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);

and also, since we are talking obj, then I'm sure you are making the same mistake as everyone else.
So I'm going to throw some links at you
http://www.opengl.org/wiki/FAQ#Multi_indexed_rendering
http://www.opengl.org/wiki/Vertex_Buffer_Objects
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
I actually forgot that you can bind multiple VBO's at once. Typically you throw all your data into 1 VBO, so you only have 1 BindBuffer Call, and then 1 call for each Vertex Attribute. You have an extra overhead of about 10 or so calls per object right now. If you start drawing 100 objects, you have 1,000 calls you don't need to have that are wasteful.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal


You are setting up all the streams at 0
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);


I suggest using 1 and 2 as well
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);

and also, since we are talking obj, then I'm sure you are making the same mistake as everyone else.
So I'm going to throw some links at you
http://www.opengl.or...dexed_rendering
http://www.opengl.or..._Buffer_Objects


Thanks for the quick reply. The streams was a mistake I overlooked by trying different things.. Anyway, it looks more like a cube now, but it looks like the last vertex is missing (a cube with one missing corner that goes into the first).

Now I know about the .obj pitfalls and I actually managed to get that done properly (I had the same issue in my software rasterizer) I reduced it down to just the vertices and indices and they seem to be correct when I check them (also in gdebugger). Looks like I'm missing one index. Any idea might cause that? I can make a screenshot if you don't know what I mean.
I found it.. I forgot that the index of obj files start at 1. I did - 1 on the indices and everything worked! Thanks for the help!

This topic is closed to new replies.

Advertisement