polyadvent

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

commit 98ebaefddb0351101304f424205d3fae7f2e314f
parent 219710e9db514203b1e080ddca5f4de8dce1d110
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 29 Apr 2018 13:02:29 -0700

happy with this for now

Diffstat:
Msrc/game.c | 3+++
Msrc/main.c | 8+++++---
Msrc/terrain.c | 15+++++++--------
Msrc/update.c | 24++++++++++++++----------
Msrc/util.c | 85++++++++++++++++++++++++++++++++++++++++---------------------------------------
5 files changed, 72 insertions(+), 63 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -32,6 +32,9 @@ void game_init(struct game *game) { mat4_id(player); mat4_id(terrain); + + terrain[14] = 20.0; + mat4_scale(player, V3(0.36,0.36,1.7), player); input_init(&game->input); diff --git a/src/main.c b/src/main.c @@ -45,11 +45,13 @@ int main(void) game_init(&game); game.terrain = &terrain; - const double size = 200; + const double size = 1000; const double pdist = 1.7; + const double scale = 0.01; /* printf("samples seed %d\n", seed); */ - const int n_samples = (int)(size * size); - struct point *samples = uniform_samples((int)(size*size), size); + const int n_samples = + (size * size) * scale*scale; + struct point *samples = uniform_samples(n_samples, size); terrain.n_samples = n_samples; /* struct point *samples = poisson_disk_samples(pdist, size, 30, &terrain.n_samples); */ diff --git a/src/terrain.c b/src/terrain.c @@ -49,16 +49,16 @@ void terrain_create(struct terrain *terrain) { u32 i; const double size = terrain->size; - const double hsize = size; - int num_verts = hsize*hsize; + float tmp1[3], tmp2[3]; - del_point2d_t *points = calloc(num_verts, sizeof(*points)); + assert(terrain->n_samples > 0); + del_point2d_t *points = calloc(terrain->n_samples, sizeof(*points)); struct perlin_settings *perlin = &terrain->settings; - float *verts = calloc(num_verts * 3, sizeof(*verts)); - float *normals = calloc(num_verts * 3, sizeof(*normals)); + + float *verts = calloc(terrain->n_samples * 3, sizeof(*verts)); + 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++) { @@ -86,7 +86,7 @@ terrain_create(struct terrain *terrain) { delaunay2d_t *del = delaunay2d_from(points, terrain->n_samples); tri_delaunay2d_t *tri = tri_delaunay2d_from(del); - num_verts = tri->num_triangles * 3; + int num_verts = tri->num_triangles * 3; float *del_verts = calloc(num_verts * 3, sizeof(*del_verts)); float *del_norms = calloc(num_verts * 3, sizeof(*del_norms)); u32 *del_indices = calloc(num_verts, sizeof(*del_indices)); @@ -151,7 +151,6 @@ terrain_create(struct terrain *terrain) { free(points); free(verts); - free(normals); free(del_verts); free(del_norms); free(del_indices); diff --git a/src/update.c b/src/update.c @@ -6,6 +6,7 @@ #include "mat4/mat4.h" #include "vec3/vec3.h" #include "poisson.h" +#include "uniform.h" void movement(struct game *game, float *obj) { float amt = 0.25; @@ -51,7 +52,6 @@ void movement(struct game *game, float *obj) { void update (struct game *game, u32 dt) { static int passed = 0; - static int last_input = 0; static int last_gen_time = 50; static float n = 1; static int first = 1; @@ -68,8 +68,10 @@ void update (struct game *game, u32 dt) { first = 0; } - if (game->input.modifiers & KMOD_LALT) + if (game->input.modifiers & KMOD_LALT) { movement(game, res->camera); + mat4_loo + } else { movement(game, res->terrain_node); /* movement(game, res->camera); */ @@ -83,10 +85,8 @@ void update (struct game *game, u32 dt) { int space_down = game->input.keystates[SDL_SCANCODE_SPACE]; int ctrl_down = game->input.modifiers & KMOD_CTRL; int now = SDL_GetTicks(); - int dinput = now - last_input; - if (dinput > 100 && space_down) { - last_input = SDL_GetTicks(); + if (space_down) { if (!stopped) { printf("terrain amp %f exp %f freq %f (%d ms)\n", ts->amplitude, @@ -114,7 +114,7 @@ void update (struct game *game, u32 dt) { for (int i = 12; i < 14; ++i) tnode[i] = max(tnode[i], 0); - tnode[14] = max(tnode[14], 20.0); + last_oz = tnode[14] = max(tnode[14], 20.0); double scale = tnode[14] * 0.01; if (scale == 0) scale = 1.0; @@ -137,16 +137,20 @@ void update (struct game *game, u32 dt) { terrain_init(game->terrain); int t1 = SDL_GetTicks(); - /* free(game->terrain->samples); */ + 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); */ + int n_samples = + (game->terrain->size * game->terrain->size) * scale*scale; + + struct point *samples = + uniform_samples(n_samples, game->terrain->size); - /* game->terrain->samples = samples; */ + game->terrain->samples = samples; + game->terrain->n_samples = n_samples; terrain_create(game->terrain); int t2 = SDL_GetTicks(); diff --git a/src/util.c b/src/util.c @@ -1,5 +1,6 @@ #include "util.h" +#include "vec3.h" #include <stdlib.h> int clampi(int a, int mina, int maxa) { @@ -43,45 +44,45 @@ double rand_0to1() { return (double) rand() / RAND_MAX; } -/* void glhLookAtf2( float *matrix, float *eyePosition3D, */ -/* float *center3D, float *upVector3D ) */ -/* { */ -/* float forward[3], side[3], up[3]; */ -/* float matrix2[16], resultMatrix[16]; */ -/* // -------------------- */ -/* forward[0] = center3D[0] - eyePosition3D[0]; */ -/* forward[1] = center3D[1] - eyePosition3D[1]; */ -/* forward[2] = center3D[2] - eyePosition3D[2]; */ -/* NormalizeVector(forward); */ -/* // -------------------- */ -/* // Side = forward x up */ -/* ComputeNormalOfPlane(side, forward, upVector3D); */ -/* NormalizeVector(side); */ -/* -------------------- */ -/* // Recompute up as: up = side x forward */ -/* ComputeNormalOfPlane(up, side, forward); */ -/* // -------------------- */ -/* matrix2[0] = side[0]; */ -/* matrix2[4] = side[1]; */ -/* matrix2[8] = side[2]; */ -/* matrix2[12] = 0.0; */ -/* // -------------------- */ -/* matrix2[1] = up[0]; */ -/* matrix2[5] = up[1]; */ -/* matrix2[9] = up[2]; */ -/* matrix2[13] = 0.0; */ -/* // -------------------- */ -/* matrix2[2] = -forward[0]; */ -/* matrix2[6] = -forward[1]; */ -/* matrix2[10] = -forward[2]; */ -/* matrix2[14] = 0.0; */ -/* // -------------------- */ -/* matrix2[3] = matrix2[7] = matrix2[11] = 0.0; */ -/* matrix2[15] = 1.0; */ -/* // -------------------- */ -/* MultiplyMatrices4by4OpenGL_FLOAT(resultMatrix, matrix, matrix2); */ -/* glhTranslatef2(resultMatrix, */ -/* -eyePosition3D[0], -eyePosition3D[1], -eyePosition3D[2]); */ -/* // -------------------- */ -/* memcpy(matrix, resultMatrix, 16*sizeof(float)); */ -/* } */ +void glhLookAtf2( float *matrix, float *eyePosition3D, + float *center3D, float *upVector3D ) +{ + float forward[3], side[3], up[3]; + float matrix2[16], resultMatrix[16]; + // -------------------- + forward[0] = center3D[0] - eyePosition3D[0]; + forward[1] = center3D[1] - eyePosition3D[1]; + forward[2] = center3D[2] - eyePosition3D[2]; + vec3_normalize(forward, forward); + // -------------------- + // Side = forward x up + ComputeNormalOfPlane(side, forward, upVector3D); + NormalizeVector(side); + -------------------- + // Recompute up as: up = side x forward + ComputeNormalOfPlane(up, side, forward); + // -------------------- + matrix2[0] = side[0]; + matrix2[4] = side[1]; + matrix2[8] = side[2]; + matrix2[12] = 0.0; + // -------------------- + matrix2[1] = up[0]; + matrix2[5] = up[1]; + matrix2[9] = up[2]; + matrix2[13] = 0.0; + // -------------------- + matrix2[2] = -forward[0]; + matrix2[6] = -forward[1]; + matrix2[10] = -forward[2]; + matrix2[14] = 0.0; + // -------------------- + matrix2[3] = matrix2[7] = matrix2[11] = 0.0; + matrix2[15] = 1.0; + // -------------------- + MultiplyMatrices4by4OpenGL_FLOAT(resultMatrix, matrix, matrix2); + glhTranslatef2(resultMatrix, + -eyePosition3D[0], -eyePosition3D[1], -eyePosition3D[2]); + // -------------------- + memcpy(matrix, resultMatrix, 16*sizeof(float)); +}