Using the Upgraded cScene Class

[ LiB ]

Using the Upgraded cScene Class

You can now take a look at the upgraded cScene class with the new photon mapping methods applied. These methods cannot be applied to the PhotonMap class directly because they require special elements of the cScene class. The CreatePhotons() method creates photons from the light sources. The ProcessPhotonHit() method processes the nature of a photon based on a probabilistic solution. The PhotonContribution() method calculates the radiance estimate for a point in 3D space.

 class cScene: public PhotonMap {    // Rest of Code is assumed    // called to scatter photons from the light sources    void      CreatePhotons();    // called when an intersection has occurred to select   // a probabilistic solution    void      ProcessPhotonHit(int iObjIndex,                  cVector3 tPhotonDir,                  cVector3 tInter, photon_t tPhoton,                  int depth,                  BOOL insideobj);   // used to find the radiance estimate   color3      PhotonContribution(int iObjectIndex,                   cVector3 tDirection,                   cVector3 tInter); }; 

Upgrading the Init Scene Method

You also need to upgrade the Init_Scene() method found in the cScene class to accommodate the photon-mapping algorithm. The new logic asks the users for the amount of photons to scatter per object as well as how many photons to search for when finding the radiance estimate for a point in 3D space. It loads this information into the member variables in the PhotonMap class for building and rendering the global photon map. The scene is read in from file and Render_Algorithm is set to photon mapping. Here is the code:

 void cScene::Init_Scene() {    char buffer[256];    cout << "Please Type in Path and File Name\n";    cout << "Usage (ex. C:\sample.txt)" << endl;    cout << "Enter Information: ";    cin  >> buffer;    // read in scene    Read_In_Scene(buffer);    // Which Algorithm?    //Render_Algorithm = RAY_TRACING;    Render_Algorithm = PHOTON_MAPPING;    // if photon mapping then ask for more info    if (Render_Algorithm & PHOTON_MAPPING)    {       // ask for photon mapping      long m,f;      cout << "How Many Photons Per Object?\n";      cin  >> m;      cout << "How Many Photons for pixel Contribution?\n";      cin  >> f;      this->SetupPhotonMapping(m,f);   } } 

Updating the Trace Ray in Scene Function

You now should add the photon contribution method to the Tray_Ray_In_Scene() function located in the cScene class. Only the logic based on the Rendering_Algorithm variable has changed. If the variable is set to pho-ton mapping, it will ignore all ray tracing code and add the radiance estimate for the point of intersection. This method has one very small change whereby the photon contribution method is added to the point of intersection.

This is the part of the photon mapping application where you use the sample ray from the image plane to the point of intersection and apply the radiance estimate. (See Figure 12.10.) The radiance estimate is a composite of the sample ray and the irradiance to calculate the final lighting solution for the point. This is the final solution that's assigned to the source pixel on the image plane.

Figure 12.10. The radiance estimate is found by combin ing backward ray tracing and the total incoming irradiance for the point of intersection.

graphic/12fig10.gif


The new code appears in bold:

 bool cScene::Tray_Ray_In_Scene(cRay ray, int depth, color3 *pColor) {    cVector3 Hit, Normal;    cVector3 Reflected_Dir,         Refracted_Dir;    int      iClosestObject;    color3       color_Object = color3::Black,           intensity_Shade = color3::Black;    color3 color = color3::Black;    // added in chapter 9 - ray tracer 2    color3 reflected_Shade = color3::Black;    color3 refracted_Shade = color3::Black;    if(depth > MAX_TRACE_DEPTH)    {        *pColor = color;        return false;    }    // find closest object    iClosestObject = Find_Closest_Object(ray, &Hit);    // get object normal    Normal = pObjectList[iClosestObject].Normal(Hit);    // if object is in the local array list    if(iClosestObject > -1)    {        // =================================================================        // object color        // =================================================================        color_Object = pObjectList[iClosestObject].Color;        // =================================================================        // BEGIN RAY TRACING        // =================================================================        if (this->Render_Algorithm & RAY_TRACING)        {            // Ray Tracing uses an ambient shading to ensure objects are lit           color = (color * pObjectList[iClosestObject].fAmbientFactor);           // diffuse shading & specular highlights              intensity_Shade = ShadePoint(Hit, Normal, iClosestObject);                // combine our information                color.r += color_Object.r * intensity_Shade.r;                color.g += color_Object.g * intensity_Shade.g;                color.b += color_Object.b * intensity_Shade.b;                // Calculate Reflection             if(RenderSetting.use_specular_reflection &&                 pObjectList[iClosestObject].fSpecularFactor > 0.0f)             {              Reflected_Dir =                  ray.direction.Vector_Reflection (Normal) ;              if(!Tray_Ray_In_Scene(cRay(Hit, Reflected_Dir),                       depth + 1, &reflected_Shade))                    reflected_Shade = color3::Black;             }             color += (reflected_Shade                      * pObjectList[iClosestObject].fSpecularFactor);                // Calculate Refraction                // FROM AIR (1.003) INTO SURFACE             if(RenderSetting.use_refraction &&                    pObjectList[iClosestObject].fOpacity < 1.0f)             {                  if(ray.direction.Vector_Refraction(Normal,                      1.003f,                      pObjectList[iClosestObject].fIndex_Of_Refraction,                      &Refracted_Dir))                  {                   if(!Tray_Ray_In_Scene(cRay(Hit,                       Refracted_Dir),                       depth + 1,             &refracted_Shade))         refracted_Shade = color3::Black;      }   }     color += (refracted_Shade *               pObjectList[iClosestObject].fOpacity); } // ================================================================= // END RAY TRACING // =================================================================  else if (this->Render_Algorithm & PHOTON_MAPPING)   // =================================================================   // APPLY PHOTON MAPPING   // =================================================================   {   // apply photon mapping intensity for the point   color   += PhotonContribution(   iClosestObject ,   ray.direction ,   Hit);   }  else  {  // Do nothing; no algorithm used  }  // ================================================================= // Saturate colors out of range // ================================================================= color.Sat(); // ================================================================= // Save Color // =================================================================      *pColor = color;  }  else  {     *pColor = color3::Black;     return false;  }  return true; } 

[ LiB ]


Focus On Photon Mapping
Focus On Photon Mapping (Premier Press Game Development)
ISBN: 1592000088
EAN: 2147483647
Year: 2005
Pages: 128
Authors: Marlon John

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net