Realtime Fractal Renderer Documentation
klein_bottle.cl
Go to the documentation of this file.
1#include "utils.cl"
2#include "types.cl"
3#include "sdf.cl"
4
5#define CAMERA_POSITIONS_LENGTH 1
6#define CAMERA_POSITIONS_ARRAY { (float4)(5, 0, 0, 0) }
7
8#define CAMERA_FACING_DIRECTIONS_LENGTH 1
9#define CAMERA_FACING_DIRECTIONS_ARRAY { (float4)(normalise((float3)(-1, 0, 0)), 0) }
10
11#define CAMERA_SPEED 2.5f
12
13#define SCENE_BACKGROUND_COLOUR (float3)(0.3f, 0.3f, 0.3f)
14#define DO_SOFT_SHADOWS true
15
16#define MAXIMUM_MARCH_STEPS 256
17#define MAXIMUM_MARCH_DISTANCE 15.0f
18#define SURFACE_INTERSECTION_EPSILON 0.0001f
19
20#define DO_RENDER_SURFACE_NORMALS false
21#define DO_RENDER_MARCHING_ITERATIONS false
22#define DISPLAY_BOUNDING_VOLUME false
23
25{
26 Light light;
27 light.ambient = (float3)(0.1f, 0.1f, 0.1f);
28 light.diffuse = (float3)(0.6f, 0.6f, 0.6f);
29 light.specular = (float3)(1.0f, 1.0f, 1.0f);
30 light.position = (float3)(5.0f, -5.0f, 5.0f);
31
32 return light;
33}
34
35float len(const float2 vec)
36{
37 return sqrt(vec.x * vec.x + vec.y * vec.y);
38}
39
40Material SDF(const float3 position, const float time, float* distance)
41{
42 // https://www.shadertoy.com/view/4ltSW8
43
44 float3 p = position;
45
46 // thickness
47 float t = 0.02f;
48 float d = 1e10;
49
50 //p.y += .5;
51 //p.xy *= rot(PI / 2.);
52
53 float3 q = p + (float3)(1.0f - cos((1.0f - p.y) / 3.0f * PI), 0.0f, 0.0f);
54 float y = pow(sin((1. - p.y) / 3. * PI / 2.), 2.);
55
56 float tube_hollow = max(max(f_abs(len((float2)(q.x, q.z)) - 0.5f + 0.25f * y) - t, q.y - 1.0f), -q.y - 2.0f);
57 float tube_solid = max(max(len((float2)(q.x, q.z)) - 0.5f + 0.25f * y, q.y - 1.0f), -q.y - 2.0f);
58
59 // opening (half XZ torus)
60 q = p - (float3)(0.0f, 1.0f, 0.0f);
61 d = min(d, max(f_abs(len((float2)(len((float2)(q.x, q.z)) - 1.0f, q.y)) - 0.5f) - t, -q.y));
62
63 // body (stretched XZ torus)
64 q = p;
65 d = min(d, max(max(max(f_abs(len((float2)(q.x, q.z)) - 1.5f + 1.25f * y), q.y - 1.0f), -q.y - 2.0f) - t, -tube_solid));
66
67 // tube (stretched XZ cylinder)
68 d = min(d, tube_hollow);
69
70 // handle (half XY torus)
71 q = p + (float3)(1.0f, 2.0f, 0.0f);
72 d = min(d, max(f_abs(len((float2)(len((float2)(q.x, q.y)) - 1.0f, q.z)) - 0.25f) - t, q.y));
73
74 // Distance estimation
75 *distance = d;
76
77 // Material
78 Material material;
79 float time_scaled = time * 0.5f;
80 material.ambient = ((float3)(sin(time_scaled), sin(time_scaled + 0.666f), sin(time_scaled + 1.333f)) + 1) / 2.0f;
81 material.diffuse = material.ambient;
82 material.specular = (float3)(0.5f, 0.5f, 0.5f);
83 material.shininess = 25.0f;
84
85 return material;
86}
87
88Material getMaterial(float3 position, float time)
89{
90 float distance;
91 return SDF(position, time, &distance);
92}
93
94float DE(float3 position, float time)
95{
96 float distance;
97 SDF(position, time, &distance);
98 return distance;
99}
100
101#include "main.cl"
Light getLight(float time)
Definition: klein_bottle.cl:24
Material SDF(const float3 position, const float time, float *distance)
Definition: klein_bottle.cl:40
Material getMaterial(float3 position, float time)
Definition: klein_bottle.cl:88
float DE(float3 position, float time)
Definition: klein_bottle.cl:94
float len(const float2 vec)
Definition: klein_bottle.cl:35
const uint const uint const float time
Definition: main.cl:368
A struct representing a light, for use with the phong illumination model.
Definition: types.cl:32
float3 ambient
Definition: types.cl:34
float3 position
Definition: types.cl:33
float3 diffuse
Definition: types.cl:35
float3 specular
Definition: types.cl:36
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