Verified Commit 7b2ed2fa authored by insert's avatar insert

Add point shadow code.

parent 41227dd6
use shader_generator::{ ShaderOptions, generate_vertex_shader, generate_fragment_shader };
fn main() {
println!("{}", generate_vertex_shader(ShaderOptions::new()));
println!("{}", generate_fragment_shader(ShaderOptions::new()));
}
......@@ -11,20 +11,20 @@ macro_rules! push_render {
($str: expr, $data: expr, $ctx: expr) => {$str.push_str(&include_render!($data, $ctx))};
}
#[derive(Serialize)]
#[derive(Serialize, Clone)]
pub enum RenderType {
Colour,
Texture,
}
#[derive(Serialize)]
#[derive(Serialize, Clone)]
pub struct LightingOptions {
pub specular: RenderType,
pub shadows: bool,
pub point_lights: usize,
}
#[derive(Serialize)]
#[derive(Serialize, Clone)]
pub struct ShaderOptions {
pub diffuse: RenderType,
pub lighting: Option<LightingOptions>,
......@@ -36,7 +36,7 @@ impl ShaderOptions {
diffuse: RenderType::Colour,
lighting: Some(LightingOptions {
specular: RenderType::Colour,
shadows: true,
shadows: false,
point_lights: 4,
}),
}
......
......@@ -38,7 +38,8 @@ vec4 directional_light(vec3 viewDir, vec4 mat_diffuse, vec4 mat_specular) {
(diffuse + specular));
}
vec4 point_light(PointLight light, vec3 viewDir, vec4 mat_diffuse, vec4 mat_specular) {
vec4 point_light(int i, vec3 viewDir, vec4 mat_diffuse, vec4 mat_specular) {
PointLight light = pointLights[i];
vec3 norm = normalize(fNormal);
vec3 lightDir = normalize(light.position - fPos);
......@@ -55,7 +56,10 @@ vec4 point_light(PointLight light, vec3 viewDir, vec4 mat_diffuse, vec4 mat_spec
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
return (ambient +
{% if lighting.shadows %}(1.0 - point_shadow(i)) * {% endif %}
(diffuse + specular));
}
vec4 lighting(vec4 diffuse, vec4 specular) {
......@@ -64,7 +68,7 @@ vec4 lighting(vec4 diffuse, vec4 specular) {
vec4 result = directional_light(viewDir, diffuse, specular);
for (int i=0;i<NR_POINT_LIGHTS&&i<activePointLights;i++)
result += point_light(pointLights[i], viewDir, diffuse, specular);
result += point_light(i, viewDir, diffuse, specular);
return result;
}
\ No newline at end of file
in vec4 fPosLightSpace;
uniform sampler2D shadowMap;
float directional_shadow() {
vec3 projCoords = fPosLightSpace.xyz / fPosLightSpace.w;
projCoords = projCoords * 0.5 + 0.5;
......@@ -29,4 +26,17 @@ float directional_shadow() {
{% endif %}
return shadow;
}
\ No newline at end of file
}
float point_shadow(int shadow) {
vec3 fragToLight = fPos - lightPos;
float closestDepth = texture(pointLights[shadow].shadowMap, fragToLight).r;
closestDepth *= far_plane;
float currentDepth = length(fragToLight);
float bias = 0.05;
float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
return shadow;
}
\ No newline at end of file
in vec4 fPosLightSpace;
uniform vec3 viewPos;
uniform sampler2D shadowMap;
struct DirectionalLight {
vec3 direction;
......@@ -20,6 +23,8 @@ struct PointLight {
vec3 ambient;
vec3 diffuse;
vec3 specular;
sampler2D shadowMap;
};
#define NR_POINT_LIGHTS {{ lighting.point_lights }}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment