This commit is contained in:
Jorijn van der Graaf 2025-05-05 06:00:35 +02:00
commit cb848e7109
7 changed files with 16 additions and 19 deletions

View file

@ -8,14 +8,15 @@ module;
module Crafter.Graphics; module Crafter.Graphics;
import Crafter.Math; import Crafter.Math;
import Crafter.Event;
using namespace Crafter; using namespace Crafter;
Camera::Camera(float fov, float aspectRatio, float near, float far) { Camera::Camera(float fov, float aspectRatio, float near, float far) {
projection = MatrixRowMajor<float, 4, 4, 1>::Perspective(fov, aspectRatio, near, far); projection = MatrixRowMajor<float, 4, 4, 1>::Perspective(fov, aspectRatio, near, far);
view = MatrixRowMajor<float, 4, 4, 1>::Rotation(0, 0, ToRadian(180.0f))*MatrixRowMajor<float, 4, 4, 1>::Translation(1.0f, 0.0f, -10.0f); view = MatrixRowMajor<float, 4, 4, 1>::Identity();
Update();
} }
void Camera::Update() { void Camera::Update() {
projectionView = projection*view; projectionView = projection*view;
onUpdate.Invoke();
} }

View file

@ -6,6 +6,7 @@ export module Crafter.Graphics:Camera;
import :VulkanBuffer; import :VulkanBuffer;
import Crafter.Component; import Crafter.Component;
import Crafter.Math; import Crafter.Math;
import Crafter.Event;
namespace Crafter { namespace Crafter {
export class Camera : public Component { export class Camera : public Component {
@ -13,6 +14,7 @@ namespace Crafter {
MatrixRowMajor<float, 4, 4, 1> projection; MatrixRowMajor<float, 4, 4, 1> projection;
MatrixRowMajor<float, 4, 4, 1> view; MatrixRowMajor<float, 4, 4, 1> view;
MatrixRowMajor<float, 4, 4, 1> projectionView; MatrixRowMajor<float, 4, 4, 1> projectionView;
Event<void> onUpdate;
Camera(float fov, float aspectRatio, float near, float far); Camera(float fov, float aspectRatio, float near, float far);
void Update(); void Update();
}; };

View file

@ -15,14 +15,19 @@ import Crafter.Math;
namespace Crafter { namespace Crafter {
export template <typename VertexType> export template <typename VertexType>
class MeshShader { class MeshShader : public Component {
public: public:
MatrixRowMajor<float, 4, 4, 1> transform; MatrixRowMajor<float, 4, 4, 1> transform;
ComponentRefOwning<Mesh<VertexType>> mesh; ComponentRefOwning<Mesh<VertexType>> mesh;
ComponentRefOwning<Camera> camera; ComponentRefOwning<Camera> camera;
Buffer<MatrixRowMajor<float, 4, 4, 1>> mvp; Buffer<MatrixRowMajor<float, 4, 4, 1>> mvp;
std::uint32_t threadCount; std::uint32_t threadCount;
MeshShader(Mesh<VertexType>* mesh, Camera* camera) : threadCount(std::ceil(static_cast<double>(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<void> cameraUpdate;
MeshShader(Mesh<VertexType>* mesh, Camera* camera) : threadCount(std::ceil(static_cast<double>(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<float, 4, 4, 1>::Identity(); transform = MatrixRowMajor<float, 4, 4, 1>::Identity();
} }
void WriteDescriptors(DescriptorSet& set) { void WriteDescriptors(DescriptorSet& set) {

View file

@ -4,13 +4,6 @@ module;
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtc/type_ptr.hpp>
export module Crafter.Graphics:TextureShader; export module Crafter.Graphics:TextureShader;
import Crafter.Component; import Crafter.Component;

View file

@ -4,13 +4,6 @@ module;
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtc/type_ptr.hpp>
export module Crafter.Graphics:VulkanTexture; export module Crafter.Graphics:VulkanTexture;
import Crafter.Component; import Crafter.Component;

View file

@ -155,9 +155,11 @@ void keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t t
if(window->heldkeys[keypress]) { if(window->heldkeys[keypress]) {
window->onKeyHold[keypress].Invoke(); window->onKeyHold[keypress].Invoke();
} else{ } else{
window->heldkeys[keypress] = true;
window->onKeyDown[keypress].Invoke(); window->onKeyDown[keypress].Invoke();
} }
} else{ } else{
window->heldkeys[keypress] = false;
window->onKeyUp[keypress].Invoke(); 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) { if (capabilities & WL_SEAT_CAPABILITY_KEYBOARD) {
wl_keyboard* keyboard = wl_seat_get_keyboard(seat); wl_keyboard* keyboard = wl_seat_get_keyboard(seat);
wl_keyboard_add_listener(keyboard, &keyboard_listener, NULL); wl_keyboard_add_listener(keyboard, &keyboard_listener, window);
} }
} }

View file

@ -439,6 +439,7 @@ void WindowWaylandVulkan::Start() {
vkCmdSetScissor(drawCmdBuffers[currentBuffer], 0, 1, &scissor); vkCmdSetScissor(drawCmdBuffers[currentBuffer], 0, 1, &scissor);
onDraw.Invoke(drawCmdBuffers[currentBuffer]); onDraw.Invoke(drawCmdBuffers[currentBuffer]);
mouseDelta = {0, 0};
VulkanDevice::vkCmdEndRenderingKHRProc(drawCmdBuffers[currentBuffer]); VulkanDevice::vkCmdEndRenderingKHRProc(drawCmdBuffers[currentBuffer]);