summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenneth Graunke <kenneth@whitecape.org>2011-01-22 16:03:32 -0800
committerKenneth Graunke <kenneth@whitecape.org>2011-01-26 21:22:46 -0800
commitbfb61a78713327dc8ae70caf52538270f89afc77 (patch)
tree48376de3bcc1e1cc3ec8e9c5565f36346755b5d7
parenteebb3ac1dcceebfdad3db33ebc672346980f4177 (diff)
Move light to world space and draw the shadows from PoV of the light.
-rwxr-xr-xsrc/phong.vert8
-rwxr-xr-xsrc/scene.cpp18
2 files changed, 17 insertions, 9 deletions
diff --git a/src/phong.vert b/src/phong.vert
index c561488..fae74c1 100755
--- a/src/phong.vert
+++ b/src/phong.vert
@@ -2,8 +2,10 @@
uniform mat4 mvp; // object -> world -> eye -> screen
uniform mat4 mv; // object -> world -> eye
uniform mat4 m; // object -> world
+uniform mat4 v; // world -> eye
uniform vec4 eye; // Eye position in world space
+uniform vec4 light_pos_es; // Light position in world-space.
// These two are in object space
attribute vec4 normal;
@@ -22,10 +24,6 @@ varying vec3 eye_ss;
varying vec2 uv_coord = uv;
-/* Position of the light in eye-space.
- */
-const vec3 light_pos_es = vec3(0.0, 3.0, 0.0);
-
void main(void)
{
gl_Position = mvp * gl_Vertex;
@@ -36,7 +34,7 @@ void main(void)
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;
+ vec3 light_direction_es = vec3(light_pos_es) - vertex_pos_es;
/* Surface space vectors for lighting */
light_ss = normalize(light_direction_es * tbn);
diff --git a/src/scene.cpp b/src/scene.cpp
index 9f72b13..333fbee 100755
--- a/src/scene.cpp
+++ b/src/scene.cpp
@@ -39,6 +39,7 @@ namespace Scene {
static bool anim = true;
static GLUvec4 eye(0.0, 1.5f, 10.0f, 0.0f);
+static GLUvec4 light(0.0, 6.0f, 0.0f, 0.0f);
/**
* Points in 3-space that the camera must hit.
@@ -104,6 +105,7 @@ static GLint phong_m_location;
static GLint phong_s_location;
static GLint phong_sampler_location;
static GLint phong_eye_location;
+static GLint phong_light_location;
static GLuint shadow_prog;
static GLint shadow_mvp_location;
@@ -113,13 +115,13 @@ static GLuint cubemap;
static patch_set_info *object = NULL;
void
-Render()
+Render(const GLUvec4 &look_at)
{
const patch_set_info *info = object;
const GLUvec4 ref(0.0f, 0.0f, 0.0f, 1.0f);
const GLUvec4 up(0.0f, 1.0f, 0.0f, 0.0f);
- const GLUmat4 view(gluLookAt(eye, ref, up));
+ const GLUmat4 view(gluLookAt(look_at, ref, up));
GLUmat4 model(gluRotate(GLUvec4(1.0, 0.0, 0.0, 0.0), 3.0 * M_PI_2));
@@ -160,6 +162,9 @@ Render()
glUniformMatrix4fv(phong_m_location, 1, false, (float *) &m);
glUniform1i(phong_sampler_location, 0);
glUniform4fv(phong_eye_location, 1, eye.values);
+
+ GLUvec4 light_pos_es = view * light;
+ glUniform4fv(phong_light_location, 1, light_pos_es.values);
}
glMultiDrawElements(info->mode, info->count, info->type,
@@ -183,14 +188,14 @@ void
RenderPhong()
{
glUseProgram(phong_prog);
- Render();
+ Render(eye);
}
void
RenderShadow()
{
glUseProgram(shadow_prog);
- Render();
+ Render(light);
}
@@ -204,6 +209,10 @@ lerp(GLUvec4 p0, GLUvec4 p1, float t)
static void
update_camera(float total_t)
{
+ /* Move the light in a circle */
+ const float light_t = 0.628318530717959f /* 2pi/10 */ * total_t;
+ light = GLUvec4(10.0*cos(light_t), 8.0, 10.0*sin(light_t), 0.0);
+
/* Figure out which segment is active based on the
* following rules:
* Segment 0 is active from [0, 4) seconds (4 seconds)
@@ -280,6 +289,7 @@ setup_shader()
phong_mv_location = glGetUniformLocation(phong_prog, "mv");
phong_m_location = glGetUniformLocation(phong_prog, "m");
phong_eye_location = glGetUniformLocation(phong_prog, "eye");
+ phong_light_location = glGetUniformLocation(phong_prog, "light_pos_es");
phong_s_location = glGetUniformLocation(phong_prog, "s");
phong_sampler_location = glGetUniformLocation(phong_prog, "sampler");