光线投射/光线追踪:从相机拍摄光线

发布于 2024-11-08 17:00:01 字数 1112 浏览 4 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉栀 2024-11-15 17:00:01

你的相机似乎沿着 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.

日暮斜阳 2024-11-15 17:00:01

我不确定我完全理解您想要实现的目标,但这也许会有所帮助:

  1. 将球体放在距相机一定距离的位置,
  2. 将光线跟踪结果输出到 PPM 文件。 PPM 的格式非常简单
  3. 完成后,只需在图像编辑器中打开 PPM 文件,看看是否看到圆圈。

此外,如果您计划拥有复杂的场景,您可能需要考虑比迭代每条光线上的所有场景对象更有效的方法。

I'm not sure I quite understand what you're trying to achieve, but maybe this could help:

  1. Put a sphere at a distance from the camera
  2. Output your ray trace results to a PPM file. A PPM's format is really trivial.
  3. After it's done, just open the PPM file in an image editor and see if you see the circle

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文