summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Ottens <kevin.ottens@kdab.com>2017-08-02 10:25:02 +0200
committerSean Harmer <sean.harmer@kdab.com>2017-09-22 11:21:08 +0000
commite67119b71d1b63c5710100349fa634113e93ada2 (patch)
tree4ada3a09ddfb38152c20ed7d16b062bc910ec234
parent11b804f9cd70d0abe4ca6168c6fc7aaddf14b82c (diff)
Get rid of adsModelNormalMapped
All materials and examples are ported away from it. It was really not related to normal mapping at all, somehow it was an implementation of adsModel working in tangent space instead of world space. Now we got it all in world space, just like for the metal/rough implementation. Change-Id: I3346277ce9b91328d70d914b319ac25f947fff0e Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
-rw-r--r--examples/qt3d/advancedcustommaterial/shaders/es2/water.frag12
-rw-r--r--examples/qt3d/advancedcustommaterial/shaders/es2/water.vert25
-rw-r--r--examples/qt3d/advancedcustommaterial/shaders/gl3/water.frag14
-rw-r--r--examples/qt3d/advancedcustommaterial/shaders/gl3/water.vert25
-rw-r--r--src/extras/extras.qrc1
-rw-r--r--src/extras/shaders/es2/coordinatesystems.inc85
-rw-r--r--src/extras/shaders/es2/light.inc.frag72
-rw-r--r--src/extras/shaders/es2/light.inc.frag10075
-rw-r--r--src/extras/shaders/es2/normaldiffusemap.frag13
-rw-r--r--src/extras/shaders/es2/normaldiffusemap.vert8
-rw-r--r--src/extras/shaders/es2/normaldiffusemapalpha.frag13
-rw-r--r--src/extras/shaders/es2/normaldiffusespecularmap.frag13
-rw-r--r--src/extras/shaders/gl3/light.inc.frag73
-rw-r--r--src/extras/shaders/gl3/normaldiffusemap.frag13
-rw-r--r--src/extras/shaders/gl3/normaldiffusemap.vert27
-rw-r--r--src/extras/shaders/gl3/normaldiffusemapalpha.frag13
-rw-r--r--src/extras/shaders/gl3/normaldiffusespecularmap.frag15
17 files changed, 189 insertions, 308 deletions
diff --git a/examples/qt3d/advancedcustommaterial/shaders/es2/water.frag b/examples/qt3d/advancedcustommaterial/shaders/es2/water.frag
index ea54a87f1..4a417ab34 100644
--- a/examples/qt3d/advancedcustommaterial/shaders/es2/water.frag
+++ b/examples/qt3d/advancedcustommaterial/shaders/es2/water.frag
@@ -1,6 +1,8 @@
#define FP highp
varying FP vec3 worldPosition;
+varying FP vec3 worldNormal;
+varying FP vec4 worldTangent;
varying FP vec2 texCoord;
varying FP vec2 waveTexCoord;
varying FP vec2 movtexCoord;
@@ -9,7 +11,6 @@ varying FP vec2 skyTexCoord;
varying FP vec3 vpos;
-varying FP mat3 tangentMatrix;
varying FP vec3 color;
uniform FP sampler2D diffuseTexture;
@@ -30,6 +31,7 @@ uniform FP float normalAmount;
uniform FP vec3 eyePosition;
#pragma include light.inc.frag
+#pragma include coordinatesystems.inc
void main()
{
@@ -50,16 +52,20 @@ void main()
// 2 Animated Layers of specularTexture mixed with the newCoord
FP vec4 specularTextureColor = texture2D( specularTexture, multexCoord+newCoord) + (texture2D( specularTexture, movtexCoord+newCoord ));
// 2 Animated Layers of normalTexture mixed with the newCoord
- FP vec3 normal = normalAmount * texture2D( normalTexture, movtexCoord+newCoord ).rgb - vec3( 1.0 )+(normalAmount * texture2D( normalTexture, multexCoord+newCoord ).rgb - vec3( 1.0 ));
+ FP vec3 tNormal = normalAmount * texture2D( normalTexture, movtexCoord+newCoord ).rgb - vec3( 1.0 )+(normalAmount * texture2D( normalTexture, multexCoord+newCoord ).rgb - vec3( 1.0 ));
// Animated skyTexture layer
FP vec4 skycolor = texture2D(skyTexture, skyTexCoord);
skycolor = skycolor * 0.4;
//Animated foamTexture layer
FP vec4 foamTextureColor = texture2D(foamTexture, texCoord);
+ FP mat3 tangentMatrix = calcWorldSpaceToTangentSpaceMatrix(worldNormal, worldTangent);
+ FP mat3 invertTangentMatrix = transpose(tangentMatrix);
+ FP vec3 wNormal = normalize(invertTangentMatrix * tNormal);
+
// Calculate the lighting model, keeping the specular component separate
FP vec3 diffuseColor, specularColor;
- adsModelNormalMapped(worldPosition, normal, eyePosition, shininess, tangentMatrix, diffuseColor, specularColor);
+ adsModel(worldPosition, wNormal, eyePosition, shininess, diffuseColor, specularColor);
// Combine final fragment color
FP vec4 outputColor = vec4(((skycolor.rgb + ka + diffuseTextureColor.rgb * (diffuseColor))+(specularColor * specularTextureColor.a*specularity)), vpos.y );
diff --git a/examples/qt3d/advancedcustommaterial/shaders/es2/water.vert b/examples/qt3d/advancedcustommaterial/shaders/es2/water.vert
index 1a7d46a46..76dd3f8c0 100644
--- a/examples/qt3d/advancedcustommaterial/shaders/es2/water.vert
+++ b/examples/qt3d/advancedcustommaterial/shaders/es2/water.vert
@@ -6,12 +6,13 @@ attribute FP vec2 vertexTexCoord;
attribute FP vec4 vertexTangent;
varying FP vec3 worldPosition;
+varying FP vec3 worldNormal;
+varying FP vec4 worldTangent;
varying FP vec2 texCoord;
varying FP vec2 movtexCoord;
varying FP vec2 multexCoord;
varying FP vec2 waveTexCoord;
varying FP vec2 skyTexCoord;
-varying FP mat3 tangentMatrix;
varying FP vec3 vpos;
uniform FP mat4 modelMatrix;
@@ -42,25 +43,9 @@ void main()
// Transform position, normal, and tangent to world coords
worldPosition = vec3(modelMatrix * vec4(vertexPosition, 1.0));
- FP vec3 normal = normalize(modelNormalMatrix * vertexNormal);
- FP vec3 tangent = normalize(vec3(modelMatrix * vec4(vertexTangent.xyz, 0.0)));
-
- // Make the tangent truly orthogonal to the normal by using Gram-Schmidt.
- // This allows to build the tangentMatrix below by simply transposing the
- // tangent -> world space matrix (which would now be orthogonal)
- tangent = normalize(tangent - dot(tangent, normal) * normal);
-
- // Calculate binormal vector. No "real" need to renormalize it,
- // as built by crossing two normal vectors.
- // To orient the binormal correctly, use the fourth coordinate of the tangent,
- // which is +1 for a right hand system, and -1 for a left hand system.
- FP vec3 binormal = cross(normal, tangent) * vertexTangent.w;
-
- // Construct matrix to transform from eye coords to tangent space
- tangentMatrix = mat3(
- tangent.x, binormal.x, normal.x,
- tangent.y, binormal.y, normal.y,
- tangent.z, binormal.z, normal.z);
+ worldNormal = normalize(modelNormalMatrix * vertexNormal);
+ worldTangent.xyz = normalize(vec3(modelMatrix * vec4(vertexTangent.xyz, 0.0)));
+ worldTangent.w = vertexTangent.w;
// Calculate animated vertex positions
diff --git a/examples/qt3d/advancedcustommaterial/shaders/gl3/water.frag b/examples/qt3d/advancedcustommaterial/shaders/gl3/water.frag
index 9657cc63a..de6e87281 100644
--- a/examples/qt3d/advancedcustommaterial/shaders/gl3/water.frag
+++ b/examples/qt3d/advancedcustommaterial/shaders/gl3/water.frag
@@ -1,6 +1,8 @@
#version 150 core
in vec3 worldPosition;
+in vec3 worldNormal;
+in vec4 worldTangent;
in vec2 texCoord;
in vec2 waveTexCoord;
in vec2 movtexCoord;
@@ -9,7 +11,6 @@ in vec2 skyTexCoord;
in vec3 vpos;
-in mat3 tangentMatrix;
in vec3 color;
uniform sampler2D diffuseTexture;
@@ -32,6 +33,7 @@ uniform vec3 eyePosition;
out vec4 fragColor;
#pragma include light.inc.frag
+#pragma include coordinatesystems.inc
void main()
{
@@ -52,16 +54,22 @@ void main()
// 2 Animated Layers of specularTexture mixed with the newCoord
vec4 specularTextureColor = texture( specularTexture, multexCoord+newCoord) + (texture( specularTexture, movtexCoord+newCoord ));
// 2 Animated Layers of normalTexture mixed with the newCoord
- vec3 normal = normalAmount * texture( normalTexture, movtexCoord+newCoord ).rgb - vec3( 1.0 )+(normalAmount * texture( normalTexture, multexCoord+newCoord ).rgb - vec3( 1.0 ));
+ vec3 tNormal = normalAmount * texture( normalTexture, movtexCoord+newCoord ).rgb - vec3( 1.0 )+(normalAmount * texture( normalTexture, multexCoord+newCoord ).rgb - vec3( 1.0 ));
// Animated skyTexture layer
vec4 skycolor = texture(skyTexture, skyTexCoord);
skycolor = skycolor * 0.4;
//Animated foamTexture layer
vec4 foamTextureColor = texture(foamTexture, texCoord);
+ mat3 tangentMatrix = calcWorldSpaceToTangentSpaceMatrix(worldNormal, worldTangent);
+ mat3 invertTangentMatrix = transpose(tangentMatrix);
+
+ vec3 wNormal = normalize(invertTangentMatrix * tNormal);
+
+
// Calculate the lighting model, keeping the specular component separate
vec3 diffuseColor, specularColor;
- adsModelNormalMapped(worldPosition, normal, eyePosition, shininess, tangentMatrix, diffuseColor, specularColor);
+ adsModel(worldPosition, wNormal, eyePosition, shininess, diffuseColor, specularColor);
// Combine final fragment color
vec4 outputColor = vec4(((skycolor.rgb + ka + diffuseTextureColor.rgb * (diffuseColor))+(specularColor * specularTextureColor.a*specularity)), vpos.y );
diff --git a/examples/qt3d/advancedcustommaterial/shaders/gl3/water.vert b/examples/qt3d/advancedcustommaterial/shaders/gl3/water.vert
index a4ddcf62b..3af37e9a5 100644
--- a/examples/qt3d/advancedcustommaterial/shaders/gl3/water.vert
+++ b/examples/qt3d/advancedcustommaterial/shaders/gl3/water.vert
@@ -6,12 +6,13 @@ in vec2 vertexTexCoord;
in vec4 vertexTangent;
out vec3 worldPosition;
+out vec3 worldNormal;
+out vec4 worldTangent;
out vec2 texCoord;
out vec2 movtexCoord;
out vec2 multexCoord;
out vec2 waveTexCoord;
out vec2 skyTexCoord;
-out mat3 tangentMatrix;
out vec3 vpos;
uniform mat4 modelMatrix;
@@ -42,25 +43,9 @@ void main()
// Transform position, normal, and tangent to world coords
worldPosition = vec3(modelMatrix * vec4(vertexPosition, 1.0));
- vec3 normal = normalize(modelNormalMatrix * vertexNormal);
- vec3 tangent = normalize(vec3(modelMatrix * vec4(vertexTangent.xyz, 0.0)));
-
- // Make the tangent truly orthogonal to the normal by using Gram-Schmidt.
- // This allows to build the tangentMatrix below by simply transposing the
- // tangent -> world space matrix (which would now be orthogonal)
- tangent = normalize(tangent - dot(tangent, normal) * normal);
-
- // Calculate binormal vector. No "real" need to renormalize it,
- // as built by crossing two normal vectors.
- // To orient the binormal correctly, use the fourth coordinate of the tangent,
- // which is +1 for a right hand system, and -1 for a left hand system.
- vec3 binormal = cross(normal, tangent) * vertexTangent.w;
-
- // Construct matrix to transform from eye coords to tangent space
- tangentMatrix = mat3(
- tangent.x, binormal.x, normal.x,
- tangent.y, binormal.y, normal.y,
- tangent.z, binormal.z, normal.z);
+ worldNormal = normalize(modelNormalMatrix * vertexNormal);
+ worldTangent.xyz = normalize(vec3(modelMatrix * vec4(vertexTangent.xyz, 0.0)));
+ worldTangent.w = vertexTangent.w;
// Calculate animated vertex positions
diff --git a/src/extras/extras.qrc b/src/extras/extras.qrc
index ce959af93..6a51cfea6 100644
--- a/src/extras/extras.qrc
+++ b/src/extras/extras.qrc
@@ -6,6 +6,7 @@
<file>shaders/es2/light.inc.frag100</file>
<file>shaders/gl3/metalrough.inc.frag</file>
<file>shaders/gl3/coordinatesystems.inc</file>
+ <file>shaders/es2/coordinatesystems.inc</file>
<file>shaders/gl3/phong.vert</file>
<file>shaders/gl3/phong.frag</file>
<file>shaders/es2/phong.vert</file>
diff --git a/src/extras/shaders/es2/coordinatesystems.inc b/src/extras/shaders/es2/coordinatesystems.inc
new file mode 100644
index 000000000..fad0040c7
--- /dev/null
+++ b/src/extras/shaders/es2/coordinatesystems.inc
@@ -0,0 +1,85 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt3D module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+FP mat3 transpose(const in FP mat3 inputMatrix)
+{
+ FP vec3 i0 = inputMatrix[0];
+ FP vec3 i1 = inputMatrix[1];
+ FP vec3 i2 = inputMatrix[2];
+
+ FP mat3 outputMatrix = mat3(
+ vec3(i0.x, i1.x, i2.x),
+ vec3(i0.y, i1.y, i2.y),
+ vec3(i0.z, i1.z, i2.z)
+ );
+
+ return outputMatrix;
+}
+
+FP mat3 calcWorldSpaceToTangentSpaceMatrix(const in FP vec3 wNormal, const in FP vec4 wTangent)
+{
+ // Make the tangent truly orthogonal to the normal by using Gram-Schmidt.
+ // This allows to build the tangentMatrix below by simply transposing the
+ // tangent -> eyespace matrix (which would now be orthogonal)
+ FP vec3 wFixedTangent = normalize(wTangent.xyz - dot(wTangent.xyz, wNormal) * wNormal);
+
+ // Calculate binormal vector. No "real" need to renormalize it,
+ // as built by crossing two normal vectors.
+ // To orient the binormal correctly, use the fourth coordinate of the tangent,
+ // which is +1 for a right hand system, and -1 for a left hand system.
+ FP vec3 wBinormal = cross(wNormal, wFixedTangent.xyz) * wTangent.w;
+
+ // Construct matrix to transform from world space to tangent space
+ // This is the transpose of the tangentToWorld transformation matrix
+ FP mat3 tangentToWorldMatrix = mat3(wFixedTangent, wBinormal, wNormal);
+ FP mat3 worldToTangentMatrix = transpose(tangentToWorldMatrix);
+ return worldToTangentMatrix;
+}
+
diff --git a/src/extras/shaders/es2/light.inc.frag b/src/extras/shaders/es2/light.inc.frag
index 57a30036f..aec04139d 100644
--- a/src/extras/shaders/es2/light.inc.frag
+++ b/src/extras/shaders/es2/light.inc.frag
@@ -14,78 +14,6 @@ struct Light {
uniform Light lights[MAX_LIGHTS];
uniform int lightCount;
-void adsModelNormalMapped(const in FP vec3 vpos, const in FP vec3 vnormal, const in FP vec3 eye, const in FP float shininess,
- const in FP mat3 tangentMatrix,
- out FP vec3 diffuseColor, out FP vec3 specularColor)
-{
- diffuseColor = vec3(0.0);
- specularColor = vec3(0.0);
-
- FP vec3 snormal = normalize( vec3( tangentMatrix[0][2], tangentMatrix[1][2], tangentMatrix[2][2] ) );
-
- FP vec3 n = normalize( vnormal );
-
- FP vec3 s, ts;
- Light light;
- for (int i = 0; i < MAX_LIGHTS; ++i) {
- if (i >= lightCount) // Make brcm happy with the for loop
- break;
- if (i == 0)
- light = lights[0];
- else if (i == 1)
- light = lights[1];
- else if (i == 2)
- light = lights[2];
- else if (i == 3)
- light = lights[3];
- else if (i == 4)
- light = lights[4];
- else if (i == 5)
- light = lights[5];
- else if (i == 6)
- light = lights[6];
- else if (i == 7)
- light = lights[7];
-
- FP float att = 1.0;
- if ( light.type != TYPE_DIRECTIONAL ) {
- s = light.position - vpos;
- if ( dot(snormal, s) < 0.0 )
- att = 0.0;
- else {
- ts = normalize( tangentMatrix * s );
- if (length( light.attenuation ) != 0.0) {
- FP float dist = length(s);
- att = 1.0 / (light.attenuation.x + light.attenuation.y * dist + light.attenuation.z * dist * dist);
- }
- s = normalize( s );
- if ( light.type == TYPE_SPOT ) {
- if ( degrees(acos(dot(-s, normalize(light.direction))) ) > light.cutOffAngle)
- att = 0.0;
- }
- }
- } else {
- if ( dot(snormal, -light.direction) > 0.0 )
- s = normalize( tangentMatrix * -light.direction );
- else
- att = 0.0;
- }
-
- FP float diffuse = max( dot( ts, n ), 0.0 );
-
- FP float specular = 0.0;
- if (diffuse > 0.0 && shininess > 0.0 && att > 0.0) {
- FP vec3 r = reflect( -ts, n );
- FP vec3 v = normalize( tangentMatrix * ( eye - vpos ) );
- FP float normFactor = ( shininess + 2.0 ) / 2.0;
- specular = normFactor * pow( max( dot( r, v ), 0.0 ), shininess );
- }
-
- diffuseColor += att * light.intensity * diffuse * light.color;
- specularColor += att * light.intensity * specular * light.color;
- }
-}
-
void adsModel(const in FP vec3 vpos, const in FP vec3 vnormal, const in FP vec3 eye, const in FP float shininess,
out FP vec3 diffuseColor, out FP vec3 specularColor)
{
diff --git a/src/extras/shaders/es2/light.inc.frag100 b/src/extras/shaders/es2/light.inc.frag100
index b4988ad82..6c6f6a259 100644
--- a/src/extras/shaders/es2/light.inc.frag100
+++ b/src/extras/shaders/es2/light.inc.frag100
@@ -14,81 +14,6 @@ struct Light {
uniform Light lights[MAX_LIGHTS];
uniform int lightCount;
-void adsModelNormalMapped(const in FP vec3 vpos, const in FP vec3 vnormal, const in FP vec3 eye, const in FP float shininess,
- const in FP mat3 tangentMatrix,
- out FP vec3 diffuseColor, out FP vec3 specularColor)
-{
- diffuseColor = vec3(0.0);
- specularColor = vec3(0.0);
-
- FP vec3 n = normalize( vnormal );
-
- // 0
- if (lightCount < 1)
- return;
- FP vec3 s;
- FP float att = 1.0;
- if ( lights[0].type != TYPE_DIRECTIONAL ) {
- s = tangentMatrix * ( lights[0].position - vpos );
- if (length( lights[0].attenuation ) != 0.0) {
- FP float dist = length(s);
- att = 1.0 / (lights[0].attenuation.x + lights[0].attenuation.y * dist + lights[0].attenuation.z * dist * dist);
- }
- s = normalize( s );
- if ( lights[0].type == TYPE_SPOT ) {
- if ( degrees(acos(dot(-s, normalize(lights[0].direction))) ) > lights[0].cutOffAngle)
- att = 0.0;
- }
- } else {
- s = normalize( tangentMatrix * -lights[0].direction );
- }
-
- FP float diffuse = max( dot( s, n ), 0.0 );
-
- FP float specular = 0.0;
- if (diffuse > 0.0 && shininess > 0.0 && att > 0.0) {
- FP vec3 r = reflect( -s, n );
- FP vec3 v = normalize( tangentMatrix * ( eye - vpos ) );
- FP float normFactor = ( shininess + 2.0 ) / 2.0;
- specular = normFactor * pow( max( dot( r, v ), 0.0 ), shininess );
- }
-
- diffuseColor += att * lights[0].intensity * diffuse * lights[0].color;
- specularColor += att * specular;
-
- // 1
- if (lightCount < 2)
- return;
- att = 1.0;
- if ( lights[1].type != TYPE_DIRECTIONAL ) {
- s = tangentMatrix * ( lights[1].position - vpos );
- if (length( lights[1].attenuation ) != 0.0) {
- FP float dist = length(s);
- att = 1.0 / (lights[1].attenuation.x + lights[1].attenuation.y * dist + lights[1].attenuation.z * dist * dist);
- }
- s = normalize( s );
- if ( lights[1].type == TYPE_SPOT ) {
- if ( degrees(acos(dot(-s, normalize(lights[1].direction))) ) > lights[1].cutOffAngle)
- att = 0.0;
- }
- } else {
- s = normalize( tangentMatrix * -lights[1].direction );
- }
-
- diffuse = max( dot( s, n ), 0.0 );
-
- specular = 0.0;
- if (diffuse > 0.0 && shininess > 0.0 && att > 0.0) {
- FP vec3 r = reflect( -s, n );
- FP vec3 v = normalize( tangentMatrix * ( eye - vpos ) );
- FP float normFactor = ( shininess + 2.0 ) / 2.0;
- specular = normFactor * pow( max( dot( r, v ), 0.0 ), shininess );
- }
-
- diffuseColor += att * lights[1].intensity * diffuse * lights[1].color;
- specularColor += att * specular;
-}
-
void adsModel(const in FP vec3 vpos, const in FP vec3 vnormal, const in FP vec3 eye, const in FP float shininess,
out FP vec3 diffuseColor, out FP vec3 specularColor)
{
diff --git a/src/extras/shaders/es2/normaldiffusemap.frag b/src/extras/shaders/es2/normaldiffusemap.frag
index c69aa8b81..e1f19b81a 100644
--- a/src/extras/shaders/es2/normaldiffusemap.frag
+++ b/src/extras/shaders/es2/normaldiffusemap.frag
@@ -1,8 +1,9 @@
#define FP highp
varying FP vec3 worldPosition;
+varying FP vec3 worldNormal;
+varying FP vec4 worldTangent;
varying FP vec2 texCoord;
-varying FP mat3 tangentMatrix;
uniform sampler2D diffuseTexture;
uniform sampler2D normalTexture;
@@ -15,16 +16,22 @@ uniform FP float shininess; // Specular shininess factor
uniform FP vec3 eyePosition;
#pragma include light.inc.frag
+#pragma include coordinatesystems.inc
void main()
{
// Sample the textures at the interpolated texCoords
FP vec4 diffuseTextureColor = texture2D( diffuseTexture, texCoord );
- FP vec3 normal = 2.0 * texture2D( normalTexture, texCoord ).rgb - vec3( 1.0 );
+ FP vec3 tNormal = 2.0 * texture2D( normalTexture, texCoord ).rgb - vec3( 1.0 );
+
+ FP mat3 tangentMatrix = calcWorldSpaceToTangentSpaceMatrix(worldNormal, worldTangent);
+ FP mat3 invertTangentMatrix = transpose(tangentMatrix);
+
+ FP vec3 wNormal = normalize(invertTangentMatrix * tNormal);
// Calculate the lighting model, keeping the specular component separate
FP vec3 diffuseColor, specularColor;
- adsModelNormalMapped(worldPosition, normal, eyePosition, shininess, tangentMatrix, diffuseColor, specularColor);
+ adsModel(worldPosition, wNormal, eyePosition, shininess, diffuseColor, specularColor);
// Combine spec with ambient+diffuse for final fragment color
gl_FragColor = vec4( ka + diffuseTextureColor.rgb * diffuseColor + ks * specularColor, 1.0 );
diff --git a/src/extras/shaders/es2/normaldiffusemap.vert b/src/extras/shaders/es2/normaldiffusemap.vert
index ecc689f69..0681e370a 100644
--- a/src/extras/shaders/es2/normaldiffusemap.vert
+++ b/src/extras/shaders/es2/normaldiffusemap.vert
@@ -4,6 +4,8 @@ attribute vec2 vertexTexCoord;
attribute vec4 vertexTangent;
varying vec3 worldPosition;
+varying vec3 worldNormal;
+varying vec4 worldTangent;
varying vec2 texCoord;
varying mat3 tangentMatrix;
@@ -20,9 +22,13 @@ void main()
texCoord = vertexTexCoord * texCoordScale;
// Transform position, normal, and tangent to world coords
+ worldPosition = vec3(modelMatrix * vec4(vertexPosition, 1.0));
+ worldNormal = normalize(modelNormalMatrix * vertexNormal);
+ worldTangent.xyz = normalize(vec3(modelMatrix * vec4(vertexTangent.xyz, 0.0)));
+ worldTangent.w = vertexTangent.w;
+
vec3 normal = normalize( modelNormalMatrix * vertexNormal );
vec3 tangent = normalize( modelNormalMatrix * vertexTangent.xyz );
- worldPosition = vec3( modelMatrix * vec4( vertexPosition, 1.0 ) );
// Calculate binormal vector
vec3 binormal = normalize( cross( normal, tangent ) );
diff --git a/src/extras/shaders/es2/normaldiffusemapalpha.frag b/src/extras/shaders/es2/normaldiffusemapalpha.frag
index 98acbf01d..599445b9b 100644
--- a/src/extras/shaders/es2/normaldiffusemapalpha.frag
+++ b/src/extras/shaders/es2/normaldiffusemapalpha.frag
@@ -1,8 +1,9 @@
#define FP highp
varying FP vec3 worldPosition;
+varying FP vec3 worldNormal;
+varying FP vec4 worldTangent;
varying FP vec2 texCoord;
-varying FP mat3 tangentMatrix;
uniform sampler2D diffuseTexture;
uniform sampler2D normalTexture;
@@ -15,16 +16,22 @@ uniform FP float shininess; // Specular shininess factor
uniform FP vec3 eyePosition;
#pragma include light.inc.frag
+#pragma include coordinatesystems.inc
void main()
{
// Sample the textures at the interpolated texCoords
FP vec4 diffuseTextureColor = texture2D( diffuseTexture, texCoord );
- FP vec3 normal = 2.0 * texture2D( normalTexture, texCoord ).rgb - vec3( 1.0 );
+ FP vec3 tNormal = 2.0 * texture2D( normalTexture, texCoord ).rgb - vec3( 1.0 );
+
+ FP mat3 tangentMatrix = calcWorldSpaceToTangentSpaceMatrix(worldNormal, worldTangent);
+ FP mat3 invertTangentMatrix = transpose(tangentMatrix);
+
+ FP vec3 wNormal = normalize(invertTangentMatrix * tNormal);
// Calculate the lighting model, keeping the specular component separate
FP vec3 diffuseColor, specularColor;
- adsModelNormalMapped(worldPosition, normal, eyePosition, shininess, tangentMatrix, diffuseColor, specularColor);
+ adsModel(worldPosition, wNormal, eyePosition, shininess, diffuseColor, specularColor);
// Combine spec with ambient+diffuse for final fragment color
// Use the alpha from the diffuse texture (for alpha to coverage)
diff --git a/src/extras/shaders/es2/normaldiffusespecularmap.frag b/src/extras/shaders/es2/normaldiffusespecularmap.frag
index b30c1bd5f..6dc2fba02 100644
--- a/src/extras/shaders/es2/normaldiffusespecularmap.frag
+++ b/src/extras/shaders/es2/normaldiffusespecularmap.frag
@@ -1,8 +1,9 @@
#define FP highp
varying FP vec3 worldPosition;
+varying FP vec3 worldNormal;
+varying FP vec4 worldTangent;
varying FP vec2 texCoord;
-varying FP mat3 tangentMatrix;
uniform sampler2D diffuseTexture;
uniform sampler2D specularTexture;
@@ -15,17 +16,23 @@ uniform FP float shininess; // Specular shininess factor
uniform FP vec3 eyePosition;
#pragma include light.inc.frag
+#pragma include coordinatesystems.inc
void main()
{
// Sample the textures at the interpolated texCoords
FP vec4 diffuseTextureColor = texture2D( diffuseTexture, texCoord );
FP vec4 specularTextureColor = texture2D( specularTexture, texCoord );
- FP vec3 normal = 2.0 * texture2D( normalTexture, texCoord ).rgb - vec3( 1.0 );
+ FP vec3 tNormal = 2.0 * texture2D( normalTexture, texCoord ).rgb - vec3( 1.0 );
+
+ FP mat3 tangentMatrix = calcWorldSpaceToTangentSpaceMatrix(worldNormal, worldTangent);
+ FP mat3 invertTangentMatrix = transpose(tangentMatrix);
+
+ FP vec3 wNormal = normalize(invertTangentMatrix * tNormal);
// Calculate the lighting model, keeping the specular component separate
FP vec3 diffuseColor, specularColor;
- adsModelNormalMapped(worldPosition, normal, eyePosition, shininess, tangentMatrix, diffuseColor, specularColor);
+ adsModel(worldPosition, wNormal, eyePosition, shininess, diffuseColor, specularColor);
// Combine spec with ambient+diffuse for final fragment color
gl_FragColor = vec4( ka + diffuseTextureColor.rgb * diffuseColor + specularTextureColor.rgb * specularColor, 1.0 );
diff --git a/src/extras/shaders/gl3/light.inc.frag b/src/extras/shaders/gl3/light.inc.frag
index ce5c581cf..1af88ba77 100644
--- a/src/extras/shaders/gl3/light.inc.frag
+++ b/src/extras/shaders/gl3/light.inc.frag
@@ -24,79 +24,6 @@ struct EnvironmentLight {
uniform EnvironmentLight envLight;
uniform int envLightCount = 0;
-void adsModelNormalMapped(const in vec3 worldPos,
- const in vec3 tsNormal,
- const in vec3 worldEye,
- const in float shininess,
- const in mat3 tangentMatrix,
- out vec3 diffuseColor,
- out vec3 specularColor)
-{
- diffuseColor = vec3(0.0);
- specularColor = vec3(0.0);
-
- // We perform all work in tangent space, so we convert quantities from world space
- vec3 tsPos = tangentMatrix * worldPos;
- vec3 n = normalize(tsNormal);
- vec3 v = normalize(tangentMatrix * (worldEye - worldPos));
- vec3 s = vec3(0.0);
-
- for (int i = 0; i < lightCount; ++i) {
- float att = 1.0;
- float sDotN = 0.0;
-
- if (lights[i].type != TYPE_DIRECTIONAL) {
- // Point and Spot lights
-
- // Transform the light position from world to tangent space
- vec3 tsLightPos = tangentMatrix * lights[i].position;
- vec3 sUnnormalized = tsLightPos - tsPos;
- s = normalize(sUnnormalized); // Light direction in tangent space
-
- // Calculate the attenuation factor
- sDotN = dot(s, n);
- if (sDotN > 0.0) {
- if (lights[i].constantAttenuation != 0.0
- || lights[i].linearAttenuation != 0.0
- || lights[i].quadraticAttenuation != 0.0) {
- float dist = length(sUnnormalized);
- att = 1.0 / (lights[i].constantAttenuation +
- lights[i].linearAttenuation * dist +
- lights[i].quadraticAttenuation * dist * dist);
- }
-
- // The light direction is in world space, convert to tangent space
- if (lights[i].type == TYPE_SPOT) {
- // Check if fragment is inside or outside of the spot light cone
- vec3 tsLightDirection = tangentMatrix * lights[i].direction;
- if (degrees(acos(dot(-s, tsLightDirection))) > lights[i].cutOffAngle)
- sDotN = 0.0;
- }
- }
- } else {
- // Directional lights
- // The light direction is in world space, convert to tangent space
- s = normalize(tangentMatrix * -lights[i].direction);
- sDotN = dot(s, n);
- }
-
- // Calculate the diffuse factor
- float diffuse = max(sDotN, 0.0);
-
- // Calculate the specular factor
- float specular = 0.0;
- if (diffuse > 0.0 && shininess > 0.0) {
- float normFactor = (shininess + 2.0) / 2.0;
- vec3 r = reflect(-s, n); // Reflection direction in tangent space
- specular = normFactor * pow(max(dot(r, v), 0.0), shininess);
- }
-
- // Accumulate the diffuse and specular contributions
- diffuseColor += att * lights[i].intensity * diffuse * lights[i].color;
- specularColor += att * lights[i].intensity * specular * lights[i].color;
- }
-}
-
void adsModel(const in vec3 worldPos,
const in vec3 worldNormal,
const in vec3 worldEye,
diff --git a/src/extras/shaders/gl3/normaldiffusemap.frag b/src/extras/shaders/gl3/normaldiffusemap.frag
index a99a7ed73..e814da22f 100644
--- a/src/extras/shaders/gl3/normaldiffusemap.frag
+++ b/src/extras/shaders/gl3/normaldiffusemap.frag
@@ -1,8 +1,9 @@
#version 150 core
in vec3 worldPosition;
+in vec3 worldNormal;
+in vec4 worldTangent;
in vec2 texCoord;
-in mat3 tangentMatrix;
uniform sampler2D diffuseTexture;
uniform sampler2D normalTexture;
@@ -17,16 +18,22 @@ uniform vec3 eyePosition;
out vec4 fragColor;
#pragma include light.inc.frag
+#pragma include coordinatesystems.inc
void main()
{
// Sample the textures at the interpolated texCoords
vec4 diffuseTextureColor = texture( diffuseTexture, texCoord );
- vec3 normal = 2.0 * texture( normalTexture, texCoord ).rgb - vec3( 1.0 );
+ vec3 tNormal = 2.0 * texture( normalTexture, texCoord ).rgb - vec3( 1.0 );
+
+ mat3 tangentMatrix = calcWorldSpaceToTangentSpaceMatrix(worldNormal, worldTangent);
+ mat3 invertTangentMatrix = transpose(tangentMatrix);
+
+ vec3 wNormal = normalize(invertTangentMatrix * tNormal);
// Calculate the lighting model, keeping the specular component separate
vec3 diffuseColor, specularColor;
- adsModelNormalMapped(worldPosition, normal, eyePosition, shininess, tangentMatrix, diffuseColor, specularColor);
+ adsModel(worldPosition, wNormal, eyePosition, shininess, diffuseColor, specularColor);
// Combine spec with ambient+diffuse for final fragment color
fragColor = vec4( ka + diffuseTextureColor.rgb * diffuseColor + ks * specularColor, 1.0 );
diff --git a/src/extras/shaders/gl3/normaldiffusemap.vert b/src/extras/shaders/gl3/normaldiffusemap.vert
index 8da443e8d..5f4aeb1ef 100644
--- a/src/extras/shaders/gl3/normaldiffusemap.vert
+++ b/src/extras/shaders/gl3/normaldiffusemap.vert
@@ -6,8 +6,9 @@ in vec2 vertexTexCoord;
in vec4 vertexTangent;
out vec3 worldPosition;
+out vec3 worldNormal;
+out vec4 worldTangent;
out vec2 texCoord;
-out mat3 tangentMatrix;
uniform mat4 modelMatrix;
uniform mat3 modelNormalMatrix;
@@ -20,27 +21,11 @@ void main()
// Pass through scaled texture coordinates
texCoord = vertexTexCoord * texCoordScale;
- // Transform position, normal, and tangent to world coords
+ // Transform position, normal, and tangent to world space
worldPosition = vec3(modelMatrix * vec4(vertexPosition, 1.0));
- vec3 normal = normalize(modelNormalMatrix * vertexNormal);
- vec3 tangent = normalize(vec3(modelMatrix * vec4(vertexTangent.xyz, 0.0)));
-
- // Make the tangent truly orthogonal to the normal by using Gram-Schmidt.
- // This allows to build the tangentMatrix below by simply transposing the
- // tangent -> world space matrix (which would now be orthogonal)
- tangent = normalize(tangent - dot(tangent, normal) * normal);
-
- // Calculate binormal vector. No "real" need to renormalize it,
- // as built by crossing two normal vectors.
- // To orient the binormal correctly, use the fourth coordinate of the tangent,
- // which is +1 for a right hand system, and -1 for a left hand system.
- vec3 binormal = cross(normal, tangent) * vertexTangent.w;
-
- // Construct matrix to transform from eye coords to tangent space
- tangentMatrix = mat3(
- tangent.x, binormal.x, normal.x,
- tangent.y, binormal.y, normal.y,
- tangent.z, binormal.z, normal.z);
+ worldNormal = normalize(modelNormalMatrix * vertexNormal);
+ worldTangent.xyz = normalize(vec3(modelMatrix * vec4(vertexTangent.xyz, 0.0)));
+ worldTangent.w = vertexTangent.w;
// Calculate vertex position in clip coordinates
gl_Position = mvp * vec4(vertexPosition, 1.0);
diff --git a/src/extras/shaders/gl3/normaldiffusemapalpha.frag b/src/extras/shaders/gl3/normaldiffusemapalpha.frag
index ce5cf0e90..030291087 100644
--- a/src/extras/shaders/gl3/normaldiffusemapalpha.frag
+++ b/src/extras/shaders/gl3/normaldiffusemapalpha.frag
@@ -1,8 +1,9 @@
#version 150 core
in vec3 worldPosition;
+in vec3 worldNormal;
+in vec4 worldTangent;
in vec2 texCoord;
-in mat3 tangentMatrix;
uniform sampler2D diffuseTexture;
uniform sampler2D normalTexture;
@@ -17,16 +18,22 @@ uniform vec3 eyePosition;
out vec4 fragColor;
#pragma include light.inc.frag
+#pragma include coordinatesystems.inc
void main()
{
// Sample the textures at the interpolated texCoords
vec4 diffuseTextureColor = texture( diffuseTexture, texCoord );
- vec3 normal = 2.0 * texture( normalTexture, texCoord ).rgb - vec3( 1.0 );
+ vec3 tNormal = 2.0 * texture( normalTexture, texCoord ).rgb - vec3( 1.0 );
+
+ mat3 tangentMatrix = calcWorldSpaceToTangentSpaceMatrix(worldNormal, worldTangent);
+ mat3 invertTangentMatrix = transpose(tangentMatrix);
+
+ vec3 wNormal = normalize(invertTangentMatrix * tNormal);
// Calculate the lighting model, keeping the specular component separate
vec3 diffuseColor, specularColor;
- adsModelNormalMapped(worldPosition, normal, eyePosition, shininess, tangentMatrix, diffuseColor, specularColor);
+ adsModel(worldPosition, wNormal, eyePosition, shininess, diffuseColor, specularColor);
// Combine spec with ambient+diffuse for final fragment color
// Use the alpha from the diffuse texture (for alpha to coverage)
diff --git a/src/extras/shaders/gl3/normaldiffusespecularmap.frag b/src/extras/shaders/gl3/normaldiffusespecularmap.frag
index b60cfe84c..67daccebf 100644
--- a/src/extras/shaders/gl3/normaldiffusespecularmap.frag
+++ b/src/extras/shaders/gl3/normaldiffusespecularmap.frag
@@ -1,8 +1,9 @@
#version 150 core
in vec3 worldPosition;
+in vec3 worldNormal;
+in vec4 worldTangent;
in vec2 texCoord;
-in mat3 tangentMatrix;
out vec4 fragColor;
@@ -15,19 +16,23 @@ uniform float shininess; // Specular shininess factor
uniform vec3 eyePosition;
#pragma include light.inc.frag
+#pragma include coordinatesystems.inc
void main()
{
// Sample the textures at the interpolated texCoords
vec4 diffuseTextureColor = texture( diffuseTexture, texCoord );
vec4 specularTextureColor = texture( specularTexture, texCoord );
- vec3 normal = 2.0 * texture( normalTexture, texCoord ).rgb - vec3( 1.0 );
+ vec3 tNormal = 2.0 * texture( normalTexture, texCoord ).rgb - vec3( 1.0 );
+
+ mat3 tangentMatrix = calcWorldSpaceToTangentSpaceMatrix(worldNormal, worldTangent);
+ mat3 invertTangentMatrix = transpose(tangentMatrix);
+
+ vec3 wNormal = normalize(invertTangentMatrix * tNormal);
// Calculate the lighting model, keeping the specular component separate
vec3 diffuseColor, specularColor;
- adsModelNormalMapped(worldPosition, normal, eyePosition,
- shininess, tangentMatrix,
- diffuseColor, specularColor);
+ adsModel(worldPosition, wNormal, eyePosition, shininess, diffuseColor, specularColor);
// Combine spec with ambient+diffuse for final fragment color
fragColor = vec4((ka + diffuseColor) * diffuseTextureColor.rgb