Normals are moving when moving camera?

Started by
6 comments, last by MARS_999 12 years, 8 months ago
I am use VTF to make a terrain patch, and calculating the normals in the VS. When I rotate the camera I can see the normals change colors.... I mapped in the FS XYZ to RGB and watched the output and the colors change....

when I use this

normal = normalize(gl_NormalMatrix * normal);

and if I take that out

they stay mapped rgb = xyz

Any ideas?

Thanks!
Advertisement
...and calculating the normals in the VS. When I rotate the camera I can see the normals change...
That sounds correct -- gl_NormalMatrix is transforming your vertices from world space into view/eye space, and then when you move your camera, you're changing the basis of your view space.

Is your lighting done in view space or world space?
I think it's world space... But not 100% sure, how I can tell?
Thanks!

This is for a directional light BTW....

VS

vec4 newVertex = gl_Vertex;
newVertex.y = texture2DLod(heightTexture, TexCoord.st, 0.0).r;
normal = ComputeNormalSobelFilter(TexCoord.st, heightTexture);
normal = normalize(gl_NormalMatrix * normal);
lightDir = normalize(gl_LightSource[1].position.xyz);
gl_Position = gl_ModelViewProjectionMatrix * newVertex;



FS

vec3 n = normalize(normal);
float NdotL = max(dot(lightDir, n), 0.0);
if(NdotL > 0.0)
color +=(gl_LightSource[1].diffuse * NdotL);

I think it's world space... But not 100% sure, how I can tell?
Thanks!

Use it to colour the surface. If its viewspace, then normals pointing to the right of the screen will be red.
I haven't used openGL for a fair while, but i beleive that gl_NormalMatrix is actually derived from the ViewProjection matrix as traditionally the fixed function pipeline for GL did lighting in clipspace.

If you want your normals in viewspace, multiply float4(normal, 0) by the gl_ModelMatrix instead.
gl_NormalMatrix is the inverse transpose of gl_ModelViewMatrix (in 3×3 format), so the normals will be in view space.
Remember that OpenGL transforms the directions/positions/etc. of your lights as you set them, but only as you set them.
Each time your camera moves you need to reset the lights.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

YogurtEmperor

I call this each frame....

glLoadIdentity();
// Set the camera
gluLookAt(x, 1.0f, z,
x+lx, 1.0f, z+lz,
0.0f, 1.0f, 0.0f);

GLfloat lightPos[] = {0.0, 1.0, -1.0, 0.0};
glLightfv(GL_LIGHT1, GL_POSITION, lightPos);


then render the terrain....

So what I am hearing is expect the rgb colors of the normal.xyz mapping to change colors when I rotate the camera then?
If you print the normals as colors, they should change as you move.
But lighting itself of course should not.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Thanks for the help! That is exactly what I am seeing, no lights moving, but the normals colors are changing.

This topic is closed to new replies.

Advertisement