diff options
Diffstat (limited to 'basicsuite/advancedcustommaterial/shaders/gl3')
5 files changed, 385 insertions, 0 deletions
diff --git a/basicsuite/advancedcustommaterial/shaders/gl3/coordinatesystems.inc b/basicsuite/advancedcustommaterial/shaders/gl3/coordinatesystems.inc new file mode 100644 index 0000000..ed3d2cb --- /dev/null +++ b/basicsuite/advancedcustommaterial/shaders/gl3/coordinatesystems.inc @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +mat3 calcWorldSpaceToTangentSpaceMatrix(const in vec3 wNormal, const in 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) + 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. + 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 + mat3 tangentToWorldMatrix = mat3(wFixedTangent, wBinormal, wNormal); + mat3 worldToTangentMatrix = transpose(tangentToWorldMatrix); + return worldToTangentMatrix; +} + diff --git a/basicsuite/advancedcustommaterial/shaders/gl3/light.inc.frag b/basicsuite/advancedcustommaterial/shaders/gl3/light.inc.frag new file mode 100644 index 0000000..0b64263 --- /dev/null +++ b/basicsuite/advancedcustommaterial/shaders/gl3/light.inc.frag @@ -0,0 +1,25 @@ +const int MAX_LIGHTS = 8; +const int TYPE_POINT = 0; +const int TYPE_DIRECTIONAL = 1; +const int TYPE_SPOT = 2; +struct Light { + int type; + vec3 position; + vec3 color; + float intensity; + vec3 direction; + float constantAttenuation; + float linearAttenuation; + float quadraticAttenuation; + float cutOffAngle; +}; +uniform Light lights[MAX_LIGHTS]; +uniform int lightCount; + +// Pre-convolved environment maps +struct EnvironmentLight { + samplerCube irradiance; // For diffuse contribution + samplerCube specular; // For specular contribution +}; +uniform EnvironmentLight envLight; +uniform int envLightCount = 0; diff --git a/basicsuite/advancedcustommaterial/shaders/gl3/phong.inc.frag b/basicsuite/advancedcustommaterial/shaders/gl3/phong.inc.frag new file mode 100644 index 0000000..47a6ecd --- /dev/null +++ b/basicsuite/advancedcustommaterial/shaders/gl3/phong.inc.frag @@ -0,0 +1,138 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#pragma include light.inc.frag + +void adsModel(const in vec3 worldPos, + const in vec3 worldNormal, + const in vec3 worldView, + const in float shininess, + out vec3 diffuseColor, + out vec3 specularColor) +{ + diffuseColor = vec3(0.0); + specularColor = vec3(0.0); + + // We perform all work in world space + vec3 n = normalize(worldNormal); + 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 + + // Light position is already in world space + vec3 sUnnormalized = lights[i].position - worldPos; + s = normalize(sUnnormalized); // Light direction + + // 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 already + if (lights[i].type == TYPE_SPOT) { + // Check if fragment is inside or outside of the spot light cone + if (degrees(acos(dot(-s, lights[i].direction))) > lights[i].cutOffAngle) + sDotN = 0.0; + } + } + } else { + // Directional lights + // The light direction is in world space already + s = normalize(-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 world space + specular = normFactor * pow(max(dot(r, worldView), 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; + } +} + +vec4 phongFunction(const in vec4 ambient, + const in vec4 diffuse, + const in vec4 specular, + const in float shininess, + const in vec3 worldPosition, + const in vec3 worldView, + const in vec3 worldNormal) +{ + // Calculate the lighting model, keeping the specular component separate + vec3 diffuseColor, specularColor; + adsModel(worldPosition, worldNormal, worldView, shininess, diffuseColor, specularColor); + + // Combine spec with ambient+diffuse for final fragment color + vec3 color = (ambient.rgb + diffuseColor) * diffuse.rgb + + specularColor * specular.rgb; + + return vec4(color, diffuse.a); +} diff --git a/basicsuite/advancedcustommaterial/shaders/gl3/water.frag b/basicsuite/advancedcustommaterial/shaders/gl3/water.frag new file mode 100644 index 0000000..6e97223 --- /dev/null +++ b/basicsuite/advancedcustommaterial/shaders/gl3/water.frag @@ -0,0 +1,79 @@ +#version 150 core + +in vec3 worldPosition; +in vec3 worldNormal; +in vec4 worldTangent; +in vec2 texCoord; +in vec2 waveTexCoord; +in vec2 movtexCoord; +in vec2 multexCoord; +in vec2 skyTexCoord; + +in vec3 vpos; + +in vec3 color; + +uniform sampler2D diffuseTexture; +uniform sampler2D specularTexture; +uniform sampler2D normalTexture; +uniform sampler2D waveTexture; +uniform sampler2D skyTexture; +uniform sampler2D foamTexture; + +uniform float offsetx; +uniform float offsety; +uniform float specularity; +uniform float waveStrenght; +uniform vec4 ka; +uniform vec3 specularColor; +uniform float shininess; +uniform float normalAmount; +uniform vec3 eyePosition; + +out vec4 fragColor; + +#pragma include phong.inc.frag +#pragma include coordinatesystems.inc + +void main() +{ + // Move waveTexCoords + vec2 waveMovCoord = waveTexCoord; + waveMovCoord.x += offsetx; + waveMovCoord.y -= offsety; + vec4 wave = texture(waveTexture, waveMovCoord); + + //Wiggle the newCoord by r and b colors of waveTexture + vec2 newCoord = texCoord; + newCoord.x += wave.r * waveStrenght; + newCoord.y -= wave.b * waveStrenght; + + // Sample the textures at the interpolated texCoords + // Use default texCoord for diffuse (it does not move on x or y, so it can be used as "ground under the water"). + vec4 diffuseTextureColor = texture(diffuseTexture, texCoord); + // 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 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); + vec3 worldView = normalize(eyePosition - worldPosition); + + vec4 diffuse = vec4(diffuseTextureColor.rgb, vpos.y); + vec4 specular = vec4(specularTextureColor.a*specularity); + vec4 outputColor = phongFunction(ka, diffuse, specular, shininess, worldPosition, worldView, wNormal); + + outputColor += vec4(skycolor.rgb, vpos.y); + outputColor += (foamTextureColor.rgba*vpos.y); + + fragColor = vec4(outputColor.rgb,1.0); +} + diff --git a/basicsuite/advancedcustommaterial/shaders/gl3/water.vert b/basicsuite/advancedcustommaterial/shaders/gl3/water.vert new file mode 100644 index 0000000..3af37e9 --- /dev/null +++ b/basicsuite/advancedcustommaterial/shaders/gl3/water.vert @@ -0,0 +1,73 @@ +#version 150 core + +in vec3 vertexPosition; +in vec3 vertexNormal; +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 vec3 vpos; + +uniform mat4 modelMatrix; +uniform mat3 modelNormalMatrix; +uniform mat4 mvp; + +uniform float offsetx; +uniform float offsety; +uniform float vertYpos; +uniform float texCoordScale; +uniform float waveheight; +uniform float waveRandom; + + +void main() +{ + // Scale texture coordinates for for fragment shader + texCoord = vertexTexCoord * texCoordScale; + movtexCoord = vertexTexCoord * texCoordScale; + multexCoord = vertexTexCoord * (texCoordScale*0.5); + waveTexCoord = vertexTexCoord * (texCoordScale * 6); + skyTexCoord = vertexTexCoord * (texCoordScale * 0.2); + + // Add Animated x and y Offset to SKY, MOV and MUL texCoords + movtexCoord = vec2(texCoord.x+offsetx,texCoord.y+offsety); + multexCoord = vec2(texCoord.x-offsetx,texCoord.y+offsety); + skyTexCoord = vec2(texCoord.x-(offsetx/2),texCoord.y-(offsety/2)); + + // 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; + + // Calculate animated vertex positions + + float sinPos = (vertexPosition.z)+(vertexPosition.x); + float sinPos2 = (vertexPosition.y/2)+(vertexPosition.z); + vec3 vertMod = vec3(vertexPosition.x,vertexPosition.y,vertexPosition.z); + + vertMod = vec3(vertMod.x+=sin(vertYpos*2.2-sinPos2)*waveheight, + vertMod.y=sin(vertYpos*2.2+sinPos)*waveheight, + vertMod.z-=sin(vertYpos*2.2-cos(sinPos2))*waveheight); + + vec3 vertModCom = vec3(vertMod.x+=cos(vertYpos*2.2-cos(sinPos2))*waveheight, + vertMod.y=sin(vertYpos*2.2+cos(sinPos))*waveheight, + vertMod.z-=cos(vertYpos*2.2-cos(sinPos))*waveheight); + + + // Add wave animation only to vertices above world pos.y zero + if(vertexPosition.y < 0.0){vertModCom = vertexPosition;} + else{vertModCom = vertModCom;} + + vpos = vertModCom; + + // Calculate vertex position in clip coordinates + gl_Position = mvp * vec4(vertModCom, 1.0); +} |