Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

Normals are moving when moving camera?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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?

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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
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
If you print the normals as colors, they should change as you move.
But lighting itself of course should not.


L. Spiro

Share this post


Link to post
Share on other sites
Thanks for the help! That is exactly what I am seeing, no lights moving, but the normals colors are changing.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!