Sign in to follow this  

OpenGL Another problem with Depth, this time with an alpha channel

Recommended Posts

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: 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 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-

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
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:

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Forgot to say you have to disable depth testing when alpha blending things after sorting them.

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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?

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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.


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.

Share this post

Link to post
Share on other sites
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!

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
Man, when I set out to do this, I didn't know it would be such a specific application that it would be this much hassle. Oh well...I'm almost there...

Thanks for the input!

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By DelicateTreeFrog
      Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
      Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
      For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
      So, here's what the plan is so far as far as loading goes:
      Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
      Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
      Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
      The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
      So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
      With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!
    • By JJCDeveloper
      I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
    • By ferreiradaselva
      Both functions are available since 3.0, and I'm currently using `glMapBuffer()`, which works fine.
      But, I was wondering if anyone has experienced advantage in using `glMapBufferRange()`, which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
      Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
  • Popular Now