shaderFlatColorPerFace.geom 1.13 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// ShaderFlatColorPerFace::geometryShaderText
uniform float explode;
uniform mat4 ModelViewProjectionMatrix;
uniform mat4 NormalMatrix;
uniform mat4 ModelViewMatrix;
uniform vec3 lightPosition;
uniform vec4 ambient;

VARYING_IN vec3 colorVertex[];
VARYING_OUT vec4 ColorFS;

void main(void)
{
	vec3 v1 = POSITION_IN(1).xyz - POSITION_IN(0).xyz;
	vec3 v2 = POSITION_IN(2).xyz - POSITION_IN(0).xyz;
	vec3 N  = cross(v1,v2);
	N  =  normalize (vec3(NormalMatrix*vec4(N,0.0))); 
	vec3 center = POSITION_IN(0).xyz + POSITION_IN(1).xyz + POSITION_IN(2).xyz; 
	center /= 3.0;
	vec4 newPos =  ModelViewMatrix * vec4(center,0.0);
	vec3 L =  normalize (lightPosition - newPos.xyz);
	float lambertTerm = dot(N,L);
		
	int i;
25
	vec3 averageColor=vec3(0.,0.,0.);
Sylvain Thery's avatar
Sylvain Thery committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
	for(i=0; i< NBVERTS_IN; i++)
	{
		averageColor +=colorVertex[i];
	}

	averageColor /= float(NBVERTS_IN);
	
	ColorFS = ambient;
	if(lambertTerm > 0.0)
		ColorFS += vec4(averageColor,1.0) * lambertTerm;
			
	for(i=0; i< NBVERTS_IN; i++)
	{
		vec4 pos =  explode * POSITION_IN(i) + (1.0-explode)* vec4(center,1.0);
		gl_Position = ModelViewProjectionMatrix *  pos;
			
		EmitVertex();
	}
	EndPrimitive();
}