| 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 450 core |
| 6 |
|
| 7 |
layout(std140, binding = 0) uniform qt3d_render_view_uniforms { |
| 8 |
mat4 viewMatrix; |
| 9 |
mat4 projectionMatrix; |
| 10 |
mat4 uncorrectedProjectionMatrix; |
| 11 |
mat4 clipCorrectionMatrix; |
| 12 |
mat4 viewProjectionMatrix; |
| 13 |
mat4 inverseViewMatrix; |
| 14 |
mat4 inverseProjectionMatrix; |
| 15 |
mat4 inverseViewProjectionMatrix; |
| 16 |
mat4 viewportMatrix; |
| 17 |
mat4 inverseViewportMatrix; |
| 18 |
vec4 textureTransformMatrix; |
| 19 |
vec3 eyePosition; |
| 20 |
float aspectRatio; |
| 21 |
float gamma; |
| 22 |
float exposure; |
| 23 |
float time; |
| 24 |
}; |
| 25 |
|
| 26 |
layout(std140, binding = 2) uniform extras_uniforms { |
| 27 |
float texCoordScale; |
| 28 |
vec3 ka; // Ambient reflectivity |
| 29 |
vec3 ks; // Specular reflectivity |
| 30 |
float shininess; // Specular shininess factor |
| 31 |
float opacity; // Alpha channel |
| 32 |
vec3 lightPosition; |
| 33 |
vec3 lightIntensity; |
| 34 |
}; |
| 35 |
|
| 36 |
layout(binding=3) uniform sampler2D diffuseTexture; |
| 37 |
layout(binding=4) uniform sampler2D normalTexture; |
| 38 |
layout(binding=5) uniform sampler2D specularTexture; |
| 39 |
|
| 40 |
layout(location = 0) in vec3 lightDir; |
| 41 |
layout(location = 1) in vec3 viewDir; |
| 42 |
layout(location = 2) in vec2 texCoord; |
| 43 |
|
| 44 |
layout(location = 0) out vec4 fragColor; |
| 45 |
|
| 46 |
void dsbModel(const in vec3 norm, const in vec2 flipXTexCoord, out vec3 ambientAndDiff, out vec3 spec) |
| 47 |
{ |
| 48 |
// Reflection of light direction about normal |
| 49 |
vec3 r = reflect(-lightDir, norm); |
| 50 |
|
| 51 |
vec3 diffuseColor = texture(diffuseTexture, flipXTexCoord).rgb; |
| 52 |
vec3 specularColor = texture(specularTexture, flipXTexCoord).rgb; |
| 53 |
|
| 54 |
// Calculate the ambient contribution |
| 55 |
vec3 ambient = lightIntensity * ka * diffuseColor; |
| 56 |
|
| 57 |
// Calculate the diffuse contribution |
| 58 |
float sDotN = max(dot(lightDir, norm), 0.0); |
| 59 |
vec3 diffuse = lightIntensity * diffuseColor * sDotN; |
| 60 |
|
| 61 |
// Sum the ambient and diffuse contributions |
| 62 |
ambientAndDiff = ambient + diffuse; |
| 63 |
|
| 64 |
// Calculate the specular highlight contribution |
| 65 |
spec = vec3(0.0); |
| 66 |
if (sDotN > 0.0) |
| 67 |
spec = (lightIntensity * (shininess / (8.0 * 3.14))) * pow(max(dot(r, viewDir), 0.0), shininess); |
| 68 |
|
| 69 |
spec *= specularColor; |
| 70 |
} |
| 71 |
|
| 72 |
void main() |
| 73 |
{ |
| 74 |
vec2 flipXTexCoord = texCoord; |
| 75 |
flipXTexCoord.x = 1.0 - texCoord.x; |
| 76 |
|
| 77 |
// Sample the textures at the interpolated texCoords |
| 78 |
vec4 normal = 2.0 * texture(normalTexture, flipXTexCoord) - vec4(1.0); |
| 79 |
|
| 80 |
// Calculate the lighting model, keeping the specular component separate |
| 81 |
vec3 ambientAndDiff, spec; |
| 82 |
dsbModel(normalize(normal.xyz), flipXTexCoord, ambientAndDiff, spec); |
| 83 |
vec3 result = ambientAndDiff + spec; |
| 84 |
|
| 85 |
// Combine spec with ambient+diffuse for final fragment color |
| 86 |
fragColor = vec4(result, opacity); |
| 87 |
} |