pointSprite.frag 1.41 KB
Newer Older
1
// PointSprite::fragmentShaderText
2

3
4
5
uniform mat4 ProjectionMatrix;
uniform vec3 lightPos;
uniform vec3 ambiant;
6
uniform float size;
7
#ifdef WITH_PLANE
Sylvain Thery's avatar
Sylvain Thery committed
8
9
	uniform vec3 eyePos;
	VARYING_FRAG vec3 shiftedEye;
10
11
#endif

12
13
14
15
16
17
#ifdef WITH_COLOR_PER_VERTEX 
	VARYING_FRAG vec3 colorsprite;
#else
	uniform vec3 colorsprite;
#endif

Sylvain Thery's avatar
Sylvain Thery committed
18
VARYING_FRAG vec2 spriteCoord;
19
20
VARYING_FRAG vec3 sphereCenter;

21
22
void main(void)
{
23
#ifdef WITH_PLANE
Sylvain Thery's avatar
Sylvain Thery committed
24
25
26
27
28
29
30
31
	vec3 billboard_frag_pos = vec3(spriteCoord, 0.0) * size;
	vec3 ray_direction = normalize(billboard_frag_pos - shiftedEye);
	float av = dot(shiftedEye,ray_direction);
	float arg = av*av - dot(shiftedEye,shiftedEye) + size*size;
	if (arg< 0.0)
		discard;
	float t = -av - sqrt(arg);
	vec3 frag_position_eye = ray_direction * t + eyePos	;
32
#else
Sylvain Thery's avatar
Sylvain Thery committed
33
	vec3 billboard_frag_pos = sphereCenter + vec3(spriteCoord, 0.0) * size;
34
35
36
37
	vec3 ray_direction = normalize(billboard_frag_pos);
	float TD = -dot(ray_direction,sphereCenter);
	float c = dot(sphereCenter, sphereCenter) - size * size;
	float arg = TD * TD - c;
Sylvain Thery's avatar
Sylvain Thery committed
38
    
39
40
	if (arg < 0.0)
		discard;
Sylvain Thery's avatar
Sylvain Thery committed
41

42
	float t = -c / (TD - sqrt(arg));
Sylvain Thery's avatar
Sylvain Thery committed
43
44
45
46
	vec3 frag_position_eye = ray_direction * t ;
#endif	


47
48
49
50
51
52
53
54
	vec4 pos = ProjectionMatrix * vec4(frag_position_eye, 1.0);
	gl_FragDepth = (pos.z / pos.w + 1.0) / 2.0;

	vec3 N = normalize(frag_position_eye - sphereCenter);
	vec3 L = normalize (lightPos - frag_position_eye);
	float lambertTerm = dot(N,L);
	
	gl_FragColor = vec4(colorsprite*lambertTerm + ambiant ,1.0);
Sylvain Thery's avatar
Sylvain Thery committed
55

56
}