2
votes

I am implementing a ray tracer and it currently has an orthographic projection. I want to make it into a perspective projection. I know in orthographic you send out a ray from every pixel and check for intersections. In perspective projection, the starting position of the ray is constant rather than starting from every pixel.

So I assume that in perspective projection the ray's starting position should be the camera's position. The problem is that I don't think I ever explicitly placed a camera, so I do not know what to change my ray's starting position to.

How can I determine where my camera is placed? I tried (0,0,0), but that just leaves me with a blank image so I don't think it is right.

2
If you have a blank image, your camera is not looking at your object(s). When you place a perspective camera, you are placing the point from which all the rays originate. When you rotate the camera or manipulate its zoom, you are manipulating the grid of pixels through which the rays are cast into the scene. Try to do the math to find which camera parameters you need to have your rays actually hit your object. Apart from this general advise, I cannot help you as I cannot understand your problem from the few bits of information you've given.cmaster - reinstate monica

2 Answers

1
votes

In orthographic projection, the rays through each pixel would have the same direction, as if the rays originated from a camera behind the screen placed at infinite distance.

For perspective projection, the camera has to be placed at a finite distance behind the screen. Each ray should originate from the camera and go through each pixel of the screen. The distance between the screen and camera depends on the viewing angle.

0
votes

You can triangulate the distance from the camera to your object by first picking an angle for the perspective projection. A simple example: picking an angle of 60° for the vertical Field of View (FOV) and assuming your object's center is at (0,0,0) and you want to place the camera to look down the Z axis towards the center of your object. This forms a triangle, where you can triangulate the distance with trigonometric formula: distance = (objectHeight/2) / tan(60/2). So you place the camera at (0,0,distance). You can use the same concept for your actual object location.