Esempio n. 1
0
Spectrum Path::computeLi( const vec2f &pFilm, const SceneData &i_scene, const Camera &i_camera, const Sampler &i_sampler, int i_depth )
{
    Camera::CameraSample cameraSample;
    cameraSample.pixelSample = pFilm + i_sampler.sample2D();
    cameraSample.lensSample = i_sampler.sample2D();
    
    Ray ray;
    float weight = i_camera.generateRay( cameraSample, &ray );
    
    // Radiance
    Spectrum L = Spectrum( 0.0f );
    
    // Importance
    Spectrum beta = Spectrum( 1.0 );
    
    for ( int bounces = 0;; bounces++ ) {
        
        RayIntersection intersection;
        bool foundIntersection = i_scene.intersect( ray, intersection );
        
        float maxDepth = 5;
        if ( !foundIntersection || bounces >= maxDepth ) {
            break;
        }
        
        Spectrum Le = intersection.Le();
        if ( Le.isBlack() )
        {
            // Direct illumination sampling

            Spectrum Ld = beta * uniformSampleOneLight( intersection, i_sampler, i_scene );
            L += Ld;
            
            // BSDF Sampling
            const shading::BSDF &bsdf = intersection.getBSDF();
            const vec3f &surfacePosition = intersection.getSurfacePoint().pos;
            
            vec3f woLocal = -intersection.worldToSurface( -intersection.dir );
            vec3f wiLocal;
            float pdf = 1.0;
            
            const vec2f &bsdfSample = i_sampler.sample2D();
            Spectrum f = bsdf.sampleF( woLocal, &wiLocal, bsdfSample, &pdf );
            
            // No point of continuing if no color
            if ( f.isBlack() || pdf == 0.0 ) {
                break;
            }
            
            // Transform to world space
            vec3f wiWorld = intersection.surfaceToWorld( wiLocal );
            
            // Generate new ray
            ray = Ray( surfacePosition, wiWorld );
            
            beta *= ( f * absDot( wiWorld, intersection.getSurfacePoint().normal ) ) / pdf;
            
            // Russian Roulette
            if ( beta.y() < 1.0f && bounces > 3 ) {
                
                float q = std::max( .05f, 1 - beta.maxComponentValue() );
                
                if ( i_sampler.sample1D() > q ) {
                    beta /= ( 1.0 - q );
                }
                else {
                    break;
                }
            }
        }
        else {
            L += beta * intersection.Le() / 50.0;
            break;
        }
    }
    
    return weight * L;
}
Esempio n. 2
0
Spectrum Path::estimateDirectLighting( const RayIntersection i_intersection, RenderablePtr i_light, const Sampler &i_sampler, const SceneData &i_scene )
{
    // Direct contribution
    Spectrum Ld = Spectrum::black();
    
    float lightPdf = 0.0f;
    float surfacePdf = 0.0f;
    
    vec3f wiWorld;
    
    const vec3f &woWorld = -i_intersection.dir;
    const vec3f &woLocal = i_intersection.worldToSurface( woWorld );
    const vec3f &normal = i_intersection.getSurfacePoint().normal;
    const shading::BSDF &bsdf = i_intersection.getBSDF();
    
    /* ==================== */
    /*     Sample Light     */
    /* ==================== */
    {
        VisibilityTester visibilityTester;
        const vec3f &uLight = i_sampler.sample3D();
        
        Spectrum Li = i_light->sampleIncidenceRadiance( i_intersection, uLight, &wiWorld, &lightPdf, &visibilityTester );
        
        // Convert to local (surface) coords
        const vec3f &wiLocal = i_intersection.worldToSurface( wiWorld );
        
        // Sample light
        if ( !Li.isBlack() && lightPdf > 0.0f ) {
            
            const Spectrum &f = bsdf.computeF( woLocal, wiLocal ) * absDot( wiWorld, normal );
            surfacePdf = bsdf.pdf( woLocal, wiLocal );
            
            if ( !f.isBlack() ) {
                
                // Ray is in shadow
                if ( visibilityTester.isOccluded( i_scene ) ) {
                    Li = Spectrum::black();
                }
                
                if ( !Li.isBlack() ) {
                    
                    // Weight with MIS
                    float misWeight = powerHeuristic( lightPdf, surfacePdf );
                    Ld += ( Li * f * misWeight ) / lightPdf;
                }
            }
        }
    }
    
    /* ==================== */
    /*     Sample BSDF      */
    /* ==================== */
    {
        const vec2f &uSurface = i_sampler.sample2D();
        vec3f wiLocal;
        
        const Spectrum &f = bsdf.sampleF( woLocal, &wiLocal, uSurface, &surfacePdf ) * absDot( wiWorld, normal );
        
        if ( !f.isBlack() && surfacePdf > 0.0f ) {
            
            // TODO computer light PDF
            lightPdf = 0.0;
            
            // No contribution, return
            if ( lightPdf == 0.0 ) {
                return Ld;
            }

            // Convert to local (surface) coords
            vec3f wiWorld = i_intersection.surfaceToWorld( wiLocal );
            
            const Ray bsdfRay = Ray( i_intersection.getSurfacePoint().pos, wiWorld );
            RayIntersection lightIntersection;
            
            bool foundIntersection = i_scene.intersect( bsdfRay, lightIntersection );
            Spectrum Li = Spectrum::black();
            
            if ( foundIntersection ) {
                
                if ( lightIntersection.m_shapeId == i_light->getIdentifier() ) {
                    Li = i_light->computeRadiance( lightIntersection.getSurfacePoint(), wiWorld );
                }
            }
            
            if ( !Li.isBlack() ) {
                
                // Weight with MIS
                float misWeight = powerHeuristic( lightPdf, surfacePdf );
                Ld += ( Li * f * misWeight ) / surfacePdf;
            }
        }
    }
    
    return Ld;
}