Sorting for Transparency

Started by
3 comments, last by mmakrzem 9 years, 1 month ago

Hello,

So I would like to optimize my drawing calls to account for transparency.

Long story short, what I have been doing is sorting all of my OpenGL meshes from opaque to fully transparent before the scene is loaded and then drawing.

However, after reading, you must also sort from furthest to nearest.

The problem is I am not sure how this can be done dynamically in the scene with an std::vector or QVector based iteration of all of my objects I need to draw.

Consider the logical steps I am going through now:

  • Load all of my objects into Computer RAM via loaders.
  • With loaders I assign the correct materials to the object (colors, textures, etc.)
  • Sort by transparency
  • Load into OpenGL
  • Draw into OpenGL using the rendering loop.

I guess what I am asking is how do people sort the objects drawn from furthest to nearest per frame based on the active camera position?

What's the high speed way that I am missing?

Thank you.

Advertisement
You could do it like this:
- you have a vector with all your objects (of a class or struct type)
- each object has a property with current distance to camera
- you make a simple int vector, with 1, 2, 3 etc (for all objects)
- then sort the distances to camera of all necessary objects and store their ID in the right order in the int vector.

Voila, now when rendering you can iterate through the objects using that index.

Sidenote; sorting transparent objects back to front sounds more like mandatory then optimizing

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

You could do it like this:
- you have a vector with all your objects (of a class or struct type)
- each object has a property with current distance to camera
- you make a simple int vector, with 1, 2, 3 etc (for all objects)
- then sort the distances to camera of all necessary objects and store their ID in the right order in the int vector.

Voila, now when rendering you can iterate through the objects using that index.

Sidenote; sorting transparent objects back to front sounds more like mandatory then optimizing

Thank you so much; this sounds like a workable solution.

I could extend this using threads to keep the sorting dynamic when the camera moves around.

I am just curious as to how to do this without interfering that much with the rendering thread; I'll need a mutex to lock the both the OpenGL object vector and the camera distance int vector until after sorting is complete when the camera moves.

You could update the sorting per X frames, each frame might be overkill. Just to be sure, you just have to sort the non-opaque objects.

Your render system/ class doesn't need to know much on the objects itself. It should just take a vector with renderables and the int vector with the the order and which renderables to be rendered.

Somewhere earlier in the process you can also make sure that only visible renderables get in the int vector, before sorting.
Good luck

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

The game engine that I describe on my website does the following. All visible objects are grouped into 2 buckets. One bucket for opaque objects, and one bucket for objects that have transparent/translucent pieces.

Each frame I calculate the distance from my object's centroid to the camera's location. When it is time to render, I first draw all the objects that are in the opaque bucket making rendering the items based on distance from FRONT to BACK. Then I draw all the objects that are in the other bucket making sure to render the items from BACK to FRONT.

This topic is closed to new replies.

Advertisement