光线投射/光线追踪:从相机拍摄光线
我正在用 C# 编写一个简单的光线追踪器/光线投射器。我过去对向量已经过期,所以我编写了一个名为 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!
}
}
}
I am writing a simple raytracer/raycaster in c#. I have expirence in the past with Vectors, so I wrote a class called Vector3D, as you can see in the code below. I also wrote a class to handle the Rays. For now, I am concerned about ensuring that the rays, are orginating from the camera, and being casted to all of the pixels on the screen, then to objects in front of the camera in the scene. I have expiremented with writing text to the Output (Debug.WriteLine), though it hard to see if it is really working. Would the following code be appropriate, or would you recommend another method or a site to refer/guide me?
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!
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的相机似乎沿着 z 轴发射光线,如果这就是你想要的效果,那么看起来不错。请注意,您的光线方向未标准化,这可能会或可能不会成为以后的问题。你必须牢记这一点并采取相应的行动。无论如何,我认为对于基础知识 Phantom 的教程 是我开始的原因(并且上瘾了!)光线追踪,我认为这对你来说可能是一本很好的读物。
Your camera seems to be firing rays down the z-axis, if this is what you were after it looks good. Note that your ray's direction is not normalized, which may or may not be a problem later on. You have to keep it in mind and act accordingly. Anyway, I think for the very basics Phantom's tutorial is what got me started (and addicted!) with ray tracing, and I think it might be a good read for you.
我不确定我完全理解您想要实现的目标,但这也许会有所帮助:
此外,如果您计划拥有复杂的场景,您可能需要考虑比迭代每条光线上的所有场景对象更有效的方法。
I'm not sure I quite understand what you're trying to achieve, but maybe this could help:
Also, if you plan on having complex scenes, you may want to consider something more efficient than iterating through all the scene objects on every ray.