Beispiel #1
0
Matrix4f SpotLight::lightProjectionMatrix() const
{
    return projectionMatrix() * lightMatrix();
}
Beispiel #2
0
Matrix4f SpotLight::inverseLightMatrix() const
{
    return lightMatrix().inverse();
}
void RenderLists::buildLightData(
        Camera* camera,
        Light* shadowCaster,
        LightData& lightData )
{
    unsigned i = 0;
    for ( std::vector<Light*>::iterator it = m_lights.begin();
          it != m_lights.end(); ++it )
    {
        Light* light = *it;

        // TODO: FIX THIS SHIT
        glm::vec3 pos( light->getTransform()->translation );

        if ( light->getTransform()->parent != NULL )
        {
            glm::mat4 lightMatrix( 1.0f );
            light->getTransform()->parent->apply( lightMatrix );
            pos =  ( lightMatrix * glm::vec4( pos, 1.0f ) ).xyz();
        }
        // glm::vec3 pos( light->getTransform()->translation );

        // calculate rotation
        glm::mat4 rotMat = glm::mat4( 1.0f );
        rotMat *= glm::rotate(
            light->getTransform()->rotation.z * util::math::DEGREES_TO_RADIANS,
            glm::vec3( 0.0f, 0.0f, 1.0f )
        );
        rotMat *= glm::rotate(
            light->getTransform()->rotation.y * util::math::DEGREES_TO_RADIANS,
            glm::vec3( 0.0f, 1.0f, 0.0f )
        );
        rotMat *= glm::rotate(
            light->getTransform()->rotation.x * util::math::DEGREES_TO_RADIANS,
            glm::vec3( 1.0f, 0.0f, 0.0f )
        );
        glm::vec3 rot( 0.0f, 0.0f, -1.0f );
        rot = glm::vec3( rotMat * glm::vec4( rot, 0.0f ) );
        // add generic data
        lightData.names.push_back( light->getId() );
        lightData.types.push_back(
            static_cast<int>( light->getLightType() ) );
        lightData.colours.push_back( light->getValue().r );
        lightData.colours.push_back( light->getValue().g );
        lightData.colours.push_back( light->getValue().b );

        // light type specific data
        switch ( light->getLightType() )
        {
            case light::DIRECTIONAL:
            {
                pos = glm::vec3(
                        camera->getViewMatrix() * glm::vec4( pos, 0.0f ) );
                rot = glm::vec3(
                        camera->getViewMatrix()* glm::vec4( rot, 0.0f ) );
                lightData.attenuations.push_back( 1.0f );
                lightData.attenuations.push_back( 0.0f );
                lightData.attenuations.push_back( 0.0f );
                lightData.arcs.push_back( 0.0f );
                lightData.arcs.push_back( 0.0f );
                break;
            }
            case light::POINT:
            {
                PointLight* point = dynamic_cast<PointLight*>( light );
                pos = glm::vec3(
                        camera->getViewMatrix() * glm::vec4( pos, 1.0f ) );
                rot = glm::vec3(
                        camera->getViewMatrix() * glm::vec4( rot, 1.0f ) );
                lightData.attenuations.push_back( point->getConstantAtt() );
                lightData.attenuations.push_back( point->getLinearAtt() );
                lightData.attenuations.push_back( point->getQuadraticAtt() );
                lightData.arcs.push_back( 0.0f );
                lightData.arcs.push_back( 0.0f );
                break;
            }
            case light::SPOT:
            {
                SpotLight* spot = dynamic_cast<SpotLight*>( light );
                pos = glm::vec3(
                        camera->getViewMatrix() * glm::vec4( pos, 1.0f ) );
                rot = glm::vec3(
                        camera->getViewMatrix() * glm::vec4( rot, 0.0f ) );
                lightData.attenuations.push_back( spot->getConstantAtt() );
                lightData.attenuations.push_back( spot->getLinearAtt() );
                lightData.attenuations.push_back( spot->getQuadraticAtt() );
                lightData.arcs.push_back(
                        util::math::cosd( spot->getOuterArc() ) );
                lightData.arcs.push_back(
                        util::math::cosd( spot->getInnerArc() ) );
                break;
            }
        }

        // positions
        lightData.positions.push_back( pos.x );
        lightData.positions.push_back( pos.y );
        lightData.positions.push_back( pos.z );
        // rotations
        lightData.rotations.push_back( rot.x );
        lightData.rotations.push_back( rot.y );
        lightData.rotations.push_back( rot.z );

        // inverse
        lightData.inverses.push_back( light->isInversed() );

        // shadow map
        lightData.shadowMap = m_shadowMap.getTexture();
        // shadow caster
        if ( *it == shadowCaster )
        {
            lightData.shadowCaster = i;
        }

        ++i;
    }
}