Skip to content

Commit

Permalink
Shadows on Directional Lights are now optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Wozniak authored and ColdenCullen committed May 6, 2014
1 parent 8576ad6 commit ce81582
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 60 deletions.
63 changes: 35 additions & 28 deletions source/components/lights.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ class Light : IComponent
{
private:
vec3 _color;
bool _castShadows;

public:
/// The color the light gives off.
mixin( Property!( _color, AccessModifier.Public ) );
/// If it should cast shadows
mixin( Property!( _castShadows ) );

this( vec3 color )
{
this.color = color;
_castShadows = false;
}

static this()
Expand Down Expand Up @@ -73,38 +77,41 @@ public:
mixin( Property!( _projView ) );
mixin( Property!( _shadowMapSize ) );

this( vec3 color, vec3 direction )
this( vec3 color, vec3 direction, bool castShadows )
{
this.direction = direction;
super( color );

// generate framebuffer for shadow map
shadowMapFrameBuffer = 0;
glGenFramebuffers( 1, cast(uint*)&_shadowMapFrameBuffer );
glBindFramebuffer( GL_FRAMEBUFFER, _shadowMapFrameBuffer );

// generate depth texture of shadow map
shadowMapSize = 2048;
glGenTextures( 1, cast(uint*)&_shadowMapTexture );
glBindTexture( GL_TEXTURE_2D, _shadowMapTexture );
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, shadowMapSize, shadowMapSize, 0, GL_DEPTH_COMPONENT, GL_FLOAT, null );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );

glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _shadowMapTexture, 0 );

// don't want any info besides depth
glDrawBuffer( GL_NONE );
// don't want to read from gpu
glReadBuffer( GL_NONE );

// check for success
if( glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE )
this.castShadows = castShadows;
if( castShadows )
{
logFatal("Shadow map frame buffer failure.");
assert(false);
// generate framebuffer for shadow map
shadowMapFrameBuffer = 0;
glGenFramebuffers( 1, cast(uint*)&_shadowMapFrameBuffer );
glBindFramebuffer( GL_FRAMEBUFFER, _shadowMapFrameBuffer );

// generate depth texture of shadow map
shadowMapSize = 2048;
glGenTextures( 1, cast(uint*)&_shadowMapTexture );
glBindTexture( GL_TEXTURE_2D, _shadowMapTexture );
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, shadowMapSize, shadowMapSize, 0, GL_DEPTH_COMPONENT, GL_FLOAT, null );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );

glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _shadowMapTexture, 0 );

// don't want any info besides depth
glDrawBuffer( GL_NONE );
// don't want to read from gpu
glReadBuffer( GL_NONE );

// check for success
if( glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE )
{
logFatal("Shadow map frame buffer failure.");
assert(false);
}
}
}

Expand Down
61 changes: 31 additions & 30 deletions source/graphics/adapters/adapter.d
Original file line number Diff line number Diff line change
Expand Up @@ -264,51 +264,52 @@ public:
*/
void shadowPass()
{


foreach( light; directionalLights )
{
glBindFramebuffer( GL_FRAMEBUFFER, light.shadowMapFrameBuffer );
glClear( GL_DEPTH_BUFFER_BIT );
glViewport( 0, 0, light.shadowMapSize, light.shadowMapSize );

// determine the world space volume for all objects
AABB frustum;
foreach( object; scene.objects )
if( light.castShadows )
{
if( object.mesh && object.stateFlags.drawMesh )
glBindFramebuffer( GL_FRAMEBUFFER, light.shadowMapFrameBuffer );
glClear( GL_DEPTH_BUFFER_BIT );
glViewport( 0, 0, light.shadowMapSize, light.shadowMapSize );

// determine the world space volume for all objects
AABB frustum;
foreach( object; scene.objects )
{
frustum.expand( (object.transform.matrix * vec4(object.mesh.boundingBox.min, 1.0f)).xyz );
frustum.expand( (object.transform.matrix * vec4(object.mesh.boundingBox.max, 1.0f)).xyz );
if( object.mesh && object.stateFlags.drawMesh )
{
frustum.expand( (object.transform.matrix * vec4(object.mesh.boundingBox.min, 1.0f)).xyz );
frustum.expand( (object.transform.matrix * vec4(object.mesh.boundingBox.max, 1.0f)).xyz );
}
}
}

light.calculateProjView( frustum );
light.calculateProjView( frustum );

foreach( object; scene.objects )
{
if( object.mesh && object.stateFlags.drawMesh )
foreach( object; scene.objects )
{
// set the shader
Shader shader = object.mesh.animated
? Shaders.animatedShadowMap
: Shaders.shadowMap;
if( object.mesh && object.stateFlags.drawMesh )
{
// set the shader
Shader shader = object.mesh.animated
? Shaders.animatedShadowMap
: Shaders.shadowMap;

glUseProgram( shader.programID );
glBindVertexArray( object.mesh.glVertexArray );
glUseProgram( shader.programID );
glBindVertexArray( object.mesh.glVertexArray );

shader.bindUniformMatrix4fv( shader.WorldViewProjection,
light.projView * object.transform.matrix);
shader.bindUniformMatrix4fv( shader.WorldViewProjection,
light.projView * object.transform.matrix);

if( object.mesh.animated )
shader.bindUniformMatrix4fvArray( shader.Bones, object.animation.currBoneTransforms );
if( object.mesh.animated )
shader.bindUniformMatrix4fvArray( shader.Bones, object.animation.currBoneTransforms );

glDrawElements( GL_TRIANGLES, object.mesh.numVertices, GL_UNSIGNED_INT, null );
glDrawElements( GL_TRIANGLES, object.mesh.numVertices, GL_UNSIGNED_INT, null );

glBindVertexArray(0);
glBindVertexArray(0);
}
}
glBindFramebuffer( GL_FRAMEBUFFER, 0 );
}
glBindFramebuffer( GL_FRAMEBUFFER, 0 );
}

foreach( light; pointLights ){}
Expand Down
3 changes: 2 additions & 1 deletion source/graphics/shaders/glsl/directionallight.d
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ immutable string directionallightFS = q{
{
vec3 color;
vec3 direction;
float shadowless;
};

in vec4 fPosition_s;
Expand Down Expand Up @@ -111,6 +112,6 @@ immutable string directionallightFS = q{
// specularIntensity is the light's contribution
vec3 specular = ( pow( specularScale, 8 ) * light.color * specularIntensity);

color = shadowValue(position_v) * vec4( ( diffuse + specular ), 1.0f );
color = max( light.shadowless , shadowValue(position_v) ) * vec4( ( diffuse + specular ), 1.0f );
}
};
3 changes: 3 additions & 0 deletions source/graphics/shaders/shaders.d
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ private enum ShaderUniform
LightRadius = "light.radius",
LightFalloffRate = "light.falloffRate",
LightPosition = "light.pos_v",
LightShadowless = "light.shadowless",
EyePosition = "eyePosition_w",
/// Animations
Bones = "bones",
Expand Down Expand Up @@ -340,6 +341,7 @@ public:
{
bindUniform3f( LightDirection, light.direction);
bindUniform3f( LightColor, light.color );
bindUniform1f( LightShadowless, cast(float)(!light.castShadows) );
}

/**
Expand All @@ -349,6 +351,7 @@ public:
{
bindUniform3f( LightDirection, ( transform * vec4( light.direction, 0.0f ) ).xyz );
bindUniform3f( LightColor, light.color );
bindUniform1f( LightShadowless, cast(float)(!light.castShadows) );
}

/**
Expand Down
4 changes: 3 additions & 1 deletion source/utility/config.d
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ static this()
{
vec3 color;
vec3 dir;
bool shadows = false;
node.tryFind( "Color", color );
node.tryFind( "Direction", dir );
return cast(Light)new DirectionalLight( color, dir );
node.tryFind( "CastShadows", shadows );
return cast(Light)new DirectionalLight( color, dir, shadows );
} );
constructor.addConstructorMapping( "!Light-Ambient", ( ref Node node )
{
Expand Down

0 comments on commit ce81582

Please sign in to comment.