开发者

get uvw coordinates from pixel coordinates for ray-tracing

开发者 https://www.devze.com 2023-03-28 18:21 出处:网络
I\'m trying to implement a simple ray-tracing algorithm so the first step开发者_JAVA百科 is to convert pixel coordinates into uvw coordinates system

I'm trying to implement a simple ray-tracing algorithm so the first step开发者_JAVA百科 is to convert pixel coordinates into uvw coordinates system iam using those two equations that i found in a book

get uvw coordinates from pixel coordinates for ray-tracing

where l,r,b,t are the view frustum points , (i,j) are the pixel indexes , (nx,ny) are the scene width and height

then to calculate canonical coordinate i use

get uvw coordinates from pixel coordinates for ray-tracing

i want to understand the previous equations and why they give uwv coordinates for perspective projection not for orthogonal projection (when i use orthogonal projection the equation still gives the result as if perspective projection is used)


Let's assume your camera is some sort of a pyramid. It has a bottom face which I'll refer to as the "camera screen", and the height of the pyramid, also known as the focal length, will be marked as F (or in your equations, Ws).

         T(op)
       *---------*
       |\       /|
       | \     / |
       |  \   /  |
       |   \ /   |
L(eft) |    *E(ye| R(ight)
       |   / \   |
       |  /   \  |
       | /     \ |
       |/       \|
       *---------*
         B(ottom)

Let's assume j goes from the bottom to the top (from -Ny/2 to +Ny/2 in steps of 1/Ny), and i goes from left to right (from -Nx/2 to +Nx/2 in steps of 1/Nx). Note that if Ny is even, j goes up to Nx/2-1 (and similar when Nx is even).

As you go from bottom to top in the image, on the screen, you move from the B value to the T value. At the fraction d (between 0=bottom and 1=top) of your way from bottom to top, your height is

Vs = T + (B-T) * d

A bit of messing around shows that the fraction d is actually:

d = (j + 0.5) / Ny

So:

Vs = T + (B-T) * (j + 0.5) / Ny

And similarly:

Us = L + (R-L) * (i + 0.5) / Nx

Now, let's denote U as the vector going from left to right, V from bottom to top, 'W' going from the eye forward. All these vectors are normalized.

Now, assume the eye is located directly above (0,0) where that is exactly above the center of the rectangular face of the pyramid.

To go from the eye directly to (0,0) you would go:

Ws * W

And then to go from that point to another point on the screen at indexes (i,j) you would go:

Us * U + Vs * V

You will be able to see that Us = 0 for i = 0 and Vs = 0 for j = 0 (since B = -T and L = -R, as the eye is directly above the center of the rectangle).

And finally, if we compose it together, a point on the screen at indexes (i,j) is

S = E + Us * U + Vs * V + Ws * W

Enjoy!

0

精彩评论

暂无评论...
验证码 换一张
取 消