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;
}