Pre-multiply V and P matrices

This commit is contained in:
Camden Dixie O'Brien committed 2025-10-04 14:10:00 +01:00
1 parent e7f4f4b51d
commit 2cf4f6832d
5 files changed
+21 -25

No files matched your search

+2 -3
View File
@@ -3,8 +3,7 @@
layout(location = 0) out vec3 frag_normal;
layout(push_constant) uniform vp_t {
mat4 view;
mat4 proj;
mat4 transform;
} vp;
vec2 vertices[4] = vec2[](
@@ -16,6 +15,6 @@ vec2 vertices[4] = vec2[](
void main() {
const vec2 pos = 3 * vertices[gl_VertexIndex];
gl_Position = vp.proj * vp.view * vec4(pos.x, 0, pos.y, 1);
gl_Position = vp.transform * vec4(pos.x, 0, pos.y, 1);
frag_normal = vec3(0, 1, 0);
}
+4 -4
View File
@@ -496,7 +496,7 @@ void gpu_init(gpu_ctx_t *ctx, GLFWwindow *window, const char *name)
const VkPushConstantRange push_constant_range = {
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
.offset = 0,
.size = sizeof(vp_t),
.size = sizeof(mat4_t),
};
// Load vertex and fragment shader modules
@@ -956,7 +956,7 @@ void gpu_model_cleanup(const gpu_ctx_t *ctx, const gpu_model_t *model)
}
void gpu_render(
const gpu_ctx_t *ctx, vp_t vp, size_t model_count,
const gpu_ctx_t *ctx, mat4_t vp, size_t model_count,
const gpu_model_t *models, const mat4_t **bone_matrices,
const mat4_t *model_transforms)
{
@@ -1001,7 +1001,7 @@ void gpu_render(
ctx->cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->floor_pipeline);
vkCmdPushConstants(
ctx->cmd_buf, ctx->model_pipeline_layout, VK_SHADER_STAGE_VERTEX_BIT,
0, sizeof(vp_t), &vp);
0, sizeof(mat4_t), &vp);
vkCmdDraw(ctx->cmd_buf, 4, 1, 0, 0);
// Draw models
@@ -1009,7 +1009,7 @@ void gpu_render(
ctx->cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->model_pipeline);
vkCmdPushConstants(
ctx->cmd_buf, ctx->model_pipeline_layout, VK_SHADER_STAGE_VERTEX_BIT,
0, sizeof(vp_t), &vp);
0, sizeof(mat4_t), &vp);
for (unsigned i = 0; i < model_count; ++i) {
models[i].transform_ptr[curr_img] = model_transforms[i];
memcpy(
+1 -6
View File
@@ -9,11 +9,6 @@
#define MAX_FRAMES_IN_FLIGHT 4
typedef struct {
mat4_t view;
mat4_t proj;
} vp_t;
typedef struct {
VkInstance inst;
VkSurfaceKHR surface;
@@ -73,7 +68,7 @@ void gpu_model_load(
void gpu_model_cleanup(const gpu_ctx_t *ctx, const gpu_model_t *model);
void gpu_render(
const gpu_ctx_t *ctx, vp_t vp, size_t model_count,
const gpu_ctx_t *ctx, mat4_t vp, size_t model_count,
const gpu_model_t *models, const mat4_t **bone_matrices,
const mat4_t *model_transforms);
+12 -9
View File
@@ -12,7 +12,7 @@
#define DT (1'000'000 / 60) // Assume 60 Hz for now
#define LIL_GUY_COUNT 2
#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))
static void
key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
@@ -59,11 +59,10 @@ int main(int argc, char *argv[])
const vec3_t cam = { 0, 4, -8 };
const vec3_t tgt = { 0, 0, 0 };
const vp_t vp = {
.view = mat4_lookat(cam, tgt, (vec3_t) { 0, 1, 0 }),
.proj = mat4_perspective((float)W / H, PI / 8, 0.1, 100.0),
};
mat4_t transforms[] = {
const mat4_t view = mat4_lookat(cam, tgt, (vec3_t) { 0, 1, 0 });
const mat4_t proj = mat4_perspective((float)W / H, PI / 8, 0.1, 100.0);
const mat4_t vp = mat4_mul(proj, view);
mat4_t transforms[NELEMS(gpu_models)] = {
mat4_mul(
MAT4_TRANSLATE(1, 0, 0),
(mat4_t) {
@@ -96,9 +95,13 @@ int main(int argc, char *argv[])
animation_update(&anim, &model, DT);
const mat4_t *bone_matrices[]
= { anim.bone_matrices, anim.bone_matrices };
gpu_render(&gpu, vp, 2, gpu_models, bone_matrices, transforms);
const mat4_t *bone_matrices[NELEMS(gpu_models)] = {
anim.bone_matrices,
anim.bone_matrices,
};
gpu_render(
&gpu, vp, NELEMS(gpu_models), gpu_models, bone_matrices,
transforms);
}
// Clean up
+2 -3
View File
@@ -8,8 +8,7 @@ layout(location = 3) in vec4 weights;
layout(location = 0) out vec3 frag_normal;
layout(push_constant) uniform vp_t {
mat4 view;
mat4 proj;
mat4 transform;
} vp;
layout(binding = 0) uniform transform_t {
@@ -26,7 +25,7 @@ void main() {
+ weights.y * bones.matrices[bone_indices.y]
+ weights.z * bones.matrices[bone_indices.z]
+ weights.w * bones.matrices[bone_indices.w];
gl_Position = vp.proj * vp.view * transform.model * skinning_matrix
gl_Position = vp.transform * transform.model * skinning_matrix
* vec4(position, 1.0);
frag_normal = transpose(
inverse(mat3(transform.model * skinning_matrix))) * normal;