Jump to content

  • Log In with Google      Sign In   
  • Create Account


Blending with alpha problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Shawn619   Members   -  Reputation: 316

Like
0Likes
Like

Posted 31 May 2013 - 12:27 AM

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?


Edited by Shawn619, 31 May 2013 - 12:28 AM.


Sponsor:

#2 phil_t   Crossbones+   -  Reputation: 2480

Like
1Likes
Like

Posted 31 May 2013 - 01:06 AM

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.



#3 C0lumbo   Crossbones+   -  Reputation: 1836

Like
1Likes
Like

Posted 31 May 2013 - 01:40 AM

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)



#4 Shawn619   Members   -  Reputation: 316

Like
0Likes
Like

Posted 31 May 2013 - 02:25 AM

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
}

Edited by Shawn619, 31 May 2013 - 02:26 AM.


#5 plainoldcj   Members   -  Reputation: 592

Like
1Likes
Like

Posted 31 May 2013 - 04:42 AM

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


#6 Shawn619   Members   -  Reputation: 316

Like
0Likes
Like

Posted 31 May 2013 - 08:36 AM

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


Edited by Shawn619, 31 May 2013 - 09:09 AM.


#7 plainoldcj   Members   -  Reputation: 592

Like
1Likes
Like

Posted 31 May 2013 - 11:47 AM

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.



#8 Shawn619   Members   -  Reputation: 316

Like
0Likes
Like

Posted 31 May 2013 - 12:08 PM

O ok, that worked great, thanks!

 

335h18j.jpg

 

I'm really excited about this next game.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS