Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

causticsphotontracer.cpp

Go to the documentation of this file.
00001 /*
00002     toxic - A Global Illumination Renderer
00003     Copyright (C) 2003-2004 Francois Beaune
00004     Contact: http://toxicengine.sourceforge.net/
00005 
00006     This file is part of toxic.
00007 
00008     toxic is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     toxic is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with toxic; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #include "causticsphotontracer.h"   // include first
00024 #include "common/math/point3.h"
00025 #include "common/math/real.h"
00026 #include "common/math/vector3.h"
00027 #include "color3.h"
00028 #include "context.h"
00029 #include "hit.h"
00030 #include "ibdf.h"
00031 #include "ilight.h"
00032 #include "isurfaceshader.h"
00033 #include "photonmap.h"
00034 #include "ray.h"
00035 #include "scene.h"
00036 #include "statistics.h"
00037 #include "surfacebasis.h"
00038 #include "utilities.h"
00039 
00040 #include <cassert>
00041 
00042 using namespace sheep;
00043 using namespace toxic;
00044 
00045 void CausticsPhotonTracer::trace_photon(const Context &context,
00046                                         PhotonMap *photonmap,
00047                                         const Scene *scene,
00048                                         Ray ray,
00049                                         Color3 power) const
00050 {
00051     assert(photonmap);
00052     assert(scene);
00053 
00054     bool specularbounce = false;
00055 
00056     while(true) {
00057         Hit hit;
00058 
00059         ++context.m_statistics->m_photon_rays;
00060 
00061         assert(ray.m_direction.IsUnitLength());
00062 
00063         if(!scene->Trace(context, ray, &hit)) {
00064             // Holes in the scene are modelled as totally absorptive surfaces.
00065             break;
00066         }
00067 
00068         // Extract intersection data.
00069         Point3 point;
00070         Vector3 geometric_normal;
00071         hit.ExtractIntersection(ray, &point, &geometric_normal, 0);
00072         const ISurfaceShader *surface_shader = hit.m_object->GetSurfaceShader();
00073 
00074         const SurfaceBasis surfacebasis(geometric_normal);
00075 
00076         ShadingData shadingdata;
00077         surface_shader->Shade(hit, &shadingdata);
00078 
00079         if(shadingdata.m_bdf->IsDiffuse() && specularbounce) {
00080             ++context.m_statistics->m_cpm_photons;
00081             photonmap->StorePhoton(
00082                 point,              // world space
00083                 power,
00084                 -ray.m_direction    // world space (away from surface)
00085 #ifdef ENABLE_RADIANCES_PRECOMPUTATION
00086                 , geometric_normal          // world space
00087 #endif  // ENABLE_RADIANCES_PRECOMPUTATION
00088                 );
00089         }
00090 
00091         if(!shadingdata.m_bdf->IsSpecular())
00092             break;
00093 
00094         specularbounce = true;
00095 
00096         const Vector3 local_incoming = surfacebasis.TransformToLocal(-ray.m_direction);
00097 
00098         Vector3 scattereddir;
00099         Real prob;
00100         Real bsdf_value;
00101 
00102         shadingdata.m_bdf->Sample(
00103             context,
00104             local_incoming,
00105             &scattereddir,
00106             &prob,
00107             &bsdf_value);
00108 
00109         const Vector3 world_scattereddir = surfacebasis.TransformToWorld(scattereddir);
00110 
00111         power *= bsdf_value * shadingdata.m_reflectance * (world_scattereddir * geometric_normal) / prob;
00112 
00113         // Replace the current ray by the reflected ray.
00114         ray.NewId();
00115         ray.m_origin = point + 1.0e-8 * geometric_normal;   // shift ray origin to avoid false intersections
00116         ray.m_direction = world_scattereddir;
00117     }
00118 }

Generated on Tue May 11 01:31:49 2004 for toxic by doxygen 1.3.6