Home » Programming » C++ Raytracer » Ray Tracer Part Two – Creating the Camera

Ray Tracer Part Two – Creating the Camera

gridcoin728x90_40b-99980e

The camera is responsible for creating a ray which passes from they eye point through the pixel we want to render.

The camera will be defined by an ‘eye point’, a ‘look at point’, and a ‘field of view’.  It is also necessary to define an ‘up vector’ for the camera, however this will be assumed to be always up in this case (0,1,0).

When a camera is constructed, several attributes will be set which will allow the easy generation of rays later. The first parameter we will calculate is the Point located at the bottom left of the view plane.  We will then calculate two vectors, one which will be added to the bottom left point for every increase in x on the view plane, and one which will be added to the bottom left point for every increase in y on the view plane.

Constructing the camera

Camera

Our constructor requirements,

Camera(Point eyePoint, Point lookAtPoint, float fov, unsigned int xResolution, unsigned int yResolution)

First calculate the “viewDirection” vector.

viewDirection = lookAtPoint - eyePoint;

Next calculate the V vector , here we will assume up = (0,1,0).

U = viewDirection x up

Now recalculate the up vector, (if the camera is tilted, the cameras up vector will not be (0,1,0), right?)

V  = U x viewDirection

Be sure to normalise the U and V vectors at this point

Obtaining the bottom left point on the viewplane is now easy.

viewPlaneHalfWidth= tan(fieldOfView/2)
aspectRatio = yResolution/xResolution
viewPlaneHalfHeight = aspectRatio*viewPlaneHalfWidth

Camera2
viewPlaneBottomLeftPoint = lookatPoint- V*viewPlaneHalfHeight - U*viewPlaneHalfWidth

So now we need the increment vectors, which will be added to the bottomLeftViewPlanePoint to give our viewplane coordiantes for each pixel.

xIncVector = (U*2*halfWidth)/xResolution;
yIncVector = (V*2*halfHeight)/yResolution;

Getting a ray

Now we want a method that can return a ray that passes from the eye point through any pixel on the viewplane.  This can be easily obtained by calculating the view plane point, and then subtracting eye point. x and y are the viewplane coordinates of the pixel we want to render, numbering from the bottom left.

viewPlanePoint = viewPlaneBottomLeftPoint + x*xIncVector + y*yIncVector
castRay = viewPlanePoint - eyePoint;

Conclusion

This is by know means the only way to create a camera, but is fairly intuitive to get started. It is also easily modified at a later date to allow the calculation of anti-aliasing rays.
Next – Adding a Sphere


Leave a comment

Contact

Email: sjh148@uclive.ac.nz