Using the Ray

[ LiB ]

Using the Ray

You learned about rays in Chapter 3; this section recaps a bit on what you learned there. Typical lines have a starting and ending point. Rays have a starting pointan origin where they begina direction in which they are heading, the distance they have traveled, and a destination. Rays will be used in ray tracing and photon scattering. As Chapter 3 mentioned, rays can be used to simulate the movement of light from one point to another. Rays are constructed to convey the line segment from any two points in 2D or 3D space, as shown in Figure 6.11.

Figure 6.11. An exam ple of a ray.The O refers to the origin of the ray, Dir refers to the direction of the ray, D refers to the destination of the ray,and Distance refers to the length of the ray.

graphic/06fig11.gif


For example, the origin of a ray can begin at the surface level of one object (sphere, triangle, or polygon) and the destination can end at the surface level of another. Rays can also begin at the light source and end at the object's surface level. Rays are perfect for object intersections, as you will soon discover. The four major components you'll declare in the ray class are the origin, the direction in unit vector, distance as a scalar, and the destination. Let's begin constructing the ray class. The class will consist of the independent variables of the cRay class and a variety of class methods . Two constructors are defined to set up the cRay class, one for general construction and the other for parameters.

NOTE

TIP

A very thin beam of light is classified as a ray.Ever heard the phrase ray of light ? The ray class is fashioned from a ray of light seen in nature.

 class cRay { public:    cVector3 origin,             direction,             destination;    // how far we have traveled    // from the origin    float distance; public:    // Default constructor    cRay()    {       origin = cVector3::vZero;       direction = cVector3::vZero;       distance = 0.0f;    }    // Custom  two-member input constructor    cRay( cVector3 o, cVector3 d )    {       origin = o;       direction = d;    } 

Setting the Ray's Origin

The first thing that is needed for a ray is the origin. The origin can be relative or absolute. For example the origin of a ray can begin at a certain point in space or it can be begin from the world origin (0,0,0). The origin of the ray is very important in order to calculate the ray direction.

 inline void Set_Origin( float x, float y, float z ) {  origin.x = x;  origin.y = y;  origin.z = z; } inline void Set_Origin( const cVector3 &o )  {   origin = o;  } 

Finding the Ray's Direction

To compute the direction vector of a ray, you must subtract the destination from the origin. This returns the direction vector between the origin and the destination point. To find the actual distance between these two vectors in scalar form, you simply find the magnitude from the origin to the destination point. You then normalize the direction vector to unit space.

 inline void Find_Direction( void )  {   direction = destination - origin;   distance = direction.Mag();   direction.Normalize();  } 

Setting the Ray's Destination

When assigning the destination point directly, the class will automatically compute the distance and the direction vector, as follows :

 inline void Set_Destination( float x, float y, float z )  {   destination.x = x;   destination.y = y;   destination.z = z;   Find_Direction();  } inline void Set_Destination( const cVector3 &d )  {   destination = d;   Find_Direction();  } 

Finding the Ray's Destination

To compute the destination point, you add the direction vector to the origin and multiply it by the distance traveled. The formula is as follows (destination = origin + direction * distance). This is used for ray object intersections:

 inline void Find_Destination()     {      destination = origin + direction * distance;     } 

Setting the Ray's Direction

This function is used to set the direction and compute the distance and the destination points' given the direction as a parameter.

 inline void Set_Direction( const cVector3 &dir )     {      direction   = dir;      destination = origin + direction;      distance    = dir.Mag();     } 

Normalizing the Ray's Direction

This method will be used to normalize the direction vector to unit space.

 inline void Normalize_Direction( void )     {     direction.Normalize();     } };  // end of class 

[ 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