From cb72bd9b0291ba3dc0ee32fc00c7a3b5119b2e23 Mon Sep 17 00:00:00 2001 From: Dave Houlton Date: Fri, 30 Mar 2018 17:10:17 -0600 Subject: demos: Add some cube lighting Can stand it no longer. Added some minimal n-dot-L screen-space lighting. Change-Id: Ifaf664a5651f59d8904cbb064018c00d385ec3ed --- demos/cube.frag | 10 +++++++++- demos/cube.vert | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/demos/cube.frag b/demos/cube.frag index 70ba93ec..5bf6507a 100644 --- a/demos/cube.frag +++ b/demos/cube.frag @@ -24,7 +24,15 @@ layout (binding = 1) uniform sampler2D tex; layout (location = 0) in vec4 texcoord; +layout (location = 1) in vec3 frag_pos; layout (location = 0) out vec4 uFragColor; + +const vec3 lightDir= vec3(0.424, 0.566, 0.707); + void main() { - uFragColor = texture(tex, texcoord.xy); + vec3 dX = dFdx(frag_pos); + vec3 dY = dFdy(frag_pos); + vec3 normal = normalize(cross(dX,dY)); + float light = max(0.0, dot(lightDir, normal)); + uFragColor = light * texture(tex, texcoord.xy); } diff --git a/demos/cube.vert b/demos/cube.vert index 8178d838..63380326 100644 --- a/demos/cube.vert +++ b/demos/cube.vert @@ -28,9 +28,11 @@ layout(std140, binding = 0) uniform buf { } ubuf; layout (location = 0) out vec4 texcoord; +layout (location = 1) out vec3 frag_pos; void main() { texcoord = ubuf.attr[gl_VertexIndex]; gl_Position = ubuf.MVP * ubuf.position[gl_VertexIndex]; + frag_pos = gl_Position.xyz; } -- cgit v1.2.3