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