Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uniforms with matrix tranformations and ply loading #14

Merged
merged 15 commits into from
Dec 2, 2024
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ OBJS :=$(call GETOBJS,$(SRCS))
TARGET :=shooter

.DEFAULT_GOAL: dbg
dbg: $(BINDIR)/$(TARGET)
rel: $(BINDIR)/$(TARGET)
dbg: $(BINDIR)/$(TARGET) shaders
rel: $(BINDIR)/$(TARGET) shaders

# It exists so run this
ifeq ($(SDL3_EXISTS),0)
Expand Down
Binary file removed res/shaders/glsl/test.frag.spv
Binary file not shown.
Binary file removed res/shaders/glsl/test.vert.spv
Binary file not shown.
Binary file removed res/shaders/metal/test.frag.metallib
Binary file not shown.
Binary file removed res/shaders/metal/test.vert.metallib
Binary file not shown.
10 changes: 8 additions & 2 deletions shaders/glsl/test.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ layout(location = 2) in vec3 normal;

layout(location = 0) out vec4 colorOut;

layout(set = 0, binding = 0) uniform UniformBufferObject {
mat4 model;
mat4 view;
mat4 projection;
} ubo;

void main() {
gl_Position = vec4(position, 1.0);
colorOut = vec4(normal, 1.0);
gl_Position = ubo.projection * ubo.view * ubo.model * vec4(position, 1.0);
colorOut = vec4(normal, 1.0); // Example: pass the normal as color
}
7 changes: 4 additions & 3 deletions src/linear.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

// Header
#include "linear.h"
#include "common.h"

// Function definitions
u0 vec2_add_vec2 ( vec2 *p_result, vec2 a, vec2 b )
Expand Down Expand Up @@ -804,7 +805,7 @@ u0 mat4_view_from_vec3 ( mat4 *p_view, vec3 eye, vec3 target, vec3 up )
{
no_view:
#ifndef NDEBUG
printf("[camera] Null pointer provided for parameter \"p_view\" in call to function \"%s\"\n", __FUNCTION__);
die("p_view: nullptr");
#endif

// Error
Expand Down Expand Up @@ -841,11 +842,11 @@ u0 mat4_perspective_from_vec3 ( mat4 *p_projection, float fov, float aspect, flo
{
no_projection:
#ifndef NDEBUG
printf("[camera] Null pointer provided for parameter \"p_projection\" in call to function \"%s\"\n", __FUNCTION__);
die("p_projection: nullptr");
#endif

// Error
return;
}
}
}
}
2 changes: 1 addition & 1 deletion src/linear.h
Original file line number Diff line number Diff line change
Expand Up @@ -671,4 +671,4 @@ u0 mat4_perspective_from_vec3
float aspect,
float near_clip,
float far_clip
);
);
18 changes: 15 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
#include <SDL3/SDL_main.h>

#include "common.h"
#include "linear.h"
#include "mesh.h"
#include "shapes.h"

#define SHADERFORMATS (SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_METALLIB)
#define USEMSAA true

#define WINDOW_WIDTH 800.0f
#define WINDOW_HEIGHT 600.0f
static SDL_Window* window;
static SDL_GPUDevice* gpu;
static SDL_GPUGraphicsPipeline* pipeline;
Expand Down Expand Up @@ -77,7 +80,8 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[])
sdldie("SDL_Init");
}

if (!(window = SDL_CreateWindow("obama", 800, 600, SDL_WINDOW_HIGH_PIXEL_DENSITY))) {
if (!(window = SDL_CreateWindow(
"obama", WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_HIGH_PIXEL_DENSITY))) {
sdldie("SDL_CreateWindow");
}
if (!(gpu = SDL_CreateGPUDevice(SHADERFORMATS, true, NULL))) {
Expand All @@ -92,7 +96,7 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[])
.num_samplers = 0,
.num_storage_buffers = 0,
.num_storage_textures = 0,
.num_uniform_buffers = 0,
.num_uniform_buffers = 1,
.props = 0,
}))
.shader) {
Expand Down Expand Up @@ -241,14 +245,21 @@ SDL_AppResult SDL_AppIterate(void* appstate)
sdldie("SDL_AcquireGPUSwapchainTexture");
}

// To many frames in flight? Don't render this frame.
// Too many frames in flight? Don't render this frame.
if (!swap.tex) {
SDL_SubmitGPUCommandBuffer(cmdbuf);
return SDL_APP_CONTINUE;
}

// TODO: Resize window textures

mat4 _mat4s[3];
mat4_identity(&_mat4s[0]);
mat4_identity(&_mat4s[1]);
mat4_perspective_from_vec3(&_mat4s[2], 80.0f, WINDOW_WIDTH / WINDOW_HEIGHT, 0.1f, 100.0f);

SDL_PushGPUVertexUniformData(cmdbuf, 0, &_mat4s, sizeof(_mat4s));

SDL_GPURenderPass* pass = SDL_BeginGPURenderPass(cmdbuf,
&(SDL_GPUColorTargetInfo) {
.clear_color = { .r = 0.2f, .g = 0.3f, .b = 0.3f, .a = 1.0f },
Expand All @@ -260,6 +271,7 @@ SDL_AppResult SDL_AppIterate(void* appstate)
.cycle_resolve_texture = !!tex_msaa,
},
1, NULL);

SDL_BindGPUGraphicsPipeline(pass, pipeline);
mesh_bind(&mesh, pass);
SDL_DrawGPUIndexedPrimitives(pass, mesh.idxs.len, 1, 0, 0, 0);
Expand Down