tiger

03-02-2007, 07:40 AM

hi. i'm a newbie of opengl and GLSL. i'm writing a phong shader but it don't work. Light type would be directional. Can you segnale me where i'm wrong, please? Thanks

(sorry for my bad english :) )

vertex:

varying vec3 normal,lightDir;

varying vec3 vertexPosition;

uniform vec3 ucLightDir;

uniform vec4 ucLightAmbient,ucLightDiffuse,ucLightSpecular;

void main() {

normal = normalize(gl_NormalMatrix * gl_Normal);

vertexPosition = (gl_ModelViewProjectionMatrix * gl_Vertex).xyz;

lightDir=ucLightDir;

gl_TexCoord[0] = gl_MultiTexCoord0;

//transform vertices

gl_Position = ftransform();

} Fragment:

uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];

uniform gl_LightModelParameters gl_LightModel;

varying vec3 normal;

varying vec3 lightDir;

varying vec3 vertexPosition;

uniform vec4 ucLightAmbient,ucLightDiffuse,ucLightSpecular;

uniform sampler2D tex;

//fragment shader

void main()

{

vec4 ambient,diffuse,specular;

//normalizing

vec3 nNormal = normalize ( normal );

vec3 nLightDir = normalize ( lightDir );

//DIFFUSE TERM

float lambertTerm = dot(nNormal,nLightDir);

//clamp 0-1 range

lambertTerm = max(lambertTerm,0.0);

diffuse = gl_FrontMaterial.diffuse * ucLightDiffuse * lambertTerm;

//AMBIENT TERM

vec4 lightAmbient = ucLightAmbient * gl_FrontMaterial.ambient;

vec4 globalAmbient = gl_LightModel.ambient * gl_FrontMaterial.ambient;

ambient = lightAmbient + globalAmbient;

//SPECULAR TERM

if(lambertTerm>0.0) {

vec3 eye = normalize (vertexPosition);

vec3 reflectionVec = reflect(-nLightDir, normal);

reflectionVec = normalize(reflectionVec);

vec3 halfVector = vec3(eye+nLightDir);

float RdotE = max( dot (nNormal,halfVector) , 0.0);

specular = gl_FrontMaterial.specular * ucLightSpecular * pow( RdotE , gl_FrontMaterial.shininess);

} //if(cosine>0.0) {

vec4 texel = texture2D(tex,gl_TexCoord[0].xy);

//set color

gl_FragColor = (diffuse + ambient + specular ) ;

}

(sorry for my bad english :) )

vertex:

varying vec3 normal,lightDir;

varying vec3 vertexPosition;

uniform vec3 ucLightDir;

uniform vec4 ucLightAmbient,ucLightDiffuse,ucLightSpecular;

void main() {

normal = normalize(gl_NormalMatrix * gl_Normal);

vertexPosition = (gl_ModelViewProjectionMatrix * gl_Vertex).xyz;

lightDir=ucLightDir;

gl_TexCoord[0] = gl_MultiTexCoord0;

//transform vertices

gl_Position = ftransform();

} Fragment:

uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];

uniform gl_LightModelParameters gl_LightModel;

varying vec3 normal;

varying vec3 lightDir;

varying vec3 vertexPosition;

uniform vec4 ucLightAmbient,ucLightDiffuse,ucLightSpecular;

uniform sampler2D tex;

//fragment shader

void main()

{

vec4 ambient,diffuse,specular;

//normalizing

vec3 nNormal = normalize ( normal );

vec3 nLightDir = normalize ( lightDir );

//DIFFUSE TERM

float lambertTerm = dot(nNormal,nLightDir);

//clamp 0-1 range

lambertTerm = max(lambertTerm,0.0);

diffuse = gl_FrontMaterial.diffuse * ucLightDiffuse * lambertTerm;

//AMBIENT TERM

vec4 lightAmbient = ucLightAmbient * gl_FrontMaterial.ambient;

vec4 globalAmbient = gl_LightModel.ambient * gl_FrontMaterial.ambient;

ambient = lightAmbient + globalAmbient;

//SPECULAR TERM

if(lambertTerm>0.0) {

vec3 eye = normalize (vertexPosition);

vec3 reflectionVec = reflect(-nLightDir, normal);

reflectionVec = normalize(reflectionVec);

vec3 halfVector = vec3(eye+nLightDir);

float RdotE = max( dot (nNormal,halfVector) , 0.0);

specular = gl_FrontMaterial.specular * ucLightSpecular * pow( RdotE , gl_FrontMaterial.shininess);

} //if(cosine>0.0) {

vec4 texel = texture2D(tex,gl_TexCoord[0].xy);

//set color

gl_FragColor = (diffuse + ambient + specular ) ;

}