summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaszlo Agocs <laszlo.agocs@theqtcompany.com>2015-11-30 17:50:01 +0100
committerLaszlo Agocs <laszlo.agocs@theqtcompany.com>2015-12-01 11:45:30 +0000
commit407f1845e3ba4e5c745ce071d16c3826bd00c62e (patch)
tree5155276599448707f98afeb77600f7901b291027
parentd687399d5c6e7bf2375326d12cdbd00381871118 (diff)
Add light support to GL2/ES2 default shader
However, take only the first light into account in order to keep pure ES2 devices working. We will need to figure something out later. NB this leads to a funny but totally correct effect in examples with multiple animated lights since the light that is taken into account is the one closest to the center of the lit entity. Change-Id: If2ad7c0bd5db177e48d763e1e1c86e65add45c8d Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
-rw-r--r--src/render/render.qrc1
-rw-r--r--src/render/shaders/es2/default.frag24
-rw-r--r--src/render/shaders/es2/light.inc.frag43
-rw-r--r--src/render/shaders/gl3/default.frag2
-rw-r--r--src/render/shaders/gl3/default.vert4
5 files changed, 57 insertions, 17 deletions
diff --git a/src/render/render.qrc b/src/render/render.qrc
index 3cdcaf138..95b87626d 100644
--- a/src/render/render.qrc
+++ b/src/render/render.qrc
@@ -5,6 +5,7 @@
<file>shaders/gl3/light.inc.frag</file>
<file>shaders/es2/default.frag</file>
<file>shaders/es2/default.vert</file>
+ <file>shaders/es2/light.inc.frag</file>
<file>shaders/gl3/phong.vert</file>
<file>shaders/gl3/phong.frag</file>
<file>shaders/es2/phong.vert</file>
diff --git a/src/render/shaders/es2/default.frag b/src/render/shaders/es2/default.frag
index b9c7c8919..b61723930 100644
--- a/src/render/shaders/es2/default.frag
+++ b/src/render/shaders/es2/default.frag
@@ -1,22 +1,20 @@
#define FP highp
-uniform FP vec4 lightPosition;
-uniform FP vec3 lightIntensity;
-uniform FP vec3 kd; // Diffuse reflectivity
-uniform FP vec3 ka; // Ambient reflectivity
+uniform FP vec3 ka; // Ambient reflectivity
+uniform FP vec3 kd; // Diffuse reflectivity
+uniform FP vec3 ks; // Specular reflectivity
+uniform FP float shininess; // Specular shininess factor
+
+uniform FP vec3 eyePosition;
varying FP vec3 position;
varying FP vec3 normal;
+#pragma include light.inc.frag
+
void main()
{
- // Calculate the vector from the light to the fragment
- FP vec3 s = normalize( vec3( lightPosition ) - position );
-
- // Calculate the diffuse lighting factor
- FP vec3 n = normalize( normal );
- FP float diffuse = max( dot( s, n ), 0.0 );
-
- // Multiply by incoming light intensity
- gl_FragColor = vec4( lightIntensity * (kd * diffuse + ka), 1.0 );
+ FP vec3 diffuseColor, specularColor;
+ calculateLightColors(position, normal, eyePosition, shininess, diffuseColor, specularColor);
+ gl_FragColor = vec4( ka + kd * diffuseColor + ks * specularColor, 1.0 );
}
diff --git a/src/render/shaders/es2/light.inc.frag b/src/render/shaders/es2/light.inc.frag
new file mode 100644
index 000000000..d5376e934
--- /dev/null
+++ b/src/render/shaders/es2/light.inc.frag
@@ -0,0 +1,43 @@
+const int MAX_LIGHTS = 8;
+struct Light {
+ FP vec3 position;
+ FP vec3 color;
+ FP float intensity;
+ FP vec3 direction;
+ FP vec3 attenuation;
+// FP float cutOffAngle;
+};
+uniform Light lights[MAX_LIGHTS];
+uniform int lightCount;
+
+void calculateLightColors(FP vec3 vpos, FP vec3 vnormal, FP vec3 eye, FP float shininess, out FP vec3 diffuseColor, out FP vec3 specularColor)
+{
+ diffuseColor = vec3(0.0);
+ specularColor = vec3(0.0);
+
+ FP vec3 n = normalize( vnormal );
+
+ // TODO dynamic indexing may not be supported with GLSL 1.00 so take only the first light into account
+ FP vec3 lightDir = lights[0].direction;
+ FP float att = 1.0;
+ if (length( lightDir ) == 0.0) {
+ lightDir = lights[0].position - vpos;
+ if (length( lights[0].attenuation ) != 0.0) {
+ FP float dist = length(lightDir);
+ att = 1.0 / (lights[0].attenuation.x + lights[0].attenuation.y * dist + lights[0].attenuation.z * dist * dist);
+ }
+ }
+
+ FP vec3 s = normalize( lightDir );
+ FP float diffuse = max( dot( s, n ), 0.0 );
+
+ FP float specular = 0.0;
+ if (diffuse > 0.0 && shininess > 0.0) {
+ FP vec3 r = reflect( -s, n );
+ FP vec3 v = normalize( eye - vpos );
+ specular = ( shininess / ( 8.0 * 3.14 ) ) * pow( max( dot( r, v ), 0.0 ), shininess );
+ }
+
+ diffuseColor += att * lights[0].intensity * diffuse * lights[0].color;
+ specularColor += specular;
+}
diff --git a/src/render/shaders/gl3/default.frag b/src/render/shaders/gl3/default.frag
index 35c337f62..febbc5eae 100644
--- a/src/render/shaders/gl3/default.frag
+++ b/src/render/shaders/gl3/default.frag
@@ -1,4 +1,4 @@
-#version 150
+#version 150 core
uniform vec3 ka; // Ambient reflectivity
uniform vec3 kd; // Diffuse reflectivity
diff --git a/src/render/shaders/gl3/default.vert b/src/render/shaders/gl3/default.vert
index 9513c9c2d..f0a134702 100644
--- a/src/render/shaders/gl3/default.vert
+++ b/src/render/shaders/gl3/default.vert
@@ -1,8 +1,6 @@
-#version 150
+#version 150 core
-//layout (location = 0)
in vec3 vertexPosition;
-//layout (location = 1)
in vec3 vertexNormal;
out vec3 position;