光线追踪:何时对矢量进行归一化?

发布于 2024-11-26 18:24:49 字数 216 浏览 3 评论 0原文

我正在重写我的光线追踪器,只是想更好地理解它的某些方面。

我似乎已经解决了有关法线以及如何将它们乘以变换矩阵转置的逆的问题。

我感到困惑的是我什么时候应该标准化我的方向向量?

我正在关注某本书,有时它会明确指出要规范化我的向量,而其他情况则不然,我发现我需要这样做。

归一化向量方向相同,单位长度为 1?所以我不清楚什么时候有必要?

谢谢

I am rewriting my ray tracer and just trying to better understand certain aspects of it.

I seem to have down pat the issue regarding normals and how you should multiply them by the inverse of the transpose of a transformation matrix.

What I'm confused about is when I should be normalizing my direction vectors?

I'm following a certain book and sometimes it'll explicitly state to Normalize my vector and other cases it doesn't and I find out that I needed to.

Normalized vector is in the same direction with just unit length 1? So I'm unclear when it is necessary?

Thanks

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

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

发布评论

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

评论(5

看轻我的陪伴 2024-12-03 18:24:49

除非您正在处理矢量之间的角度,或者除非您正在旋转矢量,否则您永远不需要标准化矢量。

就是这样。

在前一种情况下,所有三角函数都要求向量落在单位圆上,这意味着向量已标准化。在后一种情况下,您将除以幅度,旋转矢量,确保它保持为一个单位,然后将幅度乘回来。归一化仅与范围有关。

  • 如果有人告诉您坐标系是由 n 个单位向量定义的,请知道 i-hat、j-hat、k-hat 等可以是以下任意向量: 任何长度和方向,只要它们都不平行。这是仿射变换的核心。

  • 如果有人试图告诉您点积需要标准化向量,请摇头微笑。当您使用点积来获取两个向量之间的角度时,它只需要归一化向量。

但是标准化不会让数学变得“更简单”吗?

并非如此——它增加了幅度计算和除法。 0..1 之间的数字与 0..x 之间的数字没有什么不同。

话虽如此,有时你会为了与他人良好相处而正常化。但是,如果您发现自己在调用方法之前将向量标准化为原则问题,请考虑使用附加到向量的标志来节省一步。从数学上来说,这并不重要,但实际上,它可以对性能产生巨大的影响。

再说一次......这都是关于旋转矢量或测量其相对于另一个矢量的角度。如果您不这样做,请不要浪费周期。

You never need to normalize a vector unless you are working with the angles between vectors, or unless you are rotating a vector.

That's it.

In the former case, all of your trig functions require your vectors to land on a unit circle, which means the vectors are normalized. In the latter case, you are dividing out the magnitude, rotating the vector, making sure it stays a unit, and then multiplying the magnitude back in. Normalization just goes with the territory.

  • If someone tells you that coordinate system are defined by n unit vectors, know that i-hat, j-hat, k-hat, and so on can be any arbitrary vector(s) of any length and direction, so long as none of them are parallel. This is the heart of affine transformations.

  • If someone tries to tell you that the dot product requires normalized vectors, shake your head and smile. The dot product only needs normalized vectors when you are using it to get the angle between two vectors.

But doesn't normalization make the math "simpler"?

Not really -- It adds a magnitude computation and a division. Numbers between 0..1 are no different than numbers between 0..x.

Having said that, you sometimes normalize in order to play well with others. But if you find yourself normalizing vectors as a matter of principle before calling methods, consider using a flag attached to the vector to save yourself a step. Mathematically, it is unimportant, but practically, it can make a huge difference in performance.

So again... it's all about rotating a vector or measuring its angle against another vector. If you aren't doing that, don't waste cycles.

霞映澄塘 2024-12-03 18:24:49

tl;dr:归一化向量简化了您的数学计算。它们还减少了图像中非常难以诊断的视觉伪影的数量。

归一化向量方向相同,单位长度仅为 1?所以
我不清楚什么时候有必要?

您几乎总是希望光线追踪器中的所有向量都被归一化。

最简单的例子是相交测试:弹跳光线击中另一个物体的位置。

考虑一条射线,其中:

p(t) = p_0 + v * t

在这种情况下,沿该射线 p(t) 的任意点被定义为距原始点 p_0 的偏移量以及沿特定方向的偏移量v。对于参数t的每一个增量,生成的p(t)将移动另一个长度增量,其长度等于向量v的长度。

请记住,您知道 p_0v。当您尝试找到该光线下一次撞击另一个物体的点时,您必须求解该t。在该表示中使用归一化向量 v 显然更方便(即使并非总是明显必要)。

然而,相同的向量v也用于照明计算。想象一下,我们有另一个指向光源的方向向量u。出于非常简单的着色模型的目的,我们可以将特定点处的光定义为这两个向量之间的点积:

L(p) = v * u

诚然,这是一个非常无趣的反射模型,但它抓住了讨论的要点。如果反射指向光,则表面上的点是明亮的,否则会变暗。

现在,请记住,编写此点积的另一种方式是向量的大小乘以它们之间角度的余弦的乘积:

L(p) = ||v|| ||u|| cos(theta)

如果 uv 具有单位长度(标准化),则方程将与两个向量之间的角度成正比。但是,如果 v 不是单位长度,比如说因为您在反射上面的光线模型中的矢量后没有进行标准化,那么现在您的照明模型就有问题了。表面上使用较大 v 的斑点将比不使用较大 v 的斑点亮得多。

tl;dr: Normalized vectors simplify your math. They also reduce the number of very hard to diagnose visual artifacts in your images.

Normalized vector is in the same direction with just unit length 1? So
I'm unclear when it is necessary?

You almost always want all vectors in a ray tracer to be normalized.

The simplest example is that of the intersection test: where does a bouncing ray hit another object.

Consider a ray where:

p(t) = p_0 + v * t

In this case, a point anywhere along that ray p(t) is defined as an offset from the original point p_0 and an offset along a particular direction v. For every increment of parameter t, the resulting p(t) will move another increment of length equal to the length of the vector v.

Remember, you know p_0 and v. When you are trying to find the point where this ray next hits another object, you have to solve for that t. It is obviously more convenient, if not always obviously necessary, to use normalized vector vs in that representation.

However, that same vector v is used in lighting calculations. Imagine that we have another direction vector u that points towards a lighting source. For the purpose of a very simple shading model, we can define the light at a particular point to be the dot product between those two vectors:

L(p) = v * u

Admittedly, this is a very uninteresting reflection model but it captures the high points of the discussion. A spot on a surface is bright if reflection points towards the light and dim if not.

Now, remember that another way of writing this dot product is the product of the magnitudes of the vectors times the cosine of the angle between them:

L(p) = ||v|| ||u|| cos(theta)

If u and v are of unit length (normalized), then the equation will evaluate to be proportional to the angle between the two vectors. However, if v is not of unit length, say because you didn't bother to normalize after reflecting the vector in the ray model above, now your lighting model has a problem. Spots on the surface using a larger v will be much brighter than spots that do not.

挽手叙旧 2024-12-03 18:24:49

每当您在某些受其长度影响的数学中使用方向向量时,就必须对其进行归一化。

最典型的例子是点积,它用于大多数照明方程。有时您还需要对照明计算中使用的矢量进行标准化,即使您相信它们是正常的。

例如,在三角形上使用插值法线时。常识告诉您,由于顶点的法线是法线,因此通过插值得到的向量也是。常识就这么多了……事实是它们会更短,除非它们顺便都指向同一个方向。这意味着您会将三角形遮蔽得太暗(更糟糕的是,光源距离表面越近,效果越明显,这是一个......非常有趣的结果)。

向量可能会或可能不会标准化的另一个例子是叉积,具体取决于您在做什么。例如,当使用两个叉积构建正交基时,您必须至少标准化一次(尽管如果您天真地这样做,您最终会更频繁地这样做)。
如果您只关心生成的“向上向量”的方向或符号,则不需要标准化。

It is necessary to normalize a direction vector whenever you use it in some math that is influenced by its length.

The prime example is the dot product, which is used in most lighting equations. You also sometimes need to normalize vectors that you use in lighting calculations, even if you believe that they are normal.

For example, when using an interpolated normal on a triangle. Common sense tells you that since the normals at the vertices are normal, the vectors you get by interpolating are too. So much for common sense... the truth is that they will be shorter unless they incidentially all point into the same direction. Which means that you will shade the triangle too dark (to make matters worse, the effect is more pronounced the closer the light source gets to the surface, which is a... very funny result).

Another example where a vector might or might not be normalized is the cross product, depending on what you are doing. For example, when using the two cross products to build an orthonormal base, then you must at least normalize once (though if you do it naively, you end up doing it more often).
If you only care about the direction of the resulting "up vector", or about the sign, you don't need to normalize.

↙厌世 2024-12-03 18:24:49

我会回答相反的问题。什么时候不需要正常化?几乎所有与照明相关的计算都需要单位向量 - 然后点积给出向量之间角度的余弦,这非常有用。有些方程仍然可以应付,但变得更加复杂(本质上是在方程中进行归一化),这主要留下了交叉测试。

如果有单位向量,则可以简化许多相交测试的方程。有些不需要它 - 例如,如果您有一个平面方程(具有单位法线),您可以找到光线与平面的交点,而无需标准化光线方向矢量。该距离将以射线方向矢量长度表示。如果您想要的只是与一堆这些平面相交(相对距离都是正确的),这可能没问题。但是,一旦您想要与使用归一化光线方向计算的不同距离进行比较,距离值将无法正确比较。

您可能会在完成一些不需要的工作后考虑对方向向量进行归一化 - 也许您有一个可以在没有归一化向量的情况下遍历的加速结构。但这也不相关,因为最终光线会击中某些物体,并且您将需要用它进行照明/着色计算。所以你不妨从一开始就对它们进行归一化……

换句话说,任何具体的计算可能不需要归一化的方向向量,但给定的方向向量几乎肯定需要在过程中的某个时刻进行归一化。

I'll answer the opposite question. When do you NOT need to normalize? Almost all calculations related to lighting require unit vectors - the dot product then gives you the cosine of the angle between vectors which is really useful. Some equations can still cope but become more complex (essentially doing the normalization in the equation) That leaves mostly intersection tests.

Equations for many intersection tests can be simplified if you have unit vectors. Some do not require it - for example if you have a plane equation (with a unit normal) you can find the ray-plane intersection without normalizing the ray direction vector. The distance will be in terms of the ray direction vectors length. This might be OK if all you want is to intersect a bunch of those planes (the relative distances will all be correct). But as soon as you want to compare with a different distance - calculated using the normalized ray direction - the distance values will not compare properly.

You might think about normalizing a direction vector AFTER doing some work that does not require it - maybe you have an acceleration structure that can be traversed without a normalized vector. But that isn't relevant either because eventually the ray will hit something and you're going to want to do a lighting/shading calculation with it. So you may as well normalize them from the start...

In other words, any specific calculation may not require a normalized direction vector, but a given direction vector will almost certainly need to be normalized at some point in the process.

请恋爱 2024-12-03 18:24:49

向量用于存储两个概念上不同的元素:空间中的点和方向:

  • 如果您要存储空间中的点(例如相机的位置、光线的原点、三角形的顶点),您不希望标准化,因为您将修改向量的值,并丢失特定位置。
  • 如果您要存储一个方向(例如相机向上、光线方向、对象法线),那么您想要标准化,因为在这种情况下您感兴趣的不是点的具体值,而是它代表的方向,所以你不需要大小。归一化在这种情况下很有用,因为它简化了一些操作,例如计算两个向量的余弦,如果两者都归一化,则可以使用点积来完成此操作。

Vectors are used to store two conceptually different elements: points in space and directions:

  • If you are storing a point in space (for example the position of the camera, the origin of the ray, the vertices of triangles) you don't want to normalize, because you would be modifying the value of the vector, and losing the specific position.
  • If you are storing a direction (for example the camera up, the ray direction, the object normals) you want to normalize, because in this case you are interested not in the specific value of the point, but on the direction it represents, so you don't need the magnitude. Normalization is useful in this case because it simplifies some operations, such as calculating the cosine of two vectors, something that can be done with a dot product if both are normalized.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文