Designing the Scene

[ LiB ]

Designing the Scene

The scene is the final layer that all of your developed classes and logic will utilize. The logic that determines how the rendering application will perform must be defined in the scene. The scene will load the model, assign the materials, set up the lights, and render the 3D environment. You are going to design the basic scene object and then append more data and functionality to it later during the course of this book. The design of the scene class includes lights, objects, and the image buffer. Ray tracing and photon mapping are rendering algorithms that will be applied to the scene class. For now, let's define the basic attributes of the scene. These attributes encapsulate what you have learned up to this point.

The scene class will have n lights and objects. You declare two storage arrays and counter variables . This is because your scene will have n amount of light and n amount of objects so you need to dynamically load in the data on the fly. The scene will have two methods that add lights and objects to the internal object and light list.

The allocated memory heap will be set up for each array. Every time the array is at the limit for the light array, it will re-allocate five more data blocks for each interval. It will also re-allocate 50 objects at each memory upgrade for the object list and have an internal image data buffer to hold the image that you'll render to later, using one of your selected rendering algorithms. You are going to define a few member methods to manipulate the image data buffer for color operations. The scene will have a few methods to load, unload, and set up the scene. You'll declare the render width and render height for the color buffer so that all of the future renders will be rendered to an 800x600 bitmap. The class and data are located in the file cScene.h .

 // 50 objects at a time to re-allocate #define MEMORY_OBJECTS_CHUNK   50  // Five lights at a time to re-allocate #define MEMORY_LIGHT_CHUNK      5 #define RENDER_WIDTH          800 #define RENDER_HEIGHT          600 

The scene class will contain a few methods for the different types of procedures necessary to loading a 3D environment. Two variables are defined to count the number of lights and objects loaded. Two arrays are used to dynamically allocate lights and objects. The scene constructor and destructor are used to allocate and unallocate memory. Init_Scene() and Kill_Scene() are used to load objects at run time and unload them when we're done. Two methods are defined one to add an object to the object list and another to add a light to the light list. An image buffer is defined to hold the rendered image; this is used after we implement a rendering algorithm. Two color methods are defined to plot a selected color in the image buffer as well as fill the whole image buffer with a selected color. Here is the code:

 class cScene { public:    cScene();    ~cScene();    // scene load and destroy    void   Init_Scene();    void   Kill_Scene();    // light  methods    void   AddToObjectList(cObject ** pList, long *lCount, cObject tObject);    void   AddToLightList(cLight ** pList, int *nCount, cLight tObject);    // color methods    void   PlotPixel(int x, int y, color3 color);    void   FillColor(color3 color); public:    cObject * pObjectList;    long      nObjectCount;    cLight    * pLightList;    int      nLightCount;    color3    ColorBuffer[ RENDER_WIDTH * RENDER_HEIGHT ]; }; 

The Scene Constructor

The scene constructor resets all pointers to NULL and resets the counting variables to to ensure you don't run into any problems later.

 cScene::cScene() {     // objects counts     pObjectList       = NULL;     nObjectCount        = 0;     pLightList       = NULL;     nLightCount       = 0; } 

The Scene Destructor

The scene destructor releases any allocated memory and resets all pointers to NULL to ensure proper cleanup when you're done.

 cScene::~cScene() {    if(pObjectList) delete [] pObjectList;    pObjectList = NULL;    if(pLightList)  delete [] pLightList;    pLightList  = NULL; } 

Init Scene

This method will be used later to load and set up any code and data into the scene.

 void cScene::Init_Scene() {    // TODO: code goes here } 

Kill Scene

This method will be used later to perform any shutdown logic and release any allocated data in the scene.

 void cScene::Kill_Scene() {    // TODO: code goes here } 

Add an Object

This method allocates or re- allocates data when the object array is at its peak. It then assigns a passed parameter into the internal object list.

 void cScene::AddToObjectList(cObject ** pList, long *lCount, cObject tObject) {   int cnt = 0;   if ((*lCount % MEMORY_OBJECTS_CHUNK) == 0)   {      cnt = ((*lCount/MEMORY_OBJECTS_CHUNK)+1) * MEMORY_OBJECTS_CHUNK;      *pList = (cObject *) realloc(*pList,sizeof(cObject) * cnt);   }   (*pList)[(*lCount)++] = tObject; } 

Add a Light

This method allocates or re-allocates data when the light array is at its peak. It then assigns a passed parameter into the internal light list.

 void cScene::AddToLightList(cLight ** pList, int *nCount, cLight tLight) {    int cnt = 0;    if ((*nCount % MEMORY_LIGHT_CHUNK) == 0)    {        cnt = ((*nCount/MEMORY_LIGHT_CHUNK)+1) * MEMORY_LIGHT_CHUNK;        *pList = (cLight*) realloc(*pList,sizeof(cLight) * cnt);    }    (*pList)[(*nCount)++] = tLight; } 

Plot a Pixel

The plot pixel method changes the value of a pixel (using its X and Y coordinates) in the internal color buffer.

 void cScene::PlotPixel(int x, int y, color3 color) {   if(x < 0  x > RENDER_WIDTH  y < 0  y > RENDER_HEIGHT) return;   ColorBuffer[y * RENDER_WIDTH + x] = color; } 

Fill Buffer with Color

This procedure cycles through the color buffer and assigns a selected color:

 void cScene::FillColor(color3 color) {   // fill with color   for(int x=0; x<RENDER_WIDTH; x++)     for(int y =0; y<RENDER_HEIGHT; y++)       PlotPixel(x,y, color); } 

[ 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