Jump to content
  • Advertisement
Sign in to follow this  
LorinAtzberger

OpenGL Light position problem

This topic is 3321 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 have a weird problem. I can't get the lights to work ok with shaders. I tested without shaders and the light positions are always placed in the correct position so it's not from the code. I tried all the code I can find and everythime I get either a light that moves with the camera or one that just acts weird. Frag:
varying vec4 diffuse,ambientGlobal, ambient;
varying vec3 normal,lightDir,halfVector;
varying float dist;


void main()
{
	vec3 n,halfV,viewV,ldir;
	float NdotL,NdotHV;
	vec4 color = ambientGlobal;
	float att;
	
	/* a fragment shader can't write a verying 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,normalize(lightDir)),0.0);

	if (NdotL > 0.0) {
	
		att = 1.0 / (gl_LightSource[0].constantAttenuation +
				gl_LightSource[0].linearAttenuation * dist +
				gl_LightSource[0].quadraticAttenuation * dist * dist);
		color += att * (diffuse * NdotL + ambient);
	
		
		halfV = normalize(halfVector);
		NdotHV = max(dot(n,halfV),0.0);
		color += att * gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(NdotHV,gl_FrontMaterial.shininess);
	}

	gl_FragColor = color;
}
Vert"
varying vec4 diffuse,ambientGlobal,ambient;
varying vec3 normal,lightDir,halfVector;
varying float dist;

void main()
{	
	vec4 ecPos;
	vec3 aux;
	
	/* first transform the normal into eye space and normalize the result */
	normal = 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 */
	ecPos = gl_ModelViewMatrix * gl_Vertex;
	aux = vec3(gl_LightSource[0].position-ecPos);
	lightDir = normalize(vec3(gl_LightSource[0].position));
	
	/* compute the distance to the light source to a varying variable*/
	dist = length(aux);

	/* 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;
	ambientGlobal = gl_LightModel.ambient * gl_FrontMaterial.ambient;
	
		
	gl_Position = ftransform();
} 
I'm quite a begginnner at this glsl thing so I'm not sure if I'm doing a stupid mistake. Can you recommend any book with practical examples? Thank you.

Share this post


Link to post
Share on other sites
Advertisement
I've seen this in another post but I tried both and I get the exact same result
I even tried them in the shader designer utility and they both act as if the light was where the camera is.

Share this post


Link to post
Share on other sites
I think it might be because of the way my lights are set up so here's what I do:

I set up my camera with these commands
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(left+dx,right+dx,bottom+dy,top+dy,zNear,zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-eyedx,-eyedy,0);

Then my lights like this
glPushMatrix();
glMultMatrixf(*mat->grid);
float pos[] = {px,py,-pz};//,w};
glLightfv(gl_light[light_no],GL_POSITION,pos);
glPopMatrix();

I can get the light position with methods like EntityX() but those are in model space.
I'm not sure how this whole projection model matrix works.

I just want to be able to set up my camera and put the lights on

Share this post


Link to post
Share on other sites
I fixed up the problem with the lighting. I got a per pixel shader to work nice.
Now I'm trying the shaders that come with Shader Designer to see how they work.
I like the parallax and relief ones so I tried testing them.
Everything is se up correct but when I start I get something like this ( a little different for the relief shader)

Share this post


Link to post
Share on other sites
Ha ... i know why...

i do this.

float lightPosition[3] = {position.x, position.y, position.z};

which bypasses the View matrix and only passed in the lights world position. I must not be multiplying it correctly in my shader.

Share this post


Link to post
Share on other sites
Yeah but since I used the shaders from ShaderDesigner and they work there. I tought they would work ok in my program as well.
Anyway I pass my lights as this:
float pos[] = {EntityX(true),EntityY(true),-EntityZ(true),1};
glLightfv(gl_light[light_no],GL_POSITION,pos);

If you find out what the problem is please tell me. I'll try to fix this as well :).

Share this post


Link to post
Share on other sites
well....

The light position you need has to be in view space. when you get it in the shader... you need to make sure that the position has been multiplied by the view matrix.... my problem is im multiplying my lightPos by the MODELVIEW matrix... which already has the position in it.... one could consider this error to be overkill... what i need to do is just pass in the view matrix and mult... or pass in 1,1,-1 and mult by the inverse of the modelview.. (since moving the camera is like doing the opposite kind of move of the entire world) for example moving the camera forward is the same as moving the world backword.. hence the inverse.

Make sense? Our problems are definitely the same... we are just doing the math in different spots.

Share this post


Link to post
Share on other sites
Actually this might be the problem for you buit it's not for me. My shader acts the same even without light. It acutally doesn't use lights at all in it.
Here's the code:

Vert:

attribute vec3 tangent;
attribute vec3 binormal;
varying vec3 eyeVec;

void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;

mat3 TBN_Matrix;// = mat3(tangent, binormal, gl_Normal);
TBN_Matrix[0] = gl_NormalMatrix * tangent;
TBN_Matrix[1] = gl_NormalMatrix * binormal;
TBN_Matrix[2] = gl_NormalMatrix * gl_Normal;
vec4 Vertex_ModelView = gl_ModelViewMatrix * gl_Vertex;
eyeVec = vec3(Vertex_ModelView) * TBN_Matrix ;
// Vertex transformation
gl_Position = ftransform();
}


Frag:
uniform vec2 scaleBias;
uniform sampler2D basetex;
uniform sampler2D bumptex;
varying vec3 eyeVec;


void main()
{
vec2 texUV, srcUV = gl_TexCoord[0].xy;
float height = texture2D(bumptex, srcUV).r;
float v = height * scaleBias.x - scaleBias.y;
vec3 eye = normalize(eyeVec);
texUV = srcUV + (eye.xy * v);

vec3 rgb = texture2D(basetex, texUV).rgb;

// output final color
gl_FragColor = vec4(rgb, 1.0);
//gl_FragColor = vec4(vec3(rgb)*height, 1.0);



}

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!