PDA

View Full Version : Lighting and Texture in WebGL



Phinehas
02-24-2011, 09:30 AM
Hi,

I try to create some color model with lighting effect and texture without lighting effect. However, I encounter some problems.

I use the following shader script:


<script id="shader-fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif

varying vec4 vTransformedNormal;
varying vec4 vPosition;

uniform vec3 uAmbientColor;

uniform vec3 uPointLightingLocation;
uniform vec3 uPointLightingSpecularColor;
uniform vec3 uPointLightingDiffuseColor;

varying vec4 vColor;
varying vec3 vLightWeighting;

void main(void) {
vec3 lightWeighting;
vec3 lightDirection = normalize(uPointLightingLocation - vPosition.xyz);
vec3 normal = normalize(vTransformedNormal.xyz);

float specularLightWeighting = 0.0;
float shininess = 32.0;
if (shininess < 255.0) {
vec3 eyeDirection = normalize(-vPosition.xyz);
vec3 reflectionDirection = reflect(-lightDirection, normal);

specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), shininess);
}

float diffuseLightWeighting = max(dot(normal, lightDirection), 0.0);
lightWeighting = uAmbientColor + uPointLightingSpecularColor * specularLightWeighting + uPointLightingDiffuseColor * diffuseLightWeighting;

gl_FragColor = vec4(vColor.rgb * lightWeighting, vColor.a);
}
</script>

<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec4 aVertexColor;

uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat4 uNMatrix;

varying vec4 vColor;
varying vec4 vTransformedNormal;
varying vec4 vPosition;

void main(void) {
vPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
gl_Position = uPMatrix * vPosition;
vColor = aVertexColor;

vTransformedNormal = uNMatrix * vec4(aVertexNormal, 1.0);
}
</script>

<script id="shader-fs-texture" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif

varying vec2 vTextureCoord;

uniform sampler2D uSampler;

void main(void) {
gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
}
</script>

<script id="shader-vs-texture" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;

uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;

varying vec2 vTextureCoord;


void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
vTextureCoord = aTextureCoord;
}
</script>

However, I find that if I draw the texture, the color model will be black and I see nothing in the scene. If I does not draw the texture, I can see the object. Can you help me to solve the problem? Is there any mistake in shader script?



gl.uniform3f(shaderProgram.ambientColorUniform, parseFloat(0.4), parseFloat(0.4), parseFloat(0.4));
gl.uniform3f(shaderProgram.pointLightingLocationUn iform, parseFloat(10), parseFloat(-10), parseFloat(10));
gl.uniform3f(shaderProgram.pointLightingSpecularCo lorUniform, 1, 1, 1);
gl.uniform3f(shaderProgram.pointLightingDiffuseCol orUniform, parseFloat(1), parseFloat(1), parseFloat(1));



gl.useProgram(shaderProgram);
drawAxisLine(gl, axis);

// gl.useProgram(shaderProgramTexture);
// drawAxisLetter(gl, axis);

ZbuffeR
02-25-2011, 03:22 AM
Do you check for every GL error, compile step, link step ?

Try replacing this line in your texture shader :


gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));

By something like this :


gl_FragColor = vec4(1.0,0.0,1.0,1.0);

You should see the object in bright purple.
If it is not the case, continue to go up the chain to find the cause.
It is was indeed purple, then it means the texture access has a problem.
Verify you set up the texture sampler correctly :
http://www.opengl.org/wiki/GLSL_:_common_mistakes#Binding_A_Texture
Verify that you do generate mipmaps (needed by default).