qvolray

view volray.p.glsl @ 5:0c3874aa717a

slice
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 04 Apr 2012 01:37:33 +0300
parents 3e53a16d4667
children f40e4edfee7e
line source
1 uniform sampler3D volume;
2 uniform sampler2D ray_tex;
3 uniform sampler1D xfer_tex;
5 struct Ray {
6 vec3 origin, dir;
7 };
9 struct AABBox {
10 vec3 min, max;
11 };
13 struct ISect {
14 bool hit;
15 float t;
16 vec3 pos;
17 vec3 normal;
18 };
20 vec3 sky(Ray ray);
21 vec3 ray_march(Ray ray);
22 vec3 shade(Ray ray, ISect isect);
23 Ray get_primary_ray();
24 ISect intersect_aabb(Ray ray, AABBox aabb);
26 void main()
27 {
28 const AABBox aabb = AABBox(vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, 1.0));
29 Ray ray = get_primary_ray();
31 vec3 color = vec3(0.0, 0.0, 0.0);
33 ISect res = intersect_aabb(ray, aabb);
34 if(res.hit) {
35 ray.origin += ray.dir * res.t;
36 color = ray_march(ray);
37 }
39 gl_FragColor = vec4(color, 1.0);
40 }
42 vec3 sky(Ray ray)
43 {
44 vec3 col1 = vec3(0.75, 0.78, 0.8);
45 vec3 col2 = vec3(0.56, 0.7, 1.0);
47 float t = max(ray.dir.y, -0.5);
48 return mix(col1, col2, t);
49 }
51 float eval(vec3 pos)
52 {
53 vec3 tc = pos * 0.5 + 0.5;
54 return texture1D(xfer_tex, texture3D(volume, tc).x).x;
55 }
57 vec3 ray_march(Ray ray)
58 {
59 const float ray_step = 0.1;
60 float energy = 1.0;
61 vec3 pos = ray.origin;
62 float col = 0.0;
64 for(int i=0; i<40; i++) {
65 float val = eval(pos) * energy;
66 col += val;
67 energy -= val;
68 if(energy < 0.001) {
69 break;
70 }
71 pos += ray.dir * ray_step;
72 }
74 return vec3(col, col, col);
75 }
77 vec3 shade(Ray ray, ISect isect)
78 {
79 vec3 ldir = normalize(vec3(10.0, 10.0, -10.0) - isect.pos);
80 vec3 vdir = -ray.dir;
81 vec3 hdir = normalize(ldir + vdir);
83 float ndotl = dot(ldir, isect.normal);
84 float ndoth = dot(hdir, isect.normal);
86 vec3 dcol = vec3(1.0, 1.0, 1.0) * max(ndotl, 0.0);
87 vec3 scol = vec3(0.6, 0.6, 0.6) * pow(max(ndoth, 0.0), 50.0);
89 return vec3(0.01, 0.01, 0.01) + dcol + scol;
90 }
92 Ray get_primary_ray()
93 {
94 Ray ray;
95 vec2 tc = gl_TexCoord[0].xy;
96 ray.dir = gl_NormalMatrix * normalize(texture2D(ray_tex, tc).xyz);
97 ray.origin = (gl_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
98 return ray;
99 }
101 ISect intersect_aabb(Ray ray, AABBox aabb)
102 {
103 ISect res;
104 res.hit = false;
106 if(ray.origin.x >= aabb.min.x && ray.origin.y >= aabb.min.y && ray.origin.z >= aabb.min.z &&
107 ray.origin.x < aabb.max.x && ray.origin.y < aabb.max.y && ray.origin.z < aabb.max.z) {
108 res.t = 0.0;
109 res.hit = true;
110 return res;
111 }
113 vec4 bbox[2];
114 bbox[0] = vec4(aabb.min.x, aabb.min.y, aabb.min.z, 0);
115 bbox[1] = vec4(aabb.max.x, aabb.max.y, aabb.max.z, 0);
117 int xsign = int(ray.dir.x < 0.0);
118 float invdirx = 1.0 / ray.dir.x;
119 float tmin = (bbox[xsign].x - ray.origin.x) * invdirx;
120 float tmax = (bbox[1 - xsign].x - ray.origin.x) * invdirx;
122 int ysign = int(ray.dir.y < 0.0);
123 float invdiry = 1.0 / ray.dir.y;
124 float tymin = (bbox[ysign].y - ray.origin.y) * invdiry;
125 float tymax = (bbox[1 - ysign].y - ray.origin.y) * invdiry;
127 if(tmin > tymax || tymin > tmax) {
128 return res;
129 }
131 if(tymin > tmin) tmin = tymin;
132 if(tymax < tmax) tmax = tymax;
134 int zsign = int(ray.dir.z < 0.0);
135 float invdirz = 1.0 / ray.dir.z;
136 float tzmin = (bbox[zsign].z - ray.origin.z) * invdirz;
137 float tzmax = (bbox[1 - zsign].z - ray.origin.z) * invdirz;
139 if(tmin > tzmax || tzmin > tmax) {
140 return res;
141 }
143 res.t = tmin;
144 res.hit = true;
145 res.pos = ray.origin + ray.dir * res.t;
147 /*if(res.pos.x > res.pos.y && res.pos.x > res.pos.z) {
148 res.normal = vec3(1.0, 0.0, 0.0);
149 } else if(res.pos.x < res.pos.y && res.pos.x < res.pos.z) {
150 res.normal = vec3(-1.0, 0.0, 0.0);
151 } else if(res.pos.y > res.pos.x && res.pos.y > res.pos.z) {
152 res.normal = vec3(0.0, 1.0, 0.0);
153 } else if(res.pos.y < res.pos.x && res.pos.y < res.pos.z) {
154 res.normal = vec3(0.0, -1.0, 0.0);
155 } else if(res.pos.z > res.pos.x && res.pos.z > res.pos.y) {
156 res.normal = vec3(0.0, 0.0, 1.0);
157 } else {
158 res.normal = vec3(0.0, 0.9, -1.0);
159 }
160 if(res.pos.x < 0.0) {
161 res.normal = vec3(1.0, 0.0, 0.0);
162 } else {
163 res.normal = vec3(0.0, 0.0, 1.0);
164 }*/
165 return res;
166 }