Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  [SOLVED]Blending without having to draw back to front  (Read 1226 times)
0 Members and 1 Guest are viewing this topic.
Offline Mickelukas

JGO Ninja
***

Posts: 731
Medals: 25


Java guru wanabee


« on: 2009-04-05 09:40:18 »

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!

Offline DzzD

JGO Kernel
*****

Posts: 2134
Medals: 16



« Reply #1 on: 2009-04-05 10:22:04 »

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.

Offline Mickelukas

JGO Ninja
***

Posts: 731
Medals: 25


Java guru wanabee


« Reply #2 on: 2009-04-05 10:45:15 »

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


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.

Games published by our own members! Go get 'em!
Offline DzzD

JGO Kernel
*****

Posts: 2134
Medals: 16



« Reply #3 on: 2009-04-05 10:50:12 »

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

Offline Mickelukas

JGO Ninja
***

Posts: 731
Medals: 25


Java guru wanabee


« Reply #4 on: 2009-04-05 10:58:43 »

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 Smiley
2. Trying to minimize the calculations for people with not so great cpus.

Offline Mickelukas

JGO Ninja
***

Posts: 731
Medals: 25


Java guru wanabee


« Reply #5 on: 2009-04-05 11:04:06 »

No, there aren't that many faces but I'm:
1. Lazy Smiley
2. Trying to minimize the calculations for people with not so great cpus.

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


1  
2  
3  
4  
    //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

Offline DzzD

JGO Kernel
*****

Posts: 2134
Medals: 16



« Reply #6 on: 2009-04-05 11:19:05 »

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

Offline Mickelukas

JGO Ninja
***

Posts: 731
Medals: 25


Java guru wanabee


« Reply #7 on: 2009-04-05 14:11:15 »

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!

Offline Mickelukas

JGO Ninja
***

Posts: 731
Medals: 25


Java guru wanabee


« Reply #8 on: 2009-04-05 14:43:37 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
      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 Smiley

Offline lhkbob

JGO Neuromancer
****

Posts: 1174
Medals: 35



« Reply #9 on: 2009-04-06 13:21:46 »

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).

Games published by our own members! Go get 'em!
Offline Mickelukas

JGO Ninja
***

Posts: 731
Medals: 25


Java guru wanabee


« Reply #10 on: 2009-04-07 02:53:11 »

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  Smiley)

Offline Orangy Tang

JGO Kernel
*****

Posts: 2960
Medals: 37


Monkey for a head


« Reply #11 on: 2009-04-07 06:26:58 »

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. Roll Eyes

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Mickelukas

JGO Ninja
***

Posts: 731
Medals: 25


Java guru wanabee


« Reply #12 on: 2009-04-07 09:30:38 »

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. Roll Eyes

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.

Offline DzzD

JGO Kernel
*****

Posts: 2134
Medals: 16



« Reply #13 on: 2009-04-07 09:58:36 »

isn't Alpha testing might look "jirky" on the border ?

Offline Mickelukas

JGO Ninja
***

Posts: 731
Medals: 25


Java guru wanabee


« Reply #14 on: 2009-04-07 10:09:59 »

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 Smiley )

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.199 seconds with 21 queries.