Realtime Fractal Renderer Documentation
sdf.cl
Go to the documentation of this file.
1#ifndef SDF_CL
3#define SDF_CL
4
5#include "utils.cl"
6
7float sphereSDF(const float3 position, const float3 centre, const float radius)
8{
9 return magnitude(centre - position) - radius;
10}
11
12float boxSDF(const float3 position, const float3 centre, const float3 dimensions)
13{
14 float3 q = absolute(centre - position) - dimensions;
15 float length = magnitude((float3)(max(q.x, 0.0f), max(q.y, 0.0f), max(q.z, 0.0f)));
16 return length + min(max(q.x, max(q.y, q.z)), 0.0f);
17}
18
19float opUnion(const float d1, const float d2)
20{
21 return min(d1, d2);
22}
23
24float opSubtraction(const float d1, const float d2)
25{
26 return max(-d1, d2);
27}
28
29float opIntersection(const float d1, const float d2)
30{
31 return max(d1, d2);
32}
33
34float opSmoothUnion(const float d1, const float d2, const float k)
35{
36 float h = max(k - f_abs(d1 - d2), 0.0f);
37 return min(d1, d2) - h * h * 0.25f / k;
38}
39
40float opPolynomialSmoothUnion(const float a, const float b, const float k)
41{
42 float h = max(k - f_abs(a - b), 0.0f) / k;
43 return min(a, b) - h * h * k * (1.0f / 4.0f);
44}
45
46float opSmoothSubtraction(const float d1, const float d2, const float k)
47{
48 float h = max(k - f_abs(-d1 - d2), 0.0f);
49 return max(-d1, d2) + h * h * 0.25f / k;
50}
51
52float opSmoothIntersection(const float d1, const float d2, const float k)
53{
54 float h = max(k - f_abs(d1 - d2), 0.0f);
55 return max(d1, d2) + h * h * 0.25 / k;
56}
57
58
59bool isWithinBoundingSphere(const float3 position, const float3 sphereCentre, const float sphereRadius)
60{
61 return magnitude(sphereCentre - position) - sphereRadius <= 0;
62}
63
64
65#endif