我正在用c#寫一個簡單的raytracer/raycaster。我在Vector中已經過期了,所以我寫了一個名爲Vector3D的類,如下面的代碼所示。我也寫了一堂課來處理光線。就目前而言,我擔心的是確保光線從相機出發,然後被投射到屏幕上的所有像素,然後投射到場景中相機前面的物體。我已經將文本寫入輸出(Debug.WriteLine),儘管很難看到它是否真正起作用。下面的代碼是否合適,或者你會推薦其他方法或網站來引用/引導我?Raycasting/Raytracing:拍攝來自相機的光線
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!
}
}
}
+1。斷開的鏈接。 – 2013-12-04 10:35:57
謝謝,更新了一個仍在線的副本。 – Waldheinz 2013-12-04 15:24:02