replace math_graphics with glm version 1.0.3

This commit is contained in:
Sven Balzer
2026-04-15 10:50:41 +02:00
parent 07af9deb6a
commit 4ec664c8db
1676 changed files with 289261 additions and 1477 deletions
+124 -66
View File
@@ -11,15 +11,17 @@
#include <tracy/TracyC.h>
#include <webgpu/webgpu.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "defer.h"
#include "log.h"
#include "math_graphics.h"
#include "stb_image.h"
#include "change_directory.h"
#include "shaders/shaders.h"
using namespace M;
using namespace glm;
#define ASSETS_PATH "./assets/"
@@ -84,11 +86,11 @@ static float camera_fovy_degrees = 31.0f;
static float camera_tilt = 25.5f;
static float camera_distance = 13.5f;
static M4x4 view_matrix;
static M4x4 inverse_view_matrix;
static mat4x4 view_matrix;
static mat4x4 inverse_view_matrix;
static M4x4 projection_matrix;
static M4x4 inverse_projection_matrix;
static mat4x4 projection_matrix;
static mat4x4 inverse_projection_matrix;
static SDL_Time time;
@@ -96,7 +98,61 @@ static bool enable_time_tints = true;
static bool use_actual_time = true;
static SDL_DateTime calendar_time;
static V2 mouse_pos;
static vec2 mouse_pos;
float remap(float in_a, float in_b, float out_a, float out_b, float v) {
return mix(out_a, out_b, (v - in_a) / (in_b - in_a));
}
vec2 remap(vec2 in_a, vec2 in_b, vec2 out_a, vec2 out_b, vec2 v) {
return mix(out_a, out_b, (v - in_a) / (in_b - in_a));
}
mat4x4 view(vec3 player_pos, float tilt, float camera_distance) {
float s = sinf(tilt);
float c = cosf(tilt);
return {
1, 0, 0, -player_pos.x,
0, c, s, c * -player_pos.y,
0, -s, c, s * player_pos.y - camera_distance,
0, 0, 0, 1,
};
}
mat4x4 inverse_view(vec3 player_pos, float tilt, float camera_distance) {
float s = sinf(tilt);
float c = cosf(tilt);
return {
1, 0, 0, player_pos.x,
0, c, -s, -s * camera_distance + player_pos.y,
0, s, c, c * camera_distance,
0, 0, 0, 1,
};
}
mat4x4 projection(float fovy, float aspect, float near) {
float g = 1.0 / tanf(fovy * 0.5);
return {
g / aspect, 0, 0, 0,
0, g, 0, 0,
0, 0, 0, near,
0, 0, -1, 0,
};
}
mat4x4 inverse_projection(float fovy, float aspect, float near) {
float g = 1.0 / tanf(fovy * 0.5);
return {
aspect / g, 0, 0, 0,
0, 1 / g, 0, 0,
0, 0, 0, -1,
0, 0, 1 / near, 0,
};
}
#define MAX_TINT_TIMES 32
static int num_used_tint_times = 4;
@@ -107,11 +163,11 @@ static int time_tints_times[MAX_TINT_TIMES][3] = {
{ 21, 0, 0 },
};
static V3 time_tints[MAX_TINT_TIMES] = {
V3_(0.314f, 0.369f, 0.455f),
V3_(1.0f, 0.891f, 0.868f),
V3_(1.0f, 0.465f, 0.373f),
V3_(0.314f, 0.369f, 0.455f),
static vec3 time_tints[MAX_TINT_TIMES] = {
{ 0.314f, 0.369f, 0.455f },
{ 1.0f, 0.891f, 0.868f },
{ 1.0f, 0.465f, 0.373f },
{ 0.314f, 0.369f, 0.455f },
};
enum Settings_Category {
@@ -120,15 +176,15 @@ enum Settings_Category {
};
struct Vertex {
V3 pos;
V2 uv;
vec3 pos;
vec2 uv;
};
static Vertex vertices[] = {
{{ -0.5f, 0.5f }, { 0.0f, 0.0f }},
{{ -0.5f, -0.5f }, { 0.0f, 1.0f }},
{{ 0.5f, -0.5f }, { 1.0f, 1.0f }},
{{ 0.5f, 0.5f }, { 1.0f, 0.0f }},
{{ -0.5f, 0.5f, 0.0f }, { 0.0f, 0.0f }},
{{ -0.5f, -0.5f, 0.0f }, { 0.0f, 1.0f }},
{{ 0.5f, -0.5f, 0.0f }, { 1.0f, 1.0f }},
{{ 0.5f, 0.5f, 0.0f }, { 1.0f, 0.0f }},
};
static Uint16 indices[] = {
@@ -139,28 +195,28 @@ static Uint16 indices[] = {
#define grid_line_size (1.0f / 64.0f)
static Vertex grid_vertices[] = {
// LEFT
{{ -0.5f, 0.5f }},
{{ -0.5f, -0.5f + grid_line_size }},
{{ -0.5f + grid_line_size, -0.5f + grid_line_size }},
{{ -0.5f + grid_line_size, 0.5f }},
{{ -0.5f, 0.5f, 0.0f }},
{{ -0.5f, -0.5f + grid_line_size, 0.0f }},
{{ -0.5f + grid_line_size, -0.5f + grid_line_size, 0.0f }},
{{ -0.5f + grid_line_size, 0.5f, 0.0f }},
// TOP
{{ -0.5f + grid_line_size, 0.5f }},
{{ -0.5f + grid_line_size, 0.5f - grid_line_size }},
{{ 0.5f, 0.5f - grid_line_size }},
{{ 0.5f, 0.5f }},
{{ -0.5f + grid_line_size, 0.5f, 0.0f }},
{{ -0.5f + grid_line_size, 0.5f - grid_line_size, 0.0f }},
{{ 0.5f, 0.5f - grid_line_size, 0.0f }},
{{ 0.5f, 0.5f, 0.0f }},
// RIGHT
{{ 0.5f - grid_line_size, 0.5f - grid_line_size }},
{{ 0.5f - grid_line_size, -0.5f }},
{{ 0.5f, -0.5f }},
{{ 0.5f, 0.5f - grid_line_size }},
{{ 0.5f - grid_line_size, 0.5f - grid_line_size, 0.0f }},
{{ 0.5f - grid_line_size, -0.5f, 0.0f }},
{{ 0.5f, -0.5f, 0.0f }},
{{ 0.5f, 0.5f - grid_line_size, 0.0f }},
// BOTTOM
{{ -0.5f, -0.5f + grid_line_size }},
{{ -0.5f, -0.5f }},
{{ 0.5f - grid_line_size, -0.5f }},
{{ 0.5f - grid_line_size, -0.5f + grid_line_size }},
{{ -0.5f, -0.5f + grid_line_size, 0.0f }},
{{ -0.5f, -0.5f, 0.0f }},
{{ 0.5f - grid_line_size, -0.5f, 0.0f }},
{{ 0.5f - grid_line_size, -0.5f + grid_line_size, 0.0f }},
};
static Uint16 grid_indices[] = {
@@ -182,7 +238,7 @@ static Uint16 grid_indices[] = {
};
struct Instance {
V2 pos;
vec2 pos;
};
static Instance player_instance = {{ 0.0f, 0.0f }};
@@ -216,7 +272,7 @@ typedef struct Sint32x2 {
struct PerFrame {
Sint32x2 drag_start;
Sint32x2 mouse;
V2 grid_offset;
vec2 grid_offset;
Sint32 grid_width;
Sint32 map_width;
};
@@ -263,7 +319,7 @@ static TileInfo tile_infos[] = {
{ 0x0423, "tiles/grass_ground_two_corner.png", TILE_CORNER_INFO(TILEKIND_GRASS, TILEKIND_GROUND, TILEKIND_GRASS, TILEKIND_GROUND ) },
};
static V4 tile_uvs[SDL_arraysize(tile_infos)];
static vec4 tile_uvs[SDL_arraysize(tile_infos)];
static Sint32 selected_tile_kind = -1;
@@ -271,7 +327,7 @@ static Sint32 selected_tile = -1;
static Sint32 selected_rotation = 0;
static bool dragging_tile_change = false;
static V2 drag_start_pos;
static vec2 drag_start_pos;
static bool update_buffer(WGPUBuffer buffer, Uint32 offset, Uint32 num_bytes, void *data) {
wgpuQueueWriteBuffer(queue, buffer, offset, data, num_bytes);
@@ -579,7 +635,7 @@ static bool SelectableTile(const char *label, bool selected, Uint32 tile_index,
ImVec2 min = ImGui::GetItemRectMin();
ImVec2 max = ImGui::GetItemRectMax();
V4 uv = tile_uvs[tile_index] / TILE_ATLAS_SIZE;
vec4 uv = tile_uvs[tile_index] / (float)TILE_ATLAS_SIZE;
switch (orientation) {
case 0: context->CurrentWindow->DrawList->AddImageQuad((ImTextureID)tile_textures_atlas_view_unorm, min + padding, ImVec2(max.x - padding.x, min.y + padding.y), max - padding, ImVec2(min.x + padding.x, max.y - padding.y), ImVec2(uv.x, uv.y), ImVec2(uv.z, uv.y), ImVec2(uv.z, uv.w), ImVec2(uv.x, uv.w)); break;
@@ -608,24 +664,26 @@ static ImVec4 sRGB_to_linear(ImVec4 linear) {
return ImVec4(red, green, blue, linear.w);
}
static V3 Unproject(V3 screen_pos) {
V4 result = inverse_view_matrix * inverse_projection_matrix * V4_(screen_pos, 1.0f);
result.xyz /= result.w;
static vec3 Unproject(vec3 screen_pos) {
vec4 result = vec4(screen_pos, 1.0f) * inverse_projection_matrix * inverse_view_matrix;
result.x /= result.w;
result.y /= result.w;
result.z /= result.w;
return result.xyz;
return result.xyz();
}
static V2 get_floor_intersection_of_mouse(V2 mouse_pos) {
V2 mouse = remap(V2{ 0, 0 }, V2{ (float)window_width, (float)window_height }, V2{ -1, 1 }, V2{ 1, -1 }, V2{ mouse_pos.x, mouse_pos.y });
V3 camera_position = (inverse_view_matrix * V4_(0, 0, 0, 1)).xyz;
static vec2 get_floor_intersection_of_mouse(vec2 mouse_pos) {
vec2 mouse = remap(vec2(0, 0), vec2((float)window_width, (float)window_height), vec2(-1, 1), vec2(1, -1), mouse_pos);
vec3 camera_position = (vec4(0, 0, 0, 1) * inverse_view_matrix).xyz();
V3 probe = Unproject(V3_(mouse, .5));
V3 ray_dir = normalize(probe - camera_position);
vec3 probe = Unproject(vec3(mouse, .5));
vec3 ray_dir = normalize(probe - camera_position);
float t = -camera_position.z / ray_dir.z;
V3 floor_intersection = camera_position + (t * ray_dir);
vec3 floor_intersection = camera_position + (t * ray_dir);
return floor_intersection.xy;
return floor_intersection.xy();
}
#ifdef TRACY_ENABLE
@@ -1289,7 +1347,7 @@ static void SameLineOrWrap(const ImVec2& size) {
ImGui::SameLine();
}
static Sint32x2 grid_tile_pos_from_floor_intersection(V2 floor_intersection) {
static Sint32x2 grid_tile_pos_from_floor_intersection(vec2 floor_intersection) {
return {
(Sint32)SDL_floorf(floor_intersection.x + (selected_tile == -1 ? 0.5f : 1.0f)),
(Sint32)SDL_floorf(floor_intersection.y + (selected_tile == -1 ? 0.5f : 1.0f)),
@@ -1473,7 +1531,7 @@ static bool init_webgpu() {
pixel_sampler = wgpuDeviceCreateSampler(device, &pixel_sampler_descriptor);
view_projection_matrix_buffer = create_buffer(WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst, sizeof(M4x4), NULL, "view_projection_matrix_buffer");
view_projection_matrix_buffer = create_buffer(WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst, sizeof(mat4x4), NULL, "view_projection_matrix_buffer");
if (!view_projection_matrix_buffer) {
log_error("Failed to create buffer.");
return false;
@@ -1485,7 +1543,7 @@ static bool init_webgpu() {
return false;
}
tint_color_buffer = create_buffer(WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst, sizeof(V3), NULL, "tint_color_buffer");
tint_color_buffer = create_buffer(WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst, sizeof(vec3), NULL, "tint_color_buffer");
if (!tint_color_buffer) {
log_error("Failed to create buffer.");
return false;
@@ -1840,7 +1898,7 @@ int main(int argc, char **argv) {
ImGui::NewLine();
for (int i = 0; i < num_used_tint_times; i++) {
ImGui::PushID(i);
ImGui::ColorEdit3("##color", time_tints[i].E);
ImGui::ColorEdit3("##color", glm::value_ptr(time_tints[i]));
ImGui::PopID();
}
@@ -1852,7 +1910,7 @@ int main(int argc, char **argv) {
time_tints_times[i][0] == time_tints_times[i + 1][0] && time_tints_times[i][1] == time_tints_times[i + 1][1] && time_tints_times[i][2] > time_tints_times[i + 1][2]) {
int temp_time[3] = { time_tints_times[i][0], time_tints_times[i][1], time_tints_times[i][2] };
V3 temp_color = time_tints[i];
vec3 temp_color = time_tints[i];
time_tints_times[i][0] = time_tints_times[i + 1][0];
time_tints_times[i][1] = time_tints_times[i + 1][1];
@@ -2042,7 +2100,7 @@ int main(int argc, char **argv) {
if (io.WantCaptureMouse)
continue;
V2 floor_intersection = get_floor_intersection_of_mouse(V2_(event.button.x, event.button.y));
vec2 floor_intersection = get_floor_intersection_of_mouse(vec2(event.button.x, event.button.y));
Sint32x2 tile_pos = grid_tile_pos_from_floor_intersection(floor_intersection);
drag_start_pos = floor_intersection;
@@ -2094,7 +2152,7 @@ int main(int argc, char **argv) {
continue;
if (selected_tile != -1 && dragging_tile_change) {
V2 floor_intersection = get_floor_intersection_of_mouse(V2_(event.button.x, event.button.y));
vec2 floor_intersection = get_floor_intersection_of_mouse(vec2(event.button.x, event.button.y));
Sint32x2 tile_pos = grid_tile_pos_from_floor_intersection(floor_intersection);
Sint32 tile_x = clamp(0, tile_pos.x, current_map.width - 1);
@@ -2126,10 +2184,10 @@ int main(int argc, char **argv) {
} break;
case SDL_EVENT_MOUSE_MOTION: {
mouse_pos = V2_(event.motion.x, event.motion.y);
mouse_pos = vec2(event.motion.x, event.motion.y);
if (selected_tile_kind != -1 && dragging_tile_change) {
V2 floor_intersection = get_floor_intersection_of_mouse(mouse_pos);
vec2 floor_intersection = get_floor_intersection_of_mouse(mouse_pos);
Sint32x2 tile_pos = grid_tile_pos_from_floor_intersection(floor_intersection);
change_map_tile(tile_pos.x, tile_pos.y, (TileKind)selected_tile_kind);
}
@@ -2217,21 +2275,21 @@ int main(int argc, char **argv) {
float aspect_ratio = ((float) window_width / (float) window_height);
view_matrix = view (V3_((float)player.pos_x, (float)player.pos_y, 0), radians(camera_tilt), camera_distance);
inverse_view_matrix = inverse_view(V3_((float)player.pos_x, (float)player.pos_y, 0), radians(camera_tilt), camera_distance);
view_matrix = view (vec3((float)player.pos_x, (float)player.pos_y, 0), radians(camera_tilt), camera_distance);
inverse_view_matrix = inverse_view(vec3((float)player.pos_x, (float)player.pos_y, 0), radians(camera_tilt), camera_distance);
projection_matrix = projection (radians(camera_fovy_degrees), aspect_ratio, NEAR_PLANE);
inverse_projection_matrix = inverse_projection(radians(camera_fovy_degrees), aspect_ratio, NEAR_PLANE);
M4x4 view_projection_matrix = projection_matrix * view_matrix;
mat4x4 view_projection_matrix = view_matrix * projection_matrix;
update_buffer(view_projection_matrix_buffer, 0, sizeof(view_projection_matrix), &view_projection_matrix);
V2 floor_intersection = get_floor_intersection_of_mouse(mouse_pos);
vec2 floor_intersection = get_floor_intersection_of_mouse(mouse_pos);
Sint32x2 tile_pos = grid_tile_pos_from_floor_intersection(floor_intersection);
per_frame.map_width = current_map.width;
per_frame.grid_width = selected_tile != -1 ? current_map.width : current_map.width + 1;
per_frame.grid_offset = selected_tile != -1 ? V2_(-0.5f, -0.5f) : V2_(-1.0f, -1.0f);
per_frame.grid_offset = selected_tile != -1 ? vec2(-0.5f, -0.5f) : vec2(-1.0f, -1.0f);
per_frame.mouse = tile_pos;
if (dragging_tile_change && selected_tile != -1) {
@@ -2265,8 +2323,8 @@ int main(int argc, char **argv) {
double t = v / (double)time_between;
V3 tint_color = lerp(time_tints[last_time_index], time_tints[(last_time_index + 1) % num_used_tint_times], t);
if (!enable_time_tints) tint_color = V3_(1, 1, 1);
vec3 tint_color = mix(time_tints[last_time_index], time_tints[(last_time_index + 1) % num_used_tint_times], t);
if (!enable_time_tints) tint_color = vec3(1, 1, 1);
update_buffer(tint_color_buffer, 0, sizeof(tint_color), &tint_color);
}
}