diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | sdl_gpu_test.c | 48 | ||||
-rw-r--r-- | shader/tri.frag | 5 | ||||
-rw-r--r-- | shader/tri.vert | 11 |
4 files changed, 49 insertions, 17 deletions
diff --git a/Makefile b/Makefile index 0c71d9e..bf013d0 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ clean: rm -fv ${EXE} ${SPIRV} ${EXE}: *.c - ${CC} -Os -Wall -std=c23 $< -o $@ -lSDL3 + ${CC} -Os -Wall -std=c23 $< -o $@ -lSDL3 -lm %.spv: % glslc -O -c "$<" -o "$@" diff --git a/sdl_gpu_test.c b/sdl_gpu_test.c index 8bc6b73..30e6b83 100644 --- a/sdl_gpu_test.c +++ b/sdl_gpu_test.c @@ -3,8 +3,11 @@ #define SDL_MAIN_USE_CALLBACKS 1 #include <SDL3/SDL_main.h> #include <SDL3/SDL_gpu.h> +#include <SDL3/SDL_timer.h> +#include <cglm/cglm.h> #include <math.h> #include <stdio.h> +#include <string.h> #include <err.h> SDL_Window *win; @@ -202,6 +205,14 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { return SDL_APP_CONTINUE; } +void build_cam_mat(mat4 dest, float width, float height, float x, float y, float scale, float rot) { + glm_mat4_identity(dest); + glm_scale(dest, (vec3) { height < width ? 1 : height / width, height < width ? width / height : 1, 1 }); + glm_scale_uni(dest, scale); + glm_rotate_z(dest, rot, dest); + glm_translate(dest, (vec3) { -x, -y, 0 }); +} + SDL_AppResult SDL_AppIterate(void *appstate) { (void)appstate; @@ -213,17 +224,32 @@ SDL_AppResult SDL_AppIterate(void *appstate) { CHECK(SDL_WaitAndAcquireGPUSwapchainTexture(cmdbuf, win, &swapchain, &swc_width, &swc_height), "swapchain texture acquisition"); CHECK(swapchain, "nil swapchain texture"); - float how = (float)swc_height / (float)swc_width; - float woh = (float)swc_width / (float)swc_height; - float scale = (32 * 2) / fminf(swc_width, swc_height); - float scale_x = (swc_height < swc_width ? how : 1) * scale; - float scale_y = (swc_height < swc_width ? 1 : woh) * scale; - float cam_mat[4][4] = { - { scale_x, 0, 0, 0, }, - { 0, scale_y, 0, 0, }, - { 0, 0, 1, 0, }, - { 0, 0, 0, 1, }, - }; + static float t = 0; + static Uint64 last = 0; + Uint64 now = SDL_GetTicksNS(); + float dt = (now - last) / 1e9; + last = now; + t += dt; + + float mx, my; + SDL_GetMouseState(&mx, &my); + mx -= swc_width * 0.5f; + my -= swc_height * 0.5f; + mx *= 1.0f / fminf(swc_width, swc_height); + my *= 1.0f / fminf(swc_width, swc_height); + + float rot = t; + + mat4 cam_mat; + static float x = 0, y = 0; + + float s = sinf(-rot), c = cosf(-rot); + + float dx = mx * dt * 10; + float dy = -my * dt * 10; + x += dx * c - dy * s; + y += dy * c + dx * s; + build_cam_mat(cam_mat, swc_width, swc_height, x, y, 0.1, rot); SDL_PushGPUVertexUniformData(cmdbuf, 0, &cam_mat, sizeof(cam_mat)); SDL_GPURenderPass *render_pass = SDL_BeginGPURenderPass( cmdbuf, diff --git a/shader/tri.frag b/shader/tri.frag index 5599421..dea0868 100644 --- a/shader/tri.frag +++ b/shader/tri.frag @@ -1,8 +1,9 @@ #version 450 -layout(location = 0) in vec4 in_pos; +layout(location = 0) in vec2 uv; layout(location = 0) out vec4 out_color; void main(void) { - out_color = vec4(in_pos.xy + vec2(0.5,0.5), 0, 1.0); + float n = 2 * length(uv - 0.5); + out_color = vec4(vec3(uv, 1) * (1.75 - n), float(n<1)); } diff --git a/shader/tri.vert b/shader/tri.vert index 701d7c6..750b2a3 100644 --- a/shader/tri.vert +++ b/shader/tri.vert @@ -1,9 +1,14 @@ #version 450 +layout(set = 1, binding = 0) uniform UBO { + mat4 camera; +}; + layout(location = 0) in vec2 in_pos; -layout(location = 0) out vec4 out_pos; +layout(location = 1) in vec2 in_uv; +layout(location = 0) out vec2 out_uv; void main() { - gl_Position = vec4(in_pos.x, in_pos.y, 0.0, 1.0); - out_pos = gl_Position; + gl_Position = camera * vec4(in_pos, 0.0, 1.0); + out_uv = in_uv; } |