PDA

View Full Version : problem with specular lighting ?(not resolved) heeeeeeeeeeeeeeelp !



airseb
05-08-2005, 08:14 AM
Hi,
I have made a shader which worked without the specular part. But when i add the specular lighting, the reflection of the specular part seems to be on all the mesh and not concentrated on a little part, is it normal ?
otherwise what can i do ?

PS :does the specular part have to be in a separated color than the primary color ?
if yes how do it ? the gl_secondary_color is just accessible in read only.

airseb
05-09-2005, 05:58 AM
can you tell me why i have specular light on all the mesh and not at a point which move at the same time than the camera ?

Java Cool Dude
05-09-2005, 01:00 PM
Et si vous nous montrer votre code en détails pour commencer?

airseb
05-10-2005, 03:37 AM
ok this is the fragment shader(the vertex shader just contains a varying variable for normals)


varying vec3 normal ;

void main(void)
{

//ambient component
vec4 lightVector = vec4(distance(gl_LightSource[0].position.x, gl_FragCoord.x),
distance(gl_LightSource[0].position.y, gl_FragCoord.y),
distance(gl_LightSource[0].position.z, gl_FragCoord.z), 0.0) ;

vec4 ambientLight = gl_FrontMaterial.ambient * gl_LightSource[0].ambient ;

//diffuse component
//lightVector = normalize(lightVector) ;
float intensity = max(dot (normal, lightVector.xyz), 0.0);
vec4 diffuseLight = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * intensity ;

//specular component

//lightVector = normalize(lightVector) ;

vec4 eyeVector = vec4(distance(gl_ModelViewMatrixInverse[3][0], gl_FragCoord.x),
distance(gl_ModelViewMatrixInverse[3][1], gl_FragCoord.y),
distance(gl_ModelViewMatrixInverse[3][2], gl_FragCoord.z), 0.0);

//eyeVector.xyz = normalize(eyeVector.xyz);
vec3 eyePlusLight = normalize(eyeVector.xyz + lightVector.xyz) ;

vec4 specularLight=gl_FrontMaterial.specular * gl_LightSource[0].specular
*pow(max(dot(eyePlusLight, normal), 0.0), gl_FrontMaterial.shininess);

//attenuation
float attenuation = 1.0/(gl_LightSource[0].constantAttenuation +
gl_LightSource[0].linearAttenuation*distance(gl_LightSource[0].position, gl_FragCoord)
+ pow(distance(gl_LightSource[0].position, gl_FragCoord), 2.0)) ;

gl_FragColor = gl_FrontMaterial.emission + (ambientLight + diffuseLight + specularLight)
*attenuation ;

}

Ffelagund
05-10-2005, 05:28 AM
gl_FragCoord is not the coordinate of the fragment in world coordinates, but screen coordinates. To know the fragment position in world coordinates, pass the vertex position as a varying variable

airseb
05-10-2005, 06:01 AM
i have declared the varying variables also in the fragment shader and replaced gl_fragCoord by the new varying variable but now i have a black screen :-(
i do this in my vertex shader :

varying vec3 normal ;
varying vec4 coordinate ;

void main(void)
{
normal=gl_Normal ;
coordinate = gl_Position = ftransform();
}

Relic
05-10-2005, 07:04 AM
Lighting is normally done in eye space and that is gl_ModelViewMatrix * gl_Vertex;
Read the OpenGL 2.0 specs (2.11 and 2.14.1).
gl_Position is not in that space as it is calculated by gl_ModelViewProjectionMatrix * gl_Vertex;

airseb
05-12-2005, 03:33 AM
ok thanks i'll take a look and go back if i have questions

airseb
05-15-2005, 07:41 AM
i do this with renderMonkey in the vertex shader but i have a black screen :-/


varying vec3 normal ;
varying vec4 coordinate ;

void main(void)
{
normal=gl_Normal ;
coordinate = gl_ModelViewMatrix * gl_Vertex;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex ; //ftransform();
}

airseb
05-15-2005, 01:59 PM
i have normalized the vectors in the fragment shader, it didn't change something but after this i have removed attenuation and the model is lighted, however i don't see the specular part on the mesh and i would like that it works with attenuation, do you know how to do ?

this is my new fragment shader :

varying vec3 normal ;
varying vec4 coordinate ;//coordinate of the fragment

void main(void)
{

vec4 lightVector = vec4(gl_LightSource[0].position - coordinate) ;
lightVector = normalize(lightVector) ;

//ambient component
vec4 ambientLight = gl_FrontMaterial.ambient * gl_LightSource[0].ambient ;

//diffuse component
float intensity = max(dot (normal, lightVector.xyz), 0.0);
vec4 diffuseLight = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * intensity ;

//specular component

//lightVector = normalize(lightVector) ;

vec4 eyeVector = vec4(gl_ModelViewMatrixInverse[3][0] - coordinate.x,
gl_ModelViewMatrixInverse[3][1] - coordinate.y,
gl_ModelViewMatrixInverse[3][2] - coordinate.z, 0.0);
eyeVector.xyz = normalize(eyeVector.xyz);
vec3 eyePlusLight = normalize(eyeVector.xyz + lightVector.xyz) ;
vec4 specularLight=gl_FrontMaterial.specular *gl_LightSource[0].specular
*pow(max(dot(eyePlusLight, normal), 0.0), gl_FrontMaterial.shininess);


//attenuation
float attenuation = 1.0/(gl_LightSource[0].constantAttenuation +
gl_LightSource[0].linearAttenuation * distance(gl_LightSource[0].position, coordinate)
+pow( distance(gl_LightSource[0].position, coordinate),2.0) ) ;

gl_FragColor = gl_FrontMaterial.emission + (ambientLight + diffuseLight + specularLight)
/**attenuation*/ ;

}