Importing the Scene from a File

[ LiB ]

Importing the Scene from a File

Importing the scene from a file is a very important aspect to a 3D rendering application. If you were to compile an application with custom geometry embedded in the code for every render, the result would be a tedious and daunting task. It would take forever to get reasonable results and many times you would lose a perfectly defined scene. I've had bad experiences trying to be cheap and not use a definition file in my applications. Sometimes I would render a beautiful image with all the objects set up in the perfect alignment with the camera and then I lose the magnificently defined scene by changing something accidentally . Importing the scene from a file is a great idea, and all 3D rendering applications include this feature. The first thing you want to do is read in the camera from file, as well as the rendering settings, and then read in the lights, spheres, and triangles . The file will be in ASCII format. The next section explains this concept using a sample file.

A Sample File

This sample file includes the camera, lighting parameters, a light, a sphere, and a triangle class.

 Camera { <0,0,-100>, <0,0,1>, <0,1,0>, <1,0,0>, 512  } Render_Settings { Diffuse_Shading       1 Specular_Highlights  1 Diffuse_Reflection   0 Specular_Reflection  0 Refraction            0 } Light { <200,0,-1900>, (1,1,1), 1.0 } Light { <20,0,770>, (0,1,0), 1.0 } Sphere { Material { fAmbientFactor         0.1 fSpecularFactor        0.2 fSpecularExponent      30.7 fOpacity                1.0 fIndex_Of_Refraction   0.1 fDiffuseFactor         0.8 fShininess             0.5 Color                 (1,1,1) Texture               texture.ppm } <200,0,200>, 200 } Triangle { Material { fAmbientFactor         0.1 fSpecularFactor        0.5 fSpecularExponent      30.7 fOpacity                1.0 fIndex_Of_Refraction   0.1 fDiffuseFactor          0.5 fShininess               0.5 Color                     (1,1,1) Texture                   texture.ppm }   <-100,300,0>, <100,150,0>, <-30,-200,0> } 

NOTE

NOTE

The rendering settings parameter must include at least one light- ing effect set to true in order to see anything after the scene is rendered.

Linking the File Library

As you can see, the file format is very simple and everything is self-explanatory. Because you are going to work with a file system object, you need to include the file-handling library. You need to include the following library header:

 #include <fstream.h> 

Creating a String-Comparison Method

Because each class has special class names that will dictate to the parser what type of handling is needed, you must create a string-comparison method.

 // String comparison method bool COMPARE(char * in, char * out) {    if(strcmp(in,out) == 0)       return true;    else       return false; } 

Defining the Buffer Length

As you will see, you are going to constantly create storage arrays to hold text that's read in from the scene definition file. It is smart to declare a fixed-length constant that will help make the code easier to read and change later. Each string array will be 80 characters in length. Here is the definition.

 #define BUFFER_LENGTH         80 

Defining the Class Format

All of the classes will be defined in the following format. The class begins with the class name followed by an opening brace. Next are the data elements or parameters listed in comma-delimited order. After all the elements are declared, the class is ended by a closing brace . You can also have something called a complex class, which has a subclass. A subclass follows the same format as the parent class except it's listed as a data member of the parent class; try to think of it as a clustered class. Subclasses are used for special classes that have many data elements. Subclasses make code more readable and understandable.

 // simple class ClassName { data, data, data,  } or // complex class ClassName { Sub ClassName { data, data ,data,  } data, data, data,  } 

[ 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