Jump to content
  • Advertisement
Sign in to follow this  
OandO

OpenGL Couple of Issues with VBOs

This topic is 2602 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

So I've finally got enough time on my hands to start properly learning to use OpenGL, but VBOs are throwing me a little bit. I wrote some model and texture loading code for a single object OBJ file and a targa file and got that all to render fine. However it's pretty inefficient, there's a fair bit of duplication which I need to find a way of getting rid of. After getting basic load-and-draw functionality working I started looking into VBOs. Here I'm hitting on another problem: although I'm using the same data and indexing, my normals and texture coords screw up. Both of these problems stem from the fact that I can't quite pin down how I'm supposed to be indexing the data in any usable, efficient way. I get the principles behind it, but I still find OpenGL's handling of buffers a little vague.

Mesh loading and buffer generation, called once when program is started: http://pastebin.com/BEy2RCSc (commented some dodgy stuff I need help with)
Render, called repeatedly: http://pastebin.com/RAvSHNPc (I think this is probably fine, but just in case)

Just to visualise the problem I'm having, this is the mesh loaded the old way, without VBOs:
http://www.youtube.c...h?v=3BFUrZobhg8

And with the alterations for VBOs:
http://img43.imagesh...110603at232.png
http://img849.images...110603at232.png

As you can see, the vertices seem to be fine, but the normals and texture coords are off.

Share this post


Link to post
Share on other sites
Advertisement
The first few lines are already confusing me and the code looks like you seem to believe that a Vector and a Vertex is the same kind of thing. Otherwise it makes zero sense to allocate numNormals*sizeof(Vertex) for your normals.

A vector is a bunch of coordinates, a vertex is a collection of a bunch of attributes like position, normal, texcoord, color, etc... if one of them is different it's not the same vertex. Don't try anything weird like using one normal for multiple vertices. You can do that in immediate mode, but not with buffers (at least not without trickery). If you feel like trying out something new, maybe you can put something together using this:
http://www.opengl.or...nced_arrays.txt

As far as I understand, the index for each attribute stream is divided by a number you can specify yourself, so technically you could reuse a normal or texcoord if you get the order of all attributes right. Trying a thought experiment for cubes (again, assuming I understand the extension correctly):

normals[6] = {n1, n2, n3, n4, n5, n6} //setting a divisor of 4
texcoord[4] = {t1, t2, t3, t4} //setting a divisor of 6
position[6*4] = {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, .... } //setting a divisor of 1, each 4 entries make up one side of the cube
indices[6*4] = { 0,1,2,3, 4,5,6,7, 8,9,10,11, 12,13,14,15, .... }

Now when sending your draw call (as quads for simplicities sake), you would get the following vertices:

n1, t1, p1
n1, t1, p2
n1, t1, p3
n1, t1, p4

n2, t1, p5
n2, t1, p6

So after a bit of fiddling I don't think you can fix the order of the attributes to make it work. If you can find an arrangement that works (or even one that works for generic models) you're a better man than me. Rule of thumb: if you have a sharp edge, you can not share vertices.

Share this post


Link to post
Share on other sites
Alright. OBJ files store the attributes separately, so there's a list with all the coordinates of the vertices, then list of all the texture coordinates etc. which I think is probably what caused my confusion. Thanks for the advice, I'll take a crack at this ASAP.

Share this post


Link to post
Share on other sites
I didn't go through all the code, but these few lines of code are wrong:

memcpy(mesh->vbo_buffer, mesh->vertices, vertex_size);
memcpy(mesh->vbo_buffer + normal_size, mesh->meshNormals, normal_size);
memcpy(mesh->vbo_buffer + normal_size + texturecoord_size, mesh->meshTexCoords, texturecoord_size);

The offset for 2nd line should be vertex_size, and the offset for the 3rd line should be vertex_size + normal_size. When you load the positions (in your code, mesh->vertices) in, the end of that array will be mesh->vertices + vertex_size, which gives you the offset for the 2nd line. And same applies for the 2nd line, to get the offset for the 3rd line.

So the code should be:


memcpy(mesh->vbo_buffer, mesh->vertices, vertex_size);
memcpy(mesh->vbo_buffer + vertex_size, mesh->meshNormals, normal_size);
memcpy(mesh->vbo_buffer + vertex_size + normal_size, mesh->meshTexCoords, texturecoord_size);

Share this post


Link to post
Share on other sites
/facepalm

I was thinking that myself earlier, could have sworn I'd corrected it... Apparently not.

Edit: Well now it draws perfectly, and I feel a little embarrassed.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!