如何计算正切和副法线?
谈论凹凸贴图、镜面高光以及 OpenGL 着色语言 (GLSL) 中的此类内容,
我有:
- 顶点数组(例如 {0.2,0.5,0.1, 0.2,0.4,0.5, ...})
- 法线数组(例如 {0.0,0.0,1.0, 0.0,1.0,0.0, ...})
- 点光源在世界空间中的位置(例如 {0.0,1.0,-5.0})
- 观察者在世界空间中的位置(例如{0.0,0.0,0.0})(假设观看者位于世界中心)
现在,如何计算每个顶点的副法线和切线?我的意思是,计算双法线的公式是什么,我必须根据这些信息使用什么?关于切线?
无论如何,我都会构建 TBN 矩阵,因此,如果您知道直接根据这些信息构建矩阵的公式,那就太好了!
哦,是的,如果需要的话,我也有纹理坐标。 当我谈论 GLSL 时,如果有一种逐顶点解决方案会很好,我的意思是,这种解决方案不需要一次访问多个顶点信息。
---- 更新-----
我找到了这个解决方案:
vec3 tangent; vec3 binormal; vec3 c1 = cross(a_normal, vec3(0.0, 0.0, 1.0)); vec3 c2 = cross(a_normal, vec3(0.0, 1.0, 0.0)); if (length(c1)>length(c2)) { tangent = c1; } else { tangent = c2; } tangent = normalize(tangent); binormal = cross(v_nglNormal, tangent); binormal = normalize(binormal);
但我不知道它是否100%正确。
Talking about bump mapping, specular highlight and these kind of things in OpenGL Shading Language (GLSL)
I have:
- An array of vertices (e.g. {0.2,0.5,0.1, 0.2,0.4,0.5, ...})
- An array of normals (e.g. {0.0,0.0,1.0, 0.0,1.0,0.0, ...})
- The position of a point light in world space (e.g. {0.0,1.0,-5.0})
- The position of the viewer in world space (e.g. {0.0,0.0,0.0}) (assume the viewer is in the center of the world)
Now, how can I calculate the Binormal and Tangent for each vertex? I mean, what is the formula to calculate the Binormals, what I have to use based on those informations? And about the tangent?
I'll construct the TBN Matrix anyway, so if you know a formula to construct the matrix directly based on those informations will be nice!
Oh, yeh, I have the texture coordinates too, if needed.
And as I'm talking about GLSL, would be nice a per-vertex solution, I mean, one which doesn't need to access more than one vertex information at a time.
---- Update -----
I found this solution:
vec3 tangent; vec3 binormal; vec3 c1 = cross(a_normal, vec3(0.0, 0.0, 1.0)); vec3 c2 = cross(a_normal, vec3(0.0, 1.0, 0.0)); if (length(c1)>length(c2)) { tangent = c1; } else { tangent = c2; } tangent = normalize(tangent); binormal = cross(v_nglNormal, tangent); binormal = normalize(binormal);
But I don't know if it is 100% correct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与您的问题相关的输入数据是纹理坐标。切线和副法线是局部平行于对象表面的向量。在法线贴图的情况下,它们描述的是法线纹理的局部方向。
因此,您必须计算纹理向量指向的方向(在模型空间中)。假设您有一个三角形 ABC,其纹理坐标为 HKL。这给了我们向量:
现在我们想用切线空间 T, U 来表达 D 和 E,即
这是一个有 6 个未知数和 6 个方程的线性方程组,它可以写为
求逆 FG 矩阵得到
与顶点法线 T 和 U 形成局部空间基,称为切线空间,由矩阵描述
从切线空间变换到对象空间。为了进行照明计算,需要与此相反。经过一点练习,我们会发现:
对向量 T' 和 U' 进行归一化,将它们称为切线和副法线,我们获得从对象转换到切线空间的矩阵,我们在切线空间中进行照明:
我们将 T' 和 U' 存储在一起顶点法线作为模型几何体的一部分(作为顶点属性),以便我们可以在着色器中使用它们进行光照计算。 我再说一遍:您不需要在着色器中确定切线和副法线,您可以预先计算它们并将它们存储为模型几何体的一部分(就像法线一样)。
(上面垂直条之间的符号都是矩阵,绝不是行列式,通常在表示法中使用竖线而不是括号。)
The relevant input data to your problem are the texture coordinates. Tangent and Binormal are vectors locally parallel to the object's surface. And in the case of normal mapping they're describing the local orientation of the normal texture.
So you have to calculate the direction (in the model's space) in which the texturing vectors point. Say you have a triangle ABC, with texture coordinates HKL. This gives us vectors:
Now we want to express D and E in terms of tangent space T, U, i.e.
This is a system of linear equations with 6 unknowns and 6 equations, it can be written as
Inverting the FG matrix yields
Together with the vertex normal T and U form a local space basis, called the tangent space, described by the matrix
Transforming from tangent space into object space. To do lighting calculations one needs the inverse of this. With a little bit of exercise one finds:
Normalizing the vectors T' and U', calling them tangent and binormal we obtain the matrix transforming from object into tangent space, where we do the lighting:
We store T' and U' them together with the vertex normal as a part of the model's geometry (as vertex attributes), so that we can use them in the shader for lighting calculations. I repeat: You don't determine tangent and binormal in the shader, you precompute them and store them as part of the model's geometry (just like normals).
(The notation between the vertical bars above are all matrices, never determinants, which normally use vertical bars instead of brackets in their notation.)
通常,有 2 种生成 TBN 矩阵的方法:离线和在线。
在线 = 直接在片段着色器中使用派生指令。这些推导为您提供了多边形每个点的平坦 TBN 基础。为了获得平滑的效果,我们必须根据给定的(平滑)顶点法线重新正交化它。此过程对 GPU 的负担甚至比初始 TBN 提取还要重。
离线 = 准备切线作为顶点属性。这更难实现,因为它不仅会添加另一个顶点属性,而且还需要重新组合所有其他属性。此外,它不会 100% 为您提供更好的性能,因为您将获得存储/传递/动画(!)vector3 顶点属性的额外成本。
数学在很多地方都有描述(谷歌一下),包括@datenwolf 帖子。
这里的问题是两个顶点可能具有相同的法线和纹理坐标,但切线不同。这意味着您不能只向顶点添加顶点属性,您需要将顶点拆分为 2 并为克隆指定不同的切线。
获得每个顶点唯一的切线(和其他属性)的最佳方法是尽早在导出器中执行此操作。在按属性对纯顶点进行排序的阶段,您只需将切向量添加到排序键即可。
作为该问题的根本解决方案,请考虑使用四元数。单个四元数 (vec4) 可以成功地表示预定义灵活性的切向空间。保持正交(包括传递到片段着色器)、存储和提取法线(如果需要)很容易。如需了解更多信息,请访问 KRI 维基。
Generally, you have 2 ways of generating the TBN matrix: off-line and on-line.
On-line = right in the fragment shader using derivative instructions. Those derivations give you a flat TBN basis for each point of a polygon. In order to get a smooth one we have to re-orthogonalize it based on a given (smooth) vertex normal. This procedure is even more heavy on GPU than initial TBN extraction.
Off-line = prepare tangent as a vertex attribute. This is more difficult to get because it will not just add another vertex attrib but also will require to re-compose all other attributes. Moreover, it will not 100% give you a better performance as you'll get an additional cost of storing/passing/animating(!) vector3 vertex attribute.
The math is described in many places (google it), including the @datenwolf post.
The problem here is that 2 vertices may have the same normal and texture coordinate but different tangents. That means you can not just add a vertex attribute to a vertex, you'll need to split the vertex into 2 and specify different tangents for the clones.
The best way to get unique tangent (and other attribs) per vertex is to do it as early as possible = in the exporter. There on the stage of sorting pure vertices by attributes you'll just need to add the tangent vector to the sorting key.
As a radical solution to the problem consider using quaternions. A single quaternion (vec4) can successfully represent tangential space of a pre-defined handiness. It's easy to keep orthonormal (including passing to the fragment shader), store and extract normal if needed. More info on the KRI wiki.
根据kvark的回答,我想补充更多的想法。
如果您需要正交化切线空间矩阵,您必须以任何方式做一些工作。
即使您添加切线和副法线属性,它们也会在着色器阶段进行插值
最终它们既没有被标准化,也没有彼此正常化。
假设我们有一个归一化法线向量
n
,并且我们有切线t
和副法线b
或者我们可以通过如下推导来计算它们:当然可以使用叉积来计算正交化切空间矩阵,
但这仅适用于右手系统。如果矩阵被镜像(左手系统),它将变成右手系统:
在上面的代码片段中,如果切线空间是左手系统,则副法线向量会反转。
为了避免这种情况,必须采取困难的方法:
正交化任何矩阵的常见方法是 Gram-Schmidt 过程:
另一种可能性是使用 2*2 矩阵的行列式,该矩阵由纹理坐标
texC_dx
、texC_dy
的推导得出,考虑副法向量的方向。这个想法是正交矩阵的行列式是1,正交镜像矩阵的确定行列式是-1。行列式可以通过 GLSL 函数
行列式( mat2( texC_dx, texC_dy )
计算或者可以通过公式
texC_dx.x * texC_dy.y - texC_dy.x * texC_dx.y
计算。对于正交归一化切空间矩阵的计算,不再需要副法向量和单位向量的计算
可以回避副法向量的归一化(
normalize
)。Based on the answer from kvark, I would like to add more thoughts.
If you are in need of an orthonormalized tangent space matrix you have to do some work any way.
Even if you add tangent and binormal attributes, they will be interpolated during the shader stages
and at the end they are neither normalized nor they are normal to each another.
Let's assume that we have a normalized normalvector
n
, and we have the tangentt
and the binormalb
or we can calculate them from the derivations as follows:Of course an orthonormalized tangent space matrix can be calcualted by using the cross product,
but this would only work for right-hand systems. If a matrix was mirrored (left-hand system) it will turn to a right hand system:
In the code snippet above the binormal vector is reversed if the tangent space is a left-handed system.
To avoid this, the hard way must be gone:
A common way to orthogonalize any matrix is the Gram–Schmidt process:
Another possibility is to use the determinant of the 2*2 matrix, which results from the derivations of the texture coordinates
texC_dx
,texC_dy
, to take the direction of the binormal vector into account. The idea is that the determinant of a orthogonal matrix is 1 and the determined one of a orthogonal mirror matrix -1.The determinant can eihter be calcualted by the GLSL function
determinant( mat2( texC_dx, texC_dy )
or it can be calcualated by it formula
texC_dx.x * texC_dy.y - texC_dy.x * texC_dx.y
.For the calculation of the orthonormalized tangent space matrix, the binormal vector is no longer required and the calculation of the unit vector
(
normalize
) of the binormal vector can be evaded.计算切线的方法有很多种,如果法线贴图烘焙器不采用与渲染器相同的方法,您将得到微妙的伪像。许多面包师使用 MikkTSpace 算法,该算法与片段导数技巧不同。
,如果您有一个使用 MikkTSpace 的程序中的索引网格(并且没有具有相反方向的纹理坐标三角形共享索引),则该算法的困难部分大部分已为您完成,您可以像这样重建切线:
幸运的是 顶点着色器,它们被旋转到世界空间:
然后像这样计算副法线和世界空间法线(参见http://mikktspace. com/):(
副法线通常按像素计算,但有些面包师允许您选择按顶点计算并插值。此页面包含有关特定程序的信息。)
There is a variety of ways to calculate tangents, and if the normal map baker doesn't do it the same way as the renderer you'll get subtle artifacts. Many bakers use the MikkTSpace algorithm, which isn't the same as the fragment derivatives trick.
Fortunately, if you have an indexed mesh from a program that uses MikkTSpace (and no texture coordinate triangles with opposite orientations share an index) the hard part of the algorithm is mostly done for you, and you can reconstruct the tangents like this:
In the vertex shader, they are rotated into world space:
Then the binormal and world space normal are calculated like this (see http://mikktspace.com/):
(The binormal is usually calculated per pixel, but some bakers give you the option to calculate it per vertex and interpolate it. This page has information about specific programs.)