rayfract

annotate sdr/julia.p.glsl @ 3:bf1d56975cc9

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