commit d060d81b5c06a268849ce6c209c887649578e66d
parent 0b9c14b88423f086211d83468247baf2f44bacdd
Author: Christian Ermann <christianermann@gmail.com>
Date: Mon, 30 May 2022 18:13:39 -0700
Moved SDFs out of main.c.
Diffstat:
3 files changed, 55 insertions(+), 46 deletions(-)
diff --git a/include/sdf.h b/include/sdf.h
@@ -5,4 +5,9 @@
typedef float (*SDF)(const Vec3 p);
+float perlinSDF(const Vec3 p);
+float caveSDF(const Vec3 p);
+float terrainSDF(const Vec3 p);
+float sphereSDF(const Vec3 p);
+
#endif
diff --git a/src/main.c b/src/main.c
@@ -14,51 +14,6 @@
#include <math.h>
#include <stdio.h>
-float perlinSDF(const Vec3 p)
-{
- return perlin(p[0] * 0.01f, p[1] * 0.01f, p[2] * 0.01f)
- + 0.5f * perlin(p[0] * 0.05f, p[1] * 0.05f, p[2] * 0.05f)
- + 0.1f * perlin(p[0] * 0.1f, p[1] * 0.1f, p[2] * 0.1f);
-}
-
-float caveSDF(const Vec3 p)
-{
- float o1 = perlin(p[0] * 0.01f, p[1] * 0.01f, p[2] * 0.01f);
-
- return o1;
-}
-
-float terrainSDF(const Vec3 p)
-{
- float o1 = 50.0f * perlin(p[0] * 0.005f, 0.0f, p[2] * 0.005f);
- float o2 = 2.0f * perlin(p[0] * 0.050f, 0.0f, p[2] * 0.050f);
- float o3 = 1.0f * perlin(p[0] * 0.100f, 0.0f, p[2] * 0.100f);
-
- float surface = p[1] - o1 - o2 - o3;
- float cave = perlinSDF(p);
-
- float k = 32.0f;
- float h = 0.5f - 0.5f * (cave - surface) / k;
- if (h < 0.0f)
- {
- h = 0.0f;
- }
- if (h > 1.0f)
- {
- h = 1.0f;
- }
-
- float a = cave * (1.0f - h) + surface * h;
- float b = k * h * (1.0f - h);
-
- return a + b;
-}
-
-float sphereSDF(const float p[3])
-{
- return sqrtf(p[0] * p[0] + p[1] * p[1] + p[2] * p[2]) - 25.0f;
-}
-
int main(int argc, char** argv)
{
AppInfo app_info = {
@@ -73,7 +28,7 @@ int main(int argc, char** argv)
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
-
+
UserInput input = { 0 };
UserInput_init(&input, app->window);
diff --git a/src/sdf.c b/src/sdf.c
@@ -0,0 +1,49 @@
+#include "perlin.h"
+#include "sdf.h"
+
+#include <math.h>
+
+float perlinSDF(const Vec3 p)
+{
+ return perlin(p[0] * 0.01f, p[1] * 0.01f, p[2] * 0.01f)
+ + 0.5f * perlin(p[0] * 0.05f, p[1] * 0.05f, p[2] * 0.05f)
+ + 0.1f * perlin(p[0] * 0.1f, p[1] * 0.1f, p[2] * 0.1f);
+}
+
+float caveSDF(const Vec3 p)
+{
+ float o1 = perlin(p[0] * 0.01f, p[1] * 0.01f, p[2] * 0.01f);
+
+ return o1;
+}
+
+float terrainSDF(const Vec3 p)
+{
+ float o1 = 50.0f * perlin(p[0] * 0.005f, 0.0f, p[2] * 0.005f);
+ float o2 = 2.0f * perlin(p[0] * 0.050f, 0.0f, p[2] * 0.050f);
+ float o3 = 1.0f * perlin(p[0] * 0.100f, 0.0f, p[2] * 0.100f);
+
+ float surface = p[1] - o1 - o2 - o3;
+ float cave = perlinSDF(p);
+
+ float k = 32.0f;
+ float h = 0.5f - 0.5f * (cave - surface) / k;
+ if (h < 0.0f)
+ {
+ h = 0.0f;
+ }
+ if (h > 1.0f)
+ {
+ h = 1.0f;
+ }
+
+ float a = cave * (1.0f - h) + surface * h;
+ float b = k * h * (1.0f - h);
+
+ return a + b;
+}
+
+float sphereSDF(const float p[3])
+{
+ return sqrtf(p[0] * p[0] + p[1] * p[1] + p[2] * p[2]) - 25.0f;
+}