Another problem with Depth, this time with an alpha channel

Started by
9 comments, last by Langecrew 17 years, 4 months ago
Hello everybody! It's me again! I have been trying for quite some time now to render intersecting tetrahedrons whose base triangles are coplanar. Basically, everything is going great. Now, I've had to try to add semi-transparency to my little tetrahedrons. a past post for those unfamiliar with my problems: http://www.gamedev.net/community/forums/topic.asp?topic_id=426329 The problem that I now have is that: The intersecting tetrahedrons are laid out in rows and columns, each one intersects all the others bordering it. The tetrahedrons render fine and look great...from many angles, I can see the intersecting tetrahedrons along with all the tetrahedrons behind them. However, when I rotate to certain angles, I still see transparent tetrahedrons, but I can't see the ones behind them; the transparency doesn't seem to let the other parts of the image bleed through. Please see the screenshot below, perhaps it will be more clear. The topmost of the two drawings represents how the arrangement should render from all angles. Free Image Hosting at www.ImageShack.us If you look closely at the rendering at the bottom of the shot you can see that it does show some overlapping with transparency, but not all of the way down the row...also you can see that it is in fact rendering all these tetrahedrons, it just won't show them. It seems to me that there is probably just something stupid going on somewhere, but my knowledge of OpenGL is too limited for me to know. Thanks again! -Langecrew-
Advertisement
The thing is no matter if you use D3D or OpenGL the second you start alpha blending you have to sort your polygons. This is because even if the pixel is transparent its written to Depth Buffer causing everything else behind it to be occluded.

Look at this for possible solutions:
http://www.opengl.org/wiki/index.php/Alpha_Blending
Forgot to say you have to disable depth testing when alpha blending things after sorting them.
You have 2 choices
1) you can sort all your polygons by their depth from the camera,
then draw them farthest to closest.
2) you can disable depth writes (leave depth tests on), and render all your
transparent objects.
No dice. I'd be plenty willing to sort them, but how do you approach that if the collection of objects can be rotated to any angle? I tried keeping track of the angles of rotation before, and I didn't really get any satisfactory results.

I've honestly tried to avoid being the guy that posts about 2 pages of code and just asks "what's wrong with my code?" but I think I'm coming to that point. Either I've been missing something very fundamental this whole time, or OpenGL has a bug that causes unexpected behavior when primitives overlap/intersect.

anybody care to take a crack at my code?
There is no bug, what is happening is that it draws everything as it comes in. You can't properly blend
over the background if the background isnt there yet. So you have to draw what is in back first.

This means that you need to draw the back objects before the front ones. So, how?
You must have some position information for your objects, and your objects need to be seperaterated in some way.
So you cant just be drawing points with no order.

So maybe you should post a bit of code so we know how you are drawing your objects.
ie. are they a for loop with glVector3d () calls? or a loop over objects?

This whole thing is easier if you have objects of some sort that contain the position of the center of the object.

struct Object{  vector3d pos;  float size;  ...  }


from there you just need to sort on distance, so sort by
(camera.pos - cur_object.pos).size();
where size() is the vector magnitude.
That way you have all your objects in the order of distance to them, thus you can draw them in order.

If you are rotating the objects and not the just the camera, then you need to preform the roation on the data.
Since the gl calls dont affect your actuall dataset.
So, if you rotate and translate the objects with gl calls, then you need to use those same rotations and translations on your data
OR you need to preform the inverse of those actions on your camera before you sort the collection.

Since the camera is a single point and the things you are drawing are many points, it will be faster to transform the camera.
So, you need the rotation/translation of your object collection, and then invert them. From there you have to apply it to
your camera. This application to the camera depends on your camera code.
alpha to coverage. solves all those pesky alpha sorting problems. (but only allows 6 levels of transparency, when using 6x oversampling. and requires oversampling.)
Try disabling the depth testing completely. It should look the way you want it to (ie. the way it does on the "good" screenshot) from all angles in that case, since it's what makes only the front transparent objects show up (and not the ones behind) in your "bad" screenshot.

glDisable(GL_DEPTH_TEST);

Note that this is not a very good way to do transparency, and it will not work unless all of your objects are equally transparent.
Hi guys,
I wanted to post back to let you know that I'm still here. I got side-tracked on some major bugs in a different project here at work. That's why I've been out of the loop for a few days here.


KulSeran, I think you might be on to something here. Seeing as how I'm so blazingly new to OpenGL programming, I've never done anything like how you suggest. I am actually doing both, rotating the collection of objects, AND moving the camera. I use rotation on both the outline of the measurement volume and the tetrahedrons inside, as well as their outlines. I move the camera when panning left/right or up/down. So you're saying that, if I simply apply the rotation information to the actual data, then I should be OK?
Is that to say that I should:
1. apply the rotation info to the data
2. NOT rotate what is rendered, but instead render the rotated data?
What approach should I try if I move the camera, and rotate the objects?


l0calh05t, I'm intrigued. I searched for "alpha to coverage" and I found some sample code online. I couldn't get the pre-made EXE to run, and I have not yet gotten the time to look at the code at all...but it's on my list also.


shurcool, I've tried playing with the Depth Test many times. If that could provide an appropriate solution, then I think I've missed something. When I render everything with Depth Test off, then everything looks all strange when I rotate. It looks like, if you follow the tetrahedron in front when the scene is first rendered, and rotate, the tetrahedron that was in front visually looks like it stays in front of all the others even when you know it has moved behind others. It looks really wierd.


Let me give this just a couple more shots before I post my code. That way, you guys don't have to spend the time to look through my source.


Thanks again, have a great day!
-Langecrew-
To avoid the artifact that you mentioned with the depth testing disabled, you *HAVE* to sort your objects. OpenGL & D3D are not ray tracers, they are rasterizers that expect your transparent polygons to be written in back to front order to render completely.

If you cant do this per object because they are overlapping, then do it per triangle. If you really need the detail, implement the sorting using an oldschool BSP tree. But per object is usually enough, you will get less artifacts sorting that way than with no sorting. Just do it based on the distance of the centre of each object to the camera.

On another note, you want to leave depth-testing on so that solid objects occlude your transparent ones, but you want to be turning depth-writing off (glDepthMask) so the other transparent objects arent obscured.

- Shaun

This topic is closed to new replies.

Advertisement