void IndexedDirectionalLightRenderer::renderShadowMap( Renderer& renderer, const DirectionalLight* light, const IndexedLightingRenderData& data )
{
   if ( !m_shadowDepthMapShader || !m_shadowProjectionPS || !data.m_shadowDepthTexture )
   {
      return;
   }

   Camera& activeCamera = renderer.getActiveCamera();

   m_cascadeCalculationConfig.m_lightRotationMtx = light->getGlobalMtx();
   m_cascadeCalculationConfig.m_lightRotationMtx.setPosition<3>( Quad_0 );

   m_cascadeCalculationConfig.m_cascadeDimensions = (float)( data.m_shadowDepthTexture->getWidth() ) / (float)m_cascadeCalculationConfig.m_numCascadesPerTextureRow;
   data.m_renderingView->getSceneBounds( m_cascadeCalculationConfig.m_sceneBounds );
   m_cascadeCalculationConfig.m_activeCamera = &activeCamera;

   Vector lightPos;
   CascadedShadowsUtils::calculateCascadesBounds( m_cascadeCalculationConfig, m_calculatedCascadeStages );

   // set the light camera
   Camera lightCamera( "dirLightCamera", renderer, Camera::PT_ORTHO );

   // render cascades
   renderCascades( renderer, activeCamera, lightCamera, data );

   // combine the cascades
   combineCascades( renderer, data, activeCamera, lightCamera, m_cascadeCalculationConfig.m_cascadeDimensions );
}
Example #2
0
void RenderDevice::updateLightDepth( LightState& state )
{
#if 0
    const LightPtr& light = state.light;
    assert( light->getLightType() == LightType::Directional );

    Texture* shadowDepthTexture;
    
    if( !shadowDepthBuffer )
    {
        shadowDepthBuffer = createRenderBuffer( Settings(512, 512) );
    }

    if( shadowTextures.find(light) == shadowTextures.end() )
    {
        shadowDepthTexture = shadowDepthBuffer->createRenderTexture();
        shadowTextures[light] = shadowDepthTexture;
    }
    else
    {
        shadowDepthTexture = shadowTextures[light];
    }

    CameraPtr lightCamera( new Camera(*camera) );
    TransformPtr lightTransform( new Transform(*state.transform.get()) );
    
    EntityPtr lightCameraEntity( new Entity("ShadowCamera") );
    lightCameraEntity->addTransform(); /*Component( lightTransform );*/
    lightCameraEntity->addComponent( lightCamera );

    RenderView* lightView = new View(lightCamera, shadowDepthBuffer);

    if( !shadowDepthBuffer->check() )
        return;

    // TODO: turn off color writes (glColorMask?)
    lightView->update();
    shadowDepthBuffer->unbind();

    Matrix4x4 bias;
    bias.identity();
    bias.m11 = 0.5f;
    bias.m22 = 0.5f;
    bias.m33 = 0.5f;
    bias.tx  = 0.5f;
    bias.ty  = 0.5f;
    bias.tz  = 0.5f;

    const Frustum& frustum = lightCamera->getFrustum();
    const Matrix4x4& matProjection = frustum.projectionMatrix;

    state.projection = lightCamera->getViewMatrix()
        * matProjection * bias;
#endif
}