# perspective camera help for ray tracing

## Recommended Posts

Buzz1982    122
Hello, I am developing a simple ray tracing program for learning purpose. Currently i am able to shoot rays and intersect some objects using orthographic camera. For perspective camera i am unable to find any tutorial or link that explains how to generate rays for perspective camera. If some one knows the mathematics required to generate rays for perspective camera please help me or guide me to a link or tutorial that explains it. Thanks alot Bye

##### Share on other sites
deepsender    100
In your current orthographic projection, you have a starting point and direction for each ray. Thus, you can find a starting point and some ending point for each ray. Now, simply change the starting point of each ray to the same spot (the camera). Hope this works for you.

##### Share on other sites
Zipster    2365
The first thing you want to do is generate the camera-space basis using the given rotation (such as yaw, pitch, and roll) and translation settings of the camera. You can either do this manually, or use a utility function like gluLookAt and then grab the resulting matrix. This will give you the vectors (R,U,F), which stand for the Right, Up, and Forward vectors. You can then generate points on the view plane by using the parametric equation:

N*F + u*(w/2)*R + v*(h/2)*U

Where N is the view plane (a.k.a the near plane) distance, w and h are the width and height of the view, and u and v are your parameters in the range [-1,1]. The idea behind generating points on the near plane is that you can then subtract them from the camera position to get a direction. By varying u and v, you can generate all the rays you need for a perspective view. In essence, u and v become your "screen coordinates", and it's helpful to think of them as such.

##### Share on other sites
Buzz1982    122
I tried the following code to generate the rays for perspective camera. It seems to work for triangle but its not working correctly for sphere. Whats happening is that when i move the sphere to the left or to the right, it deforms and becomes elongated along the horizontal axis. Similarly it elongates vertically when moved up or down. However this is not the case with triangle(triangle is facing the camera). The shape of the triangle remains same no matter i move it left or right. Following is the way i generated the rays for each pixel [x,y] on the screen,

CVector3 Eye( 0.0f, 0.0f, 5.0f );
CVector3 Direction( ( float )x, ( float )y, 0.0 );
Direction = Direction - Eye;
Direction.Normalize();
CRay Ray( Eye, Direction );

Please tell me if there are some shortcomings in this method and what alternative method should i use. Also if possible can u give me the reason why sphere is deformed and why not triangle.

Thanks

Bye

##### Share on other sites
deepsender    100
This kind of distortion happens when your frustum angle is very large. Looking at your code, you could try moving the eye back with something like:

CVector3 Eye( 0.0f, 0.0f, 25.0f );

Your sphere might get very small, so then scale-down your x & y values.