Guest User

Untitled

a guest
Jun 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. #include <Raytracer.h>
  2. #include <Core/Integrator.h>
  3. #include <Integrator/DistributedRayTracingIntegrator.h>
  4. #include <Accelerator/Accelerator.h>
  5. #include <Core/Material.h>
  6. #include <Core/Light.h>
  7. #include <Core/BSDF.h>
  8. #include <Core/Scene.h>
  9. #include <math/Sampling.h>
  10. #include <iostream>
  11.  
  12. using namespace std;
  13.  
  14. DistributedRayTracingIntegrator::DistributedRayTracingIntegrator( uint32_t _recursionDepth, uint32_t _sampleCount )
  15.     : m_MaxRecursionDepth(_recursionDepth), m_SampleCount(_sampleCount)
  16. {
  17. }
  18.  
  19.  
  20. void DistributedRayTracingIntegrator::Preprocess( const Scene& _scene )
  21. {
  22.  
  23. }
  24.  
  25. vec3 DistributedRayTracingIntegrator::L( const Scene& _scene, RayDifferential<scalar_t>& _ray, const Sample& _sample, Random& _random, BSDFAllocator& _bsdfAllocator ) const
  26. {
  27.     return Lrec( _scene, _ray, _sample, _random, _bsdfAllocator, 0 );
  28. }
  29.  
  30. vec3 DistributedRayTracingIntegrator::Lrec( const Scene& _scene, RayDifferential<scalar_t>& _ray, const Sample& _sample, Random& _random, BSDFAllocator& _bsdfAllocator, uint32_t _recursionDepth ) const
  31. {
  32.    
  33.     // find an intersection with a surface
  34.  
  35.     const Accelerator* accel     = _scene.GetAccelerator();
  36.     const LightContainer* lights = _scene.GetLightContainer();
  37.  
  38.     INTERSECTION isect;
  39.     SURFACE_PROPERTIES surfaceProperties;
  40.     SHADING_PROPERTIES shadingProperties;
  41.  
  42.     // find an intersection with a surface
  43.     if( !accel->Intersect( _ray, &isect ) )
  44.         return 0;
  45.  
  46.    
  47.     const SurfaceSceneObject* sob = static_cast<const SurfaceSceneObject*>( isect.Object );
  48.  
  49.     sob->GetSurfaceProperties( isect, &surfaceProperties );
  50.     sob->GetShadingProperties( surfaceProperties, &shadingProperties );
  51.  
  52.     //cout << "Lin= " << surfaceProperties.P.y << "  "  << endl;
  53.     // obtain a bsdf from the material
  54.     BSDF* bsdf = shadingProperties.material->GetBsdf( surfaceProperties, shadingProperties, &_bsdfAllocator );
  55.  
  56.     // iterate over all light sources
  57.     vec3 Lout = 0;
  58.     for( LightContainer::const_iterator cit = lights->begin(); cit != lights->end(); cit++ )
  59.     {
  60.         const Light* l = *cit;
  61.         uint32_t sampleCount = l->GetRequestedSampleCount();
  62.         vec3 LL = 0;
  63.         LIGHT_PROPERTIES lightProperties;
  64.  
  65.         for( uint32_t s = 0; s < sampleCount; s++ )
  66.         {
  67.             l->GetLightPropertiesAt( _random, shadingProperties.P, &lightProperties );
  68.  
  69.             // shadow ray
  70.             Ray<scalar_t> shadowRay = CreateShadowRay( surfaceProperties, lightProperties );
  71.             if( accel->Intersect( shadowRay, shadowRay.Tmax ) )
  72.                 continue;
  73.  
  74.             // eval bsdf
  75.             BSDF_OUT bsdfOut;
  76.             bsdf->Eval( -_ray.Direction, lightProperties.D, &bsdfOut );
  77.            
  78.             scalar_t NdL = hlsl::max<scalar_t>( 0, hlsl::dot( lightProperties.D, shadingProperties.N ) ); //hlsl::abs( hlsl::dot( lightProperties.D, shadingProperties.N ) );
  79.             LL += bsdfOut.Color * NdL * lightProperties.L / lightProperties.DistanceSquared;
  80.  
  81.         }
  82.         //if( 0 != _recursionDepth){
  83.    
  84.         Lout += LL / scalar_t(sampleCount);
  85.         //}
  86.     }
  87.    
  88.     if(_recursionDepth  <= m_MaxRecursionDepth )
  89.     {
  90.         scalar_t irgendwas;
  91.         vec3 rayOut;
  92.  
  93.         scalar_t rnd[2];
  94.         _random.Rand( &rnd[0], 2 );
  95.  
  96.         vec3 Lin;
  97.         vec3 Lcount = 0;
  98.  
  99.         for(uint32_t i = 0; i < m_SampleCount; i++)
  100.         {
  101.             UniformSampleHemisphere(rnd[0], rnd[1], surfaceProperties.N, &rayOut, &irgendwas );
  102.  
  103.             RayDifferential<scalar_t> rayi;// = Ray<scalar_t>( isect.P, rayOut );
  104.  
  105.             rayi.Direction = rayOut;
  106.             rayi.Origin = surfaceProperties.P + (rayi.Direction * rayi.Epsilon*(scalar_t)5);//hlsl::vector<double, 3>(-1110,0,0); //
  107.            
  108.             Lin = Lrec( _scene, rayi, _sample, _random, _bsdfAllocator, ( _recursionDepth + 1 ) );
  109.            
  110.             //cout << "Lin= " << hlsl::dot( rayi.Direction, shadingProperties.N ) << "  "  << endl;
  111.  
  112.             BSDF_OUT bsdfIn;
  113.             bsdf->Eval( -_ray.Direction, rayi.Direction, &bsdfIn );
  114.  
  115.             scalar_t NdL = hlsl::max<scalar_t>( 0, hlsl::dot( rayi.Direction, shadingProperties.N ) );
  116.            
  117.             //cout << "Lin= " << NdL << "  "  << endl;
  118.  
  119.             Lcount += bsdfIn.Color *NdL * Lin ;/// hlsl::distance(rayi.Origin, surfaceProperties.P);
  120.            
  121.         }
  122.         Lcount = (scalar_t)2 * Lcount * hlsl::PI<scalar_t>::value / ((scalar_t)m_SampleCount);
  123.         Lout += Lcount;
  124.  
  125.        
  126.     }
  127.  
  128.     return Lout;
  129. }
Add Comment
Please, Sign In to add comment