# strugling with lighting in GLSL

This topic is 3495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey. im trying to create a diffuse light using GLSL. Theres alot of tutorials out there on this subject but all of them (or at least the ones i found) accessed the built in matrix gl_NormalMatrix which as far as i understand is automaticaly calculated by the driver based on the other build in matrixes. for example heres one line of code i found in a tutorial: normal = normalize(gl_NormalMatrix * gl_Normal); My problem is that i do all Matrix calculations myself so the other build in matrixes never gets any value and im not quite sure how to calculate this gl_NormalMatrix myself. what i currently calculate in my engine is: the projection matrix, the view matrix, the "node" matrix (one for each node in my scene graph) using these 3 matrixes that i allready have. how can i calculate this gl_NormalMatrix myself? Thnx in Advance!

##### Share on other sites
gl_NormalMatrix is just the inverse of the transpose of mat3(gl_ModelViewMatrix).

##### Share on other sites
is there a quick way to calculate that in GLSL?
or should i calculate it beforehand?
im not quite sure how to inverse a 3x3 matrix

##### Share on other sites
Are you doing any scaling in your matrix? no-> then glNormalMatrix = glModelViewMatrix

if yes,

as long as your scales are uniform (the same on all axis) then the above also holds true.

##### Share on other sites
Quote:
 Original post by dpadam450Are you doing any scaling in your matrix? no-> then glNormalMatrix = glModelViewMatrixif yes,as long as your scales are uniform (the same on all axis) then the above also holds true.

Yes, but then you have to renormalize the normals. That's eazy if you know the scale factor ;)

Anyway, you should always have at hand, a function that invert matrices. see for example: this one .

##### Share on other sites
thnx for that function but i need to invert a 3x3 matrix not a 4x4 one? unles im misunderstanding.

thnx!

##### Share on other sites
If your matrix is affine (its last row is (0,0,0,1)) then you can invert it then take the top-left 3x3 submatrix. You can also adapt the function of steven katic to 3x3 matrices: just replace 4 by 3 and 16 by 9. There are plenty of examples in the net and here at GameDev so you can search for one that corresponds to your needs.
If you want to do it yourself see The Matrix and Quaternions FAQ.

##### Share on other sites
k thnx that helps :P

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633284
• Total Posts
3011212
• ### Who's Online (See full list)

There are no registered users currently online

×