• Create Account

## Performance, VBOs and Objects

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Danicco  Members

449
Like
0Likes
Like

Posted 10 March 2013 - 05:51 PM

Hi,

I was making a simple game in C++ and OpenGL with what I learned from a bunch of pretty old tutorials (which I discovered later), and now I'm in the process of updating everything and I've got some questions about the VBO implementation.

class Vertex
{
public:
float posX, posY, posZ;
};
class Face
{
public:
Vertex** vertexes;
//bunch of functions to get and set the vertexes and their values
};
class Object3D : public Object
{
public:
Face* faces;
Vertex* vertexes;
//bunch of functions to initialize the vertexes and faces
};


So my Object3D had an array of all vertexes informations, each with their X, Y and Z, and another array for Faces, which gave me the addresses of 3 Vertexes that composed 1 Face of the object.

It worked fine since I was calling:

void Game::Draw(Object3D* object)
{
for(int i = 0; i < object->totalFaces; i++)
{
glVertex3f(object->faces[i].GetVertex[0]->posX, object->faces[i].GetVertex[0]->posY, object->faces[i].GetVertex[0]->posZ);
glVertex3f(object->faces[i].GetVertex[1]->posX, object->faces[i].GetVertex[1]->posY, object->faces[i].GetVertex[1]->posZ);
glVertex3f(object->faces[i].GetVertex[2]->posX, object->faces[i].GetVertex[2]->posY, object->faces[i].GetVertex[2]->posZ);
}
}


But now that I'm going to turn everything into VBOs, I need a single array with the sequential float values of each of the object's faces, so it means I'll even have to repeat some vertex's values sometime when they share the same point.

This is where I'm at, I'm trying to figure what would be the best way to do this.

My animation code (unfinished) moved the vertexes according to the bone's movement and weight, and all I had to do was to change the values in my "vertexes[]" array of the Object3D, and I didn't had to worry about it's faces.

Now, I will have to change the new vertex data array I'll be buffering as VBO, and I'd like to get some ideas on how.

I have no experience releasing games, and I never seen a professional/released engine code before, so I've got literally no idea what's the common approach to this.

For now I'm thinking in getting something like this:

class Object3D : public class Object
{
public:
float* vertexData; // = new float[totalVertexes]; - this will be what I'll send/map to the VBO as data
//same as before, vertex and faces arrays
};


And everytime there's a change in the model, I'll change the vertexes array as usual, and then update the values in "vertexData" and finally update the VBO.

This seems a bad idea as I'll be doing this for every animated object in the screen... and I'd be using twice as much memory for every model since I have two variables with the same values.

Isn't there a way to send the VBO data in a different manner? If it searched by index, I think I could create a class and override the brackets operator to send the address that I want, but since it's function asks for the size, I'm guessing it moves by memory size...

Is there any good approaches to this, or what's the best way to do this? (I'd like to totally scrap the vertexes array and only work with the vertexData I've sent/mapped to the VBO)

### #2TheChubu  Members

8945
Like
1Likes
Like

Posted 10 March 2013 - 06:16 PM

You can do what you just said. Just repeat the vertices that are shared among several faces. It will work.

Or you could look up indexed drawing, which is more or less what your face system did. You have an array of all the (non repeated) vertices and an array of indices. The array of indices is what describes how to draw your mesh. Each triangle will require 3 indices, and those indices are going to be used by your GPU for fetching the vertex data in your vertex array.

Say that several faces use the 3rd vertex in your vertex array. Instead of repeating 3 times the same vertex, you just use the index array to point to the 3rd vertex for each of the faces that use it.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

### #3Danicco  Members

449
Like
0Likes
Like

Posted 17 March 2013 - 12:09 PM

Thanks, that was exactly what I was looking for!

I got one question about it now that I've implemented most of it: Can you use only one VBO ID for all of the objects' data?

In an older topic here in GameDev there was a post saying that you can use the VBO ID to store different data, though I think the example might be wrong (http://www.gamedev.net/topic/178256-vbo-texture-coords/).

I'm thinking if something like this is possible:

glGenBuffers(1, &object->ObjectID);
glBindBuffer(GL_ARRAY_BUFFER, object->ObjectID);
glBufferData(GL_ARRAY_BUFFER, object->GetVertexSize(), object->GetVertexData(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, object->GetIndexSize(), object->GetIndexData(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, object->GetTextureSize(), object->GetTextureData(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, object->GetNormalSize(), object->GetNormalData(), GL_STATIC_DRAW);

//And then for drawing
glBindBufferARB(GL_ARRAY_BUFFER_ARB, object->ObjectID);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, 0); //0 because it was the first data uploaded
glTexCoordPointer(2, GL_FLOAT, 0, object->GetIndexSize() + object->GetVertexSize()); //begin after both the index/vertex bytes' size
glNormalPointer(GL_FLOAT, 0, object->GetIndexSize() + object->GetVertexSize() + object->GetNormalSize()); //begin after all 3 datas


Is it better than creating 4 VBOs?

I can't test it since I'm still trying to figure how to fill my Texture Coords from a FBX file, any help in how do I decipher those values are also appreciated!

### #4Brother Bob  Moderators

10102
Like
0Likes
Like

Posted 17 March 2013 - 12:59 PM

You can use a single buffer object, yes, but you have to load it at different places and not keep replacing the attributes. Your current code creates a buffer object for the vertex data, and then replaces the entire buffer with the index data, and then replaces the index buffer with the texture data, and so on. You're left with a buffer containing the normal data only.

You need to allocate a buffer large enough to contain all the data, and then load parts of the buffer with the corresponding data.

glGenBuffers(1, &object->ObjectID);
glBindBuffer(GL_ARRAY_BUFFER, object->ObjectID);
glBufferData(GL_ARRAY_BUFFER, object->GetVertexSize()+object->GetTextureSize()+object->GetNormalSize(), 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, object->GetVertexSize(), object->GetVertexData());
glBufferSubData(GL_ARRAY_BUFFER, object->GetVertexSize(), object->GetNormalSize(), object->GetNormalData());
glBufferSubData(GL_ARRAY_BUFFER, object->GetVertexSize()+object->GetNormalSize(), object->GetTextureSize(), object->GetTextureData());


Also, if your index pointer is the vertex index data, then you must put it in a GL_ELEMENT_ARRAY_BUFFER object.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.