Sign in to follow this  
Aliii

OpenGL Advice needed for basic model / entity system

Recommended Posts

Im new to the modern(3.3+) OpenGL, I understand the basics: shaders, VBOs, VAOs, texture objects, units, samplers. I can compile the shaders, fill the VBOs, and draw the object I want. However I dont know how to actually put these together into a "system" where there are lots of different objects(static and moving) ....using different textures, needing different shaders, etc.

 

I understand that this is a huge topic so I dont expect detailed answers(although they are welcome:)), .....I just want a few pointers/basic rules how to go about it.

The scope is a simple 3D app, a few different models, a few instances from each. No streaming, everything is in the RAM. Its a small project but I want to do it in the proper way and I want it to be scaleable. ....so Im not really looking for a "U just have a few models so put everything into one big global...." kind of answer.

 

So here are the questions:

-Do I need different VBOs for each different model? Or do I store them in one VBO and store the start indices.

-How do I draw different instances from the same model? I guess I use instanced rendering, but how do I store/update their model matrices? Lets say some of these objects are moving and some are static. Do I use uniform buffers or VBOs for their model matrices? Do I handle/draw static and moving objects separately?

-I want per-primitive lighting. For moving objects I guess I have to calculate the lighting level in the shader using the surface normals and the light direction. What about static objects? Should I store the precalculated lighting values in a VBO for each object? 

-Basically what data do I store per model / model instance?

class/struct Model{

    VAO,
    ShaderProgram,
    Texture,
    data for the draw function(draw type, start index, etc)

};

class/struct Model Instance{

    modelMatrix

};

Thanks!

 

Share this post


Link to post
Share on other sites

1. Yes, generally you use an individual VBO per model, or even multiple VBO's if you do some kind of spatial partitioning

2. You can use instanced rendering but from my experience, they're not worth it unless you're rendering LOTS (10,000's+) of LOW-POLYCOUNT objects (maybe billboard particle effects?) You don't really even need to store the matrices... Just translate/scale before you render your model, then load identity after

3. I don't think there's "per-primitive" lighting. There is per-vertex or per-pixel. I guess unless you do somehow calculate it yourself, *shrug*

 

Your example Model looks basically correct. Note obviously you will need to support multiple textures if your object has them.

Share this post


Link to post
Share on other sites

-Do I need different VBOs for each different model? Or do I store them in one VBO and store the start indices.

Neither.
1 model = 1 or more meshes = 1 or more VBO’s (or 1 VBO per mesh with start/end indices, but you should save that for when you get more experienced).
I have explained this with a graphic here:
http://lspiroengine.com/?p=96
 

-How do I draw different instances from the same model?

By making a class called a “shared model” with the source vertex data and then make classes called “model instance” that have a pointer to the source data and use the source model’s vertex buffers for rendering themselves at their own transforms etc.


L. Spiro Edited by L. Spiro

Share this post


Link to post
Share on other sites

Thanks! So both of you are saying that it doesnt matter that I have lots of VBOs and VAOs and I make a lot of glDraw... calls(even thousands), more important is not to do state/texture/shader changes. Im I right?

 

"By making a class called a “shared model” with the source vertex data and then make classes called “model instance” that have a pointer to the source data and use the source model’s vertex buffers for rendering themselves at their own transforms etc."

 

Sure. I did something like this in "glBegin-glEnd" openGL.

for( every model)
    for( every model instance)
        if( is in the view)
            draw(....)

My question was, that now with the new stuff, whats a more ideal way to do this. I have no idea how much overhead it means to bind VAOs, do a glDrawElements/drawArrays call, and have 1000+ VBOs.

(Ive read your article BTW, it has lots of useful info, ...going to read more of them.)

 

So still every model needs a separate VBO(or more) and every model-instance needs a separate draw call. Plus I should sort the objects before rendering.

 

"You don't really even need to store the matrices... Just translate/scale before you render your model, then load identity after"

 

So whether the object is static or not, I should calculate the matrix just before rendering and send it with glUniformMatrix? It doesnt sound good.

 

"...then load identity after"

 

I dont get this. It rather sounds like glTranslate,glRotate, draw, glPopMatrix:)

 

 

Share this post


Link to post
Share on other sites


"...then load identity after"
 
I dont get this. It rather sounds like glTranslate,glRotate, draw, glPopMatrix:)

It is.  Don’t do it.

 

I’m not ready right at this moment to explain the details, but if someone else is feel free to chime in.  With your own matrix library you can just send matrices as-is without all the pushing and popping that limited fixed-function OpenGL so much.

 

 

L. Spiro

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