Creating and Using the Object Class

[ LiB ]

Creating and Using the Object Class

You'll use the object class most frequently in your 3D environment. It is the final encapsulated layer, defining a single entity that inherits all the member

methods and member variables of the Material , Sphere , and Triangle classes.

Designing the Object Class

The object class will have all the attributes of the inherited classes and will consist of a few methods and variables of its own. Because the object class inherits the two primitives, you need a default primitive member variable that will be used to determine which primitive is primary. You should define the classes and constants in the file cObject.h .

 #define OBJECT_NOTHING   0 #define OBJECT_SPHERE    1 #define OBJECT_TRIANGLE  2 

The Object structure consists of an inheritance of the Material , Sphere, and Triangle classes. It has one variable for selecting the default primitive and includes one constructor that initializes the default primitive variable and calls the inherited constructors. Four internal methods are defined: The first method finds the intersection point of the default primitive, the second returns the normal of the default primitive, the third computes the center point of the default primitive, and the fourth calculates the radius of the default primitive. Here is the code:

 class cObject : public cMaterial, public cSphere, public cTriangle { public:    int        Default_Primitive;   cObject()   {       // Set to nothing       Default_Primitive        = OBJECT_NOTHING;   };   // NOTE: Z is Positive other angles are correct   cObject(int Type)   {      switch(Type)     {      // Primitive is a Sphere      case OBJECT_SPHERE:       Default_Primitive        = OBJECT_SPHERE;      break;      // Primitive is a Triangle       case OBJECT_TRIANGLE:       Default_Primitive        = OBJECT_TRIANGLE;      break;     }        // call inherited constructors          cMaterial();          cSphere();          cTriangle();   };   bool      Find_Hitpoint(cRay *pRay);   cVector3  Normal(cVector3 hit_point);   cVector3  ObjectCenter();   float     ObjectRadius(cVector3 ); }; 

NOTE

TIP

The cObject class can also inherit other primitives you define on your own.

Object Center

This procedure returns the center point of the default primitive. If the default primitive is a sphere, the procedure will return the center point of the sphere. If it is a triangle, the procedure will return with the computed tri-angle's center point that was originally set up during the initialization. The object's center is selected by the default primitive variable.

 cVector3 cObject::ObjectCenter() {    if (Default_Primitive == OBJECT_SPHERE)       return vPosition;    if (Default_Primitive == OBJECT_TRIANGLE)       return vCenterPoint;    return cVector3::vZero; } 

The previous code simply tests the default primitive variable with the primitive constants to determine whether to return the center point of the selected primitive. The selected primitive can be Sphere or Triangle .

Object Radius

This procedure returns the radius of the selected primitive. If the default primitive is a sphere, the sphere's radius is returned. If the default primitive is a triangle, the radius of the triangle is found by subtracting each vertex by the input tCenter point. The magnitude of the farthest point on the triangle becomes the radius of the triangle.

 float cObject::ObjectRadius(cVector3 tCenter) {    float dMax, dThis;    int i;    cVector3 Dummy;    if (Default_Primitive == OBJECT_SPHERE)       return fRadius;    dMax = 0.0;    if (Default_Primitive == OBJECT_TRIANGLE)    {       for (i = 0; i < 3; i++)       {          Dummy = Vertex[i] - tCenter;          dThis = Dummy.Mag();          if (dThis > dMax)             dMax = dThis;       }    }    return dMax; } 

Finding the Hit Point

The Find_Hitpoint procedure will call the right method from the inherited class based on the default primitive value. If the primitive is a sphere, the ray will return with the destination point on the sphere. If the default primitive is a triangle, it will return with the destination point on the triangle.

 bool cObject::Find_Hitpoint(cRay * pRay) {  switch(Default_Primitive)  {       case OBJECT_SPHERE:       {          return cSphere ::Find_Hitpoint(pRay);       } break;       case OBJECT_TRIANGLE:       {          return cTriangle ::Find_Hitpoint(pRay);       } break;    } // end switch    return false;  } 

Determining the Normal

To find the normal from some arbitrary point in 3D space, you must again branch your code so that it will return the correct information based on the default primitive state. If the default primitive is a sphere, it will calculate the normal by subtracting the hit point from the sphere's center point (or position) and divide by the sphere radius. If the default primitive is a triangle, it will return the unit plane normal vector originally calculated in the triangle initialization and setup class.

 cVector3 cObject::Normal(cVector3  hit_point) {   switch(Default_Primitive)   {    case OBJECT_SPHERE:      return cVector3 ( ( hit_point - this->vPosition ) / fRadius ) ;    break;    case OBJECT_TRIANGLE:     return vNormal;    break;    }   return cVector3::vZero; } 

[ 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