PDA

View Full Version : blending and lighting at the same time



jmanji
11-10-2010, 06:39 PM
I want an object of my scene to become gradually translucent, when an event occurs, and then get back gradually again to opaque.
My blending funcion is:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
When the event occurs, the translucent object is rendered as:

glEnable(GL_BLEND);
glDisable(GL_LIGHTING);
glColor4f(1.0f, 1.0f, 1.0f, alphaCounter);
translucentObject.render();
glDisable(GL_BLEND);
glEnable(GL_LIGHTING);
The effect itself works fine, but I have this problem. In the beginning and end of the effect, since lighting must be disabled for blending, the object I test looks for a moment brighter. This make sense since when lighting is enabled, this object should be darker at the position I placed it, but when lighting is disabled it is rendered only according to the glColor4f values.
Is there any way to have lighting enabled when blending? Or is there any other workaround for my problem?

Alfonse Reinheart
11-10-2010, 07:39 PM
since lighting must be disabled for blending

Says who?

jmanji
11-11-2010, 03:00 AM
I thought that this was the case, at least when using this blend function. And when I do have lighting enabled, blending does not work at all.

ZbuffeR
11-11-2010, 06:42 AM
Then you are doing some else wrong.

jmanji
11-11-2010, 09:44 AM
Yes, but what might that be... Even if I put only this simple code in my render function,



glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_LIGHTING);
glClearColor(0.1f, 0.5f, 0.9f, 1.0f);

Rectangle r1 = new Rectangle(1.0f, 1.0f, 1.0f);
Rectangle r2 = new Rectangle(1.0f, 1.0f, 1.0f);

glTranslatef(0.0f, 0.0f, -1.0f);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
r1.render();

glTranslatef(0.5f, 0.0f, 0.0f);
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
r2.render();
the translucency appears only when lighting is disabled. Any ideas?

carsten neumann
11-11-2010, 03:15 PM
fixed function lighting uses material properties, set with glMaterial(). In order for the vertex colors (set by glColor4f, etc) to affect lighting you need a call to glColorMaterial().

Dark Photon
11-11-2010, 04:04 PM
fixed function lighting uses material properties, set with glMaterial(). In order for the vertex colors (set by glColor4f, etc) to affect lighting you need a call to glColorMaterial().
Right. And don't forget to glEnable( GL_COLOR_MATERIAL ) to enable the color->material behavior you select with glColorMaterial.

jmanji
11-11-2010, 04:04 PM
Thanks! I will try to use materials.