# 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,
Texture,
data for the draw function(draw type, start index, etc)

};

class/struct Model Instance{

modelMatrix

};

Thanks!

##### 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 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 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.

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

##### Share on other sites

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

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627674
• Total Posts
2978558
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 11
• 11
• 10
• 12
• 22