#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();
}