Java OpenGL saving depth buffer

浪子不回头ぞ 提交于 2019-12-13 06:47:01

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/2516086/java-opengl-saving-depth-buffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!