[SOLVED]Blending without having to draw back to front

Is there a good way to handle transparent textures without having to draw them back to front?
If not what is the best way of determining in what order to draw? My camera is spinning around and moving around on all axises so it’ll be a mess of a function to figure that out, especially when the quads containing the textures intersect with each other.

Thanks in advance!

if you have only one alpha layer, yes you can change the blending function so it make the inverse of what it should (GL_BLEND_FUNC)

http://www.opengl.org/resources/faq/technical/transparency.htm

but this is not the right way in most case

[quote]15.050 Do I need to render my primitives from back to front for correct rendering of translucent primitives to occur?

If your hardware supports destination alpha, you can experiment with different glBlendFunc() settings that use destination alpha. However, this won’t solve all the problems with depth buffered translucent surfaces. The only sure way to achieve visually correct results is to sort and render your primitives from back to front.
[/quote]

Hi Dzzd,

Thanks for the quick answer. I had seen that post which was when I realized that there might be a way. I have no clue how to use DST_ALPHA though (I know how to use SRC_ALPHA though so I can guess how it works but I didn’t get it working)

Also, it says “If your hardware supports destination alpha”, any idea if I’d run into problems with any kind of gfx card if I use it?

Thanks in advance.

This is all a matter of compatibility, but i guess that dest alpha is probably enabled in most gpu, but to be honest with you I dont know (it would be strange if it is not but…),

why dont you want to sort back-front, too many faces ? ordering can usually be done pretty fast

No, there aren’t that many faces but I’m:

  1. Lazy :slight_smile:
  2. Trying to minimize the calculations for people with not so great cpus.

I solved it with some googling (well, a few hours…).

    //draw all opaque objects
    gl.glDepthmask(false);      //make the depth buffer read-only
    //draw translucent objects in any order you would
    gl.glDepthmask(true);      //make the depth buffer writable

http://jerome.jouvie.free.fr/OpenGl/Lessons/Lesson3.php

this will not work very well but you can try it.

=> if you have two translucent object A & B, A is beween B and the camera, if you draw A then B, B will appear over A as the depthbuffer is not active

LoL, indeed. And I thought I found an easy solution :wink:

Well, if I don’t find a good way to use DST_ALPHA then I guess I’ll have to draw it in order.

I’ll leave it on solved though because the solution is that to do blending correctly you need to draw it back to front.

Thanks for your patience DzzD!

Someone with alot more of experience than me can probably explain why this works but I managed to do it while playing around. I didn’t sort anything and no matter from which direction I’m looking at it it looks great:

		gl.glEnable(GL.GL_BLEND);
		gl.glDepthMask(false); //Make the depth buffer read-only
		gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); //For the transparency of the image
		gl.glBindTexture(GL.GL_TEXTURE_2D, GetTexture());
		//Draw the transparent images here
		gl.glDepthMask(true); //Make the depth buffer writable
		//Draw the transparent images here
		gl.glDisable(GL.GL_BLEND);

Just thought I’d share and if anyone wants to explain why it works I’d be more than happy to hear :slight_smile:

That’s great to here you’ve solved it, but under what tests does it always look correct? Are you just doing cubes and squares, or does it look good with complex models that are concave (ex. there is a great dragon model that has been used by Nvidia a lot in their demos for transparency).

If your solution works for you, good, but to further your learning, you could try reading about “depth peeling.” The basics of it are to render a slice of your transparent objects at a time, and then composite them together. It gives very good results, but is usually slow (for obvious reasons).

For now the only models I have that use transparent textures are trees drawing two quads at 90 degrees and putting a png on each. It doesn’t matter from which direction I look, it still looks really nice.

If the only transparent textures that I’ll have is for trees then I won’t delve deeper into it but if I will want to add others as well I guess I’ll have to look into drawing back to front (drawing is alot slower for my application than calculating).

I attached a picture where the trees are drawn front to back and it still looks correct. (I had to put them on a height though to get them closer to the camera to make sure they looked correct :))

It’s generally better to use alpha testing rather than blending for trees and other foliage. It doesn’t need sorting and works correctly with the depth buffer, plus it uses less fill rate so it’ll be faster too.

If you’d had said you were trying to draw trees in your first post this would have been a much shorter thread. ::slight_smile:

Thanks for the idea for the trees but alpha testing didn’t look as good as blending. I will use transparency for other things though so the system is handy for me. I might use alpha testing for the trees though with the performance gain.

isn’t Alpha testing might look “jirky” on the border ?

Yep, it doesn’t look as good (can’t do semi transparency as far as I know).

I attached a picture using alpha testing, with some tweaking I could make it look the same. (you also see the window and menu system integrated into the view as a bonus :slight_smile: )