From c2afd241fca4d33acce4479556ef471cd201033d Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 26 May 2014 16:43:59 -0430 Subject: [PATCH] Finally fixed the shader. --- .../directionalPerPixel_frag.glsl | 21 ++++++++------- .../directionalPerPixel_vert.glsl | 26 +++++++++++-------- src/ve/ucv/ciens/ccg/nxtar/MainActivity.java | 4 +-- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/assets/shaders/directionalPerPixelSingleLight/directionalPerPixel_frag.glsl b/assets/shaders/directionalPerPixelSingleLight/directionalPerPixel_frag.glsl index 02c8fa3..1054ba2 100644 --- a/assets/shaders/directionalPerPixelSingleLight/directionalPerPixel_frag.glsl +++ b/assets/shaders/directionalPerPixelSingleLight/directionalPerPixel_frag.glsl @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #ifdef GL_ES precision mediump float; #endif @@ -33,27 +32,29 @@ varying vec3 v_normal; // Fragment shaded diffuse color. varying vec4 v_diffuse; -// Vector from the fragment to the light source. -varying vec3 v_lightVector; - // Vector from the fragment to the camera. varying vec3 v_eyeVector; // The light vector reflected around the fragment normal. varying vec3 v_reflectedVector; +// The clamped dot product between the normal and the light vector. +varying float v_nDotL; + void main(){ // Normalize the input varyings. - vec3 normal = normalize(v_normal); - vec3 lightVector = normalize(v_lightVector); - vec3 eyeVector = normalize(v_eyeVector); + vec3 normal = normalize(v_normal); + vec3 eyeVector = normalize(v_eyeVector); vec3 reflectedVector = normalize(v_reflectedVector); - // Specular Term: - vec4 specular = u_specular * pow(max(dot(reflectedVector, eyeVector), 0.0), u_shiny); + // Specular Term. + vec4 specular = vec4(0.0, 0.0, 0.0, 1.0); + if(v_nDotL > 0.0){ + specular = u_specular * pow(max(dot(reflectedVector, eyeVector), 0.0), u_shiny); + } // Aggregate light color. - vec4 finalColor = clamp(vec4(u_ambient.rgb + v_diffuse.rgb + specular.rgb, 1.0), 0.0, 1.0); + vec4 finalColor = clamp(vec4(/*u_ambient.rgb*/ + v_diffuse.rgb + specular.rgb, 1.0), 0.0, 1.0); // Final color. gl_FragColor = finalColor; diff --git a/assets/shaders/directionalPerPixelSingleLight/directionalPerPixel_vert.glsl b/assets/shaders/directionalPerPixelSingleLight/directionalPerPixel_vert.glsl index e15b749..9c5f559 100644 --- a/assets/shaders/directionalPerPixelSingleLight/directionalPerPixel_vert.glsl +++ b/assets/shaders/directionalPerPixelSingleLight/directionalPerPixel_vert.glsl @@ -13,13 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - // Model-view matrix. uniform mat4 u_projTrans; // The world space geometric transformation to apply to this vertex. uniform mat4 u_geomTrans; +// The inverse transpose of the geometric transformation matrix. +uniform mat4 u_normalMatrix; + // Light source position uniform vec3 u_lightPos; @@ -41,30 +43,32 @@ attribute vec4 a_normal; // Fragment normal. varying vec3 v_normal; -// Diffuse shaded color to pass to the fragment shader. +// Diffuse shaded color. varying vec4 v_diffuse; -// The vector from the vertex to the light source. -varying vec3 v_lightVector; - // The vector from the vertex to the camera. varying vec3 v_eyeVector; // The light vector reflected around the vertex normal. varying vec3 v_reflectedVector; +// The clamped dot product between the normal and the light vector. +varying float v_nDotL; + void main(){ - // Apply the geometric transformation to the original position of the vertex. vec4 transformedPosition = u_geomTrans * a_position; + vec3 lightVector = normalize(u_lightPos.xyz); + vec3 invLightVector = normalize(-u_lightPos.xyz); // Set the varyings. - v_normal = normalize(a_normal.xyz); - v_lightVector = normalize(transformedPosition.xyz - u_lightPos.xyz); - v_eyeVector = normalize(u_cameraPos.xyz - transformedPosition.xyz); - v_reflectedVector = normalize(reflect(-v_lightVector, a_normal.xyz)); + v_normal = normalize(vec4(u_normalMatrix * a_normal).xyz); + v_eyeVector = normalize(u_cameraPos.xyz - transformedPosition.xyz); + v_reflectedVector = normalize(reflect(-lightVector, v_normal)); // Diffuse Term. - v_diffuse = u_lightDiffuse * u_materialDiffuse * max(dot(a_normal.xyz, v_lightVector), 0.0); + float invNDotL = max(dot(v_normal.xyz, invLightVector), 0.0); + v_nDotL = max(dot(v_normal.xyz, lightVector), 0.0); + v_diffuse = (u_lightDiffuse * u_materialDiffuse * v_nDotL) + (vec4(0.1, 0.1, 0.2, 1.0) * u_materialDiffuse * invNDotL); gl_Position = u_projTrans * transformedPosition; } diff --git a/src/ve/ucv/ciens/ccg/nxtar/MainActivity.java b/src/ve/ucv/ciens/ccg/nxtar/MainActivity.java index cc753e0..aea4194 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/MainActivity.java +++ b/src/ve/ucv/ciens/ccg/nxtar/MainActivity.java @@ -24,7 +24,7 @@ import org.opencv.android.Utils; import org.opencv.core.Mat; import org.opencv.imgproc.Imgproc; -import ve.ucv.ciens.ccg.nxtar.interfaces.AndroidFunctionalityWrapper; +import ve.ucv.ciens.ccg.nxtar.interfaces.ActionResolver; import ve.ucv.ciens.ccg.nxtar.interfaces.ImageProcessor; import ve.ucv.ciens.ccg.nxtar.utils.ProjectConstants; import android.content.Context; @@ -51,7 +51,7 @@ import com.badlogic.gdx.math.Vector3; *

Provides operating system services to the LibGDX platform * independant code, and handles OpenCV initialization and api calls.

*/ -public class MainActivity extends AndroidApplication implements AndroidFunctionalityWrapper, ImageProcessor{ +public class MainActivity extends AndroidApplication implements ActionResolver, ImageProcessor{ /** * Tag used for logging. */