# Normals, GLSL, FP/VP

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

## Recommended Posts

I have per pixel lighting setup and running in a FP/VP using a DOT3 Bumpmap. So now I am moving to GLSL and my lighting is all messed up... I am not using the DOT3 bumpmap, I am as of now unsure what is wrong. #1. With GLSL do I still need to process my normals as I did before on the CPU? I am using a VBO to store my whole terrain data in. e.g. vertex, normals, texcoords. Or can the normals be done in GLSL? Which way is better? I am doing this in my VS.
/* first transform the normal into eye space and normalize the result */
normal = normalize(gl_NormalMatrix * gl_Normal);


Any help would be greatly appreciated. Thanks

##### Share on other sites
you still need to supply your model normals, GLSL is just like the VP/FP in this regard.

and as long as your normals are unit length I'm 99% sure you shouldnt need to renormalise that equation either...

##### Share on other sites
Well isnt the normal matrix the upper left 3x3 matrix of the modelview inverse transpose? which SHOULD mean that its an orthonormal basis that contains only rotation.. right? So yes its magnitude should remain constant

hope that helps
-Dan

##### Share on other sites
Quote:
 Original post by _the_phantom_you still need to supply your model normals, GLSL is just like the VP/FP in this regard.and as long as your normals are unit length I'm 99% sure you shouldnt need to renormalise that equation either...

Ok, so leave my code alone to upload my normals to OpenGL then. So all I should have to do is pass my normal through the vertex shader to the fragment shader then? And use the normal in the fragment shader?

##### Share on other sites
Quote:
 Original post by Ademan555Well isnt the normal matrix the upper left 3x3 matrix of the modelview inverse transpose? which SHOULD mean that its an orthonormal basis that contains only rotation.. right? So yes its magnitude should remain constanthope that helps-Dan

?? Lost me on what you're getting at? :)

##### Share on other sites
Dont need to normalize, supporting what _the_phantom_ said

hope that helps
-Dan

##### Share on other sites
Quote:
 Original post by MARS_999Ok, so leave my code alone to upload my normals to OpenGL then. So all I should have to do is pass my normal through the vertex shader to the fragment shader then? And use the normal in the fragment shader?

Well, you'll want to transform it by the normal matrix, but then you can just pass it down as a varying and use it in the fragment shader, yes

##### Share on other sites
Ok, I think I am on to something working, I see my lighting changes when I move my camera around?? I didn't have this issue when I used a FP/VP??? Any ideas?

##### Share on other sites
Quote:
 Original post by MARS_999Ok, I think I am on to something working, I see my lighting changes when I move my camera around?? I didn't have this issue when I used a FP/VP??? Any ideas?

Hmmm, how can my lightposition be moving around when my camera is moving around shouldn't it stay still if I am not updating the glLightPosition???

##### Share on other sites
The light position is transformed as if it were a vertex. You would want to specify it after you have applied the camera transformations.

##### Share on other sites
Quote:
 Original post by James TrotterThe light position is transformed as if it were a vertex. You would want to specify it after you have applied the camera transformations.

Hmm I am sure I am doing it correctly, I had no issues with my FP/VP, but here is what I am doing, I call glLookAt() first and then these functions later on

glPushMatrix();	glLoadIdentity();    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);	glPopMatrix();

The lightPosition never changes for now...

##### Share on other sites
Can you please post the whole GLSL code? You've posted only a line, we don't know how you calculate the vertex positions or the light vectors.

However, the rule of thumb is that you update the light position every frame after the camera transformation.

Quote:
 The lightPosition never changes for now...

No it doesn't, but the vertex and normals do change due to camera changes, and so the lighting changes too. You have the light pos in world coordinates, and you have "normal = normalize(gl_NormalMatrix * gl_Normal);" which transforms the normal into eye coordinates.

##### Share on other sites
Here is the FS code
   varying vec4 diffuse, ambient;   varying vec3 normal, lightDir, halfVector;   varying vec2 blendmap_texCoord, texture_coord;        void main()   {      vec3 n,halfV,viewV,ldir;      float NdotL,NdotHV;               /* The ambient term will always be present */      vec4 color = ambient;            /* a fragment shader can't write a varying variable, hence we need      a new variable to store the normalized interpolated normal */      n = normalize(normal);            /* compute the dot product between normal and ldir */      NdotL = max(dot(n,lightDir),0.0);         if (NdotL > 0.0) {         color += diffuse * NdotL;         halfV = normalize(halfVector);         NdotHV = max(dot(n,halfV),0.0);         color += gl_FrontMaterial.specular *                gl_LightSource[0].specular *                pow(NdotHV, gl_FrontMaterial.shininess);      }      gl_FragColor = color;   }

VS code
   varying vec4 diffuse, ambient;   varying vec3 normal, lightDir, halfVector;   varying vec2 blendmap_texCoord, texture_coord;          void main()   {         //gl_TexCoord[0] = gl_MultiTexCoord0;      blendmap_texCoord = vec2(gl_MultiTexCoord0);      texture_coord = blendmap_texCoord * 20;                           /* first transform the normal into eye space and       normalize the result */      normal = normalize(gl_NormalMatrix * gl_Normal);            /* now normalize the light's direction. Note that       according to the OpenGL specification, the light       is stored in eye space. Also since we're talking about       a directional light, the position field is actually direction */      lightDir = normalize(vec3(gl_LightSource[0].position));         /* Normalize the halfVector to pass it to the fragment shader */      halfVector = normalize(gl_LightSource[0].halfVector.xyz);                     /* Compute the diffuse, ambient and globalAmbient terms */      diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;      ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;      ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;           gl_Position = ftransform();   }

HTH you help me!!! ;)

##### Share on other sites
Quote:
 /* now normalize the light's direction. Note that according to the OpenGL specification, the light is stored in eye space. Also since we're talking about a directional light, the position field is actually direction */ lightDir = normalize(vec3(gl_LightSource[0].position));

How do you expect it to be stored in eye space when you call glLightfv() with the identity as the modelview matrix? As others said, call Lightf() immediately after camera transformations.

##### Share on other sites
Quote:
Original post by mikeman
Quote:
 /* now normalize the light's direction. Note that according to the OpenGL specification, the light is stored in eye space. Also since we're talking about a directional light, the position field is actually direction */ lightDir = normalize(vec3(gl_LightSource[0].position));

How do you expect it to be stored in eye space when you call glLightfv() with the identity as the modelview matrix? As others said, call Lightf() immediately after camera transformations.

Not trying to be stupid or ungrateful, but I think I stated earlier that in my FP/VP everything worked fine... My FFP setup code for my light and camera haven't changed when I call them, so I can't see how I am calling them in the wrong place? So here is my rendering code which I dont' think is wrong but anyway...
	//reset viewport to our original viewport size	glViewport(0, 0, gWidth, gHeight);	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glLoadIdentity();		g_Camera.LookAt();	//reset light position or else the light position will jump all over	glPushMatrix();	glLoadIdentity();        glLightfv(GL_LIGHT0, GL_POSITION, terrain.lightPosition);	glPopMatrix();	glTranslatef(0.0f, -move_y, 0.0f);		//move height based on mouse wheel	glPushMatrix();	terrain.DrawTerrainVBO(timer.speedfactor, false); 	glPopMatrix();

##### Share on other sites
I honestly don't understand why we're arguing about this instead of just going and make that little change on your code and see for yourself that it works. I don't know why it worked on your previous settings. All I know is the error I see in your current settings, and I just pointed it out(along with others).

##### Share on other sites
Quote:
 Original post by mikemanI honestly don't understand why we're arguing about this instead of just going and make that little change on your code and see for yourself that it works. I don't know why it worked on your previous settings. All I know is the error I see in your current settings, and I just pointed it out(along with others).

I did make the change I moved the glLight() right after glLookAt()????

##### Share on other sites
//reset light position or else the light position will jump all overglPushMatrix();glLoadIdentity(); <---- Take this line away [wink]glLightfv(GL_LIGHT0, GL_POSITION, terrain.lightPosition);glPopMatrix();

I believe that should do it. If you're curious about it, you should look it up in chapter 5 of the OpenGL programming guide (the "red book"). Look at example 5-4, and 5-5, pages 192 and 193.

I think that the book is freely available on the internet somewhere if you don't have it with you.

Hope this helps.

Edit: On second thought, of the above four lines of code, only the call to glLightfv should be necessary.

##### Share on other sites
Quote:
 Original post by James Trotter//reset light position or else the light position will jump all overglPushMatrix();glLoadIdentity(); <---- Take this line away [wink]glLightfv(GL_LIGHT0, GL_POSITION, terrain.lightPosition);glPopMatrix();I believe that should do it. If you're curious about it, you should look it up in chapter 5 of the OpenGL programming guide (the "red book"). Look at example 5-4, and 5-5, pages 192 and 193.I think that the book is freely available on the internet somewhere if you don't have it with you.Hope this helps.Edit: On second thought, of the above four lines of code, only the call to glLightfv should be necessary.

Ok, that works now!! ;) Thanks all for the help and understanding, it just threw me off when my FP/VP worked fine without having to call the glLightfv() to set the lightposition after I position the camera...

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628642
• Total Posts
2983992

• 9
• 10
• 20
• 20
• 13