changeset 28:d2839cbfaad8

Some work on the default shaders.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 22 Nov 2019 08:39:48 +0200
parents 097184bd34a8
children 5c7f63fe5c19
files shader.frag shader.vert
diffstat 2 files changed, 49 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/shader.frag	Fri Nov 22 08:39:27 2019 +0200
+++ b/shader.frag	Fri Nov 22 08:39:48 2019 +0200
@@ -1,9 +1,42 @@
-void main()
+varying vec3 snormal;
+varying vec3 svertex;
+uniform int nlights;
+
+
+float maxdot(vec3 sveca, vec3 svecb)
+{
+  return max(dot(sveca, svecb), 0.0);
+}
+
+
+vec4 mclamp(vec4 svec)
+{
+  return clamp(svec, 0.0, 1.0);
+}
+
+
+void main(void)
 {
-gl_FragColor = vec4(
-	gl_FragCoord.x * 0.1,
-	gl_FragCoord.y * 0.001,
-	0.0,
-	1.0);
-};
+  vec3 snormal = normalize(snormal);
+  vec3 srcVec = normalize(-svertex);
+//vec4 poo = vec4(0.0, 0.0, 0.0, 0.0);
+  vec4 poo = gl_FragCoord;
+//poo += vec4(snormal.x, snormal.y, snormal.z, 0.0);
+  vec4 finalColor = vec4(poo.x * 0.1, poo.y * 0.001, poo.z, 1.0);
+
+  for (int i = 0; i < nlights; i++)
+  {
+    vec3 lightVec = normalize(gl_LightSource[i].position.xyz - svertex);
+    vec3 refVec = normalize(-reflect(lightVec, snormal));
 
+    vec4 ambient = gl_FrontLightProduct[i].ambient;
+    vec4 diffuse = mclamp(gl_FrontLightProduct[i].diffuse * maxdot(snormal, lightVec));
+    vec4 specular = mclamp(
+	gl_FrontLightProduct[i].specular *
+	pow(maxdot(refVec, srcVec), gl_FrontMaterial.shininess));
+
+    finalColor += ambient + diffuse + specular;
+  }
+
+  gl_FragColor = gl_FrontLightModelProduct.sceneColor + finalColor;
+}
--- a/shader.vert	Fri Nov 22 08:39:27 2019 +0200
+++ b/shader.vert	Fri Nov 22 08:39:48 2019 +0200
@@ -1,4 +1,11 @@
-void main()
+varying vec3 snormal;
+varying vec3 svertex;
+
+
+void main(void)
 {
-gl_Position = ftransform();
+   svertex = vec3(gl_ModelViewMatrix * gl_Vertex);
+   snormal = normalize(gl_NormalMatrix * gl_Normal);
+
+   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
 }