qvolray

view volray.p.glsl @ 0:b050ce167ff1

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 31 Mar 2012 02:08:42 +0300
parents
children 57072295eb83
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 ray_march(Ray ray);
16 vec3 shade(Ray ray, ISect isect);
17 Ray get_primary_ray();
19 void main()
20 {
21 Ray ray = get_primary_ray();
23 gl_FragColor = vec4(ray_march(ray), 1.0);
24 }
26 /*vec3 sky(Ray ray)
27 {
28 vec3 col1 = vec3(0.75, 0.78, 0.8);
29 vec3 col2 = vec3(0.56, 0.7, 1.0);
31 float t = max(ray.dir.y, -0.5);
32 return mix(col1, col2, t);
33 }*/
35 vec3 ray_march(Ray ray)
36 {
37 const float ray_step = 0.05;
38 float energy = 1.0;
39 vec3 pos = ray.origin;
41 // assuming view space
42 while(pos.z < 1.0) {
43 energy -= texture3D(volume, pos).x;
44 pos += ray.dir * ray_step;
45 }
47 return vec3(energy, energy, energy);
48 }
50 vec3 shade(Ray ray, ISect isect)
51 {
52 vec3 ldir = normalize(vec3(10.0, 10.0, -10.0) - isect.pos);
53 vec3 vdir = -ray.dir;
54 vec3 hdir = normalize(ldir + vdir);
56 float ndotl = dot(ldir, isect.normal);
57 float ndoth = dot(hdir, isect.normal);
59 vec3 dcol = vec3(1.0, 1.0, 1.0) * max(ndotl, 0.0);
60 vec3 scol = vec3(0.6, 0.6, 0.6) * pow(max(ndoth, 0.0), 50.0);
62 return vec3(0.01, 0.01, 0.01) + dcol + scol;
63 }
65 Ray get_primary_ray()
66 {
67 Ray ray;
68 vec2 tc = gl_TexCoord[0].xy;
69 ray.dir = gl_NormalMatrix * normalize(texture2D(ray_tex, tc).xyz);
70 ray.origin = (gl_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
71 return ray;
72 }