Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
904 views
in Technique[技术] by (71.8m points)

opengl es 2.0 - normal matrix for non uniform scaling

Im trying to calculate the normal matrix for my GLSL shaders on OpenGL 2.0.

The theory is : a normal matrix is the top left 3x3 matrix of the ModelView, transposed and inverted.

It seems to be correct as I have been rendering my scenes correctly, until I imported a model from maya and found non-uniform scales. Loaded models have a weird lighting, while my procedural ones are correct, so I put my money on the normal matrix calculation.

How is it computed with non uniform scale?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You already figured out that you need the transposed inverted matrix for transforming the normals. For a scaling matrix, that's easy to calculate.

A non-uniform 3x3 scaling matrix looks like this:

[ sx  0   0  ]
[ 0   sy  0  ]
[ 0   0   sz ]

with sx, sy and sz being the scaling factors for the 3 coordinate directions.

The inverse of this is:

[ 1 / sx  0       0      ]
[ 0       1 / sy  0      ]
[ 0       0       1 / sz ]

Transposing it changes nothing, so this is already your normal transformation matrix.

Note that, unlike for example a rotation, this transformation matrix will not keep vectors normalized when it is applied to a normalized vector. So after applying this matrix in your shader, you will have to re-normalize the result before using it for lighting calculations.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...