| 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 |
uniform highp mat4 viewMatrix; |
| 6 |
|
| 7 |
uniform highp vec3 lightPosition; |
| 8 |
uniform highp vec3 lightIntensity; |
| 9 |
|
| 10 |
uniform highp vec3 ka; // Ambient reflectivity |
| 11 |
uniform highp float shininess; // Specular shininess factor |
| 12 |
uniform highp float opacity; // Alpha channel |
| 13 |
|
| 14 |
uniform sampler2D diffuseTexture; |
| 15 |
uniform sampler2D specularTexture; |
| 16 |
uniform sampler2D normalTexture; |
| 17 |
|
| 18 |
varying highp vec4 positionInLightSpace; |
| 19 |
|
| 20 |
varying highp vec3 lightDir; |
| 21 |
varying highp vec3 viewDir; |
| 22 |
varying highp vec2 texCoord; |
| 23 |
|
| 24 |
highp vec3 dsbModel(const highp vec3 norm, const highp vec2 flipYTexCoord) |
| 25 |
{ |
| 26 |
// Reflection of light direction about normal |
| 27 |
highp vec3 r = reflect(-lightDir, norm); |
| 28 |
|
| 29 |
highp vec3 diffuseColor = texture2D(diffuseTexture, flipYTexCoord).rgb; |
| 30 |
highp vec3 specularColor = texture2D(specularTexture, flipYTexCoord).rgb; |
| 31 |
|
| 32 |
// Calculate the ambient contribution |
| 33 |
highp vec3 ambient = lightIntensity * ka * diffuseColor; |
| 34 |
|
| 35 |
// Calculate the diffuse contribution |
| 36 |
highp float sDotN = max(dot(lightDir, norm), 0.0); |
| 37 |
highp vec3 diffuse = lightIntensity * diffuseColor * sDotN; |
| 38 |
|
| 39 |
// Calculate the specular highlight contribution |
| 40 |
highp vec3 specular = vec3(0.0); |
| 41 |
if (sDotN > 0.0) |
| 42 |
specular = (lightIntensity * (shininess / (8.0 * 3.14))) * pow(max(dot(r, viewDir), 0.0), shininess); |
| 43 |
|
| 44 |
specular *= specularColor; |
| 45 |
|
| 46 |
return ambient + diffuse + specular; |
| 47 |
} |
| 48 |
|
| 49 |
void main() |
| 50 |
{ |
| 51 |
highp vec2 flipYTexCoord = texCoord; |
| 52 |
flipYTexCoord.y = 1.0 - texCoord.y; |
| 53 |
|
| 54 |
// Sample the textures at the interpolated texCoords |
| 55 |
highp vec4 normal = 2.0 * texture2D(normalTexture, flipYTexCoord) - vec4(1.0); |
| 56 |
|
| 57 |
highp vec3 result = dsbModel(normalize(normal.xyz), flipYTexCoord); |
| 58 |
|
| 59 |
// Combine spec with ambient+diffuse for final fragment color |
| 60 |
gl_FragColor = vec4(result, opacity); |
| 61 |
} |