Blame view

example/gradient.glsl 1016 Bytes
ee96a02c   David Mayerich   first commit from...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  vec3 grad(vec3 p, float d, sampler3D t)
  {
  	
  	vec3 px = vec3(p.x-d, p.y, p.z);
  	vec3 py = vec3(p.x, p.y-d, p.z);
  	vec3 pz = vec3(p.x, p.y, p.z-d);
  	
  	float pVal = texture(t, p).g;
  	
  	float dx = pVal - texture(t, px).r;
  	float dy = pVal - texture(t, py).r;
  	float dz = pVal - texture(t, pz).r;
  	
  	return vec3(dx, dy, dz);
  }
  
  vec4 computeColor(vec3 p)
  {
  	//compute the gradient
  	vec3 gradient = grad(p, 0.01, texIntensity);
  	vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
  	float gradmag = length(gradient);
  	
  	//compute the color based on the gradient
  	
  	if(gradmag == 0.0)
  		return vec4(0.0, 0.0, 0.0, 0.0);
  		
  	color.rgb = abs(normalize(gradient));
  	color.a = gradmag;
  	
  	//compute lighting
  	vec3 lightDir = -normalize(diffuse0);
  	float light0 = dot(lightDir, gradient);
  	if(light0 < 0) light0 = 0;
  	
  	lightDir = -normalize(diffuse1);
  	float light1 = dot(lightDir, gradient);
  	if(light1 < 0) light1 = 0;
  	
  	float ambient = 0.5;
  	
  	float lighting = light0 + light1 + ambient;
  	color.rgb = color.rgb*lighting;
  	return color;
  
  }