diff --git a/apps/demos/scenery/scenes/ingame.c b/apps/demos/scenery/scenes/ingame.c index 2e3ecc2..551ae47 100644 --- a/apps/demos/scenery/scenes/ingame.c +++ b/apps/demos/scenery/scenes/ingame.c @@ -12,24 +12,17 @@ #include -static void ingame_tick(State *state) { +#define TERRAIN_FREQUENCY 0.1f +#define TERRAIN_DISTANCE 32 +#define HALF_TERRAIN_DISTANCE ((float)TERRAIN_DISTANCE / 2) +#define PLAYER_HEIGHT 0.7f + +static float heightmap[TERRAIN_DISTANCE][TERRAIN_DISTANCE]; + + +static void process_fly_mode(State *state) { SceneIngame *scn = (SceneIngame *)state->scene; - input_action("player_left", CONTROL_A); - input_action("player_right", CONTROL_D); - input_action("player_forward", CONTROL_W); - input_action("player_backward", CONTROL_S); - input_action("player_jump", CONTROL_SPACE); - input_action("player_run", CONTROL_LSHIFT); - input_action("mouse_capture_toggle", CONTROL_ESCAPE); - - if (scn->mouse_captured) { - const float sensitivity = 0.4f * (float)DEG2RAD; /* TODO: put this in a better place */ - scn->yaw += (float)ctx.mouse_movement.x * sensitivity; - scn->pitch -= (float)ctx.mouse_movement.y * sensitivity; - scn->pitch = clampf(scn->pitch, (float)-M_PI * 0.49f, (float)M_PI * 0.49f); - } - DrawCameraFromPrincipalAxesResult dir_and_up = draw_camera_from_principal_axes(scn->pos, (float)M_PI_2, scn->roll, scn->pitch, scn->yaw); @@ -52,27 +45,107 @@ static void ingame_tick(State *state) { if (input_action_pressed("player_run")) scn->pos.y -= speed; +} - /* toggle mouse capture with end key */ - if (input_action_just_pressed("mouse_capture_toggle")) - scn->mouse_captured = !scn->mouse_captured; - input_mouse_captured(scn->mouse_captured); +static void process_ground_mode(State *state) { + SceneIngame *scn = (SceneIngame *)state->scene; - #define TERRAIN_FREQUENCY 0.1f - #define TERRAIN_DISTANCE 64 + DrawCameraFromPrincipalAxesResult dir_and_up = + draw_camera_from_principal_axes(scn->pos, (float)M_PI_2, scn->roll, scn->pitch, scn->yaw); - float const half_terrain_distance = (float)TERRAIN_DISTANCE / 2; + const Vec3 right = m_vec_norm(m_vec_cross(dir_and_up.direction, dir_and_up.up)); + const float speed = 0.18f; /* TODO: put this in a better place */ - for (int ly = TERRAIN_DISTANCE; ly--;) { - for (int lx = TERRAIN_DISTANCE; lx--;) { - float x = SDL_truncf(scn->pos.x + half_terrain_distance - (float)lx); - float y = SDL_truncf(scn->pos.z + half_terrain_distance - (float)ly); + Vec3 target = scn->pos; - float d0 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6; - float d1 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6; - float d2 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6; - float d3 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6; + /* gravity */ + { + float height0, height1, height2, weight0, weight1, weight2; + + height0 = heightmap[(int)HALF_TERRAIN_DISTANCE][(int)HALF_TERRAIN_DISTANCE]; + height1 = heightmap[(int)HALF_TERRAIN_DISTANCE + 1][(int)HALF_TERRAIN_DISTANCE + 1]; + + Vec2 incell = { scn->pos.x - floorf(scn->pos.x), scn->pos.z - floorf(scn->pos.z) }; + + /* who needs barycentric coordinates, am i right? */ + weight0 = 1 / sqrtf(powf(incell.x, 2) + powf(incell.y, 2)); + weight1 = 1 / sqrtf(powf(1 - incell.x, 2) + powf(1 - incell.y, 2)); + + /* find which triangle we're directly under */ + /* for this manhattan distance is sufficient */ + if (incell.x + (1 - incell.y) < (1 - incell.x) + incell.y) { + height2 = heightmap[(int)HALF_TERRAIN_DISTANCE][(int)HALF_TERRAIN_DISTANCE + 1]; + weight2 = 1 / sqrtf(powf(incell.x, 2) + powf(1 - incell.y, 2)); + } else { + height2 = heightmap[(int)HALF_TERRAIN_DISTANCE + 1][(int)HALF_TERRAIN_DISTANCE]; + weight2 = 1 / sqrtf(powf(1 - incell.x, 2) + powf(incell.y, 2)); + } + + float height = (height0 * weight0 + height1 * weight1 + height2 * weight2) / (weight0 + weight1 + weight2); + + + if (target.y > height + PLAYER_HEIGHT) + target.y = target.y - 0.4f; + + if (target.y < height + PLAYER_HEIGHT) + target.y = height + PLAYER_HEIGHT; + + } + + /* movement */ + { + Vec3 direction = {0, 0, 0}; + + if (input_action_pressed("player_left")) + direction = m_vec_sub(direction, m_vec_scale(right, speed)); + + if (input_action_pressed("player_right")) + direction = m_vec_add(direction, m_vec_scale(right, speed)); + + if (input_action_pressed("player_forward")) + direction = m_vec_add(direction, m_vec_scale(dir_and_up.direction, speed)); + + if (input_action_pressed("player_backward")) + direction = m_vec_sub(direction, m_vec_scale(dir_and_up.direction, speed)); + + while (vec3_length(direction) > 0) { + target = m_vec_add(target, direction); + break; + } + } + + /* interpolate */ + scn->pos.x = (target.x - scn->pos.x) * (0.13f / 0.9f) + scn->pos.x; + scn->pos.y = (target.y - scn->pos.y) * (0.13f / 0.9f) + scn->pos.y; + scn->pos.z = (target.z - scn->pos.z) * (0.13f / 0.9f) + scn->pos.z; +} + + +static void generate_terrain(SceneIngame *scn) { + for (int ly = 0; ly < TERRAIN_DISTANCE; ly++) { + for (int lx = 0; lx < TERRAIN_DISTANCE; lx++) { + float x = floorf(scn->pos.x - HALF_TERRAIN_DISTANCE + (float)lx); + float y = floorf(scn->pos.z - HALF_TERRAIN_DISTANCE + (float)ly); + + float height = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 1; + + heightmap[lx][ly] = height; + } + } +} + + +static void draw_terrain(SceneIngame *scn) { + for (int ly = TERRAIN_DISTANCE - 1; ly > 0; ly--) { + for (int lx = 0; lx < TERRAIN_DISTANCE - 1; lx++) { + int32_t x = (int32_t)(floorf(scn->pos.x - HALF_TERRAIN_DISTANCE + (float)lx)); + int32_t y = (int32_t)(floorf(scn->pos.z - HALF_TERRAIN_DISTANCE + (float)ly)); + + float d0 = heightmap[lx][ly]; + float d1 = heightmap[lx + 1][ly]; + float d2 = heightmap[lx + 1][ly - 1]; + float d3 = heightmap[lx][ly - 1]; draw_triangle("/assets/grass.png", (Vec3){ (float)x, d0, (float)y }, @@ -91,6 +164,45 @@ static void ingame_tick(State *state) { (Vec2){ 0, 128 }); } } +} + + +static void ingame_tick(State *state) { + SceneIngame *scn = (SceneIngame *)state->scene; + + input_action("player_left", CONTROL_A); + input_action("player_right", CONTROL_D); + input_action("player_forward", CONTROL_W); + input_action("player_backward", CONTROL_S); + input_action("player_jump", CONTROL_SPACE); + input_action("player_run", CONTROL_LSHIFT); + input_action("mouse_capture_toggle", CONTROL_ESCAPE); + input_action("toggle_camera_mode", CONTROL_C); + + if (scn->mouse_captured) { + const float sensitivity = 0.4f * (float)DEG2RAD; /* TODO: put this in a better place */ + scn->yaw += (float)ctx.mouse_movement.x * sensitivity; + scn->pitch -= (float)ctx.mouse_movement.y * sensitivity; + scn->pitch = clampf(scn->pitch, (float)-M_PI * 0.49f, (float)M_PI * 0.49f); + } + + if (input_action_just_pressed("toggle_camera_mode")) + scn->flying_camera = !scn->flying_camera; + + if (scn->flying_camera) { + process_fly_mode(state); + } else { + process_ground_mode(state); + } + + /* toggle mouse capture with end key */ + if (input_action_just_pressed("mouse_capture_toggle")) + scn->mouse_captured = !scn->mouse_captured; + + input_mouse_captured(scn->mouse_captured); + + generate_terrain(scn); + draw_terrain(scn); draw_skybox("/assets/miramar/miramar_*.tga"); draw_fog(0.9f, 1.0f, 0.05f, (Color){ 140, 147, 160, 255 }); @@ -115,5 +227,7 @@ Scene *ingame_scene(State *state) { m_opt(channel, "soundtrack"), m_opt(repeat, true)); + new_scene->pos = (Vec3){ 0.1f, 0.0, 0.1f }; + return (Scene *)new_scene; } diff --git a/apps/demos/scenery/scenes/ingame.h b/apps/demos/scenery/scenes/ingame.h index a553971..eadec51 100644 --- a/apps/demos/scenery/scenes/ingame.h +++ b/apps/demos/scenery/scenes/ingame.h @@ -18,6 +18,7 @@ typedef struct SceneIngame { float roll; bool mouse_captured; + bool flying_camera; } SceneIngame; diff --git a/include/twn_vec.h b/include/twn_vec.h index ccf74d5..83e626a 100644 --- a/include/twn_vec.h +++ b/include/twn_vec.h @@ -33,6 +33,10 @@ static inline float vec3_dot(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; } +static inline float vec3_length(Vec3 a) { + return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z); +} + static inline Vec3 vec3_cross(Vec3 a, Vec3 b) { return (Vec3) { a.y * b.z - a.z * b.y, @@ -78,6 +82,10 @@ static inline Vec3 vec3_rotate(Vec3 v, float angle, Vec3 axis) { Vec2i: vec2_from_vec2i, \ )(p_any_vec2)) +#define m_vec_add(p_any_vec0, p_any_vec1) (_Generic((p_any_vec0), \ + Vec3: vec3_add \ + )(p_any_vec0, p_any_vec1)) + #define m_vec_sub(p_any_vec0, p_any_vec1) (_Generic((p_any_vec0), \ Vec3: vec3_sub \ )(p_any_vec0, p_any_vec1))