| 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 vec3 ks; // Specular reflectivity |
| 12 |
uniform highp float shininess; // Specular shininess factor |
| 13 |
uniform highp float opacity; // Alpha channel |
| 14 |
|
| 15 |
uniform sampler2D diffuseTexture; |
| 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 dbModel(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 |
|
| 31 |
// Calculate the ambient contribution |
| 32 |
highp vec3 ambient = lightIntensity * ka * diffuseColor; |
| 33 |
|
| 34 |
// Calculate the diffuse contribution |
| 35 |
highp float sDotN = max(dot(lightDir, norm), 0.0); |
| 36 |
highp vec3 diffuse = lightIntensity * diffuseColor * sDotN; |
| 37 |
|
| 38 |
// Calculate the specular highlight contribution |
| 39 |
highp vec3 specular = vec3(0.0); |
| 40 |
if (sDotN > 0.0) |
| 41 |
specular = (lightIntensity * ks) * pow(max(dot(r, viewDir), 0.0), shininess); |
| 42 |
|
| 43 |
return ambient + diffuse + specular; |
| 44 |
} |
| 45 |
|
| 46 |
void main() |
| 47 |
{ |
| 48 |
highp vec2 flipYTexCoord = texCoord; |
| 49 |
flipYTexCoord.y = 1.0 - texCoord.y; |
| 50 |
|
| 51 |
highp vec4 normal = 2.0 * texture2D(normalTexture, flipYTexCoord) - vec4(1.0); |
| 52 |
|
| 53 |
highp vec3 result = dbModel(normalize(normal.xyz), flipYTexCoord); |
| 54 |
|
| 55 |
gl_FragColor = vec4(result, opacity); |
| 56 |
} |