Question

I'm not entirely sure if I can do what I want here, but I have a bunch of objects being rendered in OpenGL (using JOGL). For one group of objects, I want to ensure that certain objects in that group are rendered in front of other objects in that group. I've tried clearing the depth buffer bit and rendering the "front" objects last, and that works, except it messes up other depth buffering on screen.

What it comes down to is I have a list of objects being rendered and I want to ensure that certain objects in that list are rendered in front of other objects (they are all at the same Z coordinate though). Is there a way to do that?

thanks, Jeff

Was it helpful?

Solution

This is a very common technique to draw transparent objects (objects that have parts with alpha!=1).

The most common approach is to first build a tree of objects to be drawn that you can then sort along to the "depth" after being passed through the projection*camera matrices. Basically instead of just blindly throwing triangles at the GPU, you send your objects one by one as you're processing them in the world to a temporary buffer. These objects have full knowledge of every triangle and every vertex color/vertex shader/texture name+id etc. Then you can sort your buffer (either naively, object by object, or a full blown sort based on similar patches across objects).

The glDepthMask trick, if i recall correctly, goes something like this:

glDepthMask(true);
drawOpaqueObjects();
glDepthMask(false);
drawTransparentObjects();

For best results the transparent objects are sorted back-to-front, but in most (simple) applications this doesn't matter.

edit: note that for the 2nd technique, while you enable and then disable depth buffer WRITING, you still use depth buffer TESTING, so transparent objects behind your normal objects (a helmet pointing "into" the screen for example) won't get drawn.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top