From 809617e6c431d6bbddee8a6e0628a6eb1121c569 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Mon, 5 May 2025 05:14:47 +0200 Subject: [PATCH 1/2] working crafter math --- Crafter.Graphics-Camera.cpp | 12 +++++++++--- Crafter.Graphics-Camera.cppm | 7 ++++--- Crafter.Graphics-MeshShader.cppm | 10 +++++----- main.cpp | 12 +++--------- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/Crafter.Graphics-Camera.cpp b/Crafter.Graphics-Camera.cpp index 33c6d70..504a91d 100644 --- a/Crafter.Graphics-Camera.cpp +++ b/Crafter.Graphics-Camera.cpp @@ -3,13 +3,19 @@ module; #include #include "VulkanBuffer.h" #include +#include +#include module Crafter.Graphics; import Crafter.Math; using namespace Crafter; Camera::Camera(float fov, float aspectRatio, float near, float far) { - projection = Matrix::Projection(fov, aspectRatio, near, far); - view = Matrix::Idenity(); - projectionView = camera.matrices.perspective*camera.matrices.view; + projection = MatrixRowMajor::Perspective(fov, aspectRatio, near, far); + view = MatrixRowMajor::Rotation(0, 0, ToRadian(180.0f))*MatrixRowMajor::Translation(1.0f, 0.0f, -10.0f); + Update(); +} + +void Camera::Update() { + projectionView = projection*view; } \ No newline at end of file diff --git a/Crafter.Graphics-Camera.cppm b/Crafter.Graphics-Camera.cppm index 06ccf6b..104103e 100644 --- a/Crafter.Graphics-Camera.cppm +++ b/Crafter.Graphics-Camera.cppm @@ -10,9 +10,10 @@ import Crafter.Math; namespace Crafter { export class Camera : public Component { public: - Matrix projection; - Matrix view; - Matrix projectionView; + MatrixRowMajor projection; + MatrixRowMajor view; + MatrixRowMajor projectionView; Camera(float fov, float aspectRatio, float near, float far); + void Update(); }; } diff --git a/Crafter.Graphics-MeshShader.cppm b/Crafter.Graphics-MeshShader.cppm index 6c9b57d..079a273 100644 --- a/Crafter.Graphics-MeshShader.cppm +++ b/Crafter.Graphics-MeshShader.cppm @@ -11,19 +11,19 @@ import Crafter.Component; import :Mesh; import :Camera; import :VulkanPipeline; - +import Crafter.Math; namespace Crafter { export template class MeshShader { public: - //glm::mat4 transform; + MatrixRowMajor transform; ComponentRefOwning> mesh; ComponentRefOwning camera; - Buffer mvp; + Buffer> mvp; std::uint32_t threadCount; MeshShader(Mesh* mesh, Camera* camera) : threadCount(std::ceil(static_cast(mesh->indexCount)/64/3)), mvp(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT), mesh(mesh), camera(camera) { - + transform = MatrixRowMajor::Identity(); } void WriteDescriptors(DescriptorSet& set) { set.Write(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &mvp.descriptor); @@ -31,7 +31,7 @@ namespace Crafter { set.Write(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2, &mesh.component->indicies.descriptor); } void Update() { - //mvp.value[0] = camera.component->projectionView*transform; + *mvp.value = camera.component->projectionView*transform; } }; } diff --git a/main.cpp b/main.cpp index 3c75ade..b89b802 100644 --- a/main.cpp +++ b/main.cpp @@ -2,16 +2,10 @@ #include #include #include -#define GLM_FORCE_RADIANS -#define GLM_FORCE_DEPTH_ZERO_TO_ONE -#define GLM_ENABLE_EXPERIMENTAL -#include -#include -#include -#include import Crafter.Graphics; import Crafter.Asset; import Crafter.Event; +import Crafter.Math; using namespace Crafter; typedef VulkanShader<"MeshShaderXYZUV.spirv", "main", VK_SHADER_STAGE_MESH_BIT_EXT, 3, {{{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2}}}> MeshVulkanShader; @@ -29,14 +23,14 @@ int main() { Asset asset; asset.LoadFull("cannon.cras"); - Camera camera; + Camera camera(1.57079633, 16 / 9, 0.01, 512); + camera.Update(); Mesh* mesh = Mesh::FromAssetUV(asset.entries[0].data.data()); DescriptorSet descriptors; Pipeline::GetDescriptorSet(descriptors); MeshShader meshShader(mesh, &camera); meshShader.WriteDescriptors(descriptors); - meshShader.transform = glm::mat4(1.0f); meshShader.Update(); Asset asset2; From cb848e71090ca44fcf92721b9223d04d42cfb58d Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Mon, 5 May 2025 06:00:35 +0200 Subject: [PATCH 2/2] update --- Crafter.Graphics-Camera.cpp | 5 +++-- Crafter.Graphics-Camera.cppm | 2 ++ Crafter.Graphics-MeshShader.cppm | 9 +++++++-- Crafter.Graphics-TextureShader.cppm | 7 ------- Crafter.Graphics-VulkanTexture.cppm | 7 ------- Crafter.Graphics-WindowWayland.cpp | 4 +++- Crafter.Graphics-WindowWaylandVulkan.cpp | 1 + 7 files changed, 16 insertions(+), 19 deletions(-) diff --git a/Crafter.Graphics-Camera.cpp b/Crafter.Graphics-Camera.cpp index 504a91d..f3df871 100644 --- a/Crafter.Graphics-Camera.cpp +++ b/Crafter.Graphics-Camera.cpp @@ -8,14 +8,15 @@ module; module Crafter.Graphics; import Crafter.Math; +import Crafter.Event; using namespace Crafter; Camera::Camera(float fov, float aspectRatio, float near, float far) { projection = MatrixRowMajor::Perspective(fov, aspectRatio, near, far); - view = MatrixRowMajor::Rotation(0, 0, ToRadian(180.0f))*MatrixRowMajor::Translation(1.0f, 0.0f, -10.0f); - Update(); + view = MatrixRowMajor::Identity(); } void Camera::Update() { projectionView = projection*view; + onUpdate.Invoke(); } \ No newline at end of file diff --git a/Crafter.Graphics-Camera.cppm b/Crafter.Graphics-Camera.cppm index 104103e..ca1dbc9 100644 --- a/Crafter.Graphics-Camera.cppm +++ b/Crafter.Graphics-Camera.cppm @@ -6,6 +6,7 @@ export module Crafter.Graphics:Camera; import :VulkanBuffer; import Crafter.Component; import Crafter.Math; +import Crafter.Event; namespace Crafter { export class Camera : public Component { @@ -13,6 +14,7 @@ namespace Crafter { MatrixRowMajor projection; MatrixRowMajor view; MatrixRowMajor projectionView; + Event onUpdate; Camera(float fov, float aspectRatio, float near, float far); void Update(); }; diff --git a/Crafter.Graphics-MeshShader.cppm b/Crafter.Graphics-MeshShader.cppm index 079a273..2005b4e 100644 --- a/Crafter.Graphics-MeshShader.cppm +++ b/Crafter.Graphics-MeshShader.cppm @@ -15,14 +15,19 @@ import Crafter.Math; namespace Crafter { export template - class MeshShader { + class MeshShader : public Component { public: MatrixRowMajor transform; ComponentRefOwning> mesh; ComponentRefOwning camera; Buffer> mvp; std::uint32_t threadCount; - MeshShader(Mesh* mesh, Camera* camera) : threadCount(std::ceil(static_cast(mesh->indexCount)/64/3)), mvp(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT), mesh(mesh), camera(camera) { + EventListener cameraUpdate; + MeshShader(Mesh* mesh, Camera* camera) : threadCount(std::ceil(static_cast(mesh->indexCount)/64/3)), mvp(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT), mesh(mesh), camera(camera), cameraUpdate( + &camera->onUpdate, [this](){ + Update(); + } + ) { transform = MatrixRowMajor::Identity(); } void WriteDescriptors(DescriptorSet& set) { diff --git a/Crafter.Graphics-TextureShader.cppm b/Crafter.Graphics-TextureShader.cppm index 22b530f..03f4c83 100644 --- a/Crafter.Graphics-TextureShader.cppm +++ b/Crafter.Graphics-TextureShader.cppm @@ -4,13 +4,6 @@ module; #include #include #include -#define GLM_FORCE_RADIANS -#define GLM_FORCE_DEPTH_ZERO_TO_ONE -#define GLM_ENABLE_EXPERIMENTAL -#include -#include -#include -#include export module Crafter.Graphics:TextureShader; import Crafter.Component; diff --git a/Crafter.Graphics-VulkanTexture.cppm b/Crafter.Graphics-VulkanTexture.cppm index d8ea3ed..0283d5c 100644 --- a/Crafter.Graphics-VulkanTexture.cppm +++ b/Crafter.Graphics-VulkanTexture.cppm @@ -4,13 +4,6 @@ module; #include #include #include -#define GLM_FORCE_RADIANS -#define GLM_FORCE_DEPTH_ZERO_TO_ONE -#define GLM_ENABLE_EXPERIMENTAL -#include -#include -#include -#include export module Crafter.Graphics:VulkanTexture; import Crafter.Component; diff --git a/Crafter.Graphics-WindowWayland.cpp b/Crafter.Graphics-WindowWayland.cpp index daea4ae..f247a75 100644 --- a/Crafter.Graphics-WindowWayland.cpp +++ b/Crafter.Graphics-WindowWayland.cpp @@ -155,9 +155,11 @@ void keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t t if(window->heldkeys[keypress]) { window->onKeyHold[keypress].Invoke(); } else{ + window->heldkeys[keypress] = true; window->onKeyDown[keypress].Invoke(); } } else{ + window->heldkeys[keypress] = false; window->onKeyUp[keypress].Invoke(); } @@ -196,7 +198,7 @@ void WindowWayland::seat_handle_capabilities(void* data, wl_seat* seat, uint32_t } if (capabilities & WL_SEAT_CAPABILITY_KEYBOARD) { wl_keyboard* keyboard = wl_seat_get_keyboard(seat); - wl_keyboard_add_listener(keyboard, &keyboard_listener, NULL); + wl_keyboard_add_listener(keyboard, &keyboard_listener, window); } } diff --git a/Crafter.Graphics-WindowWaylandVulkan.cpp b/Crafter.Graphics-WindowWaylandVulkan.cpp index 8177da0..8c824ac 100644 --- a/Crafter.Graphics-WindowWaylandVulkan.cpp +++ b/Crafter.Graphics-WindowWaylandVulkan.cpp @@ -439,6 +439,7 @@ void WindowWaylandVulkan::Start() { vkCmdSetScissor(drawCmdBuffers[currentBuffer], 0, 1, &scissor); onDraw.Invoke(drawCmdBuffers[currentBuffer]); + mouseDelta = {0, 0}; VulkanDevice::vkCmdEndRenderingKHRProc(drawCmdBuffers[currentBuffer]);