原文地址:http://blog.csdn.net/ichild1964/article/details/9728357

参考:http://www.gamedev.net/topic/598985-gl-normalmatrix-in-glsl-how-to-calculate/

当用glsl传normal到fragment的时候总要用gl_Normal* gl_NormalMatrix,是为什么呢?做bumpmap的时候,binormal和tangent需不需要也乘以gl_NormalMatrix呢,如果需要把TBN转到世界空间,而不是把灯光转到TBN的话需不需要乘以gl_NormalMatrix或者是需要乘以其它矩阵?有没有想过perspective投影之后模型成什么样子呢?在Direct3D你不需要关心这些,因为都处理好了,所以一路稀里糊涂下来效果也基本上做的出来,在OpenGL就不行了。下面解释了什么是 gl_NormalMatrix,简而言之gl_NormalMatrix是为了令法线在perspective projection之后和投影后的模型平面依然垂直,但是它并不是投影矩阵哦,而是投影矩阵的逆矩阵的翻转矩阵 。呵呵,顺便想象下投影后的物体是什么样子吧,这样就会幡然醒悟为什么需要重新找垂直于平面的发现了。

Normal Matrix

Normals are funny.  They're vec3's, since you don't want perspective on normals.   And they don't actually scale quite right--a 45 degree surface with a 45 degree normal, scaled by glScalef(1,0.1,1), drops the surface down to near 0 degrees, but actually tilts the normal *up*, in the opposite direction from the surface, to near 90 degrees.

Mathematically,
if between two points a and b on the surface, dot(n,b-a)==0, then after
applying a matrix M to the points, you want the normal to still be
perpendicular.  The question is, what matrix N do you have to apply to
the normal to make this happen?  In other words, find N such that
    dot( N * n , M * a - M * b) == 0

We
can solve this by noting that dot product can be expresed as matrix
multiplication--dot(x,y) = transpose(x) * y, where we treat an ordinary
column-vector as a little matrix, and flip it horizontally.  So
   transpose(N * n) * (M*a - M*b) == 0         (as above, but write using transpose and matrix multiplication)
   transpose(N * n) * M * (a-b) == 0              (collect both copies of M)
   transpose(n) * transpose(N) * M * (a-b) == 0    (transpose-of-product is product-of-transposes in opposite order)

OK. 
This is really similar to our assumption that the original normal was
perpendicular to the surface--that dot(n,b-a) == transpose(n) * (a-b) ==
0.  In fact, the only difference is the new matrices wedged in the
middle.  If we pick N to make the term in the middle the identity, then
our new normal will be perpendicular to the surface too:
    transpose(N) * M == I   (the identity matrix)
This is the definition for matrix inverses, so the "normal matrix" N = transpose(inverse(M)).

If
you look up the GLSL definition for "gl_NormalMatrix", it's defined as
"the transpose of the inverse of the gl_ModelViewMatrix".  Now you know
why!

Opengl的gl_NormalMatrix【转】的更多相关文章

  1. Opengl的gl_NormalMatrix

    原文地址:http://blog.csdn.net/ichild1964/article/details/9728357 参考:http://www.gamedev.net/topic/598985- ...

  2. OpenGL Shader in OpenCASCADE

    OpenGL Shader in OpenCASCADE eryar@163.com Abstract. As implementation of one of the strategic steps ...

  3. RenderMonkey 练习 第四天 【OpenGL Texture Bump】

    BumpTexture 1. 新建一个OpenGL 空effect; 2. 添加相关变量 右击Effect节点选择Add Variable->float->float / float3 添 ...

  4. RenderMonkey 练习 第三天 【OpenGL renderToTexture】

    渲染到纹理: 1. 新建一个OpenGL 空effect; 2. 添加渲染目标纹理, Add Texture-> Add Render Texture 3. 添加一个渲染pass 4. 将pas ...

  5. OpenGL中GLSL渲染茶壶光照完整程序

    顶点着色器VertexShader.txt: uniform vec3 lightposition;//光源位置 uniform vec3 eyeposition;//相机位置 uniform vec ...

  6. 【AR实验室】OpenGL ES绘制相机(OpenGL ES 1.0版本)

    0x00 - 前言 之前做一些移动端的AR应用以及目前看到的一些AR应用,基本上都是这样一个套路:手机背景显示现实场景,然后在该背景上进行图形学绘制.至于图形学绘制时,相机外参的解算使用的是V-SLA ...

  7. [OpenGL超级宝典]专栏前言

    我小时候的梦想呢,是做宇航员或者科学家或者是做一款属于自己的游戏,后来前面两个梦想都没有实现,于是我就来实现我的第三个梦想了,,,我呢,也算是零基础,因为我的专业是物联网工程,这个专业覆盖面之广,简直 ...

  8. OpenGL超级宝典笔记----渲染管线

    在OpenGL中任何事物都在3D空间中,但是屏幕和窗口是一个2D像素阵列,所以OpenGL的大部分工作都是关于如何把3D坐标转变为适应你屏幕的2D像素.3D坐标转为2D坐标的处理过程是由OpenGL的 ...

  9. OpenGL超级宝典笔记----框架搭建

    自从工作后,总是或多或少的会接触到客户端3d图形渲染,正好自己对于3d图形的渲染也很感兴趣,所以最近打算从学习OpenGL的图形API出发,进而了解3d图形的渲染技术.到网上查了一些资料,OpenGL ...

随机推荐

  1. 图层的transform属性

    Main.storyboard // //  ViewController.m //  7A11.图层的transform属性 // //  Created by huan on 16/2/4. // ...

  2. [SmartFoxServer概述]Zones和Rooms结构

    Zones和Rooms结构: 相对于SFS 1.X而言,在Zones和Rooms的配置上,SFS2X有了显著的改善.尤其是我们建立了房组这样一个简单的概念,它允许在一个逻辑组中管理Rooms,从而独立 ...

  3. 黑客们的故事(连载六):IT世界里的理想主义者

    一头疏于梳理的长发和一把肆意生长的大胡子,往往可能是人们对于理查德马修斯托曼的第一印象.这位马上就要过60岁生日的自由软件基金会主席不用手机.不买房.没有汽车,看起来像是个流浪汉,但是却是美国工程院院 ...

  4. 4.27-4.30webstorm

    本周学习了html的基础课程,运用的软件是webstorm,网页的结构大体为: <html><head> My First Heading </head> < ...

  5. (原创)即使最可怕的自然力量,也不失美丽——火山喷发(摄影,欣赏)

    文中图片摘自腾讯文化:www.cal.qq.com 1.Abstract     最可怕的力量也潜含着最美丽的风景奇观,虽然不能亲眼目睹,但透过大师的视角,一样也能体会到自然力量撼动的美丽. 2.Co ...

  6. C#函数式编程之惰性求值

    惰性求值 在开始介绍今天要讲的知识之前,我们想要理解严格求值策略和非严格求值策略之间的区别,这样我们才能够深有体会的明白为什么需要利用这个技术.首先需要说明的是C#语言小部分采用了非严格求值策略,大部 ...

  7. 无环的visitor模式

    无环的访问者模式,是来改进原有访问者模式的不足之处的,是Robert C. Martin首次提出的.我们知道访问者模式的优点是为被访问继承体系动态添加行为,而无须改变继承体系.但是GOF访问者模式的缺 ...

  8. [计算机图形学] 基于C#窗口的Bresenham直线扫描算法、种子填充法、扫描线填充法模拟软件设计(二)

    上一节链接:http://www.cnblogs.com/zjutlitao/p/4116783.html 前言: 在上一节中我们已经大致介绍了该软件的是什么.可以干什么以及界面的大致样子.此外还详细 ...

  9. [BTS] The adapter "SQL" raised an error message. Details "The Messaging Engine is shutting down. ".

    Get a warning in event log. Log Name:      ApplicationSource:        BizTalk ServerDate:          3/ ...

  10. Jquery对文本框的值、字符串的验证;正则表达式字符串的验证

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...