Jump to content
  • Advertisement
Sign in to follow this  
WhiskyJoe

VBO Trouble

This topic is 2490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!