Skip to content
Snippets Groups Projects
Commit 095707f0 authored by jorge.josegonc's avatar jorge.josegonc
Browse files

problem directionnal and choose hourglass

parent f7b83658
Branches master
No related tags found
No related merge requests found
......@@ -56,16 +56,20 @@ var FSHADER_SOURCE =
'varying vec3 v_normal;\n' +
'varying vec4 v_VertexPosition;\n' +
'uniform vec3 u_LightPosition;\n' +
'uniform vec3 u_AmbientLight;\n' +
'uniform vec3 u_LightColor;\n' +
'uniform bool typeLight;\n' +
'uniform vec3 u_lightDirection;\n' +
'void main() {\n' +
' vec3 ambient = u_AmbientLight * v_Color.rgb;\n' +
' vec3 lightDirection = normalize(u_LightPosition - vec3(v_VertexPosition));\n' +
' float nDotL = max(dot(v_normal, lightDirection), 0.0);\n' +
' float nDotL = (typeLight) ? max(dot(v_normal, u_lightDirection), 0.0) : max(dot(v_normal, lightDirection), 0.0);\n' +
' vec3 diffuse = u_LightColor * v_Color.rgb * nDotL;\n' +
' vec3 shadowCoord = (v_PositionFromLight.xyz/v_PositionFromLight.w)/2.0 + 0.5;\n' +
' vec4 rgbaDepth = texture2D(u_ShadowMap, shadowCoord.xy);\n' +
' float depth = rgbaDepth.r;\n' + // Retrieve the z-value from R
' float visibility = (shadowCoord.z > depth + 0.005) ? 0.0 : 1.0;\n' +
' gl_FragColor = vec4(diffuse * visibility, v_Color.a);\n' +
' gl_FragColor = vec4(ambient + diffuse * visibility, v_Color.a);\n' +
'}\n';
const PERSPECTIVE_WIDTH_FROM_LIGHT = 2048
......@@ -91,6 +95,8 @@ var tmp_normalMatrix = new Matrix4();
var rotationSecFrustrumMatrix = new Matrix4();
var translateFrustrumMatrix = new Matrix4();
var isDirectionnal = false;
// Event of key down, move camera or light positions
document.addEventListener("keydown", (event) => {
switch (event.key) {
......@@ -130,6 +136,8 @@ document.addEventListener("keydown", (event) => {
case 'h':
light_z -= STEP;
break;
case 'x':
isDirectionnal = !isDirectionnal;
default:
break;
}
......@@ -172,9 +180,12 @@ function main() {
normalProgram.u_ModelMatrix = gl.getUniformLocation(normalProgram, 'u_ModelMatrix');
normalProgram.u_LightPosition = gl.getUniformLocation(normalProgram, 'u_LightPosition');
normalProgram.u_LightColor = gl.getUniformLocation(normalProgram, 'u_LightColor');
normalProgram.u_AmbientLight = gl.getUniformLocation(normalProgram, 'u_AmbientLight');
normalProgram.typeLight = gl.getUniformLocation(normalProgram, 'typeLight');
if (normalProgram.a_Position < 0 || normalProgram.a_Color < 0 || normalProgram.a_Normal < 0 || !normalProgram.u_MvpMatrix ||
!normalProgram.u_MvpMatrixFromLight || !normalProgram.u_ShadowMap || !normalProgram.u_Clicked || !normalProgram.u_NormalMatrix ||
!normalProgram.u_ModelMatrix || !normalProgram.u_LightPosition || !normalProgram.u_LightColor) {
!normalProgram.u_ModelMatrix || !normalProgram.u_LightPosition || !normalProgram.u_LightColor || !normalProgram.u_AmbientLight ||
!normalProgram.typeLight) {
console.log('Failed to get the storage location of attribute or uniform variable from normalProgram');
return;
}
......@@ -285,7 +296,12 @@ function main() {
gl.useProgram(normalProgram);
gl.uniform3f(normalProgram.u_LightColor, 1.0, 1.0, 1.0); // The light is white
gl.uniform3f(normalProgram.u_AmbientLight, 0.1, 0.1, 0.1);
gl.uniform3f(normalProgram.u_LightPosition, light_x, light_y, light_z);
gl.uniform1i(normalProgram.typeLight, isDirectionnal);
var lightD = new Vector3([light_x, light_y, light_z]);
lightD.normalize();
gl.uniform3fv(normalProgram.u_lightDirection, lightD.elements);
gl.uniform1i(normalProgram.u_ShadowMap, 0); // Pass 0 because gl.TEXTURE0 is enabled
// Draw the two frustrum and plane for the normal program
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment