Directional Light is not working

I have a shader code which is working.
But I am trying for directional light.
Though light effect is coming.
When I zoom…
zoom in(move the eye location near to the model)
model is becoming brighter.

zoom out(move the eye location faraway from the model)
model is becoming dull.

I don’t want this to happen.
I could not under-stand, how get that effect because I am nowhere using the eye location.
Can some one help me. Thanks in advance.

Here I have the shader code for directional lighting

vertex shader

uniform vec3 eye;
out vec3 lightDirection;
out vec4 vColor;

out vec4 amb;
out vec4 dif;
out vec4 spc;
out float shi;

void main(void)  
{ 
   amb = gl_FrontLightProduct[0].ambient;
   dif = gl_FrontLightProduct[0].diffuse;
   spc = gl_FrontLightProduct[0].specular;
   shi = gl_FrontMaterial.shininess;

   //vv = vec3(gl_ModelViewMatrix * gl_Vertex);       
   //vN = normalize(gl_NormalMatrix * gl_Normal);
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;  
   vColor = gl_Color;
}

geometry shader

#version 330
#extension GL_ARB_gpu_shader5 : enable
layout (triangles) in;
layout(triangle_strip, max_vertices = 6) out;

uniform vec3 eye;
uniform vec4 light_pos;
//in vec3 vN[];
//in vec3 vv[];
out vec3 N;
out vec3 v;

in vec4 amb[];
in vec4 dif[];
in vec4 spc[];
in float shi[];
in vec4 vColor[];

out vec4 fResColor;

vec3 calculateTriangleNormal(){
	vec3 tangent = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz;
	vec3 bitangent = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz;
	vec3 normal = cross(tangent, bitangent);	
	return normalize(normal);
}
void main()
{
    int iSize = gl_in.length();
    int i = 0;
    vec4 CG = (gl_in[0].gl_Position + gl_in[1].gl_Position + gl_in[2].gl_Position)/3.0;
    
    vec3 normal = calculateTriangleNormal();
    
    v = CG.xyz;
    	
    	vec3 L = light_pos.xyz;
    	if(dot(L,normal)>0)
    	   N = normal;
    	else
    	   N = -normal;
    	vec3 E = vec3(-v);
    	vec3 R = normalize(-reflect(L,N));  
    	     
    	vec4 Idiff = dif[0] * max(dot(N,L), 0.0);
    	Idiff = clamp(Idiff, 0.0, 1.0);     
    	       
    	// calculate Specular Term:
    	vec4 Ispec = spc[0] * pow(max(dot(R,E),0.0),0.3*shi[0]);
    	Ispec = clamp(Ispec, 0.0, 1.0); 
    	
        fResColor = (vColor[i]*amb[i]) + Idiff + Ispec;
    
    for(i=0;i<iSize;++i)
    {
	gl_Position = gl_in[i].gl_Position;	
	
	
	EmitVertex();
    }

    EndPrimitive();
}

fragment shader


in vec4 fResColor;
void main (void)  
{
   gl_FragColor = fResColor;     
}

Doing lighting in clip space or NDC will produce incorrect results. It will produce wildly incorrect results if the eye position and/or light direction are in some other space.

Have your vertex shader write out eye-space positions, i.e.:

And use those.

Note that you’ll need to transform the light position (or direction) to eye space (otherwise the light will move with the viewpoint); the eye position will be (0,0,0).

  1. I am doing that to get some performance. I have changed it fragment shader,

  2. Here L is the direction of the light. So I was thinking like that will not change.
    can you help me how to make sure that light positin is not changing.

All of the vectors used in the lighting calculations must be in the same coordinate system, and that coordinate system should not include any projective transformations.

Typically, lighting calculations are performed in eye space. So, vertex positions are transformed by the model-view matrix, normals are transformed by either the model-view matrix or (if necessary) its inverse transpose, light positions and directions are transformed by the view matrix, and the eye position is just (0,0,0). The projection matrix should not be applied to anything used for lighting, so the vertex shader needs to write out the eye-space position as well as gl_Position (which is in clip space).

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.