Problem with GLSL Per Pixel Shader

Started by
8 comments, last by Expandable 18 years, 1 month ago
Hi, I have a problem with my GLSL Per Pixel Shader. I used the following sources for creating it: Lightouse3D and Phong for Dummies Hmm, it is really hard to say what's wrong, so I put a small demo together (with one lightsource [the yellow cube] and a character). But I'll try to say what's not working: The light completly ignores my character rotation. So, if I stand in front of the light, the frontside of my char is brightened, but if I rotate about 180°, the frontside is still brightened. Really strange. So, please help. Here are my shader:

varying vec3 vLightDir;
varying vec3 vNormal;
varying float dist;
varying vec3 vHalf;


uniform vec3 vLightPos;
uniform vec3 vPlayerPos;
uniform vec3 vCameraPos;

vec3 vPos;
vec3 vEye;

void main()
{
	vNormal = normalize(gl_Normal /* gl_NormalMatrix*/) * -1.0;
	vEye = (gl_Vertex * gl_ModelViewMatrix).xyz  ;
	//vEye = (gl_Vertex * gl_ModelViewMatrix).xyz;
	vPos = (vLightPos - vPlayerPos) - vEye;
	vLightDir = normalize(vPos);
	vHalf = normalize((vLightPos + vEye) * 0.5);

	dist = length(vPos);
	
	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_Position = ftransform();
}


varying vec3 vLightDir;
varying vec3 vNormal;
varying vec3 vHalf;
varying float dist;

uniform sampler2D materialmap;
uniform vec3 vLightColor;
uniform float invradius;

vec3 vLight;
vec3 vHalfAngle;
vec3 vMaterial;
vec3 n;

float diffuse;
float specular;
float att;
float dist2;

void main()
{
	vMaterial = texture2D(materialmap, gl_TexCoord[0].st).xyz;

	n = normalize(vNormal);
	vLight = normalize(vLightDir);
	vHalfAngle = normalize(vHalf);//normalize((vLight + n) * 0.5);

  diffuse = max( dot(n, vLight), 0.0);
  if(diffuse > 0.0)
  {
	  specular = pow( max( dot(n, vHalfAngle), 0.0), 8.0);
	  att = max(1.0 - pow(dist * invradius, 2.0), 0.0);
	
		vec3 sMat = vec3(1.0, 1.0, 1.0);
		vec3 dMat = vec3(1.0,1.0,1.0);
		vMaterial += vLightColor * (diffuse + specular) * att;
	}
	
	gl_FragColor.xyz =  vMaterial;
}

Here's a small demo to demonstrate the Problem: ~ 6MB Use W/A/S/D/Q/E for Movement and Cursorkeys for Camera Movement.
Advertisement
You seem to have commented out multiplying the normal by gl_NormalMatrix. Try with putting the multiply in there too.
Olli Salli
Quote:Original post by oggialli
You seem to have commented out multiplying the normal by gl_NormalMatrix. Try with putting the multiply in there too.

When multiplying with the normalmatrix, I get more strange problems :(

You can try it :) Download the demo and modify the the data/shader/model_ppl.vert and you'll see what I mean.
This line, for example, can't work:

vEye = (gl_Vertex * gl_ModelViewMatrix).xyz ;

it should be gl_ModelViewMatrix * gl_Vertex. If you have a vector v and a matrix m, m*v!=transpose(v)*m in most cases, the same thing applies when multiplying two matrices: m1*m2 != m2*m1!
Quote:Original post by Expandable
This line, for example, can't work:

vEye = (gl_Vertex * gl_ModelViewMatrix).xyz ;

it should be gl_ModelViewMatrix * gl_Vertex. If you have a vector v and a matrix m, m*v!=transpose(v)*m in most cases, the same thing applies when multiplying two matrices: m1*m2 != m2*m1!

Thank you, that's a good point. Stupid me :)
That was the reason for the confusing lighting on the character :)

But that still doesn't solve the rotation problem :(
Try the proper order for transforming the normal too. normal = normalize(gl_NormalMatrix * gl_Normal) * -1.0, that is.

Sorry, I can't try your demo, since I am a Linux user. So I can't test my solutions either.
Olli Salli
Quote:Original post by oggialli
Try the proper order for transforming the normal too. normal = normalize(gl_NormalMatrix * gl_Normal) * -1.0, that is.

Sorry, I can't try your demo, since I am a Linux user. So I can't test my solutions either.

Thanks for the response, but I tried that already and it didn't work.
see www.delphi3d.net i believe they have a simple glsl lighting example there
Quote:Original post by zedzeek
see www.delphi3d.net i believe they have a simple glsl lighting example there


Thanks for the link. But basically, the shader to the same as mine :)

But the main problem is with all the shader examples I've found:

They all have an object at position X and a light rotating around them, so they caclulate the light vector:

Light = Lightposition - Eyeposition

But I want a static light at pos X and a character moving around. So, I caclulate the lightpos dependending on the character position:

Light = (Lightposition - Playerposition) - Eyeposition

And this works fine, except for the rotation, and I don't see any reason why it's not working :(
You don't use the glLight*()-functions. So you might have to multiply the light position with the gl_ModelViewMatrix as well? Furthermore I don't understand why you need the camera position to calulcate the position of the light relative to the vertex?

This topic is closed to new replies.

Advertisement