| 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 |
|
| 17 |
varying highp vec4 positionInLightSpace; |
| 18 |
|
| 19 |
varying highp vec3 position; |
| 20 |
varying highp vec3 normal; |
| 21 |
varying highp vec2 texCoord; |
| 22 |
|
| 23 |
highp vec3 dsModel(const highp vec2 flipYTexCoord) |
| 24 |
{ |
| 25 |
// Calculate the vector from the light to the fragment |
| 26 |
highp vec3 s = normalize(vec3(viewMatrix * vec4(lightPosition, 1.0)) - position); |
| 27 |
|
| 28 |
// Calculate the vector from the fragment to the eye position |
| 29 |
// (origin since this is in "eye" or "camera" space) |
| 30 |
highp vec3 v = normalize(-position); |
| 31 |
|
| 32 |
// Reflect the light beam using the normal at this fragment |
| 33 |
highp vec3 r = reflect(-s, normal); |
| 34 |
|
| 35 |
// Calculate the diffuse component |
| 36 |
highp float diffuse = max(dot(s, normal), 0.0); |
| 37 |
|
| 38 |
// Calculate the specular component |
| 39 |
highp float specular = 0.0; |
| 40 |
if (dot(s, normal) > 0.0) |
| 41 |
specular = (shininess / (8.0 * 3.14)) * pow(max(dot(r, v), 0.0), shininess); |
| 42 |
|
| 43 |
// Lookup diffuse and specular factors |
| 44 |
highp vec3 diffuseColor = texture2D(diffuseTexture, flipYTexCoord).rgb; |
| 45 |
highp vec3 specularColor = texture2D(specularTexture, flipYTexCoord).rgb; |
| 46 |
|
| 47 |
// Combine the ambient, diffuse and specular contributions |
| 48 |
return lightIntensity * ((ka + diffuse) * diffuseColor + specular * specularColor); |
| 49 |
} |
| 50 |
|
| 51 |
void main() |
| 52 |
{ |
| 53 |
highp vec2 flipYTexCoord = texCoord; |
| 54 |
flipYTexCoord.y = 1.0 - texCoord.y; |
| 55 |
|
| 56 |
highp vec3 result = dsModel(flipYTexCoord); |
| 57 |
|
| 58 |
highp float alpha = opacity * texture2D(diffuseTexture, flipYTexCoord).a; |
| 59 |
|
| 60 |
gl_FragColor = vec4(result, alpha); |
| 61 |
} |