X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fshadowmap.cpp;h=9dadb8fe16ee4991eda732ceb0219373d8267bfe;hb=287985a83e42e1e0af0c1522272d97d96fce00f9;hp=9b27ba08d5a5f9e7fff423a1544aaafe827c9018;hpb=41339bc44d076569c680b2c24c75b30ef1254c1b;p=libs%2Fgl.git diff --git a/source/shadowmap.cpp b/source/shadowmap.cpp index 9b27ba08..9dadb8fe 100644 --- a/source/shadowmap.cpp +++ b/source/shadowmap.cpp @@ -24,6 +24,7 @@ ShadowMap::ShadowMap(unsigned s, const Scene &c, const Light &l): size(s), scene(c), light(l), + unit(3), radius(1) { depth_buf.set_min_filter(LINEAR); @@ -35,7 +36,7 @@ ShadowMap::ShadowMap(unsigned s, const Scene &c, const Light &l): depth_buf.parameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); depth_buf.parameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0); - glDrawBuffer(GL_NONE); + draw_buffer(NO_BUFFER); Framebuffer::unbind(); Texture::unbind(); } @@ -46,11 +47,26 @@ void ShadowMap::set_target(const Vector3 &t, float r) radius=r; } +void ShadowMap::set_texture_unit(unsigned u) +{ + unit=u; +} + void ShadowMap::prepare() { - const Vector4 &lpos=light.get_position(); + Vector4 lpos=light.get_position(); if(lpos.w) - throw Exception("Non-directional lights not supported at the moment"); + { + /* XXX Not really proper way to support positional lights, but good + enough when the light source is far away */ + lpos.x-=target.x; + lpos.y-=target.y; + lpos.z-=target.z; + float d=sqrt(lpos.x*lpos.x+lpos.y*lpos.y+lpos.z*lpos.z); + lpos.x/=d; + lpos.y/=d; + lpos.z/=d; + } float matrix[16]; if(abs(lpos.z)>=abs(lpos.x) && abs(lpos.z)>=abs(lpos.y)) @@ -95,18 +111,22 @@ void ShadowMap::prepare() push_matrix(); load_matrix(matrix); - Bind _bind_fbo(fbo); - glViewport(0, 0, size, size); - glClear(GL_DEPTH_BUFFER_BIT); - scene.render(); + const Framebuffer *old_fbo=Framebuffer::current(); + fbo.bind(); + clear(DEPTH_BUFFER_BIT); + scene.render("shadow"); matrix_mode(PROJECTION); pop_matrix(); matrix_mode(MODELVIEW); pop_matrix(); + if(old_fbo) + old_fbo->bind(); + else + Framebuffer::unbind(); } - depth_buf.bind_to(3); + depth_buf.bind_to(unit); float diam=radius*2; float s_eq[4]={ matrix[0]/diam, matrix[4]/diam, matrix[8]/diam, matrix[12]/diam+0.5 }; float t_eq[4]={ matrix[1]/diam, matrix[5]/diam, matrix[9]/diam, matrix[13]/diam+0.5 }; @@ -126,7 +146,7 @@ void ShadowMap::prepare() void ShadowMap::cleanup() { - TexUnit::activate(3); + TexUnit::activate(unit); Texture::unbind(); disable(GL_TEXTURE_GEN_S); disable(GL_TEXTURE_GEN_T);