PDA

View Full Version : Best Practices for drawing multiple objects

NoobCoder
12-23-2015, 08:10 AM
I am trying to write a program to draw a solar system simulation (with realistic gravity, orbits, etc.) ... for now I am sticking to 2D for simplicity purposes.

This means that I need to draw separate graphic objects for each planet. What would be the best/recommended way to do that?

Should I create one buffer (float buffers) for each planet? Should I pile them all up into one buffer using glBufferSubData and then tell OpenGL when one body buffer ends and the next one starts?

Keep in mind that I want to code this to be scalable: I want to expand this simulation to have hundreds if not thousands of bodies at some point (calculation for gravity will be peformed by a separate system that will run calculations in parallel processes, that's the easy part for me). And these bodies will become textured spheres when I move on to 3D.

So it's not enough for me to do this for a few bodies ... I want to code this so that OpenGL will be able perform well even if I throw a whole solar system and a few hundreds asteroids at it.

Thanks!

GClements
12-23-2015, 10:21 AM
If all of the objects are going to be textured spheres, you probably want to use instancing. I.e. you'd only have one sphere (or one sphere for each level of detail) and draw each planet as an instance. There's no reason to store many copies of identical geometry, or geometry which is identical up to simple transformations.

One thing to consider is that if you draw multiple objects in a single draw call, they have to use the same set of textures.

Also, probably the best way of rendering a textured sphere with modern OpenGL is to raytrace it in the fragment shader. Texture mapping a triangle mesh which approximates a sphere is actually a fairly hard problem.

On the other hand, with a perspective projection and realistic scales, only the nearest planet (and maybe a moon or two) is going to be larger than a point.

NoobCoder
12-23-2015, 01:53 PM

If all of the objects are going to be textured spheres, you probably want to use instancing. I.e. you'd only have one sphere (or one sphere for each level of detail) and draw each planet as an instance. There's no reason to store many copies of identical geometry, or geometry which is identical up to simple transformations.

Instancing ... I didn't even know it existed. You are right in that I am faced with a situation where I would have probably 3-4 types of bodies but other than size they would all look exactly the same ... so instancing makes a lot of sense. I will look into it.

One thing to consider is that if you draw multiple objects in a single draw call, they have to use the same set of textures.

Did not know that either ... thank you for pointing it out. That's why my nick name is "noob coder" ;) (at least noob in the openGL world)

Also, probably the best way of rendering a textured sphere with modern OpenGL is to raytrace it in the fragment shader. Texture mapping a triangle mesh which approximates a sphere is actually a fairly hard problem.

On the other hand, with a perspective projection and realistic scales, only the nearest planet (and maybe a moon or two) is going to be larger than a point.

Yes, indeed. In fact other than getting close to a gas giant with moons probably i won't have to render more than 2 bodies at the time (even the other moons would probably be too far realistically to be anything more than points).

THank you very much for your help!