From b2270b93ef5c2d2127bb2c6a417088d5dce319ad Mon Sep 17 00:00:00 2001 From: codeanticode Date: Sat, 11 Feb 2012 02:18:24 +0000 Subject: [PATCH] Fixed a couple of bugs in the calculation of the normal matrix --- .../src/processing/core/FillShaderVertFull.glsl | 12 ++++++++---- .../core/src/processing/core/FillShaderVertLit.glsl | 4 +++- .../src/processing/core/PGraphicsAndroid3D.java | 13 ++++++++----- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/android/core/src/processing/core/FillShaderVertFull.glsl b/android/core/src/processing/core/FillShaderVertFull.glsl index a1cb921b6..3f3be51a7 100644 --- a/android/core/src/processing/core/FillShaderVertFull.glsl +++ b/android/core/src/processing/core/FillShaderVertFull.glsl @@ -73,6 +73,7 @@ float blinnPhongFactor(vec3 lightDir, vec3 lightPos, vec3 vecNormal, float shine } void main() { + // Vertex in clip coordinates gl_Position = projmodelviewMatrix * inVertex; // Vertex in eye coordinates @@ -99,8 +100,8 @@ void main() { falloff = one_float; lightDir = -lightNormal[i]; } else { - falloff = falloffFactor(lightPos, ecVertex, lightFalloffCoefficients[i]); - lightDir = lightPos - ecVertex; + falloff = falloffFactor(lightPos, ecVertex, lightFalloffCoefficients[i]); + lightDir = normalize(lightPos - ecVertex); } spotf = spotExp > zero_float ? spotFactor(lightPos, ecVertex, lightNormal[i], @@ -110,21 +111,24 @@ void main() { if (any(greaterThan(lightAmbient[i], zero_vec3))) { totalAmbient += lightAmbient[i] * falloff; } + if (any(greaterThan(lightDiffuse[i], zero_vec3))) { totalDiffuse += lightDiffuse[i] * falloff * spotf * lambertFactor(-lightDir, ecNormal); } + if (any(greaterThan(lightSpecular[i], zero_vec3))) { totalSpecular += lightSpecular[i] * falloff * spotf * blinnPhongFactor(-lightDir, lightPos, ecNormal, inShine); - } + } } + // Calculating final color as result of all lights (plus emissive term) vertColor = vec4(totalAmbient, 1) * inAmbient + vec4(totalDiffuse, 1) * inColor + vec4(totalSpecular, 1) * inSpecular + inEmissive; // Passing texture coordinates to the fragment shader - vertTexcoord = inTexcoord; + vertTexcoord = inTexcoord; } diff --git a/android/core/src/processing/core/FillShaderVertLit.glsl b/android/core/src/processing/core/FillShaderVertLit.glsl index c2dc07dba..08d4b6e01 100644 --- a/android/core/src/processing/core/FillShaderVertLit.glsl +++ b/android/core/src/processing/core/FillShaderVertLit.glsl @@ -71,6 +71,7 @@ float blinnPhongFactor(vec3 lightDir, vec3 lightPos, vec3 vecNormal, float shine } void main() { + // Vertex in clip coordinates gl_Position = projmodelviewMatrix * inVertex; // Vertex in eye coordinates @@ -97,7 +98,7 @@ void main() { falloff = one_float; lightDir = -lightNormal[i]; } else { - falloff = falloffFactor(lightPos, ecVertex, lightFalloffCoefficients[i]); + falloff = falloffFactor(lightPos, ecVertex, lightFalloffCoefficients[i]); lightDir = normalize(lightPos - ecVertex); } @@ -120,6 +121,7 @@ void main() { } } + // Calculating final color as result of all lights (plus emissive term) vertColor = vec4(totalAmbient, 1) * inAmbient + vec4(totalDiffuse, 1) * inColor + vec4(totalSpecular, 1) * inSpecular + diff --git a/android/core/src/processing/core/PGraphicsAndroid3D.java b/android/core/src/processing/core/PGraphicsAndroid3D.java index a7e988b16..a9f34372b 100644 --- a/android/core/src/processing/core/PGraphicsAndroid3D.java +++ b/android/core/src/processing/core/PGraphicsAndroid3D.java @@ -1437,6 +1437,7 @@ public class PGraphicsAndroid3D extends PGraphics { // remove any additional modelview transformation (and less likely, projection // transformations) applied by the user after setting the camera and/or projection modelview.set(camera); + modelviewInv.set(cameraInv); calcProjmodelview(); } @@ -1672,7 +1673,9 @@ public class PGraphicsAndroid3D extends PGraphics { } // The normal matrix is the transpose of the inverse of the - // modelview: + // modelview (remember that gl matrices are column-major, + // meaning that elements 0, 1, 2 are the first column, + // 3, 4, 5 the second, etc.: glNormal[0] = modelviewInv.m00; glNormal[1] = modelviewInv.m01; glNormal[2] = modelviewInv.m02; @@ -2202,7 +2205,7 @@ public class PGraphicsAndroid3D extends PGraphics { } if (flushMode == FLUSH_WHEN_FULL && !hints[DISABLE_TRANSFORM_CACHE]) { - popMatrix(); + //popMatrix(); } } @@ -7662,9 +7665,9 @@ public class PGraphicsAndroid3D extends PGraphics { fillVertices[index ] = x * mm.m20 + y * mm.m21 + z * mm.m22 + mm.m23; index = 3 * fillVertexCount; - fillNormals[index++] = nx * nm.m00 + ny * nm.m10 + nz * nm.m02; - fillNormals[index++] = nx * nm.m01 + ny * nm.m11 + nz * nm.m12; - fillNormals[index ] = nx * nm.m02 + ny * nm.m21 + nz * nm.m22; + fillNormals[index++] = nx * nm.m00 + ny * nm.m10 + nz * nm.m20; + fillNormals[index++] = nx * nm.m01 + ny * nm.m11 + nz * nm.m21; + fillNormals[index ] = nx * nm.m02 + ny * nm.m12 + nz * nm.m22; } else { index = 3 * fillVertexCount; fillVertices[index++] = x;