Realtime Fractal Renderer Documentation
sierpinski.cl
Go to the documentation of this file.
1#include "sdf.cl"
2#include "types.cl"
3
4Material sierpinskiTetrahedronSDF(float3 position, int iterations, float bailout, float* distance)
5{
6 // http://www.fractalforums.com/sierpinski-gasket/kaleidoscopic-(escape-time-ifs)/
7
8 float r = position.x * position.x + position.y * position.y + position.z * position.z;
9 float x = position.x;
10 float y = position.y;
11 float z = position.z;
12
13 int i;
14 for (i = 0; i < iterations && r < bailout; i++) {
15 //Folding... These are some of the symmetry planes of the tetrahedron
16 if (x + y < 0)
17 {
18 float x1 = -y;
19 y = -x;
20 x = x1;
21 }
22 if (x + z < 0)
23 {
24 float x1 = -z;
25 z = -x;
26 x = x1;
27 }
28 if (y + z < 0)
29 {
30 float y1 = -z;
31 z = -y;
32 y = y1;
33 }
34
35 //Stretche about the point [1,1,1]*(SCALE-1)/SCALE; The "(SCALE-1)/SCALE" is here in order to keep the size of the fractal constant wrt SCALE
36 x = 2.0f * x - 1.0f; //equivalent to: x=SCALE*(x-cx); where cx=(SCALE-1)/SCALE;
37 y = 2.0f * y - 1.0f;
38 z = 2.0f * z - 1.0f;
39 r = x * x + y * y + z * z;
40 }
41
42 *distance = (sqrt(r) - 2.0f) * pow(2.0f, -i);
43
44 // Material
45 Material material;
46 material.ambient = (float3)(0.5f, 0.5f, 0.5f);
47 material.diffuse = material.ambient;
48 material.specular = (float3)(0.5f, 0.5f, 0.5f);
49 material.shininess = 25.0f;
50
51 return material;
52}
53
54Material sierpinskiCubeSDF(float3 position, int iterations, float* distance)
55{
56 // https://www.iquilezles.org/www/articles/menger/menger.htm
57
58 float d = boxSDF(position, (float3)(1), (float3)(1));
59 float3 res = (float3)(0);
60
61 float s = 1.0f;
62 for (int m = 0; m < iterations; m++)
63 {
64 float3 a = mod(position * s, 2.0f) - 1.0f;
65 s *= 3.0f;
66 float3 r = absolute(1.0f - 3.0f * absolute(a));
67
68 float da = max(r.x, r.y);
69 float db = max(r.y, r.z);
70 float dc = max(r.z, r.x);
71 float c = (min(da, min(db, dc)) - 1.0f) / s;
72
73 if (c > d)
74 {
75 d = c;
76 res = (float3)(d, 0.2f * da * db * dc, (1.0f + (float)(m)) / 4.0f);
77 }
78 }
79
80 *distance = d;
81
82 // Material
83 Material material;
84 material.ambient = res;
85 material.diffuse = material.ambient;
86 material.specular = (float3)(0.5f, 0.5f, 0.5f);
87 material.shininess = 25.0f;
88
89 return material;
90}
A struct representing a geometry material, for use with the Phong reflection model.
Definition: types.cl:20
float3 ambient
Definition: types.cl:21
float3 diffuse
Definition: types.cl:22
float shininess
Definition: types.cl:24
float3 specular
Definition: types.cl:23