summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/maths/maths.h42
-rw-r--r--src/maths/primitives.h160
-rw-r--r--src/renderer/backends/backend_vulkan.c46
3 files changed, 188 insertions, 60 deletions
diff --git a/src/maths/maths.h b/src/maths/maths.h
index 6fa2f9b..1432581 100644
--- a/src/maths/maths.h
+++ b/src/maths/maths.h
@@ -132,6 +132,27 @@ static inline mat4 mat4_mult(mat4 lhs, mat4 rhs) {
return out_matrix;
}
+static mat4 mat4_transposed(mat4 matrix) {
+ mat4 out_matrix = mat4_ident();
+ out_matrix.data[0] = matrix.data[0];
+ out_matrix.data[1] = matrix.data[4];
+ out_matrix.data[2] = matrix.data[8];
+ out_matrix.data[3] = matrix.data[12];
+ out_matrix.data[4] = matrix.data[1];
+ out_matrix.data[5] = matrix.data[5];
+ out_matrix.data[6] = matrix.data[9];
+ out_matrix.data[7] = matrix.data[13];
+ out_matrix.data[8] = matrix.data[2];
+ out_matrix.data[9] = matrix.data[6];
+ out_matrix.data[10] = matrix.data[10];
+ out_matrix.data[11] = matrix.data[14];
+ out_matrix.data[12] = matrix.data[3];
+ out_matrix.data[13] = matrix.data[7];
+ out_matrix.data[14] = matrix.data[11];
+ out_matrix.data[15] = matrix.data[15];
+ return out_matrix;
+}
+
#if defined(CEL_REND_BACKEND_VULKAN)
/** @brief Creates a perspective projection matrix compatible with Vulkan */
static inline mat4 mat4_perspective(f32 fov_radians, f32 aspect_ratio, f32 near_clip,
@@ -143,12 +164,31 @@ static inline mat4 mat4_perspective(f32 fov_radians, f32 aspect_ratio, f32 near_
mat4 out_matrix = { .data = { 0 } };
out_matrix.data[0] = 1.0f / (aspect_ratio * half_tan_fov);
- out_matrix.data[5] = -1.0f / half_tan_fov; // Flip Y-axis for Vulkan
+ out_matrix.data[5] = 1.0f / half_tan_fov; // Flip Y-axis for Vulkan
out_matrix.data[10] = -((far_clip + near_clip) / (far_clip - near_clip));
out_matrix.data[11] = -1.0f;
out_matrix.data[14] = -((2.0f * far_clip * near_clip) / (far_clip - near_clip));
+ // float half_tan_fov = tanf(fov_radians * 0.5);
+ // float k = far_clip / (far_clip - near_clip);
+
+ // out_matrix.data[0] = 1.0f / (aspect_ratio * half_tan_fov);
+ // out_matrix.data[5] = 1.0f / half_tan_fov;
+ // out_matrix.data[10] = k;
+ // out_matrix.data[11] = -1.0;
+ // out_matrix.data[14] = -1.0 * near_clip * k;
+
+ // f32 half_tan_fov = tan(fov_radians * 0.5f);
+ // out_matrix.data[0] = 1.0f / (aspect_ratio * half_tan_fov);
+ // out_matrix.data[5] = 1.0f / half_tan_fov;
+ // out_matrix.data[10] = -((far_clip + near_clip) / (far_clip - near_clip));
+ // out_matrix.data[11] = -1.0f;
+ // out_matrix.data[14] =
+ // -((2.0f * far_clip * near_clip) / (far_clip - near_clip));
+ // return out_matrix;
+
return out_matrix;
+ // return mat4_transposed(out_matrix);
}
#else
/** @brief Creates a perspective projection matrix */
diff --git a/src/maths/primitives.h b/src/maths/primitives.h
index fd798c1..77dbbae 100644
--- a/src/maths/primitives.h
+++ b/src/maths/primitives.h
@@ -1,55 +1,135 @@
#pragma once
#include <assert.h>
+#include <stdlib.h>
#include "core.h"
+#include "maths.h"
#include "render_types.h"
-static float cube_vertices[] = {
- // positions // normals // texture coords
- -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 0.0f,
- 0.0f, -1.0f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
- 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f,
- 0.0f, -1.0f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-
- -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0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.0f, 1.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f,
- -1.0f, 0.0f, 0.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
-
- -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.5f, -0.5f, 0.0f,
- 1.0f, 0.0f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f,
- 1.0f, 0.0f, 0.0f, 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
-};
+static const vec3 BACK_BOT_LEFT = (vec3){ 0, 0, 0 };
+static const vec3 BACK_BOT_RIGHT = (vec3){ 1, 0, 0 };
+static const vec3 BACK_TOP_LEFT = (vec3){ 0, 1, 0 };
+static const vec3 BACK_TOP_RIGHT = (vec3){ 1, 1, 0 };
+static const vec3 FRONT_BOT_LEFT = (vec3){ 0, 0, 1 };
+static const vec3 FRONT_BOT_RIGHT = (vec3){ 1, 0, 1 };
+static const vec3 FRONT_TOP_LEFT = (vec3){ 0, 1, 1 };
+static const vec3 FRONT_TOP_RIGHT = (vec3){ 1, 1, 1 };
static mesh prim_cube_mesh_create() {
mesh cube = { 0 };
cube.vertices = vertex_darray_new(36);
- for (int i = 0; i < 36; i++) {
- vertex vert = { .position = vec3_create(cube_vertices[(i * 8) + 0], cube_vertices[(i * 8) + 1],
- cube_vertices[(i * 8) + 2]),
- .normal = vec3_create(cube_vertices[(i * 8) + 3], cube_vertices[(i * 8) + 4],
- cube_vertices[(i * 8) + 5]),
- .uv = (vec2){ .x = cube_vertices[(i * 8) + 6],
- .y = cube_vertices[(i * 8) + 7] } };
- vertex_darray_push(cube.vertices, vert);
+ // back faces
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0 } });
+
+ // front faces
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_Z, .uv = (vec2){ 0 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Z, .uv = (vec2){ 0 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_Z, .uv = (vec2){ 0 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_Z, .uv = (vec2){ 0 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_Z, .uv = (vec2){ 0 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Z, .uv = (vec2){ 0 } });
+
+ // top faces
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_Y, .uv = (vec2){ 0 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_Y, .uv = (vec2){ 0 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Y, .uv = (vec2){ 0 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_Y, .uv = (vec2){ 0 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Y, .uv = (vec2){ 0 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_Y, .uv = (vec2){ 0 } });
+
+ // bottom faces
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
+
+ // right faces
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0 } });
+
+ // left faces
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
+
+ cube.indices_len = cube.vertices->len;
+ cube.indices = malloc(sizeof(u32) * cube.indices_len);
+
+ for (u32 i = 0; i < cube.indices_len; i++) {
+ cube.indices[i] = i;
}
+
+ cube.has_indices = true;
+
return cube;
}
diff --git a/src/renderer/backends/backend_vulkan.c b/src/renderer/backends/backend_vulkan.c
index 1657594..97e0a44 100644
--- a/src/renderer/backends/backend_vulkan.c
+++ b/src/renderer/backends/backend_vulkan.c
@@ -654,10 +654,10 @@ void vulkan_object_shader_update_object(vulkan_context* context, vulkan_shader*
vkCmdBindVertexBuffers(cmd_buffer, 0, 1, &context->object_vertex_buffer.handle,
(VkDeviceSize*)offsets);
- // vkCmdBindIndexBuffer(cmd_buffer, context->object_index_buffer.handle, 0, VK_INDEX_TYPE_UINT32);
+ vkCmdBindIndexBuffer(cmd_buffer, context->object_index_buffer.handle, 0, VK_INDEX_TYPE_UINT32);
- // vkCmdDrawIndexed(cmd_buffer, 6, 1, 0, 0, 0);
- vkCmdDraw(cmd_buffer, 36, 1, 0, 0);
+ vkCmdDrawIndexed(cmd_buffer, 36, 1, 0, 0, 0);
+ // vkCmdDraw(cmd_buffer, 36, 1, 0, 0);
}
bool select_physical_device(vulkan_context* ctx) {
@@ -1351,11 +1351,9 @@ void upload_data_range(vulkan_context* context, VkCommandPool pool, VkFence fenc
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
vulkan_buffer staging;
vulkan_buffer_create(context, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, flags, true, &staging);
- TRACE("HERE");
// load data into staging buffer
printf("Size: %ld\n", size);
vulkan_buffer_load_data(context, &staging, 0, size, 0, data);
- TRACE("here");
// copy
vulkan_buffer_copy_to(context, pool, fence, queue, staging.handle, 0, buffer->handle, offset,
@@ -1587,18 +1585,19 @@ bool gfx_backend_init(renderer* ren) {
mesh cube = prim_cube_mesh_create();
- const u32 vert_count = 36;
- // vertex_pos verts[4] = { 0 };
+ // const u32 vert_count = 36;
- vertex_pos verts[36] = { 0 };
+ vertex_pos* verts = malloc(sizeof(vertex_pos) * cube.vertices->len);
f32 scale = 3.0;
- for (size_t i = 0; i < 36; i++) {
+ for (size_t i = 0; i < cube.vertices->len; i++) {
verts[i].pos = vec3_mult(cube.vertices->data[i].position, scale);
verts[i].normal = cube.vertices->data[i].normal;
}
- // const f32 s = 10.0;
+ // const f32 s = 1.0;
+ // const u32 vert_count = 4;
+ // vertex_pos verts[4] = { 0 };
// verts[0].pos.x = -0.5 * s;
// verts[0].pos.y = -0.5 * s;
@@ -1612,15 +1611,16 @@ bool gfx_backend_init(renderer* ren) {
// verts[3].pos.x = 0.5 * s;
// verts[3].pos.y = -0.5 * s;
- const u32 index_count = 6;
- u32 indices[6] = { 0, 1, 2, 0, 3, 1 };
+ // const u32 index_count = 6;
+ // u32 indices[6] = { 0, 1, 2, 0, 3, 1 };
upload_data_range(&context, context.device.gfx_command_pool, 0, context.device.graphics_queue,
- &context.object_vertex_buffer, 0, sizeof(vertex_pos) * vert_count, verts);
+ &context.object_vertex_buffer, 0, sizeof(vertex_pos) * cube.vertices->len,
+ verts);
TRACE("Uploaded vertex data");
- // upload_data_range(&context, context.device.gfx_command_pool, 0, context.device.graphics_queue,
- // &context.object_index_buffer, 0, sizeof(u32) * index_count, indices);
- // TRACE("Uploaded index data");
+ upload_data_range(&context, context.device.gfx_command_pool, 0, context.device.graphics_queue,
+ &context.object_index_buffer, 0, sizeof(u32) * cube.indices_len, cube.indices);
+ TRACE("Uploaded index data");
// --- End test code
INFO("Vulkan renderer initialisation succeeded");
@@ -1661,9 +1661,9 @@ void backend_begin_frame(renderer* ren, f32 delta_time) {
VkViewport viewport;
viewport.x = 0.0;
- viewport.y = 0.0;
+ viewport.y = (f32)context.framebuffer_height;
viewport.width = (f32)context.framebuffer_width;
- viewport.height = (f32)context.framebuffer_height;
+ viewport.height = -(f32)context.framebuffer_height;
viewport.minDepth = 0.0;
viewport.maxDepth = 1.0;
@@ -1741,7 +1741,15 @@ void gfx_backend_draw_frame(renderer* ren, camera* cam, mat4 model) {
camera_view_projection(cam, SCR_HEIGHT, SCR_WIDTH, &view, &proj);
- gfx_backend_update_global_state(proj, view, VEC3_ZERO, vec4(1.0, 1.0, 1.0, 1.0), 0);
+ // proj = mat4_perspective(deg_to_rad(45.0), (f32)SCR_WIDTH / SCR_HEIGHT, 0.1, 100.0);
+
+ // proj.data[5] *= -1.0;
+
+ // vec3 pos = vec3_create(2, 2, 2);
+ // vec3 up = VEC3_Y;
+ // view = mat4_look_at(pos, VEC3_ZERO, up);
+
+ gfx_backend_update_global_state(proj, view, cam->position, vec4(1.0, 1.0, 1.0, 1.0), 0);
vulkan_object_shader_update_object(&context, &context.object_shader, model);
backend_end_frame(ren, 16.0);