| 1 |
// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 2 |
// Copyright (C) 2017 The Qt Company Ltd. |
| 3 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 4 |
|
| 5 |
#version 150 core |
| 6 |
|
| 7 |
uniform mat4 viewMatrix; |
| 8 |
|
| 9 |
uniform vec3 lightPosition; |
| 10 |
uniform vec3 lightIntensity; |
| 11 |
|
| 12 |
uniform vec3 ka; // Ambient reflectivity |
| 13 |
uniform float shininess; // Specular shininess factor |
| 14 |
uniform float opacity; // Alpha channel |
| 15 |
|
| 16 |
uniform sampler2D diffuseTexture; |
| 17 |
uniform sampler2D specularTexture; |
| 18 |
uniform sampler2D normalTexture; |
| 19 |
|
| 20 |
in vec3 lightDir; |
| 21 |
in vec3 viewDir; |
| 22 |
in vec2 texCoord; |
| 23 |
|
| 24 |
out vec4 fragColor; |
| 25 |
|
| 26 |
void dsbModel(const in vec3 norm, const in vec2 flipXTexCoord, out vec3 ambientAndDiff, out vec3 spec) |
| 27 |
{ |
| 28 |
// Reflection of light direction about normal |
| 29 |
vec3 r = reflect(-lightDir, norm); |
| 30 |
|
| 31 |
vec3 diffuseColor = texture(diffuseTexture, flipXTexCoord).rgb; |
| 32 |
vec3 specularColor = texture(specularTexture, flipXTexCoord).rgb; |
| 33 |
|
| 34 |
// Calculate the ambient contribution |
| 35 |
vec3 ambient = lightIntensity * ka * diffuseColor; |
| 36 |
|
| 37 |
// Calculate the diffuse contribution |
| 38 |
float sDotN = max(dot(lightDir, norm), 0.0); |
| 39 |
vec3 diffuse = lightIntensity * diffuseColor * sDotN; |
| 40 |
|
| 41 |
// Sum the ambient and diffuse contributions |
| 42 |
ambientAndDiff = ambient + diffuse; |
| 43 |
|
| 44 |
// Calculate the specular highlight contribution |
| 45 |
spec = vec3(0.0); |
| 46 |
if (sDotN > 0.0) |
| 47 |
spec = (lightIntensity * (shininess / (8.0 * 3.14))) * pow(max(dot(r, viewDir), 0.0), shininess); |
| 48 |
|
| 49 |
spec *= specularColor; |
| 50 |
} |
| 51 |
|
| 52 |
void main() |
| 53 |
{ |
| 54 |
vec2 flipXTexCoord = texCoord; |
| 55 |
flipXTexCoord.x = 1.0 - texCoord.x; |
| 56 |
|
| 57 |
// Sample the textures at the interpolated texCoords |
| 58 |
vec4 normal = 2.0 * texture(normalTexture, flipXTexCoord) - vec4(1.0); |
| 59 |
|
| 60 |
// Calculate the lighting model, keeping the specular component separate |
| 61 |
vec3 ambientAndDiff, spec; |
| 62 |
dsbModel(normalize(normal.xyz), flipXTexCoord, ambientAndDiff, spec); |
| 63 |
vec3 result = ambientAndDiff + spec; |
| 64 |
|
| 65 |
// Combine spec with ambient+diffuse for final fragment color |
| 66 |
fragColor = vec4(result, opacity); |
| 67 |
} |