Shaders inNormal and spaces question

Hello, I’d like to know what the vector inNormal stands for and on what space it is??

can anyone tell me if what i am stating in comments is right??
vec4 position = vec4(inPosition, 1.0); // position of vertex in model space
vec4 vertexViewSpace = gl_WorldViewMatrix * position; // pposition of the vertex in view Space
vec4 vertexCameraSpace = vertexViewSpace - vec4(g_CameraPosition,0.0); // negative vector between vertex and camera

vec4 normal = gl_WorldViewMatrix * vec4(inNormal,1.0); // normal vector of the vertex in the view sapce (aka camera space??) [/java]

inNormal is the normal vector of the vertex in model space.
But normals are bit particular. Usualy you need them in view space (most of the time for lighting) and for this you need to multiply them by the inverse transpose worldview matrix.
We have a convenient global uniform called g_NormalMatrix for this.

1 Like

thx nehon,
I’m doing this :
teh shader is behaving a weird way, its as if the whole model gets the frag color all the time… not the fragments
and My model is always Black I don’t understand why the dot product of the two normalized vectors is always inf to 0.5 ;/
vertex shader : [java]

position = g_WorldMatrix * vec4(inPosition, 1.0); // position of the vertex
varyingNormalDirection = normalize(vec3(vec4(inNormal, 0.0) * g_NormalMatrix)); // normal in world space,[/java]

then In my Frag shader I dot this :


vec3 normalDirection = normalize(varyingNormalDirection);
vec3 viewDirection = normalize(g_CameraPosition - vec3(position));

float fragmentColor = 0;
if (dot(viewDirection, normalDirection) < 0.5){
           fragmentColor = 1.0; 
gl_FragColor = vec4 (fragmentColor, fragmentColor, fragmentColor, 1.0);[/java]

I don’t have time to explain too much atm and didn’t look at the logic completely. But In your vert shader you are converting it to eye space (not world space like your comment describes), you are also multiplying them the wrong way round.

this si world space :
position = g_WorldMatrix * vec4(inPosition, 1.0);

and thats WORLD or VIEW Space, i don’t understand EYE space?
varyingNormalDirection = normalize(vec3(g_NormalMatrix * vec4(inNormal, 0.0)));

if by EYE space you mean VIEW space, then how to get the World space Normal??

its weird but this has worked for me :

varyingNormalDirection = normalize(vec3(g_WorldMatrix*vec4(inNormal, 0.0)));

maybe its because or the 0 to remove translations :confused:

yes they are synonyms Eye/View/Camera space.

g_NormalMatrix converts normals to eye/view space, not world space! (as you have found out)

What you have will work if you have uniform scale (the scale for x, y, and z are the same). If you don’t then you will get inconsistent results. You will want to use g_WorldMatrixInverseTranspose to be sure it will work. Its also a 3x3 matrix, so you can multiply it with the inNormal directly.

Edit: Heres a link which explains it in more detail (with pictures):

1 Like

Thx!! :slight_smile:

Nice now I managed to make a cool outline shader :=)

I first determine the vertexes to move, by calculating the dot product between the
Camera -> vertex and vertex normal position.

then I move those vertexes by a certain amount along their normal.

and colorize them in the frag shader in black.

the result looks awsome! :smiley:

I guess i’d have a better result with a higher res mesh!

no need for post processing :slight_smile:


1 Like

This is an old post for sure, but @navycougar can I see that shader you made? I’m trying to do something similar. Thanks!