Browse Source

Fix example shaders;

bjorn 6 years ago
parent
commit
4d660e879a

+ 2 - 0
examples/Mesh/main.lua

@@ -18,6 +18,7 @@ local function makeShader(prefix)
   return lovr.graphics.newShader(prefix .. [[
   return lovr.graphics.newShader(prefix .. [[
 out vec3 lightDirection;
 out vec3 lightDirection;
 out vec3 normalDirection;
 out vec3 normalDirection;
+out vec3 vertexPosition;
 
 
 vec3 lightPosition = vec3(10, 10, 3);
 vec3 lightPosition = vec3(10, 10, 3);
 
 
@@ -29,6 +30,7 @@ vec4 position(mat4 projection, mat4 transform, vec4 _vertex) {
 
 
   lightDirection = normalize(vec3(vLight - vVertex));
   lightDirection = normalize(vec3(vLight - vVertex));
   normalDirection = normalize(lovrNormalMatrix * lovrNormal);
   normalDirection = normalize(lovrNormalMatrix * lovrNormal);
+  vertexPosition = vVertex.xyz;
 
 
   return projection * transform * vertex;
   return projection * transform * vertex;
 }
 }

+ 3 - 2
examples/Mesh/shader.lua

@@ -1,6 +1,7 @@
 return [[
 return [[
 in vec3 lightDirection;
 in vec3 lightDirection;
 in vec3 normalDirection;
 in vec3 normalDirection;
+in vec3 vertexPosition;
 
 
 vec3 cAmbient = vec3(.25);
 vec3 cAmbient = vec3(.25);
 vec3 cDiffuse = vec3(1);
 vec3 cDiffuse = vec3(1);
@@ -12,7 +13,7 @@ vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
 
 
   if (diffuse > 0.) {
   if (diffuse > 0.) {
     vec3 r = reflect(lightDirection, normalDirection);
     vec3 r = reflect(lightDirection, normalDirection);
-    vec3 viewDirection = normalize(-vec3(gl_FragCoord));
+    vec3 viewDirection = normalize(-vertexPosition);
 
 
     float specularAngle = max(dot(r, viewDirection), 0.);
     float specularAngle = max(dot(r, viewDirection), 0.);
     specular = pow(specularAngle, 5.);
     specular = pow(specularAngle, 5.);
@@ -21,4 +22,4 @@ vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
   vec3 cFinal = pow(clamp(vec3(diffuse) * cDiffuse + vec3(specular) * cSpecular, cAmbient, vec3(1.)), vec3(.4545));
   vec3 cFinal = pow(clamp(vec3(diffuse) * cDiffuse + vec3(specular) * cSpecular, cAmbient, vec3(1.)), vec3(.4545));
   return vec4(cFinal, 1.) * graphicsColor * texture(image, uv);
   return vec4(cFinal, 1.) * graphicsColor * texture(image, uv);
 }
 }
-]]
+]]

+ 8 - 3
examples/Physics/shader.lua

@@ -1,6 +1,9 @@
 return lovr.graphics.newShader([[
 return lovr.graphics.newShader([[
-out vec3 lightDirection;
+
+// All of these are in view-space.
+out vec3 lightDirection; // A vector from the vertex to the light
 out vec3 normalDirection;
 out vec3 normalDirection;
+out vec3 vertexPosition;
 
 
 vec3 lightPosition = vec3(0, 10, 3);
 vec3 lightPosition = vec3(0, 10, 3);
 
 
@@ -10,15 +13,17 @@ vec4 position(mat4 projection, mat4 transform, vec4 vertex) {
 
 
   lightDirection = normalize(vec3(vLight - vVertex));
   lightDirection = normalize(vec3(vLight - vVertex));
   normalDirection = normalize(lovrNormalMatrix * lovrNormal);
   normalDirection = normalize(lovrNormalMatrix * lovrNormal);
+  vertexPosition = vVertex.xyz;
 
 
   return projection * transform * vertex;
   return projection * transform * vertex;
 }
 }
 ]], [[
 ]], [[
 in vec3 lightDirection;
 in vec3 lightDirection;
 in vec3 normalDirection;
 in vec3 normalDirection;
+in vec3 vertexPosition;
 
 
 vec3 cAmbient = vec3(.25);
 vec3 cAmbient = vec3(.25);
-vec3 cDiffuse = vec3(.75);
+vec3 cDiffuse = vec3(1);
 vec3 cSpecular = vec3(.35);
 vec3 cSpecular = vec3(.35);
 
 
 vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
 vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
@@ -27,7 +32,7 @@ vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
 
 
   if (diffuse > 0.) {
   if (diffuse > 0.) {
     vec3 r = reflect(lightDirection, normalDirection);
     vec3 r = reflect(lightDirection, normalDirection);
-    vec3 viewDirection = normalize(-vec3(gl_FragCoord));
+    vec3 viewDirection = normalize(-vertexPosition);
 
 
     float specularAngle = max(dot(r, viewDirection), 0.);
     float specularAngle = max(dot(r, viewDirection), 0.);
     specular = pow(specularAngle, 5.);
     specular = pow(specularAngle, 5.);

+ 7 - 2
examples/Primitives/shader.lua

@@ -1,6 +1,9 @@
 return lovr.graphics.newShader([[
 return lovr.graphics.newShader([[
-out vec3 lightDirection;
+
+// All of these are in view-space.
+out vec3 lightDirection; // A vector from the vertex to the light
 out vec3 normalDirection;
 out vec3 normalDirection;
+out vec3 vertexPosition;
 
 
 vec3 lightPosition = vec3(0, 10, 3);
 vec3 lightPosition = vec3(0, 10, 3);
 
 
@@ -10,12 +13,14 @@ vec4 position(mat4 projection, mat4 transform, vec4 vertex) {
 
 
   lightDirection = normalize(vec3(vLight - vVertex));
   lightDirection = normalize(vec3(vLight - vVertex));
   normalDirection = normalize(lovrNormalMatrix * lovrNormal);
   normalDirection = normalize(lovrNormalMatrix * lovrNormal);
+  vertexPosition = vVertex.xyz;
 
 
   return projection * transform * vertex;
   return projection * transform * vertex;
 }
 }
 ]], [[
 ]], [[
 in vec3 lightDirection;
 in vec3 lightDirection;
 in vec3 normalDirection;
 in vec3 normalDirection;
+in vec3 vertexPosition;
 
 
 vec3 cAmbient = vec3(.25);
 vec3 cAmbient = vec3(.25);
 vec3 cDiffuse = vec3(1);
 vec3 cDiffuse = vec3(1);
@@ -27,7 +32,7 @@ vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
 
 
   if (diffuse > 0.) {
   if (diffuse > 0.) {
     vec3 r = reflect(lightDirection, normalDirection);
     vec3 r = reflect(lightDirection, normalDirection);
-    vec3 viewDirection = normalize(-vec3(gl_FragCoord));
+    vec3 viewDirection = normalize(-vertexPosition);
 
 
     float specularAngle = max(dot(r, viewDirection), 0.);
     float specularAngle = max(dot(r, viewDirection), 0.);
     specular = pow(specularAngle, 5.);
     specular = pow(specularAngle, 5.);