如何在给定任意相机的情况下投射光线?

发布于 2024-08-11 02:19:05 字数 626 浏览 6 评论 0原文

我正在编写一个光线追踪器(使用左手坐标,如果这有影响的话)。这是为了自学原理,所以我还没有使用 OpenGL 或景深等复杂功能。我的相机可以有任意位置和方向;我通过三个向量来指示它们:locationlook_atsky,其行为类似于 等效 POV 射线矢量。它的“胶片”也有宽度高度。 (焦距长度是由从positionlook_at的距离来暗示的。)

我的问题是不知道如何投射光线。我有两个量,vxvy,它们指示光线应该结束的位置。它们的变化范围都是从 -1 到 1。如果它们都是 -1,我会将光线从相机位置投射到“胶片”的左上角;如果它们都是 -1,我会将光线从相机位置投射到“胶片”的左上角;如果都是1,则右下角;如果都为 0,则为中心;其余的都是显而易见的。

我对向量算术不够熟悉,无法推导出射线方程。我希望能得到有关如何操作的解释。

I'm writing a ray tracer (using left-handed coordinates, if that makes a difference). It's for the sake of teaching myself the principles, so I'm not using OpenGL or complex features like depth of field (yet). My camera can have an arbitrary position and orientation; I indicate them by way of three vectors, location, look_at, and sky, which behave like the equivalent POV-Ray vectors. Its "film" also has a width and height. (The focal length is implied by the distance from position to look_at.)

My problem is that don't know how to cast the rays. I have two quantities, vx and vy, that indicate where the ray should end up. They both vary from -1 to 1. If they're both -1, I'm casting the ray from the camera's position to the top-left corner of the "film"; if they're both 1, the bottom-right; if they're both 0, the center; and the rest is apparent.

I'm not familiar enough with vector arithmetic to derive an equation for the ray. I would appreciate an explanation of how to do so.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

挖鼻大婶 2024-08-18 02:19:05

您已经很好地描述了需要完成的工作。您的视野取决于您的相机和您要投射光线的“胶片”之间的距离。相机距离胶片越远,视野就越窄。

将胶片想象为相机指向的位图图像。假设我们将相机放置在距离位图一个单位的位置。然后我们必须通过位图的每个像素投射光线。

该向量非常简单。如果我们将相机位置设置为 (0,0,0),位图胶片就在它前面,中心位于 (0,0,1),那么右下角的光线为 - tada - (1, 1,1),左下角的是(-1,1,1)。

这意味着右下角和左下角之间的差是(2,0,0)。

假设您的水平位图分辨率应为 1000,那么您可以按如下方式迭代底线像素:

width = 1000;
cameraToBottomLeft = (-1,1,1);
bottomLeftToBottomRight = (2,0,0);

for (x = 0; x < width; x++) {
    ray = cameraToBottomLeft + (x/width) * bottomLeftToBottomRight;
    ...
}

如果很清楚,那么您只需为线条添加等效的外部循环,并且您就拥有了所需的所有光线。

然后,您可以为相机到胶片的距离以及水平和垂直分辨率添加适当的变量。完成后,您可以开始使用矩阵变换来更改外观向量和向上向量。

如果您想了解计算机图形学,入门教材可能会有很大帮助。我在大学时使用过这个,我想我喜欢它。

You've described what needs to be done quite well already. Your field of view is determined by the distance between your camera and your "film" that you're going to cast your rays through. The further away the camera is from the film, the narrower your field of view is.

Imagine the film as a bitmap image that the camera is pointing to. Say we position the camera one unit away from the bitmap. We then have to cast a ray though each of the bitmap's pixels.

The vector is extremely simple. If we put the camera location to (0,0,0), and the bitmap film right in front of it with it's center at (0,0,1), then the ray to the bottom right is - tada - (1,1,1), and the one to the bottom left is (-1,1,1).

That means that the difference between the bottom right and the bottom left is (2,0,0).

Assume that your horizontal bitmap resolution should be 1000, then you can iterate through the bottom line pixels as follows:

width = 1000;
cameraToBottomLeft = (-1,1,1);
bottomLeftToBottomRight = (2,0,0);

for (x = 0; x < width; x++) {
    ray = cameraToBottomLeft + (x/width) * bottomLeftToBottomRight;
    ...
}

If that's clear, then you just add an equivalent outer loop for your lines, and you have all the rays that you will need.

You can then add appropriate variables for the distance of the camera to the film and horizontal and vertical resolution. When that's done, you could start changing your look vector and your up vector with matrix transformations.

If you want to wrap your head around computer graphics, an introductory textbook could be of great help. I used this one in college, and I think I liked it.

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