I need to calculate ray origin and ray direction vector for ray-marching orthographic view.
I know that ray origin must move for each pixel and not stay from center and I believe this formula ro = ro + vec3(v_UV * orthRectSize, 0.); should work.
I don't know how to calculate ray direction.
I managed to get it to work by making rd = vec3(0, 0, 1) and then rotating world content. Is there better method to calculate correct values directly?
A more general way to get the ray origin and ray direction would be to reconstruct the fragment's location on both the near and far plane of the view frustum in world coordinates, and then find the direction from the fragment on the near plane to the fragment on the far plane.
First you need to convert the fragment (image x,y) to normalized device coordinates. The operation would change depending on the layout of pixels in your graphics library but generally it would look something like this Rust function:
Then you convert the fragment's position from normalized device coordinates to world coordinates by setting the position's
wcomponent to1.0and multiplying by the inverse of theprojection * viewmatrix, followed by dividing the resulting point by it'swcomponent:Put those operations together to convert from a fragment to world space:
Now calculate the fragment position on the near plane (
z = 0.0in my graphics library) and the far plane (z = 1.0). Subtract the far point from the near point, normalize it, and now you have your ray direction. The ray origin is the fragment's point on the near plane:I hope that helps!