diff options
author | Laszlo Agocs <laszlo.agocs@theqtcompany.com> | 2015-12-02 14:56:05 +0100 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@theqtcompany.com> | 2015-12-04 11:28:51 +0000 |
commit | 9ca806af52108baa80dafa2d9c3161f1baae3af0 (patch) | |
tree | 713afc57e4bb547ae318b925231c54da2d3fb4ad /src/render/shaders | |
parent | 25d72d8dbaa8a615a0d9893e755b404dc20c04a7 (diff) |
Migrate NormalDiffuseSpecularMapMaterial to be light-aware
Remove duplicated vertex shaders in the process.
[normal]diffusespecularmap.vert is the same as [normal]diffusemap.vert
hence there is no need for the former.
Change-Id: If448005c593c765551ca239af6789df2f6e53af6
Reviewed-by: Kevin Ottens <kevin.ottens@kdab.com>
Reviewed-by: Andy Nichols <andy.nichols@theqtcompany.com>
Diffstat (limited to 'src/render/shaders')
-rw-r--r-- | src/render/shaders/es2/diffusespecularmap.vert | 22 | ||||
-rw-r--r-- | src/render/shaders/es2/normaldiffusespecularmap.frag | 39 | ||||
-rw-r--r-- | src/render/shaders/es2/normaldiffusespecularmap.vert | 49 | ||||
-rw-r--r-- | src/render/shaders/gl3/diffusespecularmap.vert | 24 | ||||
-rw-r--r-- | src/render/shaders/gl3/normaldiffusespecularmap.frag | 42 | ||||
-rw-r--r-- | src/render/shaders/gl3/normaldiffusespecularmap.vert | 51 |
6 files changed, 20 insertions, 207 deletions
diff --git a/src/render/shaders/es2/diffusespecularmap.vert b/src/render/shaders/es2/diffusespecularmap.vert deleted file mode 100644 index 90c751557..000000000 --- a/src/render/shaders/es2/diffusespecularmap.vert +++ /dev/null @@ -1,22 +0,0 @@ -attribute vec3 vertexPosition; -attribute vec3 vertexNormal; -attribute vec2 vertexTexCoord; - -varying vec3 position; -varying vec3 normal; -varying vec2 texCoord; - -uniform mat4 modelView; -uniform mat3 modelViewNormal; -uniform mat4 mvp; - -uniform float texCoordScale; - -void main() -{ - texCoord = vertexTexCoord * texCoordScale; - normal = normalize( modelViewNormal * vertexNormal ); - position = vec3( modelView * vec4( vertexPosition, 1.0 ) ); - - gl_Position = mvp * vec4( vertexPosition, 1.0 ); -} diff --git a/src/render/shaders/es2/normaldiffusespecularmap.frag b/src/render/shaders/es2/normaldiffusespecularmap.frag index 8078ac14c..48eaaae7d 100644 --- a/src/render/shaders/es2/normaldiffusespecularmap.frag +++ b/src/render/shaders/es2/normaldiffusespecularmap.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 specularTexture; uniform sampler2D normalTexture; -uniform FP vec4 lightPosition; -uniform FP vec3 lightIntensity; - // TODO: Replace with a struct uniform FP vec3 ka; // Ambient reflectivity uniform FP float shininess; // Specular shininess factor -FP vec3 adsModel( const FP vec3 norm, const FP vec3 diffuseReflect, const FP vec3 specular ) -{ - // 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 * ( shininess / ( 8.0 * 3.14 ) ) * pow( max( dot( r, viewDir ), 0.0 ), shininess ); - return (ambientAndDiff + spec * specular.rgb); -} +#pragma include light.inc.frag void main() { // Sample the textures at the interpolated texCoords FP vec4 diffuseTextureColor = texture2D( diffuseTexture, texCoord ); FP vec4 specularTextureColor = texture2D( specularTexture, 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); - // Calculate the lighting model - gl_FragColor = vec4( adsModel( normalize( normal.xyz ), diffuseTextureColor.xyz, specularTextureColor.xyz ), 1.0 ); + // 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/render/shaders/es2/normaldiffusespecularmap.vert b/src/render/shaders/es2/normaldiffusespecularmap.vert deleted file mode 100644 index 1c3c979b6..000000000 --- a/src/render/shaders/es2/normaldiffusespecularmap.vert +++ /dev/null @@ -1,49 +0,0 @@ -attribute vec3 vertexPosition; -attribute vec3 vertexNormal; -attribute vec2 vertexTexCoord; -attribute vec4 vertexTangent; - -varying vec3 lightDir; -varying vec3 viewDir; -varying vec2 texCoord; - -uniform mat4 modelView; -uniform mat3 modelViewNormal; -uniform mat4 projectionMatrix; -uniform mat4 mvp; - -uniform float texCoordScale; - -// TODO: Replace with a uniform block -uniform vec4 lightPosition; -uniform vec3 lightIntensity; - -void main() -{ - // Pass through texture coordinates - texCoord = vertexTexCoord * texCoordScale; - - // 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 ) ); - - // 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 ); - - // Calculate vertex position in clip coordinates - gl_Position = mvp * vec4( vertexPosition, 1.0 ); -} diff --git a/src/render/shaders/gl3/diffusespecularmap.vert b/src/render/shaders/gl3/diffusespecularmap.vert deleted file mode 100644 index 2b27413a5..000000000 --- a/src/render/shaders/gl3/diffusespecularmap.vert +++ /dev/null @@ -1,24 +0,0 @@ -#version 150 core - -in vec3 vertexPosition; -in vec3 vertexNormal; -in vec2 vertexTexCoord; - -out vec3 position; -out vec3 normal; -out vec2 texCoord; - -uniform mat4 modelView; -uniform mat3 modelViewNormal; -uniform mat4 mvp; - -uniform float texCoordScale; - -void main() -{ - texCoord = vertexTexCoord * texCoordScale; - normal = normalize( modelViewNormal * vertexNormal ); - position = vec3( modelView * vec4( vertexPosition, 1.0 ) ); - - gl_Position = mvp * vec4( vertexPosition, 1.0 ); -} diff --git a/src/render/shaders/gl3/normaldiffusespecularmap.frag b/src/render/shaders/gl3/normaldiffusespecularmap.frag index 2d9d9e4f1..d6a5f44ae 100644 --- a/src/render/shaders/gl3/normaldiffusespecularmap.frag +++ b/src/render/shaders/gl3/normaldiffusespecularmap.frag @@ -1,58 +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 specularTexture; 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 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; +uniform vec3 eyePosition; - // Calculate the diffuse contribution - float sDotN = max( dot( lightDir, norm ), 0.0 ); - vec3 diffuse = lightIntensity * diffuseReflect * sDotN; - - // 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 * ( shininess / ( 8.0 * 3.14 ) ) * 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 specularTextureColor = texture( specularTexture, 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 ); - - // Multiply specular factor by specular texture sample - vec3 specularColor = spec * specularTextureColor.rgb; + vec3 diffuseColor, specularColor; + adsModelNormalMapped(position, normal, eyePosition, shininess, tangentMatrix, diffuseColor, specularColor); // Combine spec with ambient+diffuse for final fragment color - fragColor = vec4( ambientAndDiff + specularColor, 1.0 ); + fragColor = vec4( ka + diffuseTextureColor.rgb * diffuseColor + specularTextureColor.rgb * specularColor, 1.0 ); } diff --git a/src/render/shaders/gl3/normaldiffusespecularmap.vert b/src/render/shaders/gl3/normaldiffusespecularmap.vert deleted file mode 100644 index b0f84ec90..000000000 --- a/src/render/shaders/gl3/normaldiffusespecularmap.vert +++ /dev/null @@ -1,51 +0,0 @@ -#version 150 core - -in vec3 vertexPosition; -in vec3 vertexNormal; -in vec2 vertexTexCoord; -in vec4 vertexTangent; - -out vec3 lightDir; -out vec3 viewDir; -out vec2 texCoord; - -uniform mat4 modelView; -uniform mat3 modelViewNormal; -uniform mat4 projectionMatrix; -uniform mat4 mvp; - -uniform float texCoordScale = 1.0; - -// TODO: Replace with a uniform block -uniform vec4 lightPosition; -uniform vec3 lightIntensity; - -void main() -{ - // Pass through texture coordinates - texCoord = vertexTexCoord * texCoordScale; - - // 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 ) ); - - // 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 ); - - // Calculate vertex position in clip coordinates - gl_Position = mvp * vec4( vertexPosition, 1.0 ); -} |