rayfract

annotate sdr/julia.p.glsl @ 10:1496aae2e7d4

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