Blending, lighting and materials

  • Follow


I'm trying to get a translucent emissive surface. Without blending I
can get an emissive surface OK, but with blending enabled I dont get
what I expect. I start with -

GLfloat mat_amb[] = { 0.0, 0.0, 0.0, 0.0 };
GLfloat mat_diff[] = { 0.0, 0.0, 0.0, 0.0 };
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_amb);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diff);
GLfloat mat_specular[] = { 0.0, 0.0, 0.0, 0.0 };
GLfloat em[]  = {1.0, 0.0,0.0, 1.0};
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_EMISSION, em); 

to get a pure red purely emissive surface which  works fine. But if I
say

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

I see nothing, while if I alter the alpha of the diffuse part :

GLfloat mat_diff[] = { 0.0, 0.0, 0.0, 1.0 };

I can see it again (I have lighting enabled but no lights switched
on).

I cant interpret this behaviour - clearly I'm missing something.
0
Reply w 10/12/2004 9:13:36 AM

Walter Milner wrote:
> I'm trying to get a translucent emissive surface. Without blending I
> can get an emissive surface OK, but with blending enabled I dont get
> what I expect. I start with -
> 
> GLfloat mat_amb[] = { 0.0, 0.0, 0.0, 0.0 };
> GLfloat mat_diff[] = { 0.0, 0.0, 0.0, 0.0 };
> glMaterialfv(GL_FRONT, GL_AMBIENT, mat_amb);
> glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diff);
> GLfloat mat_specular[] = { 0.0, 0.0, 0.0, 0.0 };
> GLfloat em[]  = {1.0, 0.0,0.0, 1.0};
> glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
> glMaterialfv(GL_FRONT, GL_EMISSION, em); 
> 
> to get a pure red purely emissive surface which  works fine. But if I
> say
> 
> glEnable(GL_BLEND);
> glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
> 
> I see nothing, while if I alter the alpha of the diffuse part :
> 
> GLfloat mat_diff[] = { 0.0, 0.0, 0.0, 1.0 };
> 
> I can see it again (I have lighting enabled but no lights switched
> on).
> 
> I cant interpret this behaviour - clearly I'm missing something.

The OpenGL spec says that the alpha value comes from the diffuse 
material coefficient when lighting is enabled.

So -- you figured it out!

--
Andy V

0
Reply Andy 10/12/2004 10:39:10 PM


OK - experiment confirms this, and it makes sense. But I can't find
it, either in the Red Book or in the GL Spec, under GL_BLEND or
glBlendFunc or things to do with lighting. Where is it?

BTW will this pseudo-code work..

enable depth test
clear depth buffer
draw stuff, with z-buffer algorithm making near polygons obscure far
ones
enable blending
draw stuff, things on top of each other are blended
disable blending
draw stuff, again with z-buffer algorithm making near polygons obscure
far ones

ie can I sequentially turn blending on and off?
0
Reply w 10/13/2004 8:53:04 AM

Walter Milner wrote:

> OK - experiment confirms this, and it makes sense. But I can't find
> it, either in the Red Book or in the GL Spec, under GL_BLEND or
> glBlendFunc or things to do with lighting. Where is it?
> 
> BTW will this pseudo-code work..
> 
> enable depth test
> clear depth buffer
> draw stuff, with z-buffer algorithm making near polygons obscure far
> ones
> enable blending

Generally you also want to leave depth test enabled, and some would say 
to disable writing to the depth buffer, using glDepthMask(GL_FALSE). 
However, the usual blend (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) requires 
you to render in strict back-to-front order anyhow.

> draw stuff, things on top of each other are blended
> disable blending
> draw stuff, again with z-buffer algorithm making near polygons obscure
> far ones
> 
> ie can I sequentially turn blending on and off?

You can. However, if your new (opaque) geometry shows up behind a 
blended (translucent) object and in front of an old opaque object:

eye -> Translucent-object -> New-Opaque -> Old-Opaque

Then you cannot get the right result -- if you don't disable depth 
writing, New-Opaque will not be drawn. If you do disable depth writing, 
Translucent-object will be incorrectly replaced with New-Opaque.

--
Andy V

0
Reply Andy 10/14/2004 12:30:31 AM

3 Replies
205 Views

(page loaded in 4.153 seconds)

Similiar Articles:










7/9/2012 11:25:39 AM


Reply: