glsl lighting problem

Started by
5 comments, last by subi 17 years, 6 months ago
i am having a little trouble with my per-pixel lighting under GLSL. below is a pic of the problem. OpenGL fixed Function Lighting GLSL Lighting vert shader:

varying vec4 diffuse,ambient;
varying vec3 normal,lightDir,halfVector;
varying vec2 Texcoord;

void main()
{	
	normal = normalize(gl_NormalMatrix * gl_Normal)* -1.0;
	
	lightDir = normalize(vec3(gl_LightSource[0].position));

	halfVector = normalize(gl_LightSource[0].halfVector.xyz);
	

	diffuse =  gl_LightSource[0].diffuse;
	ambient =  gl_LightSource[0].ambient;
	
		
	gl_Position = ftransform();	
	Texcoord    = gl_MultiTexCoord0.xy;	
	
} 




frag shder:

uniform sampler2D baseMap;
varying vec4 diffuse,ambient;
varying vec3 normal,lightDir,halfVector;
varying vec2 Texcoord;


void main()
{
	vec3 n,halfV,viewV,ldir;
	float NdotL,NdotHV;
	vec4 color = ambient;
	
	vec4  tex = texture2D( baseMap, Texcoord );
		
	n = normalize(normal);
	
	NdotL = max(dot(n,lightDir),0.0);

	if (NdotL > 0.0) {
		halfV = normalize(halfVector);
		NdotHV = max(dot(n,halfV),0.0);
		color += diffuse * NdotL;
	}

	gl_FragColor = tex + color;
}


code to set up lighting


	glEnable(GL_LIGHTING);
	glEnable( GL_LIGHT0 );
	glShadeModel(GL_SMOOTH);
	GLfloat Ambient[4];
	GLfloat diffuse_light0[] = { 1.0f, 1.0f, 1.0f, 1.0f };

	Ambient[0] = 192.0f / 255.0f;
	Ambient[1] = 192.0f / 255.0f;
	Ambient[2] = 192.0f / 255.0f;
	Ambient[3] = 1.0f;


	glLightfv( GL_LIGHT0, GL_AMBIENT, Ambient);
	glLightfv( GL_LIGHT0, GL_DIFFUSE,  diffuse_light0);


every frame i update the light's position with(i have a moving camera):

GLfloat position_light0[] = {1.0f,1.0f,1.0f,0};
glLightfv( GL_LIGHT0, GL_POSITION,position_light0 );


as you can see from the first picture the lighting works just fine when i disable the shader but when i enable the shader(pic 2) it's just all bright and wrong. This shader is from lighthouse tut's. anyone notice what i am doing wrong??
Advertisement
normal = normalize(gl_NormalMatrix * gl_Normal)* -1.0;

Are you sure you need that -1?
Looks to me that your last color computation is wrong. I would use the texture as the diffuse component and leave out the ordinary diffuse color. i.e.

if (NdotL > 0.0) {
...
color += tex * NdotL;
}

gl_FragColor = color;
_____________________________
www.OddGames.comwww.OddGames.com/daniel
still doesn't work, it almost removes the texture completly
try with the ambient setted at 0,0,0

in your code :
Ambient[0] = 192.0f / 255.0f;
Ambient[1] = 192.0f / 255.0f;
Ambient[2] = 192.0f / 255.0f;
Ambient[3] = 1.0f;

it seems the ambient is very high, as you add the ambient to your lighted color if you start from 192/255 => 0.75 you can't have something black lke in you fixed pipeline

i think you don't set ambient in you fixed pipeline..

hope it helps..
and you must multiply the texture by the computed color :

gl_FragColor = tex * color;

an excellent ressource for you :
http://www.lighthouse3d.com/opengl/glsl/index.php?lights
thanks zoret u r dead right, u must multiply the texture with the color.

This topic is closed to new replies.

Advertisement