polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit 6f28e98dcd3df22ef1bfb2428a308798123cca88
parent 055372c3c8a50c6bb18230890cd86ca5fc6441ad
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 29 Apr 2018 12:26:25 -0700

wip

Diffstat:
Msrc/game.h | 1+
Msrc/main.c | 6+++---
Msrc/render.c | 7++++++-
Msrc/render.h | 4+++-
Msrc/terrain.c | 1+
Msrc/update.c | 29+++++++++++++++++++++++------
Msrc/util.c | 4++++
Msrc/util.h | 1+
8 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/src/game.h b/src/game.h @@ -18,6 +18,7 @@ struct resources { GLint light_dir; GLint mvp; GLint local; + GLint tscale; GLint normal_matrix; } uniforms; diff --git a/src/main.c b/src/main.c @@ -49,8 +49,8 @@ int main(void) /* printf("samples seed %d\n", seed); */ /* struct point *samples = poisson_disk_samples(pdist, size, 30, &terrain.n_samples); */ struct point *samples = load_samples(NULL, &terrain.n_samples); - draw_samples(samples, pdist, terrain.n_samples, size); - save_samples(samples, seed, terrain.n_samples); + /* draw_samples(samples, pdist, terrain.n_samples, size); */ + /* save_samples(samples, seed, terrain.n_samples); */ terrain.settings = (struct perlin_settings){ .depth = 1, @@ -93,7 +93,7 @@ int main(void) update(&game, ticks-last); last = ticks; - render(&game.test_resources, &terrain.geom); + render(&game, &terrain.geom); /* Swap front and back buffers */ SDL_GL_SwapWindow(window); diff --git a/src/render.c b/src/render.c @@ -120,6 +120,9 @@ init_gl(struct resources *resources, int width, int height) { resources->uniforms.mvp = glGetUniformLocation(resources->program, "mvp"); + resources->uniforms.tscale + = glGetUniformLocation(resources->program, "tscale"); + resources->uniforms.local = glGetUniformLocation(resources->program, "local"); @@ -193,13 +196,14 @@ static void render_geom (struct resources *res, } -void render (struct resources * res, struct geometry *geom) { +void render (struct game *game, struct geometry *geom) { glClearColor( 1.0f, 1.0f, 1.0f, 1.0f ); //clear background screen to black glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); check_gl(); static float id[MAT4_ELEMS] = { 0 }; mat4_id(id); + struct resources *res = &game->test_resources; float *mvp = res->test_mvp; float *normal = res->normal_matrix; @@ -222,6 +226,7 @@ void render (struct resources * res, struct geometry *geom) { glUniform3f(res->uniforms.light_dir, light[0], light[1], light[2]); glUniform1f(res->uniforms.fade_factor, fade_factor); + glUniform1f(res->uniforms.tscale, res->uniforms.tscale); glUniformMatrix4fv(res->uniforms.mvp, 1, 0, tmp_matrix); glUniformMatrix4fv(res->uniforms.local, 1, 0, player); diff --git a/src/render.h b/src/render.h @@ -3,8 +3,10 @@ #include "geometry.h" +struct game; + void init_gl(struct resources *resources, int width, int height); -void render (struct resources *resources, struct geometry *geom); +void render (struct game *game, struct geometry *geom); void wireframe_mode_on(); void wireframe_mode_off(); diff --git a/src/terrain.c b/src/terrain.c @@ -58,6 +58,7 @@ terrain_create(struct terrain *terrain) { float *normals = calloc(num_verts * 3, sizeof(*normals)); double ox = perlin->ox * (1/perlin->scale); double oy = perlin->oy * (1/perlin->scale); + double scale = terrain->settings.scale; // 100 random samples from our noise function for (i = 0; i < (u32)terrain->n_samples; i++) { diff --git a/src/update.c b/src/update.c @@ -5,6 +5,7 @@ #include "util.h" #include "mat4/mat4.h" #include "vec3/vec3.h" +#include "poisson.h" void movement(struct game *game, float *obj) { float amt = 0.25; @@ -105,10 +106,15 @@ void update (struct game *game, u32 dt) { passed = 0; if (!stopped) { - double scale = tnode[14] * 0.02; + for (int i = 12; i < 14; ++i) + tnode[i] = max(tnode[i], 0); - double ox = min(tnode[12], 0); - double oy = min(tnode[13], 0); + tnode[14] = max(tnode[14], 20.0); + + double scale = tnode[14] * 0.01; + if (scale == 0) scale = 1.0; + double ox = tnode[12]; + double oy = tnode[13]; printf("terrain %f %f %f\n", tnode[12], tnode[13], tnode[14]); @@ -120,14 +126,25 @@ void update (struct game *game, u32 dt) { /* ts.o1 = fabs(cos(n*0.2) * 0.5); */ /* ts.o2s = fabs(cos(n+2) * 0.5); */ /* ts.o2 = fabs(sin(n*0.02) * 2); */ - /* ts->freq = 1/scale*0.2; */ - /* ts->exp = fabs(cos(n)*2.0*cos(n)) + 4.0; */ - /* ts->amplitude = fabs(cos(1/n)*cos(n) + 0.5); */ + ts->freq = scale * 0.15; + + ts->amplitude = 1/scale; terrain_destroy(game->terrain); terrain_init(game->terrain); int t1 = SDL_GetTicks(); + /* free(game->terrain->samples); */ + + /* const double pdist = min(5.0, max(1.1, 1.0/scale*1.4)); */ + + /* printf("pdist %f\n", pdist); */ + + /* struct point *samples = */ + /* poisson_disk_samples(pdist, game->terrain->size, 30, &game->terrain->n_samples); */ + + /* game->terrain->samples = samples; */ + terrain_create(game->terrain); int t2 = SDL_GetTicks(); last_gen_time = t2 - t1; diff --git a/src/util.c b/src/util.c @@ -12,6 +12,10 @@ int clampi(int a, int mina, int maxa) { return a; } +double max(double a, double b) { + return a > b ? a : b; +} + double min(double a, double b) { return a < b ? a : b; } diff --git a/src/util.h b/src/util.h @@ -10,6 +10,7 @@ void check_gl(void); int clampi(int a, int mina, int maxa); double clamp(double a, double mina, double maxa); +double max(double a, double b); double min(double a, double b); double rand_0to1();