• API
• FAQ
• Tools
• Archive
daily pastebin goal
59%
SHARE
TWEET

# Sierpinski tetrahedron attempt

matrefeytontias May 19th, 2016 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #define DE_ITER 10
2. #define MARCHING_ITER 64
3. #define MARCHING_THRESHOLD 10e-6
4.
5. vec3 camera = vec3(0., 1., -5.);
6.
7. float d_sierpinski_tetrahedron(vec3 z)
8. {
9.     float Scale = 0.5;
10.     vec3 a1 = vec3(1,1,1);
11.     vec3 a2 = vec3(-1,-1,1);
12.     vec3 a3 = vec3(1,-1,-1);
13.     vec3 a4 = vec3(-1,1,-1);
14.     vec3 c;
15.     int n = 0;
16.     float dist, d;
17.     while (n < DE_ITER) {
18.          c = a1; dist = length(z-a1);
19.             d = length(z-a2); if (d < dist) { c = a2; dist=d; }
20.          d = length(z-a3); if (d < dist) { c = a3; dist=d; }
21.          d = length(z-a4); if (d < dist) { c = a4; dist=d; }
22.         z = Scale*z-c*(Scale-1.0);
23.         n++;
24.     }
25.
26.     return length(z) * pow(Scale, float(-n));
27. }
28.
29. float d_plane(vec3 p, vec3 e1, vec3 e2, vec3 a)
30. {
31.     e1 = normalize(e1);
32.     e2 = normalize(e2);
33.     vec3 pp = dot(p, e1) * e1 + dot(p, e2) * e2 + a;
34.     return length(p - pp);
35. }
36.
37. float d_sphere(vec3 p, vec3 c, float r)
38. {
39.     return length(p - c) - r;
40. }
41.
42. float DE(vec3 p)
43. {
44.     // return d_sphere(p, vec3(cos(time), sin(time), cos(time)), 1.);
45.     return d_sierpinski_tetrahedron(p);
46.     // return min(d_plane(p, vec3(1., 0., 0.), vec3(0., 0., 1.), vec3(0.)), d_sphere(p, vec3(cos(time), sin(time), cos(time)), 1.));
47. }
48.
49. void main()
50. {
51.     vec2 uv = (in_uv_coords - vec2(0.5, 0.5)) * 2;
52.     vec3 dir = normalize(vec3(uv, 1.));
53.     int i;
54.     float d = 0;
55.     bool ended = false;
56.     for(i = 0; i < MARCHING_ITER; i++)
57.     {
58.         vec3 p = camera + d * dir;
59.         float dd = DE(p);
60.         d += dd;
61.         if(abs(dd) < MARCHING_THRESHOLD)
62.         {
63.             ended = true;
64.             break;
65.         }
66.     }
67.
68.     if(ended) gl_FragColor = vec4(1., 1., 1., 0.) * (1. - (float(i) / float(MARCHING_ITER)));
69.     else gl_FragColor = vec4(0., 0., 0., 0.);
70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top