commit b380877091f7ed31136eac2701161dda4b47f124
parent ea6844914790f2c9a00fde33352c62e3fabce4a5
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 25 Apr 2018 22:01:56 -0700
terrain search
Diffstat:
5 files changed, 54 insertions(+), 15 deletions(-)
diff --git a/src/main.c b/src/main.c
@@ -42,7 +42,11 @@ int main(void)
   struct perlin_settings terrain_settings = {
     .depth = 1,
     .freq  = 0.02,
+    .o1 = 2.0, .o1s = 0.5,
+    .o2 = 4.0, .o2s = 0.25,
     .amplitude  = 1.0,
+    .ox = 0,
+    .oy = 0,
     .exp = 7.3
   };
 
diff --git a/src/terrain.c b/src/terrain.c
@@ -20,9 +20,9 @@ static const u32 plane_indices[] = {
 
 double old_noisy_boi(void *data, double x, double y) {
   struct perlin_settings *s = (struct perlin_settings*)data;
-  double e =  1.0  * perlin2d(x, y, s->freq, s->depth)
-            + 0.5  * perlin2d(2 * x, 2 * y, s->freq, s->depth)
-            + 0.25 * perlin2d(4 * x, 4 * y, s->freq, s->depth);
+  double e =  perlin2d(x, y, s->freq, s->depth)
+            + s->o1s * perlin2d(s->o1 * x, s->o1 * y, s->freq, s->depth)
+            + s->o2s * perlin2d(s->o2 * x, s->o2 * y, s->freq, s->depth);
   return pow(e, s->exp) * s->amplitude;
 }
 
@@ -48,19 +48,34 @@ terrain_create(struct terrain *terrain, struct perlin_settings *perlin) {
   const double size = 200;
   const double hsize = size/2;
   const int num_verts = hsize*hsize;
+  static int first = 1;
+  static float samples_x[40000];
+  static float samples_y[40000];
   float tmp2[3];
   del_point2d_t *points = calloc(num_verts, sizeof(*points));
   float *verts = calloc(num_verts * 3, sizeof(*verts));
   float *normals = calloc(num_verts * 3, sizeof(*normals));
+  double ox = perlin->ox;
+  double oy = perlin->oy;
 
   // 100 random samples from our noise function
   for (i = 0; i < (u32)num_verts; i++) {
     int n = i*3;
-    double dx, dy;
-    double x = rand_0to1() * size;
-    double y = rand_0to1() * size;
-    double z = old_noisy_boi((void*)perlin, x, y);
-    deriv(old_noisy_boi, (void*)perlin, x, y, z, &dx, &dy);
+    double dx, dy, x, y;
+
+    if (first) {
+      x = rand_0to1() * size;
+      y = rand_0to1() * size;
+      samples_x[i] = x;
+      samples_y[i] = y;
+    }
+    else {
+      x = samples_x[i];
+      y = samples_y[i];
+    }
+
+    double z = old_noisy_boi((void*)perlin, ox+x, oy+y);
+    deriv(old_noisy_boi, (void*)perlin, ox+x, oy+y, z, &dx, &dy);
 
     points[i].x = x;
     points[i].y = y;
@@ -81,6 +96,9 @@ terrain_create(struct terrain *terrain, struct perlin_settings *perlin) {
     normals[n+2] = tmp2[2];
   }
 
+  first = 0;
+
+
   delaunay2d_t *del = delaunay2d_from(points, num_verts);
   tri_delaunay2d_t *tri = tri_delaunay2d_from(del);
 
diff --git a/src/terrain.h b/src/terrain.h
@@ -9,6 +9,9 @@ struct perlin_settings {
   double freq;
   int depth;
   double amplitude;
+  double ox, oy;
+  double o1, o1s;
+  double o2, o2s;
   double exp;
 };
 
diff --git a/src/update.c b/src/update.c
@@ -4,6 +4,7 @@
 #include "terrain.h"
 #include "util.h"
 #include "mat4/mat4.h"
+#include "vec3/vec3.h"
 
 void movement(struct game *game, float *obj) {
   float amt = 0.25;
@@ -54,6 +55,13 @@ void update (struct game *game, u32 dt) {
   struct resources *res = &game->test_resources;
   static struct perlin_settings terrain_settings = {
     .depth = 1,
+    .freq  = 0.04,
+    .amplitude  = 1.0,
+    .ox = 0,
+    .oy = 0,
+    .o1 = 2.0, .o1s = 0.5,
+    .o2 = 4.0, .o2s = 0.25,
+    .exp = 7.3
   };
   float *light = res->light_dir;
 
@@ -68,17 +76,22 @@ void update (struct game *game, u32 dt) {
     printf("light_dir %f %f %f\n", light[0], light[1], light[2]);
   }
 
-  if (passed < last_gen_time) {
+  int space_down = game->input.keystates[SDL_SCANCODE_SPACE];
+
+  if (space_down || passed < last_gen_time) {
     passed += dt;
   } else {
     passed = 0;
 
-    terrain_settings.depth = 1;
-
-    terrain_settings.amplitude = sin(n);
-    terrain_settings.exp = fabs(sin(n) * 10.0);
-    terrain_settings.freq = fabs(cos(n) * 0.04);
-
+    terrain_settings.oy += 1.0;
+    terrain_settings.ox += 1.0;
+    /* terrain_settings.o1s = fabs(sin(1/n) * 0.25); */
+    /* terrain_settings.o1 = fabs(cos(n*0.2) * 0.5); */
+    /* terrain_settings.o2s = fabs(cos(n+2) * 0.5); */
+    /* terrain_settings.o2 = fabs(sin(n*0.02) * 2); */
+    /* terrain_settings.freq = fabs(-sin(n)*0.002 * cos(-n)) + 0.02; */
+    terrain_settings.exp = fabs(cos(n)*2.0*cos(n)) + 5.0;
+    terrain_settings.amplitude = cos(1/n)*2.0*cos(n) + 0.5;
 
     terrain_destroy(game->terrain);
     terrain_init(game->terrain);
diff --git a/src/vec3/vec3.h b/src/vec3/vec3.h
@@ -2,6 +2,7 @@ typedef float vec3;
 
 #define V3(x,y,z) ((float[3]){x,y,z})
 
+vec3 *vec3_scale(vec3 *vec, double val, vec3 *dest);
 vec3 *vec3_subtract(vec3 *vec, vec3 *vec2, vec3 *dest);
 vec3 *vec3_cross (vec3 *vec, vec3 *vec2, vec3 *dest);
 vec3 *vec3_multiply(vec3 *vec, vec3 *vec2, vec3 *dest);