From 24a7295dccfd559501753950509964869f88bec1 Mon Sep 17 00:00:00 2001 From: Kenneth Graunke Date: Wed, 27 Oct 2010 20:52:34 -0700 Subject: Convert shaders to work in surface space rather than eye space. --- src/phong.frag | 21 +++++++++------------ src/phong.vert | 25 ++++++++++++++++++------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/phong.frag b/src/phong.frag index 95eaaa7..a43a61d 100755 --- a/src/phong.frag +++ b/src/phong.frag @@ -1,23 +1,20 @@ -varying vec4 normal_es; -varying vec4 position_es; +varying vec3 light_ss; +varying vec3 eye_ss; const vec4 diffuse_color = vec4(1.0, 0.0, 0.0, 1.0); + /* Specular exponent. */ uniform float s; -/* Position of the light in eye-space. - */ -const vec4 light_es = vec4(0.0, 3.0, 0.0, 1.0); - void main(void) { - vec3 l = normalize((light_es - position_es).xyz); - vec3 v = normalize(-position_es.xyz); + vec3 l = normalize(light_ss); + vec3 v = normalize(eye_ss); vec3 h = normalize(l + v); - float n_dot_l = dot(normal_es.xyz, l); - vec4 diff = diffuse_color * n_dot_l; - float spec = pow(dot(normal_es.xyz, h), s); + vec3 n = vec3(0, 0, 1); + vec4 diff = diffuse_color * dot(n, l); + float spec = pow(dot(n, h), s); - gl_FragColor = step(0.0, n_dot_l) * vec4(diff.xyz + vec3(spec), 1.0); + gl_FragColor = step(0.0, dot(n, l)) * vec4(diff.xyz + vec3(spec), 1.0); } diff --git a/src/phong.vert b/src/phong.vert index 26f6b25..a0c1fab 100755 --- a/src/phong.vert +++ b/src/phong.vert @@ -2,20 +2,31 @@ uniform mat4 mvp; uniform mat4 mv; attribute vec4 normal; +attribute vec4 tangent; attribute vec2 uv; -/* Normal transformed to eye space. - */ -varying vec4 normal_es; +// Vector from the light to the vertex transformed to surface space. +varying vec3 light_ss; + +// ??? +varying vec3 eye_ss; -/* Vertex position transformed to eye space. +/* Position of the light in eye-space. */ -varying vec4 position_es; +const vec3 light_pos_es = vec3(0.0, 3.0, 0.0); void main(void) { gl_Position = mvp * gl_Vertex; - normal_es = mv * normal; - position_es = mv * gl_Vertex; + vec3 tangent_es = vec3(mv * tangent); + vec3 normal_es = vec3(mv * normal); + vec3 bitangent_es = cross(normal_es, tangent_es); + mat3 tbn = mat3(tangent_es, bitangent_es, normal_es); + + vec3 vertex_pos_es = vec3(mv * gl_Vertex); + vec3 light_direction_es = light_pos_es - vertex_pos_es; + + light_ss = normalize(light_direction_es * tbn); + eye_ss = -normalize(vertex_pos_es * tbn); } -- cgit v1.2.3