Jump to content
  • Advertisement
Sign in to follow this  
subi

OpenGL glsl lighting problem

This topic is 4312 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 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??

Share this post


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

Share this post


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

Share this post


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

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!