我正在用 c# 编写一个简单的光线追踪器/光线投射器。我过去使用过 Vectors,所以我编写了一个名为 Vector3D 的类,如下面的代码所示。我还写了一个类来处理光线。现在,我关心的是确保光线来自相机,并投射到屏幕上的所有像素,然后投射到场景中相机前面的对象。我已经过期将文本写入输出(Debug.WriteLine),但很难看出它是否真的有效。以下代码是否合适,或者您会推荐其他方法或网站来参考/指导我吗?
for (int x = 0; x < sizeofoutput.Width; x++)
{
for (int y = 0; y < sizeofoutput.Height; y++)
{
Vector3D lookat = new Vector3D(sizeofoutput.Width / 2, sizeofoutput.Height / 2, 0);
Vector3D lookatrev = new Vector3D(lookat.X * -1, lookat.Y * -1, 0);
Vector3D tmp2 = lookatrev + new Vector3D(x, y, 0);
Vector3D campos = new Vector3D(0, 2, -6); // camera position.
Vector3D raydir = tmp2 - campos; // ray goes into a pixel.
Vector3D rayorg = campos; // ray starts at camera.
Ray ray = new Ray(rayorg); // create the ray from the data provided.
ray.Direction = raydir;
for (int c = 0; c < sceneobj.Length; c++)
{
// find object and render!
}
}
}