Realtime Fractal Renderer Documentation
terrain.cl
Go to the documentation of this file.
1#define CAMERA_POSITIONS_LENGTH 1
2#define CAMERA_POSITIONS_ARRAY { (float4)(0, -10, 0, 0) }
3
4#define CAMERA_FACING_DIRECTIONS_LENGTH 1
5#define CAMERA_FACING_DIRECTIONS_ARRAY { (float4)(normalise((float3)(-10, -10, -10)), 0.0f) }
6
7#define SCENE_BACKGROUND_COLOUR (float3)(0.5f, 0.8f, 0.9f)
8
9#define MAXIMUM_MARCH_STEPS 300
10#define MAXIMUM_MARCH_DISTANCE 200.0f
11#define SURFACE_INTERSECTION_EPSILON 0.00001f
12
13#define CAMERA_FOCUS_DISTANCE 0.01f
14
15#define USE_BOUNDING_VOLUME true
16//#define DISPLAY_BOUNDING_VOLUME true
17//#define DO_RENDER_MARCHING_ITERATIONS true
18
19
20#define FORCE_FREE_CAMERA true
21#define CAMERA_SPEED 5.0f
22
23#include "simplexnoise1234.cl"
24#include "types.cl"
25#include "sdf.cl"
26
27#define SCALE 0.025f
28#define AMPLITUDE 10.0f
29#define ITERATIONS 3
30
31#define FREQUENCY_MULTIPLIER 2
32#define AMPLITUDE_MULTIPLIER 0.4f
33
34float getHeightAt(float x, float y)
35{
36 float frequency = SCALE;
37 float amplitude = AMPLITUDE;
38 float height = 0.0f;
39
40 for (int j = 0; j < ITERATIONS; j++)
41 {
42 height += amplitude * snoise2(x * frequency, y * frequency);
43 frequency *= FREQUENCY_MULTIPLIER;
44 amplitude *= AMPLITUDE_MULTIPLIER;
45 }
46
47 return height;
48}
49
51{
52 Light light;
53 light.ambient = (float3)(0.2f, 0.2f, 0.2f);
54 light.diffuse = (float3)(0.5f, 0.5f, 0.5f);
55 light.specular = (float3)(1.0f, 1.0f, 1.0f);
56 light.position = (float3)(0.0, -100, 0);
57
58 return light;
59}
60
61Material SDF(const float3 position, const float time, float* distance)
62{
63 // https://fileadmin.cs.lth.se/cs/Education/EDAN35/projects/16NiklasJohan_Terrain.pdf
64
65 float height = getHeightAt(position.x, position.z);
66 *distance = f_abs(position.y) - max(height, 0.0f);
67
68 float3 colour;
69
70 // Water
71 if (height <= 0.0f)
72 {
73 colour = (float3)(0, 0.05, 0.8);
74 }
75 // Land
76 else
77 {
78 // Snow
79 if (height >= 8.0f)
80 {
81 colour = (float3)(0.9, 0.9, 1);
82 }
83 // Grass
84 else
85 {
86 colour = (float3)(0.1, 0.5, 0.2);
87 }
88 }
89
90 // Material
91 Material material;
92 material.ambient = colour;
93 material.diffuse = material.ambient;
94 material.specular = (float3)(0, 0, 0);
95 material.shininess = 25.0f;
96
97 return material;
98}
99
100Material getMaterial(float3 position, float time)
101{
102 float distance;
103 return SDF(position, time, &distance);
104}
105
106float boundingVolumeDE(float3 position, float time)
107{
108 return fabs(position.y) - 12.0f;
109}
110
111float DE(float3 position, float time)
112{
113 float distance;
114 SDF(position, time, &distance);
115 return distance;
116}
117
118#include "main.cl"
const uint const uint height
Definition: main.cl:368
const uint const uint const float time
Definition: main.cl:368
float snoise2(float x, float y)
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
#define SCALE
Definition: terrain.cl:27
Light getLight(float time)
Definition: terrain.cl:50
#define FREQUENCY_MULTIPLIER
Definition: terrain.cl:31
Material SDF(const float3 position, const float time, float *distance)
Definition: terrain.cl:61
Material getMaterial(float3 position, float time)
Definition: terrain.cl:100
float boundingVolumeDE(float3 position, float time)
Definition: terrain.cl:106
float DE(float3 position, float time)
Definition: terrain.cl:111
#define ITERATIONS
Definition: terrain.cl:29
#define AMPLITUDE_MULTIPLIER
Definition: terrain.cl:32
#define AMPLITUDE
Definition: terrain.cl:28
float getHeightAt(float x, float y)
Definition: terrain.cl:34