UNITY_MATRIX_IT_MV[Matrix] (转载)

转载 http://blog.csdn.net/cubesky/article/details/38682975

前面发了一篇关于unity Matrix的文章。

http://blog.csdn.net/cubesky/article/details/38664143

其中对于一般的Matrix可以说应该有一个清晰的了解了。但是对于UNITY_MATRIX_IT_MV这些matrix估计理解起来还是比较有问题。这里再重点描述一下UNITY_MATRIX_IT_MV。

首先,我们看一下unity中Doc中的描述:

UNITY_MATRIX_IT_MV float4x4 Inverse transpose of model * view matrix.

然后我们来看一下UNITY_MATRIX_IT_MV实际的变换意义

The transpose of World2Object is the transpose of the inverse of the Object2World matrix.

  • MV transforms points from object to eye space
  • IT_MV rotates normals from object to eye space

And similarly:

  • Object2World transforms points from object to world space
  • IT_Object2World (which, as you point out, is the transpose of World2Object) rotates normals from object to world space

If it is orthogonal, the upper-left 3x3 of Object2World will be equal to that of IT_Object2World, and so will also rotate normals from object to world space.

上面这里很好的描述了UNITY_MATRIX_IT_MV的使用场景,专门针对法线进行变换。但是为什么法线的变换和定点不一样呢?让我们来看一篇推导的文章。

The gl_NormalMatrix is present in many vertex shaders. In here some light is shed on what is this matrix and what is it for. This section was inspired by the excellent book by Eric Lengyel “Mathematics for 3D Game Programming and Computer Graphics”.

Many computations are done in eye space. This has to do with the fact that lighting is commonly performed in this space, otherwise eye position dependent effects, such as specular lights would be harder to implement.

Hence we need a way to transform the normal into eye space. To transform a vertex to eye space we can write:

	vertexEyeSpace = gl_ModelViewMatrix * gl_Vertex;

So why can’t we just do the same with a normal vector? A normal is a vector of 3 floats and the modelview matrix is 4×4. Secondly, since the normal is a vector, we only want to transform its orientation. The region of the modelview matrix that contains the orientation is the top left 3×3 submatrix. So why not multiply the normal by this submatrix?

This could be easily achieved with the following code:

	normalEyeSpace = vec3(gl_ModelViewMatrix * vec4(gl_Normal,0.0));

So, gl_NormalMatrix is just a shortcut to simplify code writing or to optimize it? No, not really. The above line of code will work in some circumstances but not all.

Lets have a look at a potential problem:

;

In the above figure we see a triangle, with a normal and a tangent vectors. The following figure shows what happens when the modelview matrix contains a non-uniform scale.

Note: if the scale was uniform, then the direction of the normal would have been preserved, The length would have been affected but this can be easily fixed with a normalization.

In the above figure the Modelview matrix was applied to all the vertices as well as to the normal and the result is clearly wrong: the transformed normal is no longer perpendicular to the surface.

We know that a vector can be expressed as the difference between two points. Considering the tangent vector, it can be computed as the difference between the two vertices of the triangle’s edge. If  and  are the vertices that define the edge we know that:

Considering that a vector can be written as a four component tuple with the last component set to zero, we can multiply both sides of the equality with the Modelview matrix

This results in

As  and  are the vertices of the transformed triangle,  remains tangent to the edge of the triangle. Hence, the Modelview preserves tangents, yet it does not preserve normals.

Considering the same approach used for vector T, we can find two points  and  such that

The main issue is that the a vector defined through the transformed points, , does not necessarily remain normal, as shown in the figures above. The normal vector is not defined as a difference between two points, as the tangent vector, it is defined as a vector which is perpendicular to a surface.

So now we know that we can’t apply the Modelview in all cases to transform the normal vector. The question is then, what matrix should we apply?

Consider a 3×3 matrix G, and lets see how this matrix could be computed to properly transform the normal vectors.

We know that, prior to the matrix transformation T.N = 0, since the vectors are by definition perpendicular. We also know that after the transformation N’.T’ must remain equal to zero, since they must remain perpendicular to each other. T can be multiplied safely by the upper left 3×3 submatrix of the modelview (T is a vector, hence the w component is zero), let’s call this submatrix M.

Let’s assume that the matrix G is the correct matrix to transform the normal vector. T. Hence the following equation:

The dot product can be transformed into a product of vectors, therefore:

Note that the transpose of the first vector must be considered since this is required to multiply the vectors. We also know that the transpose of a multiplication is the multiplication of the transposes, hence:

We started by stating that the dot product between N and T was zero, so if

then we have

Which is exactly what we want. So we can compute G based on M.

Therefore the correct matrix to transform the normal is the transpose of the inverse of the M matrix. OpenGL computes this for us in the gl_NormalMatrix.

In the beginning of this section it was stated that using the Modelview matrix would work in some cases. Whenever the 3×3 upper left submatrix of the Modelview is orthogonal we have:

This is because with an orthogonal matrix, the transpose is the same as the inverse. So what is an orthogonal matrix? An orthogonal matrix is a matrix where all columns/rows are unit length, and are mutually perpendicular. This implies that when two vectors are multiplied by such a matrix, the angle between them after transformation by an orthogonal matrix is the same as prior to that transformation. Simply put the transformation preserves the angle relation between vectors, hence transformed normals remain perpendicular to tangents! Furthermore it preserves the length of the vectors as well.

So when can we be sure that M is orthogonal? When we limit our geometric operations to rotations and translations, i.e. when in the OpenGL application we only use glRotate and glTranslate and not glScale. These operations guarantee that M is orthogonal. Note: gluLookAt also creates an orthogonal matrix!

注:之所以法线不能直接使用UNITY_MATRIX_MV进行变换,是因为法线是向量,具有方向,在进行空间变换的时候,如果发生非等比缩放,方向会发生偏移。为什么呢?拿上面的例子来说,我们可以简单的把法线和切线当成三角形的两条边,显然,三角形在空间变换的时候,不管是平移,还是旋转,或者是等比缩放,都不会变形,但是如果非等比缩放,就会发生拉伸。所以法线和切线的夹角也就会发生变化。(而切线在变换前后,方向总是正确的,所以法线方向就不正确了)。

参考:

http://www.lighthouse3d.com/tutorials/glsl-tutorial/the-normal-matrix/

http://forum.unity3d.com/threads/_object2world-or-unity_matrix_it_mv.112446/

http://www.cnblogs.com/kesalin/archive/2012/12/06/3D_math.html

时间: 2024-09-28 06:27:36

UNITY_MATRIX_IT_MV[Matrix] (转载)的相关文章

【转载】The Matrix Cookbook

The Matrix Cookbook Kaare Brandt Petersen, Michael Syskind Pedersen Abstract Matrix identities, relations and approximations. A desktop reference for quick overview of mathematics of matrices. Keywords Matrix identity, matrix relations, inverse, matr

<转载> OpenGL Projection Matrix

原文 OpenGL Projection Matrix Related Topics: OpenGL Transformation Overview Perspective Projection Orthographic Projection Updates: The MathML version is available here. Overview A computer monitor is a 2D surface. A 3D scene rendered by OpenGL must b

Android中Matrix的pre post set方法理解(转载来源:Linux社区 作者:zjmdp)

虽说以前学习过线性代数和图形学原理,但是在实际中碰到matrix还是疑惑了好一阵子,今天通过向同事请教终于找到一点门路,特总结如下: Matrix主要用于对平面进行缩放,平移,旋转以及倾斜操作,为简化矩阵变换,Android封装了一系列方法来进行矩阵变换,其中包括pre系列方法:preScale,preTranslate,preRotate,preSkew,set系列方法:setScale,setTranslate,setRotate,setSkew,post系列方法:postScale,pos

SIR,CQI,RSSI(转自搜狗百科)LTE上报的CQI、PMI、RI分别有什么用(转载自C114论坛)

信号干扰比 (Signal to Interference Ratio). 定义为(RSCP/Interference)×SF.这里针对的下行信号RSCP为DPCH或者PDSCH信道上接收信号码功率:Interference为在RSCP测量的时隙上不能被接收机消除的干扰:具体获取方法依赖于具体的设备.pecker取的是对应时隙的ISCP作为Interference.SF为使用的扩频因子转换为dB,计算公式为:SIR( dB ) = RSCP(dBm)- ISCP(dBm) + 10log(SF)

paper 131:【图像算法】图像特征:GLCM【转载】

转载地址:http://www.cnblogs.com/skyseraph/archive/2011/08/27/2155776.html 一 原理 1 概念:GLCM,即灰度共生矩阵,GLCM是一个L*L方阵,L为源图像的灰度级 2 含义:描述的是具有某种空间位置关系的两个像素的联合分布,可看成两个像素灰度对的联合直方图,是一种二阶统计 3 常用的空间位置关系:有四种,垂直.水平.正负45° 4 常用的GLCM特征特征: (1)能量:  是灰度共生矩阵元素值的平方和,所以也称能量,反映了图像灰

理解CSS3 transform中的Matrix(矩阵)

一.哥,我被你吓住了 打架的时候会被块头大的吓住,学习的时候会被奇怪名字吓住(如“拉普拉斯不等式”).这与情感化设计本质一致:界面设计好会让人觉得这个软件好用! 所以,当看到上面“Matrix(矩阵)”的时候,难免会心生畏惧(即使你已经学过),正常心理.实际上,这玩意确实有点复杂. 然而,这却是屌丝逆袭的一个好机会. CSS同行间:你是不是有这样的感觉:哎呀呀,每天就是对着设计图切页面,貌似技术没有得到实质性地提升啊,或者觉得日后高度有限! 我们应该都知道二八法则(巴莱多定律),即任何一组东西中

奇异值分解(转载)

转载自:http://blog.csdn.net/zhongkejingwang/article/details/43053513 在网上看到有很多文章介绍SVD的,讲的也都不错,但是感觉还是有需要补充的,特别是关于矩阵和映射之间的对应关系.前段时间看了国外的一篇文章,叫A Singularly Valuable Decomposition The SVD of a Matrix,觉得分析的特别好,把矩阵和空间关系对应了起来.本文就参考了该文并结合矩阵的相关知识把SVD原理梳理一下. SVD不仅

POJ 2155 Matrix (D区段树)

http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18143   Accepted: 6813 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. I

Android中图像变换Matrix的原理、代码验证和应用(二)

注:本篇文章为转载文章,因为原文格式排版较乱,但是内容非常棒,所以整理一下,方便以后查看. 查看原文请戳:http://blog.csdn.net/pathuang68/article/details/6991988 Matrix介绍文章请戳:http://blog.csdn.net/pathuang68/article/details/6991867 package com.pat.testtransformmatrix; import android.app.Activity; import