from worldspace to viewspace error,

I am pretty new to Shaderprogramming and so I face now a problem.

I am trying to implement a Per Pixel Lightning Shader with only Diffuse Color Mapping (till now).

I first used the uniform variable CAMERA_POSITION of the Shader Editor.

To use this Shader without this camera position information, I converted the lightning Vector (lightVector) and the current Vertex Position (position) to viewspace, so that I can invert the position or often called ecposition and get the viewVector for the specular calculation.

Having the normal and ecPosition in worldspace will let the lightning work fine, if someone knows what is wrong it would be appreciated.

Here my Vertex Shader:

<div class=“ubbcode-block”><div class=“ubbcode-header”>Warning, Spoiler: <input type=“button” class=“form-button” value=“Show” onclick=“if (this.parentNode.parentNode.getElementsByTagName(‘div’)[1].getElementsByTagName(‘div’)[0].style.display != ‘’) { this.parentNode.parentNode.getElementsByTagName(‘div’)[1].getElementsByTagName(‘div’)[0].style.display = ‘’;this.innerText = ‘’; this.value = ‘Hide’; } else { this.parentNode.parentNode.getElementsByTagName(‘div’)[1].getElementsByTagName(‘div’)[0].style.display = ‘none’; this.innerText = ‘’; this.value = ‘Show’; }” />]<div style=“display: none;”>
varying vec3 normal;
varying vec3 position;
varying vec3 viewVec;
varying vec3 lightVector;
varying float attenuation;

void main()
{
gl_Position = ftransform();
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

    //convert to viewspace
normal = normalize(gl_NormalMatrix * gl_Normal); 
position = vec3(gl_ModelViewMatrix * gl_Vertex);
//normal = gl_Normal.xyz;
//position = gl_Vertex.xyz;
//vectors in world space = lightning is working

vec3 viewVec = normalize(-position);

lightVector = gl_LightSource[0].position.xyz - position;
float dist = length(lightVector);
attenuation = 1.0 / (gl_LightSource[0].constantAttenuation +
						   gl_LightSource[0].linearAttenuation * dist +
						   gl_LightSource[0].quadraticAttenuation * pow(dist,2.0));

}

[/QUOTE]</div>

Here is my fragment shader:

<div class=“ubbcode-block”><div class=“ubbcode-header”>Warning, Spoiler: <input type=“button” class=“form-button” value=“Show” onclick=“if (this.parentNode.parentNode.getElementsByTagName(‘div’)[1].getElementsByTagName(‘div’)[0].style.display != ‘’) { this.parentNode.parentNode.getElementsByTagName(‘div’)[1].getElementsByTagName(‘div’)[0].style.display = ‘’;this.innerText = ‘’; this.value = ‘Hide’; } else { this.parentNode.parentNode.getElementsByTagName(‘div’)[1].getElementsByTagName(‘div’)[0].style.display = ‘none’; this.innerText = ‘’; this.value = ‘Show’; }” />]<div style=“display: none;”>

uniform sampler2D texture;

varying vec3 viewVec;
varying vec3 normal;
varying vec3 position;
varying vec3 lightVector;
varying float attenuation;

void main()
{

vec4 specular = vec4 (0.0);
vec4 diffuse;
vec3 norm = normalize(normal);
vec3 viewVector = normalize(viewVec);

lightVector = normalize(lightVector);

float nxDir = max(0.0, dot(norm, lightVector));

diffuse = gl_LightSource[0].diffuse * nxDir * attenuation;

float specularPower = 0.0;

if(nxDir != 0.0) {
	vec3 halfVector = normalize(reflect(-lightVector, norm)); //reflection
	float nxHalf = max(0.0, dot(viewVector, halfVector));
	specularPower = pow(nxHalf, gl_FrontMaterial.shininess);
	specular = gl_LightSource[0].specular * specularPower * attenuation;
}

vec4 texColor = texture2D(texture, gl_TexCoord[0].st);

gl_FragColor = gl_LightSource[0].ambient + 
			   (diffuse * vec4(texColor.rgb, 1.0))+ 
			   (specular * texColor.a);

}
[/QUOTE]</div>

Is the built in uniform gl_LightSource[0].position.xyz is in view space or world space? If it is in world space, you must transform the light position in view space to make your lighting work with other variables in view space.

thanks for that quick reply and help, ok the diffuse lightning now works fine,

I will check it out later for the specular.