From bfd00f1b15b4d0a3371ac9da2d6bbd19ba4186a0 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Fri, 23 May 2014 10:57:14 -0700 Subject: [PATCH] Use local viewer model for specular. --- interface/resources/shaders/model.frag | 5 ++++- interface/resources/shaders/model.vert | 6 ++++++ interface/resources/shaders/model_normal_map.frag | 7 +++++-- interface/resources/shaders/model_normal_map.vert | 6 +++++- .../resources/shaders/model_normal_specular_map.frag | 7 +++++-- interface/resources/shaders/model_specular_map.frag | 5 ++++- interface/resources/shaders/skin_model.vert | 9 ++++++--- .../resources/shaders/skin_model_normal_map.vert | 11 +++++++---- 8 files changed, 42 insertions(+), 14 deletions(-) diff --git a/interface/resources/shaders/model.frag b/interface/resources/shaders/model.frag index a9d93f2f6a..488736abf9 100644 --- a/interface/resources/shaders/model.frag +++ b/interface/resources/shaders/model.frag @@ -14,6 +14,9 @@ // the diffuse texture uniform sampler2D diffuseMap; +// the interpolated position +varying vec4 position; + // the interpolated normal varying vec4 normal; @@ -26,7 +29,7 @@ void main(void) { gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); // compute the specular component (sans exponent) - float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position + vec4(0.0, 0.0, 1.0, 0.0)), + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - normalize(vec4(position.xyz, 0.0))), normalizedNormal)); // modulate texture by base color and add specular contribution diff --git a/interface/resources/shaders/model.vert b/interface/resources/shaders/model.vert index 904e3c2a8b..f78ed5045b 100644 --- a/interface/resources/shaders/model.vert +++ b/interface/resources/shaders/model.vert @@ -11,6 +11,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// the interpolated position +varying vec4 position; + // the interpolated normal varying vec4 normal; @@ -19,6 +22,9 @@ void main(void) { // transform and store the normal for interpolation normal = normalize(gl_ModelViewMatrix * vec4(gl_Normal, 0.0)); + // likewise with the position + position = gl_ModelViewMatrix * gl_Vertex; + // pass along the vertex color gl_FrontColor = gl_Color; diff --git a/interface/resources/shaders/model_normal_map.frag b/interface/resources/shaders/model_normal_map.frag index 392be1f1cf..8444f2d6ea 100644 --- a/interface/resources/shaders/model_normal_map.frag +++ b/interface/resources/shaders/model_normal_map.frag @@ -17,6 +17,9 @@ uniform sampler2D diffuseMap; // the normal map texture uniform sampler2D normalMap; +// the interpolated position +varying vec4 interpolatedPosition; + // the interpolated normal varying vec4 interpolatedNormal; @@ -38,8 +41,8 @@ void main(void) { gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); // compute the specular component (sans exponent) - float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position + vec4(0.0, 0.0, 1.0, 0.0)), - viewNormal)); + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - + normalize(vec4(vec3(interpolatedPosition), 0.0))), viewNormal)); // modulate texture by base color and add specular contribution gl_FragColor = base * texture2D(diffuseMap, gl_TexCoord[0].st) + diff --git a/interface/resources/shaders/model_normal_map.vert b/interface/resources/shaders/model_normal_map.vert index 3607501acf..b013a0a736 100644 --- a/interface/resources/shaders/model_normal_map.vert +++ b/interface/resources/shaders/model_normal_map.vert @@ -14,6 +14,9 @@ // the tangent vector attribute vec3 tangent; +// the interpolated position +varying vec4 interpolatedPosition; + // the interpolated normal varying vec4 interpolatedNormal; @@ -22,7 +25,8 @@ varying vec4 interpolatedTangent; void main(void) { - // transform and store the normal and tangent for interpolation + // transform and store the position, normal and tangent for interpolation + interpolatedPosition = gl_ModelViewMatrix * gl_Vertex; interpolatedNormal = gl_ModelViewMatrix * vec4(gl_Normal, 0.0); interpolatedTangent = gl_ModelViewMatrix * vec4(tangent, 0.0); diff --git a/interface/resources/shaders/model_normal_specular_map.frag b/interface/resources/shaders/model_normal_specular_map.frag index dbbb343c62..357677d82a 100644 --- a/interface/resources/shaders/model_normal_specular_map.frag +++ b/interface/resources/shaders/model_normal_specular_map.frag @@ -20,6 +20,9 @@ uniform sampler2D normalMap; // the specular map texture uniform sampler2D specularMap; +// the interpolated position +varying vec4 interpolatedPosition; + // the interpolated normal varying vec4 interpolatedNormal; @@ -41,8 +44,8 @@ void main(void) { gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); // compute the specular component (sans exponent) - float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position + vec4(0.0, 0.0, 1.0, 0.0)), - viewNormal)); + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - + normalize(vec4(interpolatedPosition.xyz, 0.0))), viewNormal)); // modulate texture by base color and add specular contribution gl_FragColor = base * texture2D(diffuseMap, gl_TexCoord[0].st) + vec4(pow(specular, gl_FrontMaterial.shininess) * diff --git a/interface/resources/shaders/model_specular_map.frag b/interface/resources/shaders/model_specular_map.frag index b955b5cfa6..a07324cd1b 100644 --- a/interface/resources/shaders/model_specular_map.frag +++ b/interface/resources/shaders/model_specular_map.frag @@ -17,6 +17,9 @@ uniform sampler2D diffuseMap; // the specular texture uniform sampler2D specularMap; +// the interpolated position in view space +varying vec4 position; + // the interpolated normal varying vec4 normal; @@ -29,7 +32,7 @@ void main(void) { gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); // compute the specular component (sans exponent) - float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position + vec4(0.0, 0.0, 1.0, 0.0)), + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - normalize(vec4(position.xyz, 0.0))), normalizedNormal)); // modulate texture by base color and add specular contribution diff --git a/interface/resources/shaders/skin_model.vert b/interface/resources/shaders/skin_model.vert index 12320ba9a9..f743609dc3 100644 --- a/interface/resources/shaders/skin_model.vert +++ b/interface/resources/shaders/skin_model.vert @@ -19,11 +19,14 @@ uniform mat4 clusterMatrices[MAX_CLUSTERS]; attribute vec4 clusterIndices; attribute vec4 clusterWeights; +// the interpolated position +varying vec4 position; + // the interpolated normal varying vec4 normal; void main(void) { - vec4 position = vec4(0.0, 0.0, 0.0, 0.0); + position = vec4(0.0, 0.0, 0.0, 0.0); normal = vec4(0.0, 0.0, 0.0, 0.0); for (int i = 0; i < INDICES_PER_VERTEX; i++) { mat4 clusterMatrix = clusterMatrices[int(clusterIndices[i])]; @@ -31,7 +34,7 @@ void main(void) { position += clusterMatrix * gl_Vertex * clusterWeight; normal += clusterMatrix * vec4(gl_Normal, 0.0) * clusterWeight; } - position = gl_ModelViewProjectionMatrix * position; + position = gl_ModelViewMatrix * position; normal = normalize(gl_ModelViewMatrix * normal); // pass along the vertex color @@ -40,5 +43,5 @@ void main(void) { // and the texture coordinates gl_TexCoord[0] = gl_MultiTexCoord0; - gl_Position = position; + gl_Position = gl_ProjectionMatrix * position; } diff --git a/interface/resources/shaders/skin_model_normal_map.vert b/interface/resources/shaders/skin_model_normal_map.vert index 872cbe2fc3..5dbc32626a 100644 --- a/interface/resources/shaders/skin_model_normal_map.vert +++ b/interface/resources/shaders/skin_model_normal_map.vert @@ -22,6 +22,9 @@ attribute vec3 tangent; attribute vec4 clusterIndices; attribute vec4 clusterWeights; +// the interpolated position +varying vec4 interpolatedPosition; + // the interpolated normal varying vec4 interpolatedNormal; @@ -29,17 +32,17 @@ varying vec4 interpolatedNormal; varying vec4 interpolatedTangent; void main(void) { - vec4 position = vec4(0.0, 0.0, 0.0, 0.0); + interpolatedPosition = vec4(0.0, 0.0, 0.0, 0.0); interpolatedNormal = vec4(0.0, 0.0, 0.0, 0.0); interpolatedTangent = vec4(0.0, 0.0, 0.0, 0.0); for (int i = 0; i < INDICES_PER_VERTEX; i++) { mat4 clusterMatrix = clusterMatrices[int(clusterIndices[i])]; float clusterWeight = clusterWeights[i]; - position += clusterMatrix * gl_Vertex * clusterWeight; + interpolatedPosition += clusterMatrix * gl_Vertex * clusterWeight; interpolatedNormal += clusterMatrix * vec4(gl_Normal, 0.0) * clusterWeight; interpolatedTangent += clusterMatrix * vec4(tangent, 0.0) * clusterWeight; } - position = gl_ModelViewProjectionMatrix * position; + interpolatedPosition = gl_ModelViewMatrix * interpolatedPosition; interpolatedNormal = gl_ModelViewMatrix * interpolatedNormal; interpolatedTangent = gl_ModelViewMatrix * interpolatedTangent; @@ -49,5 +52,5 @@ void main(void) { // and the texture coordinates gl_TexCoord[0] = gl_MultiTexCoord0; - gl_Position = position; + gl_Position = gl_ProjectionMatrix * interpolatedPosition; }