Sign in to follow this  
pressgreen

How do I manage loaded geometry?

Recommended Posts

pressgreen    678

I have created a parser that will load my custom geometry. when it loads the geometry it places the vertex array into a VBO. I want to make many of the same object or introduce new objects into the scene but use them later as one is playing the game. how do i store geometry so it is not loading from a file into the VBO when it is called upon? but rather from some sort of buffer that holds it in an efficient way until its needed perhaps.

 

one of the things I am trying to set up are missiles for a tank game. I want the user to be able to just shut a 100 missiles back to back. each missile geometry is the same. how would you recommend I create multiple missiles with our having to load the geometry for each missile every time the user fires the missile?

 

thanks

Edited by greenzone

Share this post


Link to post
Share on other sites
EddieV223    1839

You didn't specify if you are using c/c++ so I'll assume c++.

 

The easiest way to do this would be to pass around your VAO id.  For every object that uses that mesh just have a copy of that VAO id.  When you get ready to draw a missile for example you just bind the vao, send in your objects world matrix ( to the shader uniform ),  and draw.   This is called instancing.

 

Another way to handle it is more work but more robust, create a Manager Class that manages your Meshes.  You have a vector of pointers to mesh objects ( use unique_ptr ) in the mesh manager and you can get a pointer to a managed mesh object, from that Manager class, by file name. 

 

Managing Textures and Shaders can also be done in similar ways.

Edited by EddieV223

Share this post


Link to post
Share on other sites
powly k    657

In OpenGL, there are specific draw calls for instancing (drawing multiple copies of the same thing) so you don't have to care about keeping them all in memory - just keep their locations in a texture and render based on that and instanceID. Just write a class that handles a single model and load all your geometry as instances of that class in the beginning of your program.

Edited by powly k

Share this post


Link to post
Share on other sites
pressgreen    678

I am not understanding "powly k", What do you mean just keep their locations in a texture? also what are these specific OpenGL draw calls for instancing?

Share this post


Link to post
Share on other sites
powly k    657

A texture can easily be read in a shader and written to on the CPU, so you can upload your positions and orientations as a texture, though you probably should do it with uniform buffers or something. The draw calls i was referring to are glDrawArraysInstanced and glDrawElementsInstanced.

Share this post


Link to post
Share on other sites
mhagain    13430

Regarding this part:

 

 

how do i store geometry so it is not loading from a file into the VBO when it is called upon?

 

 

You seem unaware that you can have more than one VBO created.  Psuedocode for load-time might look something like this:

 

 

GLuint geometryBuffers[NUM_GEOMETRY];

glGenBuffers (NUM_GEOMETRY, geometryBuffers);

for (int i = 0; i < NUM_GEOMETRY; i++)
{
    LoadGeometryItem (i);
    glBindBuffer (GL_ARRAY_BUFFER, geometryBuffers[i]);
    glBufferData (GL_ARRAY_BUFFER, ...);
}

 

And for drawing:

 

for (int i = 0; i < NUM_GEOMETRY; i++)
{
    glBindBuffer (GL_ARRAY_BUFFER, geometryBuffers[i]);
    glEnableVertexAttribArray (...);
    glVertexAttribPointer (...);
    glDrawArrays (...);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this