PDA

View Full Version : lighting calculations causing transparency??



bsabiston
12-07-2012, 12:10 PM
Hello,

I am just learning OpenGLES 2.0 and am having trouble with textures, blending and shading. I have a working example where I texture-map a cube. I'm working off the tutorials provided here:
http://www.raywenderlich.com/4404/opengl-es-2-0-for-iphone-tutorial-part-2-textures

At the end of that tutorial they apply a decal-like second texture map, and they enable blending so that the alpha in the decal is applied correctly.

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


However, when I do it, it does work but also it has the side effect of turning all the rest of my objects half-transparent!

I think this is happening in my vertex shader, in the calculations that create the color:

attribute vec4 position;
attribute vec3 normal;


varying lowp vec4 colorVarying;


uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;


attribute vec2 TexCoordIn;
varying vec2 TexCoordOut;


void main()
{
vec3 eyeNormal = normalize(normalMatrix * normal);
vec3 lightPosition = vec3(0.0, 0.0, 1.0);
vec4 diffuseColor = vec4(0.7, 0.7, 1.0, 1.0);

float nDotVP = max(0.0, dot(eyeNormal, normalize(lightPosition)));

colorVarying = diffuseColor * nDotVP; // this multiplication causes it?

gl_Position = modelViewProjectionMatrix * position;
TexCoordOut = TexCoordIn;


}

This produces half-transparent objects.
In my tests though, if I skip the lighting multiplication (*nDotVP) above, and I just set colorVarying = diffuseColor, then the object becomes solid again. It seems like there is something in those lighting-eye calculations which is mangling the alpha value, is that possible? Anyone know why or how I can fix it?

Thanks!
Bob

AllForum
12-12-2012, 05:25 AM
Hello,

I am a beginner, so...

In this formula : colorVarying = diffuseColor * nDotVP;
The last component (alpha) in diffuseColor is 1.0. But after calculation if it is no more 1.0 then your objects are transparent.
The alpha you want is in diffuseColor. So I would do that :
colorVarying = diffuseColor * nDotVP;
colorVarying.a = diffuseColor.a;

Here you set the alpha you want.