Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED]Blending without having to draw back to front  (Read 1925 times)
0 Members and 1 Guest are viewing this topic.
Offline Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Posted 2009-04-05 15: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!

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline DzzD
« Reply #1 - Posted 2009-04-05 16: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 Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Reply #2 - Posted 2009-04-05 16: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.

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DzzD
« Reply #3 - Posted 2009-04-05 16: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 Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Reply #4 - Posted 2009-04-05 16: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.

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Reply #5 - Posted 2009-04-05 17: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

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline DzzD
« Reply #6 - Posted 2009-04-05 17: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 Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Reply #7 - Posted 2009-04-05 20: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!

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Reply #8 - Posted 2009-04-05 20: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

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline lhkbob

JGO Knight


Medals: 32



« Reply #9 - Posted 2009-04-06 19: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Reply #10 - Posted 2009-04-07 08: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)

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #11 - Posted 2009-04-07 12: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 Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Reply #12 - Posted 2009-04-07 15: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.

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline DzzD
« Reply #13 - Posted 2009-04-07 15:58:36 »

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

Offline Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Reply #14 - Posted 2009-04-07 16: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 )

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (32 views)
2014-07-18 06:55:21

Zero Volt (28 views)
2014-07-17 23:47:54

danieldean (24 views)
2014-07-17 23:41:23

MustardPeter (25 views)
2014-07-16 23:30:00

Cero (40 views)
2014-07-16 00:42:17

Riven (42 views)
2014-07-14 18:02:53

OpenGLShaders (29 views)
2014-07-14 16:23:47

Riven (29 views)
2014-07-14 11:51:35

quew8 (26 views)
2014-07-13 13:57:52

SHC (63 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!