Implementing the Camera into the Scene

[ LiB ]

Implementing the Camera into the Scene

Implementing the camera in the scene is our primary goal. You can implement the camera into your application using a few methods . The approach used here is common in most modern applications. This method is composed of a position point and three rotational vectors. The position vector is used to tell where the camera is located in the scene, and the rotational vec-tors are used to rotate the camera relative to the world. For example, the world's X axis never changes, and neither do the Y and Z axis. This means that the camera rotates relative to each static X, Y, and Z world axes. These rotational vectors are going to be used to generate the local X, Y, and Z axes of the camera. See Figure 8.4.

Figure 8.4. The camera's local coordinate system is composed of three orthogonal vectors.These vec- tors represent the local X,Y,and Z axes of the camera relative to the world's coordinate system.

graphic/08fig04.gif


One problem when setting up a camera in the scene is to consider that the camera is constantly changing in orientation and position. This creates a problem when rendering an image because the camera's setup must be perfect so that it records the world in the correct form. The rendered image on the image plane cannot be distorted or irregularly rendered due to an incorrect definition of the three local axes of the camera. The local X, Y, and Z axes must always be orthogonal ( perfectly aligned at 90 degrees apart) with each other at all times even after the camera has changed in position and orientation. The local X axis must be perpendicular to the local Y axis. The local Z axis must be perpendicular to the local Y axis, and so forth. See Figure 8.5 for an example of this alignment concept.

Figure 8.5. The camera alignment of the LOOK , UP ,and RIGHT vectors must always be orthogonal even after the cam era has been rotated and translated.

graphic/08fig05.gif


In order to set up the camera, you need to do the following:

  1. Create a location point for the camera.

  2. Set up the camera so that it will be rotated properly.

  3. Render the world using the pre-computed camera.

Why Use Three Vectors?

Because you are looking at the world from the camera, the camera can be seen as the originthe point from where everything is seeneven though it's an offset in world space. All you're doing is setting up the camera so that it will imitate a real coordinate system. Again, the camera's coordinate system is always changing relative to the world axis alignment and must be kept perpendicular even after being changed. To remedy this, each camera axis must be regenerated each frame for a game or each render for a rendering application, based on the new orientation and position of the camera. Calculating the position for the camera is not a problem, but calculating the rotation can be a bit difficult. Rendering the world to the camera's position can be a pain in the neck if not done correctly.

You are going to define your own camera and apply it to the cScene class created in the last chapter. The goal is to generate the local X, Y, and Z axes for the camera's coordinate system. This requires three offset vectors that are used to track the degrees in rotation for the X, Y, and Z axes relative to the world. Let's begin by defining and naming these rotational vectors.

The three vectors are as follows :

  • LOOK vector

  • UP vector

  • RIGHT vector

The LOOK Vector

The LOOK vector describes which way the camera is facing. For example, picture an observer standing at the world origin (0,0,0) and facing the screen. Consider a picture somewhere into the screenmaybe 30 units in. The observer or camera is focused on the picture and is standing at the world origin.

It is important to point out that this example uses the left-oriented system, so the positive Z axis is pointing into the screen. This basically means that the observer is facing the picture and the LOOK vector is the perfect candidate to represent this notion. In mathematical terms, this means that the LOOK vector is (0,0,1). This is enough information to render the world properly because you can tell where the camera is facing and positioned in the scene. If the observer turned 180 degrees in the opposite direction, the LOOK vector would now be (0, 0, -1). This is still okay because you can still tell where the observer is standing and facing. Imagine now that the observer is facing the picture once again. But now imagine the observer wanted to look at the picture from upside down instead of straight up, similar to how Spiderman hangs from the ceiling.

The observer is still looking at the picture in the same direction because the LOOK vector is (0,0,1), but the observer has changed his orientation to upside down. Another vector is required in order to track the new change in rotation. This vector is called the UP vector because it's vertical as opposed to forward (relative from the observer).

The UP Vector

The UP vector is like a long pole going straight up from the observer. The LOOK vector can be seen as a long pole going forward from the observer. The UP vector defines the rotation of the observer from upright to upside down. The UP vector is many times aligned with the world Y axis of (0,1,0) but may not be if desired. Again, this is a local coordinate system relative to the world's coordinate system and can change if required. These delta vectors just track the change in offset relative to the world alignment.

The UP vector can be rotated in such a way that it's aligned with the world X axis. For example, assume that the observer turned sideways (90 degrees) and is now lying on the floor still looking at the picture. The LOOK vector is still (0,0,1) but the UP vector (1,0,0) is now aligned with the world's X axis. This can become tricky so let's take a look at Figure 8.6 for an illustration. It does-n't take much to figure out what the RIGHT vector is, so let's cover that next .

Figure 8.6. Sideways view of the observer that depicts the LOOK vector facing the world Z axis,and the UP vector parallel to the world X axis.

graphic/08fig06.gif


The RIGHT Vector

You now know that the UP vector points straight up from the observer or straight down. You also know that the LOOK vector dictates the direction of the camera, either backwards or forwards. This basically means that the LOOK vec-tor is the local Z axis and the UP vector is the local Y axis. The local X axis must be the RIGHT vector (1,0,0), which completes the total definition of the camera coordinate system. Figure 8.7 shows an example of the RIGHT vector. The LOOK and UP vectors' values are normally entered in the world definition file of the scene, but the RIGHT vector's value can be automatically generated.

Figure 8.7. The RIGHT vector describes the wind- ing rotation of the observ er turning sideways (left to right).

graphic/08fig07.gif


This is due to the fact that, if you have two vectors, you can generate a perpendicular vector from the previous two ( LOOK and UP ) vectors by using the cross product method. Once you have the correct value for the RIGHT vector, you can generate the three local X, Y, and Z axes for the camera. All three components must be orthogonal and should be computed before render time. A simple way to remember how to calculate the three axes for the camera using these three vectors is to remember the phrase "Look Right Up Into The Sky." This works great because it lists the three steps needed to calculate the camera coordinate system.

Here is the list of newly created vectors:

  • The LOOK vector (0,0,1); manually entered

  • The UP vector (0,1,0); manually entered

  • The RIGHT vector (1,0,0); can be calculated using the cross product

Here is how you generate the three local X, Y, and Z axes for the camera:

  • Find the distance between the camera position and the LOOK vector to find the Z axis vector.

  • Normalize the Z axis vector so that it has unit length equal to 1.0.

  • By using the cross product method, cross the Z axis vector with the UP vector to find the X axis vector.

  • Normalize the result of the X axis vector so that it's 1 unit in length.

  • Cross the Z axis vector with the X axis vector to find the Y axis vector.

  • Normalize the result so the Y axis vector is 1 unit in length.

Let's take a look at the code to see how to set up the camera.

Adding the Camera Member Data

The following code shows you how to add the camera to your project. You need to declare the following new data members to the cScene class:

 // New Camera Code cVector3     CameraPosition,        Look,        Up; 

Determining the Viewing Direction

You also need another vector that determines the direction of the bright shiny highlights that will occur on objects when rendering. In order to calculate something called specular highlights, discussed in detail in the next chap-ter, you need to take this vector into account. Specular highlights occur when a very bright source of light is reflected off of a smooth surface. For example, you can sometimes be blinded by the bright reflection from the sun if the angle is relative to an object, say the window of a car, in front of you. To simulate this effect in computers, you need to take the viewer or camera direction into account. The specular highlight appears when the light goes directly into your eyes. Let's add a component for this in the cScene class. See Figure 8.8 for an example of the viewing direction vector.

Figure 8.8. The viewing direction is used to produce the bright shiny spot in a spe cific direction on a surface.

graphic/08fig08.gif


 //which way is the camera facing cVector3    Viewing_Direction; 

Tracking the Viewing Distance

You also need a scalar that tracks the distance between the view plane and the camera position point. This value is called the viewing distance . If you recall, the view plane is the plane that you project the final image on, which is then transformed and rendered into screen space. The viewing distance determines the field of view for the camera; large viewing distances result in narrow fields of view, whereas small viewing distances result in wide-angle fields of view. See Figure 8.9.

Figure 8.9. The viewing dis tance is the distance between the camera position and the image plane.This distance creates the overall perspec tive of how the scene should be rendered on the image plane.

graphic/08fig09.gif


Let's declare this value in the cScene class. The viewing distance, along with the camera coordinate system and the image plane, create the overall perspective of how the scene should appear. This is typically calculated every frame as the camera navigates through the world. The larger this value is, the smaller the overall view of the scene that is rendered on the image plane. See Figure 8.10 for an illustration.

Figure 8.10. A distance comparison between two images rendered with a viewing distance of 256 in the top image and 512 in the bottom image.

graphic/08fig10.gif


 // how much you perceive in the scene float        Viewing_Distance; 

Setting Up the Camera Coordinates

You are going to write a camera method that calculates the orthogonal X, Y, and Z axes for the camera. This function will utilize the camera position, as well as the LOOK and UP vectors listed in the scene definition file. You don't need to use the RIGHT vector because it can be automatically generated. The three passed pointers ( X_Axis , Y_Axis , and Z_Axis ) will be updated to be perpendicular with each other after the method has commenced:

 void cScene::Setup_Camera_Coords( cVector3 * X_Axis,                       cVector3 * Y_Axis, cVector3 * Z_Axis ) {    // Look    *Z_Axis = this->CameraPosition - this->Look;    Z_Axis->Normalize();    // Right    *X_Axis = this->Up ^ *Z_Axis ;    X_Axis->Normalize();     // Up    *Y_Axis = *Z_Axis ^ *X_Axis ;    Y_Axis->Normalize(); } 

The local X, Y, and Z axes of the camera are created. To rotate the system, you simply rotate two vectors at a time for a specific angle on a desired axis. Now if you wanted to rotate 90 degrees around the Z axis (the LOOK vector), you would rotate the UP (Y axis) and RIGHT (X axis) vectors 90 degrees around the LOOK vector. This is done to preserve the perpendicular relationship between vectors in order to keep the camera coordinate system correct. When spawning rays for ray tracing, you use the camera's position vector and these three rotational vectors to guide each ray in the correct direction in 3D space.

NOTE

TIP

You can write your own scene exporter from a famous 3D package such as Lightwave 3D or 3D Studio Max for this custom format by look ing at the member elements of the cScene 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