Sign in to follow this  
fazekaim

Help in blending

Recommended Posts

fazekaim    118
Hello, I have blending problem presented with these pictures http://delfin.klte.hu/~fazekaim/tree2.jpg http://delfin.klte.hu/~fazekaim/tree1.jpg So, I execute this statements: gl.glEnable(GL.GL_BLEND); gl.glBlendFunc( GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA ); ... drawing the tree and as You can see, the tree's bark is visible behind the leaves, but the other leaves aren't. Any idea could be helpfull. Thanks.

Share this post


Link to post
Share on other sites
Brother Bob    10344
Either disable depth writes while drawing the leaves (and make sure you draw the leaves last), or use alpha test to completely reject fragments outside the leaves instead of drawing them as transparent pixels.

Check out glDepthMask and/or glAlphaFunc. You don't need both in this case, one of them is enough.

Share this post


Link to post
Share on other sites
fazekaim    118
If I disable depth functions, I see all background leaves behind all points of every leaf.
My problem is, that where the leaf texture is transparent, only the bark that I can see.

Thanks a lot, by the way.

How cam the alphafunc solve my problem?

Share this post


Link to post
Share on other sites
Brother Bob    10344
I didn't say you should disable depth test, I said depth writes. Depth testing should be enabled.

Alpha test is probably the easiest way, assuming the alpha channel in the leaf texture is either 0 or 1 for leaf/no leaf. Then you could set the alpha function to drop every fragment with alpha not equal to 1. That way, the parts with no leaf at all will not be drawn at all. Using alpha blending, the leaf is still drawn although you won't see the parts outside the leaf, but the depth buffer will be updated.

Share this post


Link to post
Share on other sites
fazekaim    118
Brother Bob: The ALPHA_TEST resolved my problem. Thanks.
But, a new one is on: using glBlendFunc, I can blend the leaves normally, but with alpha testing, every leaf has a little white silhouette.

http://delfin.klte.hu/~fazekaim/c.jpg

Share this post


Link to post
Share on other sites
Brother Bob    10344
I think the borders looks a little bit too big to be what I think it is. The problem is, I think, that you don't set the alpha test to pass fragments with an alpha of 1 only, no other fragmens should pass. Try set the alpha function to GL_EQUAL with reference 1 and see if the white edge dissapears.

Otherwise, post a link to the leaf texture so I can have a look at it, because, sa I said, the white edge looks a little bit different than I would expect if this is the problem.

Share this post


Link to post
Share on other sites
fazekaim    118
With GL_EQUAL, the borders disappear, but from a long distance a leaves disappear too. Mipmapping could be the reason?

http://delfin.klte.hu/~fazekaim/equal1.jpg
http://delfin.klte.hu/~fazekaim/equal2.jpg
http://delfin.klte.hu/~fazekaim/equal3.jpg

Share this post


Link to post
Share on other sites
Brother Bob    10344
Ok, then it was as I thought then. Yes, mipmapping is why the leaves gets smaller. Or rather, bad mipmap levels, not mipmapping itself. Using a standard box-filter or so (pretty much what gluBuild2DMipmaps and SGIS_generate_mipmap uses) to generate lower mipmap levels is, generally, not going to work well with this technique. You have a few options.

The simple solution is; don't use mipmaps. Just use the base level and GL_LINEAR for filtering.

You can also choose a different color outside the leaf, a color that matches the color of the leaf itself. Instead of white, choose a orange-brown color, so that the edge that was white now is a part of the leaf, so to speak. Then you can set the alpha test reference to something other lower 1.

However, this also means the image itself must have an alpha channel of the leaf's shape. You can't use a color key to create the alpha channel when loading the texture, since the color key would be the same as the leaf color.

Share this post


Link to post
Share on other sites
zedzeek    529
glubuildmipmaps or GL_SGIS_GENERATE_MPIPMAPS is the problem,

BEST SOLUTION
--u will need to create the mipmap levels yourself (*.dds textures let u store the mipmaps with the texture)

NOT SO GOOD
--enable alpha testing AND blending so the edges fade out

Share this post


Link to post
Share on other sites
zedzeek    529
it will generate the mipmaps but they will be wrong just like GENERATE_MIPM_MAP_SGIS + gluBuildMipMaps, u need to create each mipmap level yourself, and then save them together in a texture eg *.dds or ALTERNATIVELY u can load multiple textures
eg
leave_size64x64.tga
leave_size32x32.tga
..
1x1.tga
and when u construct the mipmaps use these texture data for the mipmap levels

Share this post


Link to post
Share on other sites
fazekaim    118
As You asked, here is my leaf's texture:

http://delfin.klte.hu/~fazekaim/leaf3.tga

How do you make mipmaps? Not simply scaling, I think :)

Share this post


Link to post
Share on other sites
zedzeek    529
>>How opengl generates mimaps exactly?<<

basically glubuildmipmaps, SGIS_GENERATE_MIPMAP and the nvidia dds plugin (i assume) all use some sort of boxfilter thus say for the 4 pixels
10
10
if they downsize these 4 pixels into one pixel u will be left with (1+1+0+0)/4 = 0.5
which normally is what u want BUT not in your alpha channel for the leaves, here u want either 1 or 0 aterwise u are gonna see the white borders which u see.
thus u will have to generate the mipmaps yourself, making sure each mipmap level down to 1x1 contains alpha values of either 1 or 0 + not 0.123, 0.5, 0.876 etc which u will get if u use glubuildmipmaps, SGIS_GENERATE_MIPMAP and the nvidia dds plugin

>>If I make mipmaps my own, I will see leaves from high distance with GL_EQUAL alpha test?<<

i dont understand

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