Raytracer - Computing Eye Rays

烈酒焚心 提交于 2019-12-03 06:13:39

问题


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 calculate the eye rays in a perspective projection, without using matrices.

I believe the last time I did it was by (potentially) inefficiently rotating the eye vectors x/y degrees from the camera direction vector using a Quaternion class. This was in C++, and I'm doing this one in C#, though that's not so important.

Pseudocode (assuming V * Q = transform operation)

yDiv = fovy / height
xDiv = fovx / width

for x = 0 to width
    for y = 0 to height

        xAng = (x / 2 - width) * xDiv
        yAng = (y / 2 - height) * yDiv
        Q1 = up vector, xAng
        Q2 = camera right vector, yAng
        Q3 = mult(Q1, Q2)

        pixelRay = transform(Q3, camera direction)
        raytrace pixelRay

    next
next

I think the actual problem with this is that it's simulating a spherical screen surface, not a flat screen surface.

Mind you, whilst I know how and why to use cross products, dot products, matrices and such, my actual 3D mathematics problem solving skills aren't fantastic.

So given:

  • Camera position, direction and up-vector
  • Field of view
  • Screen pixels and/or sub-sampling divisions

What is the actual method to produce an eye ray for x/y pixel coordinates for a raytracer?

To clarify: I exactly what I'm trying to calculate, I'm just not great at coming up with the 3D math to compute it, and no ray tracer code I find seems to have the code I need to compute the eye ray for an individual pixel.


回答1:


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.



来源:https://stackoverflow.com/questions/5944109/raytracer-computing-eye-rays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!