float darkness;
int matrix_index;
vec4 region;
- float bias;
+ vec2 bias;
};
uniform ShadowMap
{
ShadowParameters shadows[max_lights];
- mat4 shd_world_matrix[max_lights];
+ mat4 shd_world_matrix[max_lights*4];
};
uniform sampler2DShadow shadow_map;
if(type==1)
{
shadow_coord = (shd_world_matrix[shadows[index].matrix_index]*world_pos).xyz;
- shadow_coord.z -= shadows[index].bias;
+ shadow_coord.z -= shadows[index].bias.x;
+ }
+ else if(type==2)
+ {
+ int base = shadows[index].matrix_index;
+ vec4 clip_coord = shd_world_matrix[base]*world_pos;
+ for(int i=1; i<4; ++i)
+ {
+ vec4 c = shd_world_matrix[base+i]*world_pos;
+ if(c.w>clip_coord.w)
+ clip_coord = c;
+ }
+ shadow_coord = clip_coord.xyz/clip_coord.w;
+ vec2 bias = shadows[index].bias;
+ shadow_coord.z = (shadow_coord.z+bias.x)/bias.y-bias.x;
}
else
return 1.0;
#include "directionallight.h"
#include "error.h"
#include "lighting.h"
+#include "pointlight.h"
#include "renderer.h"
#include "resources.h"
#include "shadowmap.h"
shdata.uniform(base+".darkness", 1.0f);
shdata.uniform(base+".matrix_index", 0);
shdata.uniform(base+".region", Vector4(0.0f, 0.0f, 1.0f, 1.0f));
- shdata.uniform(base+".bias", 0.0f);
+ shdata.uniform(base+".bias", 0.0f, 1.0f);
}
Matrix dummy_matrix;
add_light(light, s, DIRECTIONAL, c);
}
+void ShadowMap::add_light(const PointLight &light, unsigned s, Renderable &c)
+{
+ add_light(light, s, TETRAHEDRON, c);
+}
+
void ShadowMap::add_light(const Light &light, unsigned s, ShadowType type, Renderable &c)
{
if(!lighting && !lights.empty())
region.left = next_left;
}
+ unsigned n_views = (type==TETRAHEDRON ? 4 : 1);
+
lights.emplace_back();
ShadowedLight &sl = lights.back();
sl.light = &light;
sl.view_index = views.size();
sl.shadow_caster = &c;
- views.emplace_back();
- ShadowView &view = views[sl.view_index];
- view.light_index = lights.size()-1;
- view.face = 0;
+ views.resize(views.size()+n_views);
+ for(unsigned i=0; i<n_views; ++i)
+ {
+ ShadowView &view = views[sl.view_index+i];
+ view.light_index = lights.size()-1;
+ view.face = i;
+
+ if(type==TETRAHEDRON)
+ {
+ if(i>0)
+ view.face_matrix = Matrix().rotate(Geometry::Angle<float>::from_turns((i-0.5f)/3.0f), Vector3(0, 0, 1))
+ .rotate(Geometry::Angle<float>::straight()-Geometry::acos<float>(1.0f/3.0f), Vector3(1, 0, 0));
+
+ float triangle_h = sqrt(0.75f)*2.0f;
+ float bottom = 0.5f/sqrt(0.75f);
+ float distance = (sqrt(2.0f/3.0f)-sqrt(3.0f/8.0f))*2.0f;
+ view.camera.set_field_of_view(Geometry::atan<float>(triangle_h/distance)*2.0f);
+ view.camera.set_aspect_ratio(1.0f/triangle_h);
+ view.camera.set_frustum_axis(0.0f, 1.0f-bottom/triangle_h);
+ view.camera.set_frustum_rotation(-Geometry::Angle<float>::from_turns(i/4.0f));
+ }
+ }
string base = format("shadows[%d]", index);
shdata.uniform(base+".type", static_cast<int>(type));
{
string base = format("shadows[%d]", l.index);
if(l.type==DIRECTIONAL)
- shdata.uniform(base+".bias", depth_bias/l.region.width);
+ shdata.uniform(base+".bias", depth_bias/l.region.width, 0.0f);
+ else if(l.type==TETRAHEDRON)
+ {
+ float distance = (sqrt(2.0f/3.0f)-sqrt(3.0f/8.0f))*2.0f;
+ float bias = depth_bias*2.0f/(distance*l.region.width);
+ shdata.uniform(base+".bias", -1001.0f/999.0f, 1.0f-bias);
+ }
}
vector<Matrix> shadow_matrices;
v.camera.set_orthographic(radius*2, radius*2);
v.camera.set_depth_clip(-radius, radius);
}
+ else if(light.type==TETRAHEDRON)
+ {
+ v.camera.set_object_matrix((*light.light->get_matrix())*v.face_matrix);
+ v.camera.set_depth_clip(radius/1000.0f, radius);
+ }
Matrix to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.5f)).scale(0.5f);
shadow_matrices.push_back(to_texcoord*v.camera.get_projection_matrix()*v.camera.get_view_matrix());