Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What extractly mat3(a mat4 matrix) statement in glsl do?

Tags:

opengl

glsl

I'm doing a per fragment lighting and when correcting normal vecter, i got this code: vec3 f_normal = mat3(MVI) * normal; Where MVI is: mat4 MVI = transpose(inverse(ModelViewMatrix));. So what is return after mat3(MVI) statement?

like image 877
Bình Nguyên Avatar asked Jun 04 '12 10:06

Bình Nguyên


2 Answers

mat3(MVI) * normal

Returns the upper 3x3 matrix from the 4x4 matrix and multiplies the normal by that. This matrix is called the 'normal matrix'. You use this to bring your normals from world space to eye space. The upper 3x3 portion of the matrix is important for scale and rotation, while the rest is only for translation (and normals are never translated)

To take a normal from world space to eye space, you just need the 3x3 inverse transpose of the model view matrix. Unless your matrix in othro normal (no non-uniform scale) in that case the original matrix is the same as its inverse transpose.

like image 119
Stephan van den Heuvel Avatar answered Oct 19 '22 23:10

Stephan van den Heuvel


From GLSL types: "There are no restrictions on size when doing matrix construction from another matrix. So you can construct a 4x2 matrix from a 2x4 matrix; only the corresponding elements are copied.". So, you get the MVI's top-left 3x3 submatrix as a result.

like image 21
Alexander Pavlov Avatar answered Oct 19 '22 22:10

Alexander Pavlov