@@ -23,15 +23,36 @@ uniform float time;
2323
2424in vec3 normal;
2525in vec2 uv0;
26+ in vec3 lightdir;
27+ in vec3 eyepos;
28+
2629
2730void main()
2831{
32+ vec4 diffuse = vec4 (0.5 , 0.5 , 0.5 , 1.0 );
33+ vec4 ambient = vec4 (0.2 , 0.2 , 0.2 , 1.0 );
34+ vec4 specular = vec4 (1.0 , 1.0 , 1.0 , 1.0 );
35+ float shininess = 0.5 ;
36+
2937 vec4 c = texture(texture0, uv0);
30- vec3 dir = vec3 (0.0 , 0.0 , 1.0 );
31- float d = dot (dir, normal);
32- fragColor = c * d;
38+ vec4 spec = vec4 (0.0 );
39+
40+ vec3 n = normalize (normal);
41+ vec3 l = normalize (lightdir);
42+ vec3 e = normalize (eyepos);
43+
44+ float intensity = max (dot (n,l), 0.0 );
45+ if (intensity > 0.0 ) {
46+ vec3 h = normalize (l + e);
47+ float intSpec = max (dot (h, n), 0.0 );
48+ spec = specular * pow (intSpec, shininess);
49+ }
50+ float att = clamp (1.0 - length (eyepos)/ 200.0 , 0.0 , 1.0 );
51+ att *= att;
52+ fragColor = c * max (intensity * diffuse + spec, ambient) * att;
3353}
3454
55+
3556#elif defined GEOMETRY_SHADER
3657
3758layout (points) in ;
@@ -40,9 +61,13 @@ layout (triangle_strip, max_vertices = 24) out; // 4 vertices per side of the cu
4061uniform mat4 ProjM;
4162uniform mat4 ModelViewM;
4263uniform mat3 NormalM;
64+ uniform vec3 lightpos;
65+ uniform float time;
4366
4467out vec2 uv0;
4568out vec3 normal;
69+ out vec3 lightdir;
70+ out vec3 eyepos;
4671
4772// Define the 8 corners of a cube (back plane, front plane (counter clockwise))
4873vec3 cube_corners[8 ] = vec3 [] (
@@ -58,7 +83,9 @@ vec3 cube_corners[8] = vec3[] (
5883
5984#define EMIT_V(POS, UV, NORMAL) \
6085 uv0 = UV; \
61- normal = NormalM * NORMAL; \
86+ normal = normalize (NormalM * NORMAL); \
87+ lightdir = lightpos - POS.xyz; \
88+ eyepos = - POS.xyz; \
6289 gl_Position = ProjM * vec4 (POS, 1.0 ); \
6390 EmitVertex()
6491
@@ -78,6 +105,10 @@ void main()
78105 for (i = 0 ; i < 8 ; i++ )
79106 {
80107 vec3 pos = point.xyz + cube_corners[i] * 0.5 ;
108+ pos.y += sin (time + length (gl_in[0 ].gl_Position .xyz));
109+ pos.x += cos (time + gl_in[0 ].gl_Position .x);
110+ pos.z += cos (time + gl_in[0 ].gl_Position .z);
111+
81112 corners[i] = (ModelViewM * vec4 (pos, 1.0 )).xyz;
82113 }
83114 EMIT_QUAD(3 , 2 , 0 , 1 , vec3 ( 0.0 , 0.0 , - 1.0 )); // back
0 commit comments