I’ve completely stuck with camera in ray tracing. Please, take a look at my calculations and point me out where is the error. I’m using left handed coordinate system.
x,y // range [0..S) x [0..S) //pixels coordinates
Now, let’s transform pixels coordinates to parametric coordinates of camera plane:
xp = x/S * 2 – 1;
yp = y/S * 2 – 1;
xp, yp // range [-1..1] x [-1..1]
calculation of camera basis:
//eye - camera position
//up - camera up vector
//look_at - camera target point
vec3 w = normalize(look_at-eye);
vec3 u = cross(up,w);
vec3 v = cross(w,u);
so ray direction should have following coordinates:
vec3 dir = look_at – eye + xp*u + yp*v;
ray3 ray = {eye, normalize(dir)};
I think the mistake is here:
The image plane should have a normal vector
w, and either be between the eye and the look at point (the more common way in ray tracers), or be behind the eye (more closely models an actual pinhole camera). So let’s create a scalarzoom_factor. A positive number will put the plane in front of the eye, and a negative one will put it behind the eye (and flip the image).The center of the image plane is thus:
A point
(xp, yp)on the image plane is thus:Now you want the direction to be from the eye to this point on this image plane:
The
eyes cancel, so it simplifies to:This assumes
xpanypare each in a range like (-0.5, 0.5). Note that (0, 0) is the middle of the image plane with this arrangement.