volray

view volray.p.glsl @ 1:57072295eb83

progress
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 02 Apr 2012 02:11:35 +0300
parents b050ce167ff1
children 3e53a16d4667
line source
1 uniform sampler3D volume;
2 uniform sampler2D ray_tex;
4 struct Ray {
5 vec3 origin, dir;
6 };
8 struct ISect {
9 bool hit;
10 float t;
11 vec3 pos;
12 vec3 normal;
13 };
15 vec3 sky(Ray ray);
16 vec3 ray_march(Ray ray);
17 vec3 shade(Ray ray, ISect isect);
18 Ray get_primary_ray();
20 void main()
21 {
22 Ray ray = get_primary_ray();
24 gl_FragColor = vec4(ray_march(ray), 1.0);
25 //gl_FragColor = vec4(sky(ray), 1.0);
26 }
28 vec3 sky(Ray ray)
29 {
30 vec3 col1 = vec3(0.75, 0.78, 0.8);
31 vec3 col2 = vec3(0.56, 0.7, 1.0);
33 float t = max(ray.dir.y, -0.5);
34 return mix(col1, col2, t);
35 }
37 vec3 ray_march(Ray ray)
38 {
39 const float ray_step = 0.1;
40 float energy = 1.0;
41 vec3 pos = ray.origin;
42 float col = 0.0;
44 for(int i=0; i<40; i++) {
45 float val = texture3D(volume, pos).x;
46 val *= energy;
47 col += val;
48 energy -= val;
49 if(energy < 0.001) {
50 break;
51 }
52 pos += ray.dir * ray_step;
53 }
55 return vec3(col, col, col);
56 }
58 vec3 shade(Ray ray, ISect isect)
59 {
60 vec3 ldir = normalize(vec3(10.0, 10.0, -10.0) - isect.pos);
61 vec3 vdir = -ray.dir;
62 vec3 hdir = normalize(ldir + vdir);
64 float ndotl = dot(ldir, isect.normal);
65 float ndoth = dot(hdir, isect.normal);
67 vec3 dcol = vec3(1.0, 1.0, 1.0) * max(ndotl, 0.0);
68 vec3 scol = vec3(0.6, 0.6, 0.6) * pow(max(ndoth, 0.0), 50.0);
70 return vec3(0.01, 0.01, 0.01) + dcol + scol;
71 }
73 Ray get_primary_ray()
74 {
75 Ray ray;
76 vec2 tc = gl_TexCoord[0].xy;
77 ray.dir = gl_NormalMatrix * normalize(texture2D(ray_tex, tc).xyz);
78 ray.origin = (gl_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
79 return ray;
80 }