diff options
author | Kenneth Graunke <kenneth@whitecape.org> | 2010-11-10 15:43:06 -0800 |
---|---|---|
committer | Kenneth Graunke <kenneth@whitecape.org> | 2010-11-17 15:09:43 -0800 |
commit | 495488e2136a7fa697fa083fbd3f19b74884aa48 (patch) | |
tree | 3bafba8ba77f982c787ea5cdec124125f505db61 | |
parent | 047491df37025f1a783241c4014ccb5e34b22b74 (diff) |
Add environment mapping.
Still need to combine this with the bump mapping.
-rwxr-xr-x | src/main.cpp | 6 | ||||
-rwxr-xr-x | src/phong.frag | 6 | ||||
-rwxr-xr-x | src/phong.vert | 22 |
3 files changed, 32 insertions, 2 deletions
diff --git a/src/main.cpp b/src/main.cpp index 666c300..5fd429c 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -101,8 +101,10 @@ static GLUmat4 projection_matrix; static GLuint prog; static GLint mvp_location; static GLint mv_location; +static GLint m_location; static GLint s_location; static GLint sampler_location; +static GLint eye_location; static GLuint cubemap; @@ -141,7 +143,9 @@ Redisplay(const patch_set_info *info) glUniform1f(s_location, specular_exponent[i]); glUniformMatrix4fv(mvp_location, 1, false, (float *) &mvp); glUniformMatrix4fv(mv_location, 1, false, (float *) &mv); + glUniformMatrix4fv(m_location, 1, false, (float *) &m); glUniform1i(sampler_location, 0); + glUniform4fv(eye_location, 4, (float *) &eye); glMultiDrawElements(info->mode, info->count, info->type, (const GLvoid**) info->indices, @@ -266,6 +270,8 @@ load_and_compile_program_code(const char *shader) glUseProgram(prog); mvp_location = glGetUniformLocation(prog, "mvp"); mv_location = glGetUniformLocation(prog, "mv"); + m_location = glGetUniformLocation(prog, "m"); + eye_location = glGetUniformLocation(prog, "eye"); s_location = glGetUniformLocation(prog, "s"); sampler_location = glGetUniformLocation(prog, "sampler"); } else { diff --git a/src/phong.frag b/src/phong.frag index a21f8fa..b532b54 100755 --- a/src/phong.frag +++ b/src/phong.frag @@ -1,5 +1,8 @@ uniform samplerCube sampler; +varying vec3 normal_ws; +varying vec3 eye_direction_ws; + varying vec3 light_ss; varying vec3 eye_ss; varying vec2 uv_coord; @@ -38,8 +41,11 @@ void main(void) vec3 v = normalize(eye_ss); vec3 h = normalize(l + v); vec3 n = perturbed_normal(); + vec3 i = reflect(eye_ss, n); + //vec3 diff = vec3(textureCube(sampler, i)); vec3 diff = base_color * dot(n, l); float spec = pow(dot(n, h), s); gl_FragColor = step(0.0, dot(n, l)) * vec4(diff + vec3(spec), 1.0); + //gl_FragColor = textureCube(sampler, reflect(eye_direction_ws, normal_ws)); } diff --git a/src/phong.vert b/src/phong.vert index 3a57004..963c57b 100755 --- a/src/phong.vert +++ b/src/phong.vert @@ -1,10 +1,17 @@ -uniform mat4 mvp; -uniform mat4 mv; +uniform mat4 mvp; // eye -> screen +uniform mat4 mv; // world -> eye +uniform mat4 m; // object -> world +// These three are in object space attribute vec4 normal; attribute vec4 tangent; +attribute vec4 eye; // Eye position in object space + attribute vec2 uv; +varying vec3 normal_ws; +varying vec3 eye_direction_ws; + // Vector from the light to the vertex transformed to surface space. varying vec3 light_ss; @@ -29,6 +36,17 @@ void main(void) vec3 vertex_pos_es = vec3(mv * gl_Vertex); vec3 light_direction_es = light_pos_es - vertex_pos_es; + /* Surface space vectors for lighting */ light_ss = normalize(light_direction_es * tbn); eye_ss = -normalize(vertex_pos_es * tbn); + + /* World space vectors for environment mapping + * FIXME: eye should already be in world space, so I'm not sure why + * I have to do m * eye here. But it looks wrong otherwise. + */ + vec3 vertex_pos_ws = vec3(m * gl_Vertex); + vec3 eye_pos_ws = vec3(m * eye); + + normal_ws = vec3(m * normal); + eye_direction_ws = eye_pos_ws - vertex_pos_ws; } |