Designing the Photon Map Class

[ LiB ]

Designing the Photon Map Class

In order to scatter energy and manipulate photon movements, you must create a class where all photon mapping structures and functions reside. This class will be inherited into the cScene class after you are done constructing it. This is done to keep the internal photon mapping procedures as independent from the cScene class as possible. As you'll see, you'll need a few methods and objects from the cScene . Let's begin by designing the class.

The data members of the photon mapping class are composed of the following:

  • A pointer to the global photon map array called pGlobalPhoton .

  • A variable to count the number of current photons in the global pho-ton map called lNumGlobalPhotons.

The member methods of the photon mapping class are composed of the fol-lowing:

  • The photon mapping constructor is used to reset variables and null pointers.

  • The photon mapping destructor is used to unload memory.

  • The AddToPhotonList() method is used to add photons to the photon map.

Let's take a look at the class:

 class PhotonMap { public:      long       lNumGlobalPhotons;    photon_t * pGlobalPhoton; public:    PhotonMap();    ~PhotonMap();    void   AddToPhotonList(photon_t **, long *, photon_t); }; 

Now you can inherit the PhotonMap class into the cScene class.

 class cScene: public PhotonMap {   // .. rest of code assumed } 

When the cScene constructor is executed, it will call the photon-mapping constructor.

 cScene::cScene() {    // Rest of code is assumed    // photon mapping constructor    PhotonMap::PhotonMap(); } 

You now need to define the inner workings of the constructor and destructor, as well as create a method that will add photons to the photon map.

Defining the Constructor

The class constructor initializes the number of photons to zero and sets the photon map array to NULL . No photons are currently saved in the photon map array.

 PhotonMap::PhotonMap() {    lNumGlobalPhotons = 0;    pGlobalPhoton        = NULL; } 

Defining the Destructor

The scene destructor releases any allocated memory and sets the array to NULL .

 PhotonMap::~PhotonMap() {     if (pGlobalPhoton != NULL)        delete [] pGlobalPhoton;     pGlobalPhoton = NULL; } 

NOTE

NOTE

The AddToPhotonList() method reallocates an additional 1000 pho- tons at a time whenever the photon list is full.

Adding Photons to the Photon Map

The AddToPhotonList() method will add photons to the photon map (which is just an array). It will allocate and reallocate data if required. The number of photons that are allocated at each interval is 1000.

 // 1000 photons at a time to allocate #define PHOTON_REALLOC_JUMP             1000 void PhotonMap::AddToPhotonList(photon_t ** pList,               long *lCount,               photon_t tPhoton) {    int cnt = 0;    if ((*lCount % PHOTON_REALLOC_JUMP) == 0)    {       cnt = ((*lCount/PHOTON_REALLOC_JUMP)+1) * PHOTON_REALLOC_JUMP;       *pList = (photon_t *) realloc(*pList,sizeof(photon_t) * cnt);    }    (*pList)[(*lCount)++] = tPhoton; } 

You now have a class that includes a photon map that can dynamically allocate photons by using the AddToPhotonList() method. The next step on your way to implementing the algorithm is to scatter photons from the light sources. Because you've been using a ray structure to track the movements of light from one point to another, it would be wise to use it for photon movements as well. This way, you don't have to rewrite any new code for casting photons into the scene and testing for object intersections. You will upgrade the PhotonMap class throughout this chapter as you learn more about the algorithm.

[ 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