Realtime Fractal Renderer Documentation
mandelbrot.cl
Go to the documentation of this file.
1#include "utils.cl"
2
3#ifndef DISABLE_FREE_CAMERA_MANDELBROT
4#define DISABLE_FREE_CAMERA_MANDELBROT true
5#endif
6
7#if DISABLE_FREE_CAMERA_MANDELBROT
8
9#define CAMERA_POSITIONS_LENGTH 1
10#define CAMERA_POSITIONS_ARRAY { (float4)(0, -1.5, 0, 0) }
11#define CAMERA_FACING_DIRECTIONS_LENGTH 1
12#define CAMERA_FACING_DIRECTIONS_ARRAY { (float4)(normalise((float3)(-0.7, -0.3, -0.7)), 0) }
13#define CAMERA_SPEED 0.1f
14
15#endif
16
17#define MAXIMUM_MARCH_STEPS 100
18#define MAXIMUM_MARCH_DISTANCE 50.0f
19#define SURFACE_INTERSECTION_EPSILON 0.001f
20#define CAMERA_FOCUS_DISTANCE 0.0001f
21#define SCENE_BACKGROUND_COLOUR (float3)(0.1f, 0.1f, 0.1f)
22
23#define DO_AMBIENT_LIGHTING true
24#define DO_DIFFUSE_LIGHTING false
25#define DO_SPECULAR_HIGHLIGHTS false
26
27#include "types.cl"
28#include "sdf.cl"
29
30
31
32#ifndef ITERATIONS
33#define ITERATIONS 500
34#endif
35
36#ifndef ESCAPE
37#define ESCAPE 4
38#endif
39
40#ifndef SCALE
41#define SCALE 0.5f
42#endif
43
44float3 getMandelbrot(float2 position)
45{
46 // http://www.fractalforums.com/3d-fractal-generation/true-3d-mandlebrot-type-fractal/msg8505/#msg8505
47
48 float x_temp;
49 float dx_temp;
50 float r, dr, dist;
51 float escape;
52
53 //Initialize iteration variables
54 float x0 = position.x;
55 float y0 = position.y;
56 float x = 0.0f;
57 float y = 0.0f;
58 float dx = 0.0f;
59 float dy = 0.0f;
60
61 int i;
62 for (i = 0; i < ITERATIONS && x * x + y * y < ESCAPE; i++)
63 {
64 //Update z'
65 dx_temp = 2.0f * (x * dx - y * dy) + 1.0f;
66 dy = 2.0f * (x * dy + y * dx);
67 dx = dx_temp;
68
69 //Update z
70 x_temp = x * x - y * y + x0;
71 y = 2.0f * x * y + y0;
72 x = x_temp;
73 }
74
75 // Calculate distance
76 //r = sqrt(x * x + y * y);
77 //dr = sqrt(dx * dx + dy * dy);
78 //dist = 0.5f * r * log(r) / dr;
79
80 float3 colour;
81 // Outside the mandelbrot
82 if (i < ITERATIONS)
83 {
84 colour = (float3)(0.75f, 0.0f, 0.0f);
85 }
86 // Inside the mandelbrot
87 else
88 {
89 colour = (float3)(0);
90 }
91
92 return colour;
93}
94
95
96
98{
99 Light light;
100 light.ambient = (float3)(1.0f, 1.0f, 1.0f);
101 light.diffuse = (float3)(0);
102 light.specular = (float3)(0);
103 light.position = (float3)(0, -1, 0);
104
105 return light;
106}
107
108#if DISABLE_FREE_CAMERA_MANDELBROT
109
110float2 getSamplePoint(float x, float y, float time)
111{
112 return (float2)(x, y) * SCALE;
113}
114
115#endif
116
117Material getMaterial(float3 position, float time)
118{
119 // Material
120 Material material;
121 material.ambient = getMandelbrot(getSamplePoint(position.x, position.z, time));
122 material.diffuse = material.ambient;
123 material.specular = (float3)(0.5f, 0.5f, 0.5f);
124 material.shininess = 25.0f;
125
126 return material;
127}
128
129float DE(float3 position, float time)
130{
131 // Distance estimation
132 return f_abs(position.y);
133}
134
135#include "main.cl"
const uint const uint const float time
Definition: main.cl:368
#define SCALE
Definition: mandelbrot.cl:41
Light getLight(float time)
Definition: mandelbrot.cl:97
Material getMaterial(float3 position, float time)
Definition: mandelbrot.cl:117
float DE(float3 position, float time)
Definition: mandelbrot.cl:129
#define ITERATIONS
Definition: mandelbrot.cl:33
float3 getMandelbrot(float2 position)
Definition: mandelbrot.cl:44
#define ESCAPE
Definition: mandelbrot.cl:37
float2 getSamplePoint(float x, float y, float time)
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