码迷,mamicode.com
首页 > 其他好文 > 详细

opengl 法向量变换矩阵推导

时间:2016-05-13 00:51:07      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:

When lighting is enabled in OpenGL, the normal vectors are used to determine how much light is received at the specified vertex or surface. This lighting processing is performed at eye coordinate space, therefore, normal vectors in object coordinates are also transformed to eye coordinates with GL_MODELVIEW matrix.

当opengl中启动了光照功能后,法向量决定了顶点或者表面接收到了多少光。光照处理过程是在eye coordinate space眼睛坐标空间,因此,法向量也需要使用GL_MODELVIEW 转换到眼睛坐标空间。


However, normals vectors are transformed in different way as vertices do. We cannot simply multiply GL_MODELVIEW matrix and normal. Consider a normal vector (1,0,0) at vertex (0,0,0). If GL_MODELVIEW matrix is simply translating 2 unit up along Y-axis, then the vertex coordinates will be (0,2,0). But, the normal should be remain same as (1,0,0), not (1,2,0).

然而,法向量和顶点一样,每个变换矩阵不完全一样,不能简单地乘以GL_MODELVIEW 就可以了。例如顶点vertex(0,0,0)的法向量vector(1,0,0),如果GL_MODELVIEW 矩阵仅仅沿着y轴平移两个单元,那么顶点坐标变成了(0,2,0),但是法向量依然是(1,0,0)而不是(1,2,0)。

In order to understand how normal vectors are transformed to eye space, think the normals as coefficients of plane equations, which are perpendicular to the planes.

技术分享 
OpenGL Normal Vector

Imagine a triangle polygon with 3 vertices; v1-v2-v3, and the normal of this surface is 技术分享 as a homogeneous plane. (For Euclidean space, the normal is 技术分享.) If we think of the triangle as a homogeneous plane, then the plane equation becomes; (by replacing x, y, z to x/w, y/w, z/w, then multiplying w on both sides) 
技术分享

Since 3 vertices lie on this plane, the plane equation is also true when we substitute these vertices to the equation. For example, for 技术分享, it satisfies; 
技术分享

The equivalent matrix form of the plane equation is; 
技术分享

The plane equation is multiplying the transposed normal (nT) and the vertex together. 
Now, we modify the above equation to acquire normal vector transformation formula by inserting GL_MODELVIEW matrix M-1M in between; 
(the following equation is still equivalent to the above because M-1M is identity matrix.) 
技术分享

As you can see, the right part of the above equation 技术分享 is transforming vertex to eye space, and the left part 技术分享is going to be the normal vector in eye space because the plane equation is also transformed. It reads as "The transformed vertex 技术分享 lies on the transformed plane 技术分享 in eye coordinates space".

Therefore, transforming normal from object space to eye space with GL_MODELVIEW matrix M is; 
技术分享

Or, by converting pre-multiplication to post-multiplication form, we get; 
技术分享

opengl 法向量变换矩阵推导

标签:

原文地址:http://blog.csdn.net/chenhittler/article/details/51348209

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!