rayfract

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