Jump to content
  • Advertisement
Sign in to follow this  
Vincent_M

OpenGL Code Structure for Model Data

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

I've built a few model importers in the past, but I'm stuck on code structure. Should I create one Model class that can load static and dynamic (animated) geometry, and use one vertex format? I could reduce my vertex positions to GL_SHORT vectors and cache static geometry data using VBOs in OpenGL but I'd still have to use floating-point vectors for vertex positions and normals for software skinning.

What I have now is an abstract Model class that doesn't do much but manage materials (one material per mesh), then my two subclasses of my Model class will load and store data for OBJ and MD5 models. The vertex structures are different between the two, and therefore, the collection of mesh data are also different too.


Does this seem about right?

Share this post


Link to post
Share on other sites
Advertisement
Be more modular.

Most people write simple wrappers around OpenGL rather than calling OpenGL functions directly.
One thing they commonly do is to make an index buffer class, and that class will automatically resize to GL_INT if you add an index larger than 0xFFFF. Don’t hardcode GL_SHORT. Use GL_SHORT until it is no longer sufficient.

Next, break your model class into multiple classes.
Firstly, a “model” is a collection of meshes, so you will at least need another class to encapsulate the functionality of a mesh (this is what stores your vertex buffers etc.)

Although you don’t have to follow my system exactly, what I have done is to break the model class into its core parts.
CModel.
CAnimatedModel.
CDrawableModel.

A CModel just holds the actual model data. Bounding boxes, vertices, etc. The vertices can be removed after the graphics data has been compiled.
A CAnimatedModel adds animation functionality. It holds the bind pose and anything else related to animations. It is fairly small because it only holds the animation data. Model instances actually use the animation data.
A CDrawableModel is of course responsible for taking the vertices, putting them into vertex buffers, and providing a Render() function. Logically, it adds whatever functionality is needed to render the model.


This applies to the shared model data.

Next you need to make instances of this shared data.
The instances can also be layered as above, so that the CAnimatedModelInstance can update joint matrices on each tick, etc.
The CDrawableModelInstance class can perform CPU skinning (if that is what you want).


Though you don’t have to follow my system exactly, break things down into logical blocks. What parts of the model are related, or more specifically unrelated to the other parts?
It is foreseeable that you would want a model that has no graphics data. Only collision data. So don’t mix core model data with graphics model data.



Additionally, don’t use only one vertex format. Skinning on the CPU will be more efficient if you store vertices in their own buffer and normals in their own buffer, and pad them to 16 bytes between elements. This allows you to exploit SIMD. And a single vertex format is simply restrictive. Build a format based on what model data exists.


L. Spiro

Share this post


Link to post
Share on other sites
@YogurtEmperor:

Do you see an advantage to having each mesh have its own vertex/index buffers rather than just having one in the model and have the meshes use that via vertex offsets, etc?


I've been recently thinking of a better structure myself and I have something similar to what YogurtEmperor has mentioned but a little simplified IMO. Here are roughly the 5 main classes that I've been thinking of:

Geometry
Contains the model data (vertex streams like position, normal, uv, bone weights) and the vertex/index buffers formed from the available streams. There should be only one of these in memory for each unique model loaded from disk and therefore this data is shared between model instances.

AnimationController
A higher level structure which contains a number of shared Animation resources. It is responsible for updating the animations and doing any blending trickery.

Animation
Contains the key-frame data for one animation. There should be only one of these in memory for each unique animation loaded from disk and therefore this data is shared between animation controllers.

Model
Represents an instance of a model and is basically a container of Mesh objects that makes up a complete model. It can either be static or animated with an AnimationController.

Mesh
Represents part of a model and simply contains the offsets to the vertex/index buffers within the Geometry.

Share this post


Link to post
Share on other sites

@YogurtEmperor:

Do you see an advantage to having each mesh have its own vertex/index buffers rather than just having one in the model and have the meshes use that via vertex offsets, etc?

Having multiple parts in one buffer should theoretically be faster, but I personally prefer the organization and freedom of multiple buffers. Since the different meshes that make up a model can have different vertex attributes, you won’t always be able to make just one buffer for the whole model.
One thing you would want to do though if you plan on CPU skinning is to at least keep the skinned vertices and normals separate.


L. Spiro

Share this post


Link to post
Share on other sites
I've thought about concatenating everything into one vertex buffer and one index buffer, but would that be beneficial? I'm still calling sections of it using glDrawElements() per mesh.

Here's my older setup:
ModelVertex: common vertex format for all models --use to be a total buffer of unsigned chars that held data based on my model's vertex format
ModelMesh: each holds a vertex and index buffer to render a mesh of the model
Model: holds a collection of meshes which will be rendered
Object: renderable instance of a model --holds a pointer to a valid Model with data loaded. Each "Object" holds it's own transformation matrix, can be attached to other matrices, "point" at other objects, and run along a spline among other features. Object also contains a collection of AnimationChannel instances which will process skinned animations
AnimationChannel: holds current playback information such as current playback frame, playback status, and looping flag for a single animation to be blended with another animation
Animation: holds animation keys to translation and scale vectors, and quaternion keys. One only instance is held.

So, I load models up once, then place instances of it in the world using Object. This is the old setup, but now I'm giving static geometry some love by supporting the OBJ format for static objects, giving them a smaller vertex format, and using static VBOs.

Doing so, I now have two subclasses of the Model and Object for OBJ and MD5:
Model: OBJModel and MD5Model
Object: OBJObject and MD5Object

This is my current setup, but I want to switch to the COLLADA format for easier loading. If I switch to this format, I'm thinking of storing all my model data in one Model/Mesh/Vertex class, and use one Object class to load it. If it's a dynamic object, I'll have pointers to animation data, and have an animation flag in the model format. All my skinning is done on the CPU right now since my target's GPU isn't ready for GPU skinning.

[color=#1C2837][size=2]
I agree with that, but my game is targeted for iOS platforms where I've been told that interleaved vertex are much faster. I can cram the data into an interleaved buffer of unsigned chars, then ship it to the static VBO for static models... By the way, when a VBO is created, the vertex arrays can be deleted, right?

Share this post


Link to post
Share on other sites
The way I handle data is that anything static is static, and anything animated is dynamic. This way it doesn’t matter what the source of the data is. Anything with no joint weights is static, period.
I have only one format, which is custom, but if I wanted to directly load another format, it would only require a new loading routine, not a new model class.

Again, using only one vertex buffer is a valid approach, but it may give you headaches.

Yes you can delete the source vertex buffer after you make a VBO out of it.


L. Spiro

Share this post


Link to post
Share on other sites

The way I handle data is that anything static is static, and anything animated is dynamic. This way it doesn’t matter what the source of the data is. Anything with no joint weights is static, period.
I have only one format, which is custom, but if I wanted to directly load another format, it would only require a new loading routine, not a new model class.

Again, using only one vertex buffer is a valid approach, but it may give you headaches.

Yes you can delete the source vertex buffer after you make a VBO out of it.


L. Spiro


Do you perform your skinning on the GPU? Since I'm going for iOS devices, GPU skinning isn't a very good option from personal experience.

When I started implementing VBOs for my static geometry, it seems like I get an exception whenever I release my vertex from main memory. Here's what I'm doing:

void OBJMesh::CompileBuffers()
{
// generate the VBO
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(ModelVertex) * numVertices, (void*)vertices, GL_STATIC_DRAW);

// generate the IBO
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned long)*numIndices, (void*)indices, GL_STATIC_DRAW);
//ReleaseVertexData(); // release the vertex data (THIS CAUSES A CRASH WHEN DRAWING)

// use regular data for buffering
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}



ReleaseVertexData() is commented since it releases the vertex data that's generated during load-time. Uncommenting it will release the vertex data, but crash the game when it tries to draw something.

Here's a stripped-down version of my rendering code:

// draw each mesh in the model

OBJMesh *objMesh = NULL;

for(int i=0;i<model->GetNumMeshes();i++)

{

// make sure the mesh exists
objMesh = (OBJMesh*)model->GetMesh(i);
if(!objMesh || !objMesh->GetMaterial()) continue;
glBindBuffer(GL_ARRAY_BUFFER, objMesh->GetVBO());
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, objMesh->GetIBO());

// upload the vertex data from the VBO/IBO based on
glVertexAttribPointer(GL_VERTEX_ATTRIB0, 3, GL_FLOAT, GL_FALSE, sizeof(ModelVertex), 0);
glVertexAttribPointer(GL_VERTEX_ATTRIB1, 2, GL_FLOAT, GL_FALSE, sizeof(ModelVertex), (void*)12);
glVertexAttribPointer(GL_VERTEX_ATTRIB2, 3, GL_FLOAT, GL_FALSE, sizeof(ModelVertex), (void*)20);
glVertexAttribPointer(GL_VERTEX_ATTRIB3, 3, GL_FLOAT, GL_FALSE, sizeof(ModelVertex), (void*)32);
glDrawElements(GL_TRIANGLES, objMesh->GetNumIndices(), GL_UNSIGNED_INT, NULL);
}


I removed some uniform and texture-setting code, but this is what I have.

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!