Sign in to follow this  
MARS_999

Normals are moving when moving camera?

Recommended Posts

MARS_999    1627
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!

Share this post


Link to post
Share on other sites
Hodgman    51328
[quote name='MARS_999' timestamp='1313624457' post='4850537']...and calculating the normals in the VS. When I rotate the camera I can see the normals change...[/quote]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?

Share this post


Link to post
Share on other sites
MARS_999    1627
I think it's world space... But not 100% sure, how I can tell?
Thanks!

This is for a directional light BTW....

VS
[code]
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;

[/code]

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

Share this post


Link to post
Share on other sites
Digitalfragment    1504
[quote name='MARS_999' timestamp='1313627709' post='4850551']
I think it's world space... But not 100% sure, how I can tell?
Thanks!
[/quote]
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.

Share this post


Link to post
Share on other sites
L. Spiro    25638
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

Share this post


Link to post
Share on other sites
MARS_999    1627
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this