rayfract

view sdr/julia.p.glsl @ 0:09bb67c000bc

ray-fract repository
author John Tsiombikas <nuclear@siggraph.org>
date Thu, 21 Oct 2010 23:39:26 +0300
parents
children 03022062c464
line source
1 /* vim: set ft=glsl:ts=4:sw=4 */
2 uniform vec4 seed;
3 uniform sampler2D ray_tex;
4 uniform float err_thres;
5 uniform int iter;
7 #define quat(s, x, y, z) vec4(x, y, z, s)
8 #define quat_identity() vec4(0.0, 0.0, 0.0, 1.0)
10 #define vec2quat(v) (v).wxyz
12 struct Ray {
13 vec3 origin;
14 vec3 dir;
15 };
17 struct Julia {
18 bool inside;
19 vec4 q;
20 vec4 qprime;
21 };
23 struct ISect {
24 bool hit;
25 float t;
26 vec3 pos;
27 vec3 normal;
28 vec3 color;
29 float kr;
30 };
32 ISect find_intersection(Ray ray);
33 vec3 shade(Ray ray, ISect isect);
34 float amboc(ISect isect);
35 vec3 sky(Ray ray);
36 Julia julia(vec4 q, vec4 c);
37 float julia_dist(vec4 z);
38 vec3 julia_grad(vec4 z);
39 vec4 quat_mul(vec4 q1, vec4 q2);
40 vec4 quat_sq(vec4 q);
41 float quat_length_sq(vec4 q);
42 ISect ray_julia(Ray ray);
43 ISect ray_sphere(Ray ray, float rad);
44 ISect ray_floor(Ray ray);
45 Ray get_primary_ray();
48 void main()
49 {
50 Ray ray = get_primary_ray();
52 float energy = 1.0;
53 vec3 color = vec3(0.0, 0.0, 0.0);
55 while(energy > 0.001) {
56 ISect res = find_intersection(ray);
58 if(res.hit) {
59 color += shade(ray, res) * energy;
60 energy *= res.kr;
62 ray.origin = res.pos;
63 ray.dir = reflect(ray.dir, res.normal);
64 } else {
65 color += sky(ray) * energy;
66 break;
67 }
68 }
70 gl_FragColor = vec4(color, 1.0);
71 }
74 ISect find_intersection(Ray ray)
75 {
76 ISect res;
77 res.hit = false;
79 ISect bhit = ray_sphere(ray, 2.0);
80 if(bhit.hit) {
81 ray.origin = bhit.pos;
82 res = ray_julia(ray);
83 }
85 if(!res.hit) {
86 res = ray_floor(ray);
87 }
88 return res;
89 }
91 vec3 shade(Ray ray, ISect isect)
92 {
93 vec3 ldir = normalize(vec3(10.0, 10.0, -10.0) - isect.pos);
94 vec3 vdir = -ray.dir;
95 vec3 hdir = normalize(ldir + vdir);
97 float ndotl = dot(ldir, isect.normal);
98 float ndoth = dot(hdir, isect.normal);
100 vec3 dcol = /*isect.color * max(ndotl, 0.0) */ amboc(isect);
101 vec3 scol = vec3(1.0, 1.0, 1.0) * pow(max(ndoth, 0.0), 40.0);
103 return /*vec3(0.05, 0.05, 0.05) + */dcol;// + scol;
104 }
106 #define AO_STEP 0.04
107 #define AO_MAGIC 8.0
108 float amboc(ISect isect)
109 {
110 float sum = 0.0;
112 for(float fi=0.0; fi<5.0; fi+=1.0) {
113 float sample_dist = fi * AO_STEP;
114 vec3 pt = isect.pos + isect.normal * sample_dist;
115 float jdist = julia_dist(quat(pt.x, pt.y, pt.z, 0.0));
117 sum += 1.0 / pow(2.0, fi) * (sample_dist - jdist);
118 }
120 return 1.0 - AO_MAGIC * sum;
121 }
123 vec3 sky(Ray ray)
124 {
125 vec3 col1 = vec3(0.75, 0.78, 0.8);
126 vec3 col2 = vec3(0.56, 0.7, 1.0);
128 float t = max(ray.dir.y, -0.5);
129 return mix(col1, col2, t);
130 }
132 Julia julia(vec4 q, vec4 c)
133 {
134 Julia res;
135 res.inside = true;
137 res.q = q;
138 res.qprime = quat_identity();
140 for(int i=0; i<iter; i++) {
141 res.qprime = 2.0 * quat_mul(res.q, res.qprime);
142 res.q = quat_sq(res.q) + c;
144 if(dot(res.q, res.q) > 8.0) {
145 res.inside = false;
146 break;
147 }
148 }
149 return res;
150 }
152 float julia_dist(vec4 z)
153 {
154 Julia jres = julia(z, seed);
156 float lenq = length(jres.q);
157 float lenqprime = length(jres.qprime);
159 return 0.5 * lenq * log(lenq) / lenqprime;
160 }
162 #define OFFS 1e-4
163 vec3 julia_grad(vec4 z)
164 {
165 vec3 grad;
166 grad.x = julia_dist(z + quat(OFFS, 0.0, 0.0, 0.0)) - julia_dist(z - quat(OFFS, 0.0, 0.0, 0.0));
167 grad.y = julia_dist(z + quat(0.0, OFFS, 0.0, 0.0)) - julia_dist(z - quat(0.0, OFFS, 0.0, 0.0));
168 grad.z = julia_dist(z + quat(0.0, 0.0, OFFS, 0.0)) - julia_dist(z - quat(0.0, 0.0, OFFS, 0.0));
169 return grad;
170 }
172 vec4 quat_mul(vec4 q1, vec4 q2)
173 {
174 vec4 res;
175 res.w = q1.w * q2.w - dot(q1.xyz, q2.xyz);
176 res.xyz = q1.w * q2.xyz + q2.w * q1.xyz + cross(q1.xyz, q2.xyz);
177 return res;
178 }
180 vec4 quat_sq(vec4 q)
181 {
182 vec4 res;
183 res.w = q.w * q.w - dot(q.xyz, q.xyz);
184 res.xyz = 2.0 * q.w * q.xyz;
185 return res;
186 }
188 ISect ray_julia(Ray inray)
189 {
190 float dist_acc = 0.0;
191 Ray ray = inray;
192 ISect res;
194 for(float fi=0.0; ; fi+=0.1) {
195 vec4 q = quat(ray.origin.x, ray.origin.y, ray.origin.z, 0.0);
197 float dist = julia_dist(q);
199 ray.origin += ray.dir * dist;
200 dist_acc += dist;
202 if(dist < err_thres) {
203 res.hit = true;
204 res.t = dist_acc;
205 res.pos = ray.origin;
206 res.normal = normalize(julia_grad(quat(res.pos.x, res.pos.y, res.pos.z, 0.0)));
207 res.color = vec3(0.75, 0.8, 0.9);//abs(res.normal) * 0.2;
208 //res.kr = 0.6;
209 res.kr = 0.0;
210 break;
211 }
213 if(dot(ray.origin, ray.origin) > 100.0) {
214 res.hit = false;
215 break;
216 }
217 }
219 return res;
220 }
222 ISect ray_sphere(Ray ray, float rad)
223 {
224 ISect res;
225 res.hit = false;
227 float a = dot(ray.dir, ray.dir);
228 float b = 2.0 * dot(ray.dir, ray.origin);
229 float c = dot(ray.origin, ray.origin) - rad * rad;
231 float d = b * b - 4.0 * a * c;
232 if(d < 0.0) return res;
234 float sqrt_d = sqrt(d);
235 float t1 = (-b + sqrt_d) / (2.0 * a);
236 float t2 = (-b - sqrt_d) / (2.0 * a);
238 if((t1 >= 0.0 || t2 >= 0.0)) {
239 if(t1 < 0.0) t1 = t2;
240 if(t2 < 0.0) t2 = t1;
242 res.hit = true;
243 res.t = min(t1, t2);
244 res.pos = ray.origin + ray.dir * res.t;
245 res.color = vec3(1.0, 0.3, 0.2);
246 res.normal = res.pos / rad;
247 }
249 return res;
250 }
252 #define FLOOR_HEIGHT (-2.0)
254 ISect ray_floor(Ray ray)
255 {
256 ISect res;
257 res.hit = false;
259 if(ray.origin.y < FLOOR_HEIGHT || ray.dir.y >= 0.0) {
260 return res;
261 }
263 res.normal = vec3(0.0, 1.0, 0.0);
264 float ndotdir = dot(res.normal, ray.dir);
266 float t = (FLOOR_HEIGHT - ray.origin.y) / ndotdir;
267 res.pos = ray.origin + ray.dir * t;
269 if(abs(res.pos.x) > 8.0 || abs(res.pos.z) > 8.0) {
270 res.hit = false;
271 } else {
272 res.hit = true;
274 float chess = mod(floor(res.pos.x) + floor(res.pos.z), 2.0);
275 res.color = mix(vec3(0.498, 0.165, 0.149), vec3(0.776, 0.851, 0.847), chess);
276 res.kr = 0.0;
277 }
278 return res;
279 }
281 Ray get_primary_ray()
282 {
283 Ray ray;
284 vec2 tc = gl_TexCoord[0].xy;
285 ray.dir = gl_NormalMatrix * normalize(texture2D(ray_tex, tc).xyz);
286 ray.origin = (gl_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
287 return ray;
288 }