| 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 vec3 ks; // Specular reflectivity |
| 14 |
uniform float shininess; // Specular shininess factor |
| 15 |
uniform float opacity; // Alpha channel |
| 16 |
|
| 17 |
uniform sampler2D diffuseTexture; |
| 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 dbModel(const in vec3 norm, const in vec2 flipYTexCoord, 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, flipYTexCoord).rgb; |
| 32 |
|
| 33 |
// Calculate the ambient contribution |
| 34 |
vec3 ambient = lightIntensity * ka * diffuseColor; |
| 35 |
|
| 36 |
// Calculate the diffuse contribution |
| 37 |
float sDotN = max(dot(lightDir, norm), 0.0); |
| 38 |
vec3 diffuse = lightIntensity * diffuseColor * sDotN; |
| 39 |
|
| 40 |
// Sum the ambient and diffuse contributions |
| 41 |
ambientAndDiff = ambient + diffuse; |
| 42 |
|
| 43 |
// Calculate the specular highlight contribution |
| 44 |
spec = vec3(0.0); |
| 45 |
if (sDotN > 0.0) |
| 46 |
spec = (lightIntensity * ks) * pow(max(dot(r, viewDir), 0.0), shininess); |
| 47 |
} |
| 48 |
|
| 49 |
void main() |
| 50 |
{ |
| 51 |
vec2 flipYTexCoord = texCoord; |
| 52 |
flipYTexCoord.y = 1.0 - texCoord.y; |
| 53 |
|
| 54 |
// Sample the textures at the interpolated texCoords |
| 55 |
vec4 normal = 2.0 * texture(normalTexture, flipYTexCoord) - vec4(1.0); |
| 56 |
|
| 57 |
vec3 result = lightIntensity * ka * texture(diffuseTexture, flipYTexCoord).rgb; |
| 58 |
|
| 59 |
// Calculate the lighting model, keeping the specular component separate |
| 60 |
vec3 ambientAndDiff, spec; |
| 61 |
dbModel(normalize(normal.xyz), flipYTexCoord, ambientAndDiff, spec); |
| 62 |
result = ambientAndDiff + spec; |
| 63 |
|
| 64 |
// Combine spec with ambient+diffuse for final fragment color |
| 65 |
fragColor = vec4(result, opacity); |
| 66 |
} |