Blending with alpha problem

Started by
6 comments, last by Shawn619 10 years, 10 months ago

I want the alpha on the windows of my car(alpha=0.3, every other part of car alpha=1.0(no transparency)) to blend properly with my shader lighting.

I have the blend function as follows:


glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

If I disable blending all together, I get:

2qiquja.jpg

If I disable shading, enable blending, I get:

f51tt.jpg

If enable shading, enable blending, I get:

js1z5w.jpg

Do I need to change my blendFunc?

Advertisement

You need to render all your opaque geometry first (the solid parts of the cars), with depth write and depth read enabled (Not sure what the OpenGL flags are, I've only used DirectX).

Next, you render your transparent geometry with depth read enabled, but depth write disabled. For total correctness, you need to draw this transparent geometry from back to front. But looking at the above images, you might be able to get something reasonable without worrying about sorting it.

I want the alpha on the windows of my car(alpha=0.3, every other part of car alpha=1.0(no transparency)) to blend properly with my shader lighting.

It looks to me like when your shader lighting is enabled, it is interfering with the alpha output. I don't know where you're getting the 0.3 transparency for windows and 1.0 transparency for car from (uniform, texture or vertex colour), but I think your shader's lighting code is accidentally modifying it.

An alternative (or perhaps additional) fix is to disable blending when you're rendering the opaque part of the car. And as phil_t says, you want to render the opaque parts first. You probably also want to disable back face culling on your transparent parts (at least for this particular model)

I want the alpha on the windows of my car(alpha=0.3, every other part of car alpha=1.0(no transparency)) to blend properly with my shader lighting.

It looks to me like when your shader lighting is enabled, it is interfering with the alpha output. I don't know where you're getting the 0.3 transparency for windows and 1.0 transparency for car from (uniform, texture or vertex colour), but I think your shader's lighting code is accidentally modifying it.

An alternative (or perhaps additional) fix is to disable blending when you're rendering the opaque part of the car. And as phil_t says, you want to render the opaque parts first. You probably also want to disable back face culling on your transparent parts (at least for this particular model)

I think you guys are correct, and the windows now look somewhat-realistic, right? (forgive me for the low-poly count)

mszl2h.jpg

However, I am paying for it, because my FPS dramatically lowers (70-80%). Can I optimize this for better performance in any way, or is this the price I pay for transparent windows?

This is what I changed to get it running but with a FPS dramatically lowered, in Pseudo/OGL code:

From


for(all triangles){
    draw triangle
}

To


for(all triangles){
   
    if(triangles' alpha component IS opaque){
        draw triangle
    }else{
        glDisable(GL_BLEND);
        draw triangle
        glEnable(GL_BLEND);
    }
    draw triangle
}
Hi,

try to minimize state changes (the glEnable/glDisable calls) by sorting:
glDisable(GL_BLEND);
forall(opaque triangles t) draw(t);

glEnable(GL_BLEND);
forall(transparent triangles t) draw(t);

Hi,

try to minimize state changes (the glEnable/glDisable calls) by sorting:


glDisable(GL_BLEND);
forall(opaque triangles t) draw(t);

glEnable(GL_BLEND);
forall(transparent triangles t) draw(t);

It works and solves the lag problem, but it creates another problem, now I can't see opaque triangles when I look through the non-opaque windows :S

fw13f7.jpg

Hi,

from this image I would suspect that you first draw the transparent and then the opaque triangles.

Make sure you render all opaque triangles first, then all transparent ones. (forget about the sorting within each group for now)

Here is why: Lets assume you have two triangles in your scene, O and T, where O is opaque and T is transparent and

T is in front of O. You render T first. Then you render O.

Using default settings, the depth test performed by the rendering pipeline will reject all pixels of O since O lies behind T.

So pixels of O are not written in the framebuffer and this not blended with pixels of T.

O ok, that worked great, thanks!

335h18j.jpg

I'm really excited about this next game.

This topic is closed to new replies.

Advertisement