2024-07-08 00:44:20 +00:00
|
|
|
#include "ingame.h"
|
|
|
|
#include "scene.h"
|
|
|
|
|
2024-09-16 13:17:00 +00:00
|
|
|
#include "twn_game_api.h"
|
2024-10-07 15:37:44 +00:00
|
|
|
#include "twn_vec.h"
|
2024-08-19 16:19:22 +00:00
|
|
|
|
|
|
|
#define STB_PERLIN_IMPLEMENTATION
|
|
|
|
#include <stb_perlin.h>
|
2024-10-06 21:00:36 +00:00
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2024-07-08 06:46:12 +00:00
|
|
|
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2025-01-14 21:30:46 +00:00
|
|
|
#define TERRAIN_FREQUENCY 0.15f
|
2025-02-26 12:53:59 +00:00
|
|
|
#define TERRAIN_RADIUS 128
|
|
|
|
#define GRASS_RADIUS 16
|
|
|
|
#define TERRAIN_DISTANCE (TERRAIN_RADIUS * 2)
|
2025-01-02 10:26:16 +00:00
|
|
|
#define HALF_TERRAIN_DISTANCE ((float)TERRAIN_DISTANCE / 2)
|
2025-01-05 18:08:54 +00:00
|
|
|
#define PLAYER_HEIGHT 0.6f
|
2025-02-26 14:08:45 +00:00
|
|
|
#define TREE_DENSITY 0.03f
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2025-02-26 12:53:59 +00:00
|
|
|
/* TODO: pregenerate grid of levels of detail */
|
2025-01-02 10:26:16 +00:00
|
|
|
static float heightmap[TERRAIN_DISTANCE][TERRAIN_DISTANCE];
|
2024-10-22 17:32:17 +00:00
|
|
|
|
2025-01-02 10:26:16 +00:00
|
|
|
|
|
|
|
static void process_fly_mode(State *state) {
|
|
|
|
SceneIngame *scn = (SceneIngame *)state->scene;
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-10-28 09:34:48 +00:00
|
|
|
DrawCameraFromPrincipalAxesResult dir_and_up =
|
2025-02-26 13:17:44 +00:00
|
|
|
draw_camera_from_principal_axes(scn->pos, scn->roll, scn->pitch, scn->yaw, (float)M_PI_2 * 0.8f, 1, TERRAIN_RADIUS * sqrtf(3));
|
2024-10-28 09:34:48 +00:00
|
|
|
|
2025-02-26 14:08:45 +00:00
|
|
|
scn->looking_direction = dir_and_up.direction;
|
|
|
|
|
2024-10-28 09:34:48 +00:00
|
|
|
const Vec3 right = m_vec_norm(m_vec_cross(dir_and_up.direction, dir_and_up.up));
|
2024-07-30 21:05:28 +00:00
|
|
|
const float speed = 0.04f; /* TODO: put this in a better place */
|
2024-10-29 09:25:24 +00:00
|
|
|
if (input_action_pressed("player_left"))
|
2024-10-28 09:34:48 +00:00
|
|
|
scn->pos = vec3_sub(scn->pos, m_vec_scale(right, speed));
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-10-29 09:25:24 +00:00
|
|
|
if (input_action_pressed("player_right"))
|
2024-10-28 09:34:48 +00:00
|
|
|
scn->pos = vec3_add(scn->pos, m_vec_scale(right, speed));
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-10-29 09:25:24 +00:00
|
|
|
if (input_action_pressed("player_forward"))
|
2024-10-28 09:34:48 +00:00
|
|
|
scn->pos = vec3_add(scn->pos, m_vec_scale(dir_and_up.direction, speed));
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-10-29 09:25:24 +00:00
|
|
|
if (input_action_pressed("player_backward"))
|
2024-10-28 09:34:48 +00:00
|
|
|
scn->pos = vec3_sub(scn->pos, m_vec_scale(dir_and_up.direction, speed));
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-10-29 09:25:24 +00:00
|
|
|
if (input_action_pressed("player_jump"))
|
2024-10-28 09:34:48 +00:00
|
|
|
scn->pos.y += speed;
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-10-29 09:25:24 +00:00
|
|
|
if (input_action_pressed("player_run"))
|
2024-10-28 09:34:48 +00:00
|
|
|
scn->pos.y -= speed;
|
2025-01-02 10:26:16 +00:00
|
|
|
}
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-10-22 17:32:17 +00:00
|
|
|
|
2025-01-06 12:34:12 +00:00
|
|
|
static float height_at(SceneIngame *scn, Vec2 position) {
|
|
|
|
float height0, height1, height2, weight0, weight1, weight2;
|
|
|
|
|
|
|
|
int const x = (int)(HALF_TERRAIN_DISTANCE + (position.x - scn->pos.x));
|
|
|
|
int const y = (int)(HALF_TERRAIN_DISTANCE + (position.y - scn->pos.z));
|
|
|
|
|
|
|
|
height0 = heightmap[x][y];
|
|
|
|
height1 = heightmap[x + 1][y + 1];
|
|
|
|
|
|
|
|
Vec2 incell = { position.x - floorf(position.x), position.y - floorf(position.y) };
|
|
|
|
|
|
|
|
/* 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[x][y + 1];
|
|
|
|
weight2 = 1 / sqrtf(powf(incell.x, 2) + powf(1 - incell.y, 2));
|
|
|
|
} else {
|
|
|
|
height2 = heightmap[x + 1][y];
|
|
|
|
weight2 = 1 / sqrtf(powf(1 - incell.x, 2) + powf(incell.y, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (height0 * weight0 + height1 * weight1 + height2 * weight2) / (weight0 + weight1 + weight2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-01-02 10:26:16 +00:00
|
|
|
static void process_ground_mode(State *state) {
|
|
|
|
SceneIngame *scn = (SceneIngame *)state->scene;
|
|
|
|
|
|
|
|
DrawCameraFromPrincipalAxesResult dir_and_up =
|
2025-02-26 13:17:44 +00:00
|
|
|
draw_camera_from_principal_axes(scn->pos, scn->roll, scn->pitch, scn->yaw, (float)M_PI_2 * 0.8f, 1, TERRAIN_RADIUS * sqrtf(3));
|
2025-01-02 10:26:16 +00:00
|
|
|
|
2025-02-26 14:08:45 +00:00
|
|
|
scn->looking_direction = dir_and_up.direction;
|
|
|
|
|
2025-01-06 12:38:40 +00:00
|
|
|
dir_and_up.direction.y = 0;
|
|
|
|
dir_and_up.direction = vec3_norm(dir_and_up.direction);
|
|
|
|
|
2025-01-02 10:26:16 +00:00
|
|
|
const Vec3 right = m_vec_norm(m_vec_cross(dir_and_up.direction, dir_and_up.up));
|
2025-02-26 14:22:02 +00:00
|
|
|
const float speed = 0.20f; /* TODO: put this in a better place */
|
2025-01-02 10:26:16 +00:00
|
|
|
|
|
|
|
Vec3 target = scn->pos;
|
|
|
|
|
|
|
|
/* gravity */
|
|
|
|
{
|
2025-01-06 12:34:12 +00:00
|
|
|
float const height = height_at(scn, (Vec2){scn->pos.x, scn->pos.z});
|
2025-01-02 10:26:16 +00:00
|
|
|
|
|
|
|
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));
|
2024-07-29 12:21:39 +00:00
|
|
|
|
2025-01-02 10:26:16 +00:00
|
|
|
if (input_action_pressed("player_forward"))
|
|
|
|
direction = m_vec_add(direction, m_vec_scale(dir_and_up.direction, speed));
|
2024-08-19 16:19:22 +00:00
|
|
|
|
2025-01-02 10:26:16 +00:00
|
|
|
if (input_action_pressed("player_backward"))
|
|
|
|
direction = m_vec_sub(direction, m_vec_scale(dir_and_up.direction, speed));
|
2024-11-03 20:09:10 +00:00
|
|
|
|
2025-01-02 10:35:13 +00:00
|
|
|
target = m_vec_add(target, direction);
|
2025-01-02 10:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2025-02-26 12:53:59 +00:00
|
|
|
height += stb_perlin_noise3((float)x * TERRAIN_FREQUENCY / 10, (float)y * TERRAIN_FREQUENCY / 10, 0, 0, 0, 0) * 20 - 1;
|
2025-01-02 10:26:16 +00:00
|
|
|
|
|
|
|
heightmap[lx][ly] = height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-01 12:31:18 +00:00
|
|
|
|
2025-01-02 10:26:16 +00:00
|
|
|
|
2025-02-26 12:53:59 +00:00
|
|
|
static int32_t ceil_sqrt(int32_t const n) {
|
|
|
|
int32_t res = 1;
|
|
|
|
while(res * res < n)
|
|
|
|
res++;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-26 13:17:44 +00:00
|
|
|
static uint32_t adler32(const void *buf, size_t buflength) {
|
|
|
|
const uint8_t *buffer = (const uint8_t*)buf;
|
|
|
|
|
|
|
|
uint32_t s1 = 1;
|
|
|
|
uint32_t s2 = 0;
|
|
|
|
|
|
|
|
for (size_t n = 0; n < buflength; n++) {
|
|
|
|
s1 = (s1 + buffer[n]) % 65521;
|
|
|
|
s2 = (s2 + s1) % 65521;
|
|
|
|
}
|
|
|
|
return (s2 << 16) | s1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-01-02 10:26:16 +00:00
|
|
|
static void draw_terrain(SceneIngame *scn) {
|
2025-02-26 14:08:45 +00:00
|
|
|
/* used to cull invisible tiles over field of view (to horizon) */
|
|
|
|
Vec2 const d = vec2_norm((Vec2){ .x = scn->looking_direction.x, .y = scn->looking_direction.z });
|
|
|
|
float const c = cosf((float)M_PI_2 * 0.8f);
|
|
|
|
|
2025-02-26 12:53:59 +00:00
|
|
|
/* draw terrain in circle */
|
|
|
|
int32_t const rsi = (int32_t)TERRAIN_RADIUS * (int32_t)TERRAIN_RADIUS;
|
|
|
|
for (int32_t iy = -(int32_t)TERRAIN_RADIUS; iy <= (int32_t)TERRAIN_RADIUS - 1; ++iy) {
|
|
|
|
int32_t const dx = ceil_sqrt(rsi - (iy + (iy <= 0)) * (iy + (iy <= 0)));
|
|
|
|
for (int32_t ix = -dx; ix < dx; ++ix) {
|
|
|
|
int32_t lx = ix + TERRAIN_RADIUS;
|
|
|
|
int32_t ly = iy + TERRAIN_RADIUS;
|
|
|
|
|
2025-02-26 14:08:45 +00:00
|
|
|
float x = (float)(floorf(scn->pos.x - HALF_TERRAIN_DISTANCE + (float)lx));
|
|
|
|
float y = (float)(floorf(scn->pos.z - HALF_TERRAIN_DISTANCE + (float)ly));
|
|
|
|
|
|
|
|
/* cull tiles outside of vision */
|
|
|
|
if (vec2_dot(vec2_norm((Vec2){x - scn->pos.x + d.x * 2, y - scn->pos.z + d.y * 2}), d) < c)
|
|
|
|
continue;
|
2025-01-02 10:26:16 +00:00
|
|
|
|
|
|
|
float d0 = heightmap[lx][ly];
|
|
|
|
float d1 = heightmap[lx + 1][ly];
|
|
|
|
float d2 = heightmap[lx + 1][ly - 1];
|
|
|
|
float d3 = heightmap[lx][ly - 1];
|
2024-07-30 16:36:59 +00:00
|
|
|
|
2025-02-26 08:28:59 +00:00
|
|
|
draw_quad("/assets/grass.png",
|
2024-09-23 17:43:16 +00:00
|
|
|
(Vec3){ (float)x, d0, (float)y },
|
|
|
|
(Vec3){ (float)x + 1, d1, (float)y },
|
|
|
|
(Vec3){ (float)x + 1, d2, (float)y - 1 },
|
|
|
|
(Vec3){ (float)x, d3, (float)y - 1 },
|
2025-02-26 08:28:59 +00:00
|
|
|
(Rect){ .w = 128, .h = 128 },
|
2025-01-07 11:14:21 +00:00
|
|
|
(Color){255, 255, 255, 255});
|
2025-02-26 13:17:44 +00:00
|
|
|
|
|
|
|
if (((float)(adler32(&((Vec2){x, y}), sizeof (Vec2)) % 100) / 100) <= TREE_DENSITY)
|
|
|
|
draw_billboard("/assets/trreez.png",
|
|
|
|
(Vec3){ (float)x, d0 + 2.f, (float)y },
|
|
|
|
(Vec2){2.f, 2.f},
|
|
|
|
(Rect){0},
|
|
|
|
(Color){255, 255, 255, 255}, true);
|
|
|
|
}
|
2025-02-26 12:53:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t const rsi_g = (int32_t)GRASS_RADIUS * (int32_t)GRASS_RADIUS;
|
|
|
|
for (int32_t iy = -(int32_t)GRASS_RADIUS; iy <= (int32_t)GRASS_RADIUS - 1; ++iy) {
|
|
|
|
int32_t const dx = ceil_sqrt(rsi_g - (iy + (iy <= 0)) * (iy + (iy <= 0)));
|
|
|
|
for (int32_t ix = -dx; ix < dx; ++ix) {
|
|
|
|
int32_t lx = ix + TERRAIN_RADIUS;
|
|
|
|
int32_t ly = iy + TERRAIN_RADIUS;
|
|
|
|
|
2025-02-26 14:08:45 +00:00
|
|
|
float x = (float)(floorf(scn->pos.x - HALF_TERRAIN_DISTANCE + (float)lx));
|
|
|
|
float y = (float)(floorf(scn->pos.z - HALF_TERRAIN_DISTANCE + (float)ly));
|
2025-02-26 12:53:59 +00:00
|
|
|
|
|
|
|
float d0 = heightmap[lx][ly];
|
2025-01-05 16:46:05 +00:00
|
|
|
|
2025-01-06 12:34:12 +00:00
|
|
|
draw_billboard("/assets/grasses/10.png",
|
|
|
|
(Vec3){ (float)x, d0 + 0.15f, (float)y },
|
|
|
|
(Vec2){0.3f, 0.3f},
|
2025-02-20 13:19:03 +00:00
|
|
|
(Rect){0},
|
2025-01-06 12:34:12 +00:00
|
|
|
(Color){255, 255, 255, 255}, true);
|
2024-07-30 16:36:59 +00:00
|
|
|
}
|
2024-08-19 16:19:22 +00:00
|
|
|
}
|
2025-01-02 10:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void ingame_tick(State *state) {
|
|
|
|
SceneIngame *scn = (SceneIngame *)state->scene;
|
|
|
|
|
2025-02-04 04:32:25 +00:00
|
|
|
input_action("player_left", "A");
|
|
|
|
input_action("player_right", "D");
|
|
|
|
input_action("player_forward", "W");
|
|
|
|
input_action("player_backward", "S");
|
|
|
|
input_action("player_jump", "SPACE");
|
|
|
|
input_action("player_run", "LSHIFT");
|
|
|
|
input_action("mouse_capture_toggle", "ESCAPE");
|
|
|
|
input_action("toggle_camera_mode", "C");
|
2025-01-02 10:26:16 +00:00
|
|
|
|
2025-02-14 16:51:34 +00:00
|
|
|
// draw_model("models/test.obj", (Vec3){0}, (Vec3){0,0,1}, (Vec3){1.f / 64,1.f / 64,1.f / 64});
|
|
|
|
// draw_model("models/test2.obj", (Vec3){0}, (Vec3){0,0,1}, (Vec3){1.f / 64,1.f / 64,1.f / 64});
|
|
|
|
// draw_model("models/bunny.obj", (Vec3){0}, (Vec3){0,0,1}, (Vec3){4.,4.,4.});
|
2025-02-07 07:19:36 +00:00
|
|
|
|
2025-01-02 10:26:16 +00:00
|
|
|
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;
|
|
|
|
|
2025-01-13 21:28:21 +00:00
|
|
|
ctx.mouse_capture = scn->mouse_captured;
|
2025-01-02 10:26:16 +00:00
|
|
|
|
|
|
|
generate_terrain(scn);
|
|
|
|
draw_terrain(scn);
|
2024-09-26 18:02:56 +00:00
|
|
|
|
2025-01-05 16:59:23 +00:00
|
|
|
draw_skybox("/assets/miramar/miramar_*.tga");
|
2025-01-14 20:20:54 +00:00
|
|
|
|
|
|
|
ctx.fog_color = (Color){ 140, 147, 160, 255 };
|
2025-02-26 14:08:45 +00:00
|
|
|
ctx.fog_density = 0.015f;
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
static void ingame_end(State *state) {
|
2024-07-27 22:44:39 +00:00
|
|
|
free(state->scene);
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
Scene *ingame_scene(State *state) {
|
2024-07-08 00:44:20 +00:00
|
|
|
(void)state;
|
|
|
|
|
2025-01-30 18:57:20 +00:00
|
|
|
SceneIngame *new_scene = calloc(1, sizeof *new_scene);
|
2024-07-08 00:44:20 +00:00
|
|
|
new_scene->base.tick = ingame_tick;
|
|
|
|
new_scene->base.end = ingame_end;
|
|
|
|
|
2024-10-22 17:32:17 +00:00
|
|
|
new_scene->mouse_captured = true;
|
2024-08-21 13:55:34 +00:00
|
|
|
|
2024-10-07 12:23:04 +00:00
|
|
|
m_audio(m_set(path, "music/mod65.xm"),
|
2024-10-07 12:21:44 +00:00
|
|
|
m_opt(channel, "soundtrack"),
|
|
|
|
m_opt(repeat, true));
|
2024-07-08 06:46:12 +00:00
|
|
|
|
2025-01-02 10:26:16 +00:00
|
|
|
new_scene->pos = (Vec3){ 0.1f, 0.0, 0.1f };
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
return (Scene *)new_scene;
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|