summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLaszlo Agocs <laszlo.agocs@theqtcompany.com>2015-12-02 14:16:35 +0100
committerLaszlo Agocs <laszlo.agocs@theqtcompany.com>2015-12-04 11:28:46 +0000
commit25d72d8dbaa8a615a0d9893e755b404dc20c04a7 (patch)
tree89edc16cec4e4297bf41671db349f04fdb4bf4d6 /src
parentec471e983146dbaf52191c01e7ad737900e9459c (diff)
Migrate NormalDiffuseMap(Alpha)Material to be lights-aware
Change-Id: I91a6db87836c0e37af10d8de90e431240a7851e2 Reviewed-by: Kevin Ottens <kevin.ottens@kdab.com> Reviewed-by: Andy Nichols <andy.nichols@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r--src/quick3d/imports/render/defaults/qml/NormalDiffuseMapAlphaMaterial.qml6
-rw-r--r--src/quick3d/imports/render/defaults/qml/NormalDiffuseMapMaterial.qml6
-rw-r--r--src/render/defaults/qnormaldiffusemapalphamaterial.cpp2
-rw-r--r--src/render/defaults/qnormaldiffusemapmaterial.cpp4
-rw-r--r--src/render/defaults/qnormaldiffusemapmaterial_p.h2
-rw-r--r--src/render/shaders/es2/light.inc.frag56
-rw-r--r--src/render/shaders/es2/normaldiffusemap.frag37
-rw-r--r--src/render/shaders/es2/normaldiffusemap.vert25
-rw-r--r--src/render/shaders/es2/normaldiffusemapalpha.frag37
-rw-r--r--src/render/shaders/gl3/light.inc.frag60
-rw-r--r--src/render/shaders/gl3/normaldiffusemap.frag39
-rw-r--r--src/render/shaders/gl3/normaldiffusemap.vert25
-rw-r--r--src/render/shaders/gl3/normaldiffusemapalpha.frag39
13 files changed, 164 insertions, 174 deletions
diff --git a/src/quick3d/imports/render/defaults/qml/NormalDiffuseMapAlphaMaterial.qml b/src/quick3d/imports/render/defaults/qml/NormalDiffuseMapAlphaMaterial.qml
index 459434765..8757f3ee7 100644
--- a/src/quick3d/imports/render/defaults/qml/NormalDiffuseMapAlphaMaterial.qml
+++ b/src/quick3d/imports/render/defaults/qml/NormalDiffuseMapAlphaMaterial.qml
@@ -84,12 +84,6 @@ Material {
]
effect: Effect {
-
- parameters: [
- Parameter { name: "lightPosition"; value: Qt.vector4d( 0.0, 0.0, 0.0, 1.0 ) },
- Parameter { name: "lightIntensity"; value: Qt.vector3d( 1.0, 1.0, 1.0 ) }
- ]
-
ShaderProgram {
id: gl2Es2Shader
vertexShaderCode: loadSource("qrc:/shaders/es2/normaldiffusemap.vert")
diff --git a/src/quick3d/imports/render/defaults/qml/NormalDiffuseMapMaterial.qml b/src/quick3d/imports/render/defaults/qml/NormalDiffuseMapMaterial.qml
index ebbe8683c..ede2f44ec 100644
--- a/src/quick3d/imports/render/defaults/qml/NormalDiffuseMapMaterial.qml
+++ b/src/quick3d/imports/render/defaults/qml/NormalDiffuseMapMaterial.qml
@@ -84,12 +84,6 @@ Material {
]
effect: Effect {
-
- parameters: [
- Parameter { name: "lightPosition"; value: Qt.vector4d( 0.0, 0.0, 0.0, 1.0 ) },
- Parameter { name: "lightIntensity"; value: Qt.vector3d( 1.0, 1.0, 1.0 ) }
- ]
-
ShaderProgram {
id: gl2Es2Shader
vertexShaderCode: loadSource("qrc:/shaders/es2/normaldiffusemap.vert")
diff --git a/src/render/defaults/qnormaldiffusemapalphamaterial.cpp b/src/render/defaults/qnormaldiffusemapalphamaterial.cpp
index 602a111d7..bd438b3a3 100644
--- a/src/render/defaults/qnormaldiffusemapalphamaterial.cpp
+++ b/src/render/defaults/qnormaldiffusemapalphamaterial.cpp
@@ -116,8 +116,6 @@ void QNormalDiffuseMapAlphaMaterialPrivate::init()
m_normalDiffuseEffect->addParameter(m_normalParameter);
m_normalDiffuseEffect->addParameter(m_specularParameter);
m_normalDiffuseEffect->addParameter(m_shininessParameter);
- m_normalDiffuseEffect->addParameter(m_lightPositionParameter);
- m_normalDiffuseEffect->addParameter(m_lightIntensityParameter);
m_normalDiffuseEffect->addParameter(m_textureScaleParameter);
q_func()->setEffect(m_normalDiffuseEffect);
diff --git a/src/render/defaults/qnormaldiffusemapmaterial.cpp b/src/render/defaults/qnormaldiffusemapmaterial.cpp
index 259ac54d8..e038b2c81 100644
--- a/src/render/defaults/qnormaldiffusemapmaterial.cpp
+++ b/src/render/defaults/qnormaldiffusemapmaterial.cpp
@@ -67,8 +67,6 @@ QNormalDiffuseMapMaterialPrivate::QNormalDiffuseMapMaterialPrivate()
, m_normalParameter(new QParameter(QStringLiteral("normalTexture"), m_normalTexture))
, m_specularParameter(new QParameter(QStringLiteral("ks"), QColor::fromRgbF(0.95f, 0.95f, 0.95f, 1.0f)))
, m_shininessParameter(new QParameter(QStringLiteral("shininess"), 150.0f))
- , m_lightPositionParameter(new QParameter(QStringLiteral("lightPosition"), QVector4D(0.0f, 0.0f, 0.0f, 1.0f)))
- , m_lightIntensityParameter(new QParameter(QStringLiteral("lightIntensity"), QVector3D(1.0f, 1.0f, 1.0f)))
, m_textureScaleParameter(new QParameter(QStringLiteral("texCoordScale"), 1.0f))
, m_normalDiffuseGL3Technique(new QTechnique())
, m_normalDiffuseGL2Technique(new QTechnique())
@@ -130,8 +128,6 @@ void QNormalDiffuseMapMaterialPrivate::init()
m_normalDiffuseEffect->addParameter(m_normalParameter);
m_normalDiffuseEffect->addParameter(m_specularParameter);
m_normalDiffuseEffect->addParameter(m_shininessParameter);
- m_normalDiffuseEffect->addParameter(m_lightPositionParameter);
- m_normalDiffuseEffect->addParameter(m_lightIntensityParameter);
m_normalDiffuseEffect->addParameter(m_textureScaleParameter);
q_func()->setEffect(m_normalDiffuseEffect);
diff --git a/src/render/defaults/qnormaldiffusemapmaterial_p.h b/src/render/defaults/qnormaldiffusemapmaterial_p.h
index 7bf820541..996aa76c9 100644
--- a/src/render/defaults/qnormaldiffusemapmaterial_p.h
+++ b/src/render/defaults/qnormaldiffusemapmaterial_p.h
@@ -79,8 +79,6 @@ public:
QParameter *m_normalParameter;
QParameter *m_specularParameter;
QParameter *m_shininessParameter;
- QParameter *m_lightPositionParameter;
- QParameter *m_lightIntensityParameter;
QParameter *m_textureScaleParameter;
QTechnique *m_normalDiffuseGL3Technique;
QTechnique *m_normalDiffuseGL2Technique;
diff --git a/src/render/shaders/es2/light.inc.frag b/src/render/shaders/es2/light.inc.frag
index f75249e75..516d701f1 100644
--- a/src/render/shaders/es2/light.inc.frag
+++ b/src/render/shaders/es2/light.inc.frag
@@ -10,6 +10,41 @@ 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 );
+
+ // TODO dynamic indexing may not be supported with GLSL 1.00 so take only the first light into account
+ FP vec3 lightDir = lights[0].direction;
+ FP float att = 1.0;
+ if (length( lightDir ) == 0.0) {
+ lightDir = lights[0].position - vpos;
+ if (length( lights[0].attenuation ) != 0.0) {
+ FP float dist = length(lightDir);
+ att = 1.0 / (lights[0].attenuation.x + lights[0].attenuation.y * dist + lights[0].attenuation.z * dist * dist);
+ }
+ }
+
+ FP vec3 s = normalize( tangentMatrix * lightDir );
+ FP float diffuse = max( dot( s, n ), 0.0 );
+
+ FP float specular = 0.0;
+ if (diffuse > 0.0 && shininess > 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 += 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)
{
@@ -46,6 +81,23 @@ void adsModel(const in FP vec3 vpos, const in FP vec3 vnormal, const in FP vec3
void adModel(const in FP vec3 vpos, const in FP vec3 vnormal, out FP vec3 diffuseColor)
{
- FP vec3 tmp;
- adsModel(vpos, vnormal, vec3(0.0), 0.0, diffuseColor, tmp);
+ diffuseColor = vec3(0.0);
+
+ FP vec3 n = normalize( vnormal );
+
+ // TODO dynamic indexing may not be supported with GLSL 1.00 so take only the first light into account
+ FP vec3 lightDir = lights[0].direction;
+ FP float att = 1.0;
+ if (length( lightDir ) == 0.0) {
+ lightDir = lights[0].position - vpos;
+ if (length( lights[0].attenuation ) != 0.0) {
+ FP float dist = length(lightDir);
+ att = 1.0 / (lights[0].attenuation.x + lights[0].attenuation.y * dist + lights[0].attenuation.z * dist * dist);
+ }
+ }
+
+ FP vec3 s = normalize( lightDir );
+ FP float diffuse = max( dot( s, n ), 0.0 );
+
+ diffuseColor += att * lights[0].intensity * diffuse * lights[0].color;
}
diff --git a/src/render/shaders/es2/normaldiffusemap.frag b/src/render/shaders/es2/normaldiffusemap.frag
index 476a5b591..71e936e15 100644
--- a/src/render/shaders/es2/normaldiffusemap.frag
+++ b/src/render/shaders/es2/normaldiffusemap.frag
@@ -1,48 +1,31 @@
#define FP highp
-varying FP vec3 lightDir;
-varying FP vec3 viewDir;
+varying FP vec3 position;
varying FP vec2 texCoord;
+varying FP mat3 tangentMatrix;
uniform sampler2D diffuseTexture;
uniform sampler2D normalTexture;
-uniform FP vec4 lightPosition;
-uniform FP vec3 lightIntensity;
-
// TODO: Replace with a struct
uniform FP vec3 ka; // Ambient reflectivity
uniform FP vec3 ks; // Specular reflectivity
uniform FP float shininess; // Specular shininess factor
-FP vec3 adsModel( const FP vec3 norm, const FP vec3 diffuseReflect)
-{
- // Reflection of light direction about normal
- FP vec3 r = reflect( -lightDir, norm );
-
- // Calculate the ambient contribution
- FP vec3 ambient = lightIntensity * ka;
-
- // Calculate the diffuse contribution
- FP float sDotN = max( dot( lightDir, norm ), 0.0 );
- FP vec3 diffuse = lightIntensity * diffuseReflect * sDotN;
+uniform vec3 eyePosition;
- // Sum the ambient and diffuse contributions
- FP vec3 ambientAndDiff = ambient + diffuse;
-
- // Calculate the specular highlight contribution
- FP vec3 spec = vec3( 0.0 );
- if ( sDotN > 0.0 )
- spec = lightIntensity * ks * pow( max( dot( r, viewDir ), 0.0 ), shininess );
- return ambientAndDiff + spec;
-}
+#pragma include light.inc.frag
void main()
{
// Sample the textures at the interpolated texCoords
FP vec4 diffuseTextureColor = texture2D( diffuseTexture, texCoord );
- FP vec4 normal = 2.0 * texture2D( normalTexture, texCoord ) - vec4( 1.0 );
+ FP vec3 normal = 2.0 * texture2D( normalTexture, texCoord ).rgb - vec3( 1.0 );
+
+ // Calculate the lighting model, keeping the specular component separate
+ FP vec3 diffuseColor, specularColor;
+ adsModelNormalMapped(position, normal, eyePosition, shininess, tangentMatrix, diffuseColor, specularColor);
// Combine spec with ambient+diffuse for final fragment color
- gl_FragColor = vec4( adsModel( normalize( normal.xyz ), diffuseTextureColor.xyz), 1.0 );
+ gl_FragColor = vec4( ka + diffuseTextureColor.rgb * diffuseColor + ks * specularColor, 1.0 );
}
diff --git a/src/render/shaders/es2/normaldiffusemap.vert b/src/render/shaders/es2/normaldiffusemap.vert
index 1c3c979b6..fda16284d 100644
--- a/src/render/shaders/es2/normaldiffusemap.vert
+++ b/src/render/shaders/es2/normaldiffusemap.vert
@@ -3,9 +3,9 @@ attribute vec3 vertexNormal;
attribute vec2 vertexTexCoord;
attribute vec4 vertexTangent;
-varying vec3 lightDir;
-varying vec3 viewDir;
+varying vec3 position;
varying vec2 texCoord;
+varying mat3 tangentMatrix;
uniform mat4 modelView;
uniform mat3 modelViewNormal;
@@ -14,10 +14,6 @@ uniform mat4 mvp;
uniform float texCoordScale;
-// TODO: Replace with a uniform block
-uniform vec4 lightPosition;
-uniform vec3 lightIntensity;
-
void main()
{
// Pass through texture coordinates
@@ -26,23 +22,16 @@ void main()
// Transform position, normal, and tangent to eye coords
vec3 normal = normalize( modelViewNormal * vertexNormal );
vec3 tangent = normalize( modelViewNormal * vertexTangent.xyz );
- vec3 position = vec3( modelView * vec4( vertexPosition, 1.0 ) );
+ position = vec3( modelView * vec4( vertexPosition, 1.0 ) );
// Calculate binormal vector
vec3 binormal = normalize( cross( normal, tangent ) );
// Construct matrix to transform from eye coords to tangent space
- mat3 tangentMatrix = mat3 (
- tangent.x, binormal.x, normal.x,
- tangent.y, binormal.y, normal.y,
- tangent.z, binormal.z, normal.z );
-
- // Transform light direction and view direction to tangent space
- vec3 s = vec3( lightPosition ) - position;
- lightDir = normalize( tangentMatrix * s );
-
- vec3 v = -position;
- viewDir = normalize( tangentMatrix * v );
+ tangentMatrix = mat3 (
+ tangent.x, binormal.x, normal.x,
+ tangent.y, binormal.y, normal.y,
+ tangent.z, binormal.z, normal.z );
// Calculate vertex position in clip coordinates
gl_Position = mvp * vec4( vertexPosition, 1.0 );
diff --git a/src/render/shaders/es2/normaldiffusemapalpha.frag b/src/render/shaders/es2/normaldiffusemapalpha.frag
index 3768f4835..75306b3bd 100644
--- a/src/render/shaders/es2/normaldiffusemapalpha.frag
+++ b/src/render/shaders/es2/normaldiffusemapalpha.frag
@@ -1,49 +1,32 @@
#define FP highp
-varying FP vec3 lightDir;
-varying FP vec3 viewDir;
+varying FP vec3 position;
varying FP vec2 texCoord;
+varying FP mat3 tangentMatrix;
uniform sampler2D diffuseTexture;
uniform sampler2D normalTexture;
-uniform FP vec4 lightPosition;
-uniform FP vec3 lightIntensity;
-
// TODO: Replace with a struct
uniform FP vec3 ka; // Ambient reflectivity
uniform FP vec3 ks; // Specular reflectivity
uniform FP float shininess; // Specular shininess factor
-FP vec3 adsModel( const FP vec3 norm, const FP vec3 diffuseReflect)
-{
- // Reflection of light direction about normal
- FP vec3 r = reflect( -lightDir, norm );
-
- // Calculate the ambient contribution
- FP vec3 ambient = lightIntensity * ka;
-
- // Calculate the diffuse contribution
- FP float sDotN = max( dot( lightDir, norm ), 0.0 );
- FP vec3 diffuse = lightIntensity * diffuseReflect * sDotN;
+uniform vec3 eyePosition;
- // Sum the ambient and diffuse contributions
- FP vec3 ambientAndDiff = ambient + diffuse;
-
- // Calculate the specular highlight contribution
- FP vec3 spec = vec3( 0.0 );
- if ( sDotN > 0.0 )
- spec = lightIntensity * ks * pow( max( dot( r, viewDir ), 0.0 ), shininess );
- return ambientAndDiff + spec;
-}
+#pragma include light.inc.frag
void main()
{
// Sample the textures at the interpolated texCoords
FP vec4 diffuseTextureColor = texture2D( diffuseTexture, texCoord );
- FP vec4 normal = 2.0 * texture2D( normalTexture, texCoord ) - vec4( 1.0 );
+ FP vec3 normal = 2.0 * texture2D( normalTexture, texCoord ).rgb - vec3( 1.0 );
+
+ // Calculate the lighting model, keeping the specular component separate
+ FP vec3 diffuseColor, specularColor;
+ adsModelNormalMapped(position, normal, eyePosition, shininess, tangentMatrix, diffuseColor, specularColor);
// Combine spec with ambient+diffuse for final fragment color
// Use the alpha from the diffuse texture (for alpha to coverage)
- gl_FragColor = vec4( adsModel( normalize( normal.xyz ), diffuseTextureColor.xyz), diffuseTextureColor.a );
+ gl_FragColor = vec4( ka + diffuseTextureColor.rgb * diffuseColor + ks * specularColor, diffuseTextureColor.a );
}
diff --git a/src/render/shaders/gl3/light.inc.frag b/src/render/shaders/gl3/light.inc.frag
index c608dd5de..6d4b8b68f 100644
--- a/src/render/shaders/gl3/light.inc.frag
+++ b/src/render/shaders/gl3/light.inc.frag
@@ -10,6 +10,43 @@ struct Light {
uniform Light lights[MAX_LIGHTS];
uniform int lightCount;
+void adsModelNormalMapped(const in vec3 vpos, const in vec3 vnormal, const in vec3 eye, const in float shininess,
+ const in mat3 tangentMatrix,
+ out vec3 diffuseColor, out vec3 specularColor)
+{
+ diffuseColor = vec3(0.0);
+ specularColor = vec3(0.0);
+
+ vec3 n = normalize( vnormal );
+
+ int i;
+ for (i = 0; i < lightCount; ++i) {
+ vec3 lightDir = lights[i].direction;
+ float att = 1.0;
+ if (length( lightDir ) == 0.0) {
+ lightDir = lights[i].position - vpos;
+ if (length( lights[i].attenuation ) != 0.0) {
+ float dist = length(lightDir);
+ att = 1.0 / (lights[i].attenuation.x + lights[i].attenuation.y * dist + lights[i].attenuation.z * dist * dist);
+ }
+ }
+
+ vec3 s = normalize( tangentMatrix * lightDir );
+ float diffuse = max( dot( s, n ), 0.0 );
+
+ float specular = 0.0;
+ if (diffuse > 0.0 && shininess > 0.0) {
+ vec3 r = reflect( -s, n );
+ vec3 v = normalize( tangentMatrix * ( eye - vpos ) );
+ float normFactor = ( shininess + 2.0 ) / 2.0;
+ specular = normFactor * pow( max( dot( r, v ), 0.0 ), shininess );
+ }
+
+ diffuseColor += att * lights[i].intensity * diffuse * lights[i].color;
+ specularColor += specular;
+ }
+}
+
void adsModel(const in vec3 vpos, const in vec3 vnormal, const in vec3 eye, const in float shininess,
out vec3 diffuseColor, out vec3 specularColor)
{
@@ -48,6 +85,25 @@ void adsModel(const in vec3 vpos, const in vec3 vnormal, const in vec3 eye, cons
void adModel(const in vec3 vpos, const in vec3 vnormal, out vec3 diffuseColor)
{
- vec3 tmp;
- adsModel(vpos, vnormal, vec3(0.0), 0.0, diffuseColor, tmp);
+ diffuseColor = vec3(0.0);
+
+ vec3 n = normalize( vnormal );
+
+ int i;
+ for (i = 0; i < lightCount; ++i) {
+ vec3 lightDir = lights[i].direction;
+ float att = 1.0;
+ if (length( lightDir ) == 0.0) {
+ lightDir = lights[i].position - vpos;
+ if (length( lights[i].attenuation ) != 0.0) {
+ float dist = length(lightDir);
+ att = 1.0 / (lights[i].attenuation.x + lights[i].attenuation.y * dist + lights[i].attenuation.z * dist * dist);
+ }
+ }
+
+ vec3 s = normalize( lightDir );
+ float diffuse = max( dot( s, n ), 0.0 );
+
+ diffuseColor += att * lights[i].intensity * diffuse * lights[i].color;
+ }
}
diff --git a/src/render/shaders/gl3/normaldiffusemap.frag b/src/render/shaders/gl3/normaldiffusemap.frag
index 4dd48f34c..4b61813c5 100644
--- a/src/render/shaders/gl3/normaldiffusemap.frag
+++ b/src/render/shaders/gl3/normaldiffusemap.frag
@@ -1,54 +1,33 @@
#version 150 core
-in vec3 lightDir;
-in vec3 viewDir;
+in vec3 position;
in vec2 texCoord;
+in mat3 tangentMatrix;
uniform sampler2D diffuseTexture;
uniform sampler2D normalTexture;
-// TODO: Replace with a uniform block
-uniform vec4 lightPosition;
-uniform vec3 lightIntensity;
-
// TODO: Replace with a struct
uniform vec3 ka; // Ambient reflectivity
uniform vec3 ks; // Specular reflectivity
uniform float shininess; // Specular shininess factor
-out vec4 fragColor;
-
-void adsModel( const in vec3 norm, const in vec3 diffuseReflect, out vec3 ambientAndDiff, out vec3 spec )
-{
- // Reflection of light direction about normal
- vec3 r = reflect( -lightDir, norm );
-
- // Calculate the ambient contribution
- vec3 ambient = lightIntensity * ka;
-
- // Calculate the diffuse contribution
- float sDotN = max( dot( lightDir, norm ), 0.0 );
- vec3 diffuse = lightIntensity * diffuseReflect * sDotN;
+uniform vec3 eyePosition;
- // Sum the ambient and diffuse contributions
- ambientAndDiff = ambient + diffuse;
+out vec4 fragColor;
- // Calculate the specular highlight contribution
- spec = vec3( 0.0 );
- if ( sDotN > 0.0 )
- spec = lightIntensity * ks * pow( max( dot( r, viewDir ), 0.0 ), shininess );
-}
+#pragma include light.inc.frag
void main()
{
// Sample the textures at the interpolated texCoords
vec4 diffuseTextureColor = texture( diffuseTexture, texCoord );
- vec4 normal = 2.0 * texture( normalTexture, texCoord ) - vec4( 1.0 );
+ vec3 normal = 2.0 * texture( normalTexture, texCoord ).rgb - vec3( 1.0 );
// Calculate the lighting model, keeping the specular component separate
- vec3 ambientAndDiff, spec;
- adsModel( normalize( normal.xyz ), diffuseTextureColor.xyz, ambientAndDiff, spec );
+ vec3 diffuseColor, specularColor;
+ adsModelNormalMapped(position, normal, eyePosition, shininess, tangentMatrix, diffuseColor, specularColor);
// Combine spec with ambient+diffuse for final fragment color
- fragColor = vec4( ambientAndDiff + spec, 1.0 );
+ fragColor = vec4( ka + diffuseTextureColor.rgb * diffuseColor + ks * specularColor, 1.0 );
}
diff --git a/src/render/shaders/gl3/normaldiffusemap.vert b/src/render/shaders/gl3/normaldiffusemap.vert
index 65fd7231f..8b3a0716f 100644
--- a/src/render/shaders/gl3/normaldiffusemap.vert
+++ b/src/render/shaders/gl3/normaldiffusemap.vert
@@ -5,9 +5,9 @@ in vec3 vertexNormal;
in vec2 vertexTexCoord;
in vec4 vertexTangent;
-out vec3 lightDir;
-out vec3 viewDir;
+out vec3 position;
out vec2 texCoord;
+out mat3 tangentMatrix;
uniform mat4 modelView;
uniform mat3 modelViewNormal;
@@ -16,10 +16,6 @@ uniform mat4 mvp;
uniform float texCoordScale;
-// TODO: Replace with a uniform block
-uniform vec4 lightPosition;
-uniform vec3 lightIntensity;
-
void main()
{
// Pass through texture coordinates
@@ -28,23 +24,16 @@ void main()
// Transform position, normal, and tangent to eye coords
vec3 normal = normalize( modelViewNormal * vertexNormal );
vec3 tangent = normalize( modelViewNormal * vertexTangent.xyz );
- vec3 position = vec3( modelView * vec4( vertexPosition, 1.0 ) );
+ position = vec3( modelView * vec4( vertexPosition, 1.0 ) );
// Calculate binormal vector
vec3 binormal = normalize( cross( normal, tangent ) );
// Construct matrix to transform from eye coords to tangent space
- mat3 tangentMatrix = mat3 (
- tangent.x, binormal.x, normal.x,
- tangent.y, binormal.y, normal.y,
- tangent.z, binormal.z, normal.z );
-
- // Transform light direction and view direction to tangent space
- vec3 s = vec3( lightPosition ) - position;
- lightDir = normalize( tangentMatrix * s );
-
- vec3 v = -position;
- viewDir = normalize( tangentMatrix * v );
+ tangentMatrix = mat3 (
+ tangent.x, binormal.x, normal.x,
+ tangent.y, binormal.y, normal.y,
+ tangent.z, binormal.z, normal.z );
// Calculate vertex position in clip coordinates
gl_Position = mvp * vec4( vertexPosition, 1.0 );
diff --git a/src/render/shaders/gl3/normaldiffusemapalpha.frag b/src/render/shaders/gl3/normaldiffusemapalpha.frag
index 375f1ed5c..28e597b16 100644
--- a/src/render/shaders/gl3/normaldiffusemapalpha.frag
+++ b/src/render/shaders/gl3/normaldiffusemapalpha.frag
@@ -1,55 +1,34 @@
#version 150 core
-in vec3 lightDir;
-in vec3 viewDir;
+in vec3 position;
in vec2 texCoord;
+in mat3 tangentMatrix;
uniform sampler2D diffuseTexture;
uniform sampler2D normalTexture;
-// TODO: Replace with a uniform block
-uniform vec4 lightPosition;
-uniform vec3 lightIntensity;
-
// TODO: Replace with a struct
uniform vec3 ka; // Ambient reflectivity
uniform vec3 ks; // Specular reflectivity
uniform float shininess; // Specular shininess factor
-out vec4 fragColor;
-
-void adsModel( const in vec3 norm, const in vec3 diffuseReflect, out vec3 ambientAndDiff, out vec3 spec )
-{
- // Reflection of light direction about normal
- vec3 r = reflect( -lightDir, norm );
-
- // Calculate the ambient contribution
- vec3 ambient = lightIntensity * ka;
-
- // Calculate the diffuse contribution
- float sDotN = max( dot( lightDir, norm ), 0.0 );
- vec3 diffuse = lightIntensity * diffuseReflect * sDotN;
+uniform vec3 eyePosition;
- // Sum the ambient and diffuse contributions
- ambientAndDiff = ambient + diffuse;
+out vec4 fragColor;
- // Calculate the specular highlight contribution
- spec = vec3( 0.0 );
- if ( sDotN > 0.0 )
- spec = lightIntensity * ks * pow( max( dot( r, viewDir ), 0.0 ), shininess );
-}
+#pragma include light.inc.frag
void main()
{
// Sample the textures at the interpolated texCoords
vec4 diffuseTextureColor = texture( diffuseTexture, texCoord );
- vec4 normal = 2.0 * texture( normalTexture, texCoord ) - vec4( 1.0 );
+ vec3 normal = 2.0 * texture( normalTexture, texCoord ).rgb - vec3( 1.0 );
// Calculate the lighting model, keeping the specular component separate
- vec3 ambientAndDiff, spec;
- adsModel( normalize( normal.xyz ), diffuseTextureColor.xyz, ambientAndDiff, spec );
+ vec3 diffuseColor, specularColor;
+ adsModelNormalMapped(position, normal, eyePosition, shininess, tangentMatrix, diffuseColor, specularColor);
// Combine spec with ambient+diffuse for final fragment color
// Use the alpha from the diffuse texture (for alpha to coverage)
- fragColor = vec4( ambientAndDiff + spec, diffuseTextureColor.a );
+ fragColor = vec4( ka + diffuseTextureColor.rgb * diffuseColor + ks * specularColor, diffuseTextureColor.a );
}