I\'m writing a ray tracer (mostly for fun) and whilst I\'ve written one in the past, and spent a decent amount of time searching, no tutorials seem to shed light on the way to c
INPUT: camera_position_vec, direction_vec, up_vec, screen_distance
right_vec = direction_vec x up_vec
for y from 0 to 1600:
for x from 0 to 2560:
# location of point in 3d space on screen rectangle
P_3d = camera_position_vec + screen_distance*direction_vec
+ (y-800)*-up_vec
+ (x-1280)*right_vec
ray = Ray(camera_position_vec, P_3d)
yield "the eye-ray for `P_2d` is `ray`"
x
means the cross product
edit:
The answer assumed that direction_vec
is normalized, as it should be. right_vec
is in the picture (seemingly where the left should be), but right_vec
is not necessary and, if included, should always be in the same direction as -(up_vec x direction_vec)
. Furthermore the picture implies the x-coord increases as one goes right, and the y-coord increases as one goes down. The signs have been changed slightly to reflect that. A zoom may either be performed by multiplying the x- and y- terms in the equation, or more efficiently, multiplying the vectors and using scaled_up_vec
and scaled_right_vec
. A zoom is however equivalent (since aperture doesn't matter; this is a perfect pinhole camera) to changing the field of view (FoV) which is a much better nicer quantity to deal with than arbitrary "zoom". For information about how to implement FoV, seem my comment below.