Wrong Blend Mode?

Hey everyone,

I searched for this issue but I couldn’t find anything.
In short: My Objects look like this:

I ran into this problem a few years back (not with jME) and if I remember correctly it had something to do with premultiplied images.
Now, there are the two options “Alpha” and “PremultAlpha” in the AdditionalRenderState, but neither of them work.

I also tried this:
[java]

            material.getAdditionalRenderState().setAlphaTest(true);
            object.setMaterial(material);
            object.setQueueBucket(RenderQueue.Bucket.Transparent);

[/java]

I can’t figure it out!
What are the right configurations for this bug not to appear?
Thanks in advance!

Its a depth based problem, make sure you set the AlphaDiscardThreashold

1 Like

When I set it, the texture begins to disappear, depending on the value I set.
Sadly I can’t find an acceptable way in the middle.

Anything else or do I need to talk to my buddy to create better textures? :wink:

@m41q said: When I set it, the texture begins to disappear, depending on the value I set. Sadly I can't find an acceptable way in the middle.

Anything else or do I need to talk to my buddy to create better textures? :wink:

Do you understand what the actual issue is? It’s related to alpha, depth, and draw order. If you understand the fundamentals of the issue then you will understand that are no really good solutions only trade offs. Then you might be able to address the issue with the textures or some other way depending on the trade-offs desired.

…but it’s a long explanation done many times on here. I thought to avoid repeating it if you already understood the issue.

1 Like
@pspeed said: Do you understand what the actual issue is? It's related to alpha, depth, and draw order. If you understand the fundamentals of the issue then you will understand that are no really good solutions only trade offs. Then you might be able to address the issue with the textures or some other way depending on the trade-offs desired.

…but it’s a long explanation done many times on here. I thought to avoid repeating it if you already understood the issue.

Before I did jME, I programmed with LWJGL directly, so I knew exactly what the issue was. But that was quite some time ago and I don’t remember that much.
Anyways, when I worked with LWJGL, I somehow figured it out. I looked through my old source code and tried to extract my solution back then.

But now to the interesting stuff:
I checked out the jME source and managed to get it working like I want it to.
I replaced a the lines
[java]
if (state.getBlendMode() != context.blendMode){
if (state.getBlendMode() == RenderState.BlendMode.Off)
glDisable(GL_BLEND);
else
glEnable(GL_BLEND);
[/java]
with
[java]
if (state.getBlendMode() != context.blendMode){
if (state.getBlendMode() == RenderState.BlendMode.Off)
glDisable(GL_BLEND);
else
glDisable(GL_BLEND);
glEnable(GL_ALPHA_TEST);
[/java]
in LwjglRenderer.

So I don’t know why this works (it’s obviously just a temporary hack) because I just copied it from my old source code.
And I also don’t know if there are any drawbacks to using GL_ALPHA_TEST instead of GL_BLEND.

So what do you think, especially @pspeed?
Is this a viable option for rendering transparent textures?

I’d really like to upload an image of what the trees look like now, but something goes wrong everytime I upload…
You’ll just have to believe me :smiley: (or test it yourself)

IMPORTANT EDIT:
I forgot one line in the code I replaced. I also added
[java]
GL11.glAlphaFunc(GL11.GL_GREATER, 0.5f);
[/java]

Another EDIT:
Somehow, after you use [ java ], the pragraphs I write don’t get displayed correctly.

You can use alpha test by setting the alpha test on the RenderState. you do not need to hack JME to do this. You must also set the alpha threshold for alpha test to do anything, though. (Make sure you really are running the latest code for that though because there was a bug with alpha test when threshold was 0 but perhaps that won’t affect you.) You can see the code that does that just a few pages up from where the blend mode is set.

What alpha test is doing is setting a threshold for what happens with semitransparent pixels. (http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node96.html)

…Which by the way is the same thing setting alphaDiscardThreshold on the material will do… only that approach isn’t deprecated in later versions of OpenGL.

1 Like

Actually, I see that you also disabled blend mode. I’m not sure how that works for you at all, actually other than not even blending the semi-transparent pixels.

In which case, just set BlendMode to none and turn on alpha test.

1 Like

@pspeed: I finally got it; and (of course) its all doable in jME.

What I wanted: I wanted to discard the alpha values at the borders and make the rest completely opaque.

What I did wrong: While I did set the AlphaDiscardThreshold, I forgot to set the Blend mode to off. (Or the other way round!)

Now I understand why it didn’t work and what my old source code actually meant :roll: Problem solved!