I am currently working on a CPU based simple ray tracer to render few triangles as a project. I'm okay with every aspect of it except generating the actual rays. I do not wish to project world coordinates into screen space, but actually create the rays according to where the camera is located in 3D coordinates.
Right now, I have a fairly alright algorithm which allows me to generate a ray for each pixel on the screen for any rotation around the Y-Axis, and it attempts to incorporate the X-Axis as well, giving some up and down looking capability, however when the user looks up or down, the image becomes distorted.
This is what I have worked out so far:
Ray ray = new Ray(Camera.position,
new Vector3(
Math.sin(Camera.rotation.y+(x*2/Main.renderSize.width)/2) * Math.cos(Camera.rotation.x+(y*2/Main.renderSize.height)/2),
Math.sin(Camera.rotation.x+(-y*2/Main.renderSize.height)/2),
Math.cos(Camera.rotation.y+(x*2/Main.renderSize.width)/2) * Math.cos(Camera.rotation.x-(y*2/Main.renderSize.height)/2)
));
This gives me a good viewing projection when the camera is not facing in any upwards or downwards direction.
Image of projection when camera is forwards:
.
Image of projection when camera is looking slightly upwards
.
It would be greatly appreciated if anyone can help me with the algorithm or point me towards a new one or a good source. Speed is a necessity as it is all real-time. Thanks.