# Camera for Raytracing

This topic is 1772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,

I am working on a raytracer at the moment and I come across some issues with the camera model. I just don't seem to manage the calculation of the direction vector of my rays.

Let's say we have given an image resolution of resX x resY, a position pos of the camera, the up vector, the direction vector dir (the direction in which the camera is looking) and a field of view for the horizontal and vertical component fovX and fovY. With the help of these values I can manage to calculate the focal length and thus the width of my pixels.

[url=http://www.imagebanana.com/view/ysihutsh/fovx.jpg][/URL]

But when I do the same for the height I'll get another focal length.

[url=http://www.imagebanana.com/view/v041vsp2/fovy.jpg][/URL]

There is something I must be missing because the focal length determines the distance between my camera position and the image plane and thus must be unique.

But let's suppose that I've got one unique focal length, then calculating the direction of a ray for the screen coordinates (x,y) should be done with this formula.

[url=http://www.imagebanana.com/view/wa3nrteo/formel.jpg][/URL]

I adjust the direction vector in such a way, that he is always in the center of a pixel.

Edited by StanLee

##### Share on other sites

I think what the diagrams indicate is the x-focal length and y-focal length, such that the actual focal length should be equal to sqrt(fx^2 + fy^2), by the Pythagorean Theorem. Though I don't fully understand the diagrams.

What I like to do is calculate only the corners of the focal plane, and interpolate the focal point for an arbitrary pixel using bilinear interpolation. It's clearer and a bit faster. Then the camera direction is just the focal point minus the camera position (normalized, of course). You might find some of my code useful: this, along with this.

Edited by Bacterius

##### Share on other sites

Thanks a lot! Your code helped me to figure out that I totally forgot to normalize the scale of my image plane.
Before normalization my image plane was centered around (0,0,500) and the top left corner around (-512, 384, 500) in world space. Without adjustments of the object positions and sizes absolutely nothing could be seen.
Now I used your proposed equation for the focal length f:

[eqn] f = \sqrt{f_{x}^{2} + f_{y}^{2}} [/eqn]

Everything works fine by now though I still have a question. When I choose a wide angle for the horizontal field of view, let's say 120°, I see spheres getting oblong at the border of the screen.  Is this the so called "fisheye"-effect due to a wide field of view?