volray

view volray.p.glsl @ 7:f40e4edfee7e

shading
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 05 Apr 2012 00:52:11 +0300
parents 0c3874aa717a
children ae10631bb11b
line source
1 uniform sampler3D volume;
2 uniform sampler2D ray_tex;
3 uniform sampler1D xfer_tex;
4 uniform float ray_step;
5 uniform float zclip;
7 struct Ray {
8 vec3 origin, dir;
9 };
11 struct AABBox {
12 vec3 min, max;
13 };
15 struct ISect {
16 bool hit;
17 float t0, t1;
18 vec3 pos;
19 vec3 normal;
20 };
22 vec3 sky(Ray ray);
23 vec3 ray_march(Ray ray, float t0, float t1);
24 vec3 shade(Ray ray, vec3 pos, vec3 norm);
25 Ray get_primary_ray();
26 ISect intersect_aabb(Ray ray, AABBox aabb);
28 void main()
29 {
30 const AABBox aabb = AABBox(vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, 1.0));
31 Ray ray = get_primary_ray();
33 vec3 color = vec3(0.0, 0.0, 0.0);
35 ISect res = intersect_aabb(ray, aabb);
36 if(res.hit) {
37 color = ray_march(ray, res.t0, res.t1);
38 }
40 gl_FragColor = vec4(color, 1.0);
41 }
43 float eval(vec3 pos)
44 {
45 vec3 tc = pos * 0.5 + 0.5;
47 if(tc.x < 0.0 || tc.y < 0.0 || tc.z < zclip || tc.x > 1.0 || tc.y > 1.0 || tc.z > 1.0) {
48 return 0.0;
49 }
51 return texture1D(xfer_tex, texture3D(volume, tc).x).x;
52 }
54 #define OFFS 0.01
55 vec3 ray_march(Ray ray, float t0, float t1)
56 {
57 float energy = 1.0;
58 float t = t0;
59 vec3 col = vec3(0.0, 0.0, 0.0);
62 while(t < t1) {
63 vec3 pos = ray.origin + ray.dir * t;
64 t += ray_step;
66 float val = eval(pos) * energy;
67 vec3 norm;
69 norm.x = eval(pos + vec3(OFFS, 0.0, 0.0)) - val;
70 norm.y = eval(pos + vec3(0.0, OFFS, 0.0)) - val;
71 norm.z = eval(pos + vec3(0.0, 0.0, OFFS)) - val;
72 norm = normalize(norm);
74 col += shade(ray, pos, norm) * val;
75 energy -= val;
76 if(energy < 0.001) {
77 break;
78 }
79 pos += ray.dir * ray_step;
80 }
82 return col;
83 }
85 vec3 shade(Ray ray, vec3 pos, vec3 norm)
86 {
87 vec3 ldir = normalize(vec3(10.0, 10.0, -10.0) - pos);
88 vec3 vdir = -ray.dir;
89 vec3 hdir = normalize(ldir + vdir);
91 float ndotl = dot(ldir, norm);
92 float ndoth = dot(hdir, norm);
94 vec3 dcol = vec3(1.0, 1.0, 1.0) * max(ndotl, 0.0);
95 vec3 scol = vec3(0.6, 0.6, 0.6) * pow(max(ndoth, 0.0), 50.0);
97 return vec3(0.01, 0.01, 0.01) + dcol + scol;
98 }
100 Ray get_primary_ray()
101 {
102 Ray ray;
103 vec2 tc = gl_TexCoord[0].xy;
104 ray.dir = gl_NormalMatrix * normalize(texture2D(ray_tex, tc).xyz);
105 ray.origin = (gl_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
106 return ray;
107 }
109 ISect intersect_aabb(Ray ray, AABBox aabb)
110 {
111 ISect res;
112 res.hit = false;
113 res.t0 = res.t1 = 0.0;
115 if(ray.origin.x >= aabb.min.x && ray.origin.y >= aabb.min.y && ray.origin.z >= aabb.min.z &&
116 ray.origin.x < aabb.max.x && ray.origin.y < aabb.max.y && ray.origin.z < aabb.max.z) {
117 res.hit = true;
118 return res;
119 }
121 vec4 bbox[2];
122 bbox[0] = vec4(aabb.min.x, aabb.min.y, aabb.min.z, 0);
123 bbox[1] = vec4(aabb.max.x, aabb.max.y, aabb.max.z, 0);
125 int xsign = int(ray.dir.x < 0.0);
126 float invdirx = 1.0 / ray.dir.x;
127 float tmin = (bbox[xsign].x - ray.origin.x) * invdirx;
128 float tmax = (bbox[1 - xsign].x - ray.origin.x) * invdirx;
130 int ysign = int(ray.dir.y < 0.0);
131 float invdiry = 1.0 / ray.dir.y;
132 float tymin = (bbox[ysign].y - ray.origin.y) * invdiry;
133 float tymax = (bbox[1 - ysign].y - ray.origin.y) * invdiry;
135 if(tmin > tymax || tymin > tmax) {
136 return res;
137 }
139 if(tymin > tmin) tmin = tymin;
140 if(tymax < tmax) tmax = tymax;
142 int zsign = int(ray.dir.z < 0.0);
143 float invdirz = 1.0 / ray.dir.z;
144 float tzmin = (bbox[zsign].z - ray.origin.z) * invdirz;
145 float tzmax = (bbox[1 - zsign].z - ray.origin.z) * invdirz;
147 if(tmin > tzmax || tzmin > tmax) {
148 return res;
149 }
151 res.t0 = tmin;
152 res.t1 = tmax;
153 res.hit = true;
154 return res;
155 }