From 134a779d8d4b09384358e4d94a21af711eb70a09 Mon Sep 17 00:00:00 2001 From: Jakub Domagala Date: Thu, 1 Feb 2024 16:19:40 +0100 Subject: [PATCH] [#190]: Initial work for zoom agnostic Sprites --- assets/shaders/default.vert | 4 +- assets/shaders/frag.spv | Bin 1520 -> 1520 bytes assets/shaders/line.frag.spv | Bin 592 -> 592 bytes assets/shaders/line.vert | 2 + assets/shaders/line.vert.spv | Bin 1424 -> 1568 bytes assets/shaders/ui.frag.spv | Bin 736 -> 736 bytes assets/shaders/ui.vert.spv | Bin 1324 -> 1324 bytes assets/shaders/vert.spv | Bin 3816 -> 4176 bytes editor/editor.cpp | 16 +++++- editor/editor_object.cpp | 4 +- editor/gizmo.cpp | 14 +++-- editor/gui/editor_gui_object.cpp | 2 +- engine/game/enemy.cpp | 5 +- engine/game/game_object.cpp | 4 +- engine/game/level.cpp | 18 +++--- engine/game/player.cpp | 2 +- engine/renderer/camera/camera.cpp | 88 +++++++++++++----------------- engine/renderer/camera/camera.hpp | 31 ++++++----- engine/renderer/renderer.cpp | 49 +++++++++-------- engine/renderer/shader.cpp | 4 ++ engine/renderer/shader.hpp | 10 +--- engine/renderer/sprite.cpp | 27 +++++---- engine/renderer/sprite.hpp | 27 ++++----- engine/renderer/types.hpp | 13 +++++ engine/renderer/vertex.cpp | 6 +- engine/renderer/vertex.hpp | 6 +- engine/renderer/vulkan_common.hpp | 29 +++++++--- 27 files changed, 199 insertions(+), 162 deletions(-) diff --git a/assets/shaders/default.vert b/assets/shaders/default.vert index ae7d038b..3f88dbb2 100644 --- a/assets/shaders/default.vert +++ b/assets/shaders/default.vert @@ -6,6 +6,7 @@ layout(set = 0, binding = 0) uniform UniformBufferObject { mat4 u_projectionMat; + mat4 u_projectionNoZoomMat; mat4 u_viewMat; vec4 u_cameraPos; } @@ -38,6 +39,7 @@ vs_out; layout (push_constant) uniform PushConstants { float selectedIdx; + float meshType; } pushConstants; void @@ -55,5 +57,5 @@ main(void) mat4 modelMat = curInstanceData.modelMat; vec3 position = a_position; - gl_Position = ubo.u_projectionMat * ubo.u_viewMat * modelMat * vec4(position.xyz, 1.0f); + gl_Position = (pushConstants.meshType != 1.0f ? ubo.u_projectionMat : ubo.u_projectionNoZoomMat) * ubo.u_viewMat * modelMat * vec4(position.xyz, 1.0f); } diff --git a/assets/shaders/frag.spv b/assets/shaders/frag.spv index 433842f527f6c72d632842880a6532096607b844..7478986a54b0b976b6bcd5cff2ffb0f8bbfdc56b 100644 GIT binary patch delta 18 Zcmeys{ehd4nMs+Qfq{{Mdn4y_Rsbl*1K|Jw delta 18 Zcmeys{ehd4nMs+Qfq{{MYa{1#Rsbl$1Ka)E`DnMs+Qfq{{Mdn2bi696J(0`LF; delta 18 Zcmcb>a)E`DnMs+Qfq{{MYa^#S696J!0`C9- diff --git a/assets/shaders/line.vert b/assets/shaders/line.vert index cd197a1a..1dc2468d 100644 --- a/assets/shaders/line.vert +++ b/assets/shaders/line.vert @@ -7,7 +7,9 @@ layout(location = 0) in vec3 a_position; layout(set = 0, binding = 0) uniform UniformBufferObject { mat4 u_projectionMat; + mat4 u_projectionNoZoomMat; mat4 u_viewMat; + vec4 u_cameraPos; } ubo; diff --git a/assets/shaders/line.vert.spv b/assets/shaders/line.vert.spv index 9577a0240d779d2d1cf270da98f7e2237d4884bb..1fedd22f43018501a12018458ac2879b9a371065 100644 GIT binary patch delta 214 zcmbQhy?}?4nMs+Qfq{{Mdn4yj#z0O6K?V?yfuS_MpeR2pHMt}+KhG~eDnCEhH?agL z&IW`aHB4YNWtpkvsN&3E@#MtZ)S|?I{Nl-v8Q*$(FtEV&uma^h7+B$aWgx!+O$_KD m1_2~7keMKL2R5H&;$#$tH~^#ohS?`SWVUBy+N{Wu&jeD2 VU}*uMw7}#-7JEj<%^O+r83Dpe4FCWD diff --git a/assets/shaders/ui.frag.spv b/assets/shaders/ui.frag.spv index 98f7d5d235c5afd1f94727bf9d03692477537bee..537fc4805400a10c57844394ee20236483f06e62 100644 GIT binary patch delta 18 ZcmaFB`hb;_nMs+Qfq{{Mdn4y{CIBVF1EK%` delta 18 ZcmaFB`hb;_nMs+Qfq{{MYa{1%CIBVA1EBx_ diff --git a/assets/shaders/ui.vert.spv b/assets/shaders/ui.vert.spv index 5b4aee03e3d50825cd5a78df60e97092cdee765d..766f9378e8706b46d415f8398178741f2cf99415 100644 GIT binary patch delta 18 ZcmZ3(wT6q6nMs+Qfq{{Mdn2bZD*zrA0+Rp$ delta 18 ZcmZ3(wT6q6nMs+Qfq{{MYa^#JD*zr50+Ij# diff --git a/assets/shaders/vert.spv b/assets/shaders/vert.spv index 88d3ad912f545676891a9457d523b1c8f4ce2363..9223289e243d906806037804bab2e86f094ca399 100644 GIT binary patch delta 1019 zcmZ9L!A=ul6ov0}rh}C<5jMuC36X>aYojI&s2k9Q1`>%0E=Z}PEp?dEQYs3Lub_Pr z(}i2M>`Z(Bx9|W)F(Ki6)A?(4l9PM?bI(0<@AU7<&2KYhzc4#t=Gml`?ZcPy_rlfC zLc48nW^vT1Kih9bO8Y+wr(V5(>|amoI`VWkTJ2;js_izC?I+b;^_7eoo#VBq$$FB+ zC`_6#(-bnzUL)%L%ljE$tHx2gx{`FPXufU1Z6>yCf#$Tq{otB6^++W*=Bxxkf328~ zMk|Uz;YLNlGtOymyOMFiXzcMm)}Ze}rWty{{oltbdEfeIHyS?*?uCf{HV(~vap;bO zW$nqZ=-88;ZwWlvdC(1idJ{pmb2x}Et$CH{PeEUP2r9(CoHHOD%J$}uiCPr_%i2~6n|;#*NQxUq^I^u*cN{4b>t7# z*HnBXFvJZ3Z-$Nq_(k(m=tiloWfy3t?40q2k+|Qy6jkImfM9 yZRWyvImvIeqC3NH`d+|*2I@<~C+Vpj2n>LR%^oWr3RQtm&6nYf?p*)Pd%^{X?{WwL delta 603 zcmY+B%}T>S6ov0(l2j{!YF9;tVnJU(P!NSKw02h)qToW(HvY$ItGE&B)}_u%xRpGC zZy@*t?)n0PE&`sDpNfGmbMLw5-powitD|{OTeX6SlA?HW^Xh$A=Gz*nw)xbvc*nX@ zaHTD9n7UoAFtWhyVaE@HzTfH%#1SpsD72T9d2#NWwD57jHr{z&OVs}wzAUB0XQv-Q zJnl1Zc6pQC3j8_KiQEJyK7a-`Kp(Y+*wV$i0Qe+%6uVzaYgvwdCU(_II$;J?G7YNe z>R{yrPFl-yPNK$oU>=KiQE6&l4+BU?faN)`jxt9b1L}1gf}RsV&mzb@r5xhbHsZf_go;Rk2*X diff --git a/editor/editor.cpp b/editor/editor.cpp index f1aab55b..972a8c7b 100644 --- a/editor/editor.cpp +++ b/editor/editor.cpp @@ -899,7 +899,6 @@ Editor::Render(VkCommandBuffer cmdBuffer) currentLevel_->GetSprite().Render(); - gizmo_.Render(); DrawAnimationPoints(); currentLevel_->RenderGameObjects(); @@ -912,6 +911,7 @@ Editor::Render(VkCommandBuffer cmdBuffer) nullptr); renderer::QuadShader::PushConstants pushConstants = {}; pushConstants.selectedIdx = -1.0f; + pushConstants.meshType = 0.0f; if (currentSelectedGameObject_ != Object::INVALID_ID) { @@ -925,6 +925,14 @@ Editor::Render(VkCommandBuffer cmdBuffer) for (int32_t layer = renderer::NUM_LAYERS - 1; layer >= 0; --layer) { + // On-top objects + if (layer == 0) + { + pushConstants.meshType = 1.0f; + vkCmdPushConstants(cmdBuffer, renderData.pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, + sizeof(renderer::QuadShader::PushConstants), &pushConstants); + } + const auto idx = static_cast< size_t >(layer); const auto& numObjects = renderData.numMeshes.at(idx); const auto renderThisLayer = renderLayerToDraw_ == -1 ? true : renderLayerToDraw_ == layer; @@ -967,6 +975,7 @@ Editor::Render(VkCommandBuffer cmdBuffer) vkCmdDrawIndexed(cmdBuffer, renderer::EditorData::numGridLines * renderer::INDICES_PER_LINE, 1, 0, 0, 0); + // DYNAMIC LINES linePushConstants.color = glm::vec4(0.5f, 0.0f, 0.0f, 1.0f); vkCmdPushConstants(cmdBuffer, renderer::EditorData::linePipelineLayout_, VK_SHADER_STAGE_FRAGMENT_BIT, 0, @@ -1409,8 +1418,9 @@ Editor::Update() } auto& renderData = renderer::GetRenderData(); - renderData.viewMat = camera_.GetViewMatrix(); - renderData.projMat = camera_.GetProjectionMatrix(); + renderData.viewMat = camera_.viewMatrix_; + renderData.projMat = camera_.projectionMatrix_; + renderData.projNoZoomMat = camera_.projectionWithoutZoom_; renderer::UpdateData(); } diff --git a/editor/editor_object.cpp b/editor/editor_object.cpp index 5df3074b..81358413 100644 --- a/editor/editor_object.cpp +++ b/editor/editor_object.cpp @@ -14,7 +14,7 @@ EditorObject::EditorObject(Editor* editor, const glm::vec2& positionOnMap, const objectID_(linkedObject), hasLinkedObject_(true) { - auto depth = 0.0f; + auto depth = 0.01f; sprite_.SetSpriteTextured(glm::vec3{position_, depth}, size, sprite); } @@ -224,7 +224,7 @@ EditorObject::Rotate(float angle, bool cumulative) { cumulative ? sprite_.RotateCumulative(angle) : sprite_.Rotate(angle); - auto rotate = sprite_.GetRotation(renderer::Sprite::RotationType::DEGREES); + auto rotate = sprite_.GetRotation(renderer::RotationType::degrees); if (hasLinkedObject_) { switch (Object::GetTypeFromID(objectID_)) diff --git a/editor/gizmo.cpp b/editor/gizmo.cpp index ad513403..8d5d463c 100644 --- a/editor/gizmo.cpp +++ b/editor/gizmo.cpp @@ -1,18 +1,22 @@ #include "gizmo.hpp" +#include "types.hpp" namespace looper { void Gizmo::Initialize() { - gizmoCenter_.SetSpriteTextured(glm::vec3{}, {16, 16}, "centered_move.png"); + gizmoCenter_.SetSpriteTextured(glm::vec3{}, {16, 16}, "centered_move.png", + renderer::SpriteType::alwaysOnTop); gizmoCenter_.SetColor({1.0f, 1.0f, 1.0f, 1.0f}); - gizmoUp_.SetSpriteTextured(glm::vec3{}, {96, 32}, "arrow.png"); - gizmoUp_.Rotate(90.0f, renderer::Sprite::RotationType::DEGREES); + gizmoUp_.SetSpriteTextured(glm::vec3{}, {96, 32}, "arrow.png", + renderer::SpriteType::alwaysOnTop); + gizmoUp_.Rotate(90.0f, renderer::RotationType::degrees); gizmoUp_.SetColor({0.0f, 1.0f, 0.0f, 1.0f}); - gizmoSide_.SetSpriteTextured(glm::vec3{}, {96, 32}, "arrow.png"); + gizmoSide_.SetSpriteTextured(glm::vec3{}, {96, 32}, "arrow.png", + renderer::SpriteType::alwaysOnTop); gizmoSide_.SetColor({1.0f, 0.0f, 0.0f, 1.0f}); centerInitialSize_ = {{16, 16}, {96, 96}}; @@ -57,7 +61,7 @@ Gizmo::Update(const glm::vec2& centeredPos, float rotation) gizmoCenter_.Rotate(currentRotation_); } - gizmoCenter_.SetInitialPosition(glm::vec3{centeredPos, 0.0f}); + gizmoCenter_.SetInitialPosition(glm::vec3{centeredPos, renderer::LAYER_0}); gizmoUp_.SetInitialPosition( gizmoCenter_.GetPosition() diff --git a/editor/gui/editor_gui_object.cpp b/editor/gui/editor_gui_object.cpp index 36cab07c..2001dd3e 100644 --- a/editor/gui/editor_gui_object.cpp +++ b/editor/gui/editor_gui_object.cpp @@ -175,7 +175,7 @@ EditorGUI::RenderGameObjectContent() DrawWidget("Rotate", [&gameObject]() { auto rotation = - gameObject.GetSprite().GetRotation(renderer::Sprite::RotationType::DEGREES); + gameObject.GetSprite().GetRotation(renderer::RotationType::degrees); if (ImGui::InputFloat("##Rotate", &rotation, glm::degrees(renderer::Sprite::ROTATION_RANGE.first), glm::degrees(renderer::Sprite::ROTATION_RANGE.second))) diff --git a/engine/game/enemy.cpp b/engine/game/enemy.cpp index f5fe5e2e..afa3d5a2 100644 --- a/engine/game/enemy.cpp +++ b/engine/game/enemy.cpp @@ -31,7 +31,7 @@ Enemy::Enemy(Application* context, const glm::vec3& pos, const glm::ivec2& size, Enemy::Enemy(Application* context, const glm::vec2& pos, const glm::ivec2& size, const std::string& textureName, const std::vector< AnimationPoint >& keypoints, Animatable::ANIMATION_TYPE animationType) - : Enemy(context, glm::vec3{pos, 0.0f}, size, textureName, keypoints, animationType) + : Enemy(context, glm::vec3{pos, renderer::LAYER_1}, size, textureName, keypoints, animationType) { } @@ -40,7 +40,8 @@ Enemy::Setup(Application* context, const glm::vec2& pos, const glm::ivec2& size, const std::string& textureName, const std::vector< AnimationPoint >& keypoints, Animatable::ANIMATION_TYPE animationType) { - GameObject::Setup(context, glm::vec3{pos, 0.0f}, size, textureName, ObjectType::ENEMY); + GameObject::Setup(context, glm::vec3{pos, renderer::LAYER_1}, size, textureName, + ObjectType::ENEMY); SetAnimationType(animationType); initialPosition_ = currentGameObjectState_.position_; diff --git a/engine/game/game_object.cpp b/engine/game/game_object.cpp index 3c104634..8fb71508 100644 --- a/engine/game/game_object.cpp +++ b/engine/game/game_object.cpp @@ -14,7 +14,7 @@ GameObject::GameObject(Application* application, const glm::vec3& position, cons switch (type) { case looper::ObjectType::OBJECT: { - newPosition.z = 0.1f; + newPosition.z = renderer::LAYER_2; } break; @@ -57,7 +57,7 @@ GameObject::Setup(Application* application, const glm::vec3& position, const glm switch (type) { case looper::ObjectType::OBJECT: { - newPosition.z = 0.1f; + newPosition.z = renderer::LAYER_2; } break; diff --git a/engine/game/level.cpp b/engine/game/level.cpp index ee1a4ad0..deddc5fa 100644 --- a/engine/game/level.cpp +++ b/engine/game/level.cpp @@ -24,7 +24,7 @@ Level::Create(Application* context, const std::string& name, const glm::ivec2& s levelSize_ = size; background_.SetSpriteTextured(glm::vec3(static_cast< float >(levelSize_.x) / 2.0f, - static_cast< float >(levelSize_.y) / 2.0f, 0.3f), + static_cast< float >(levelSize_.y) / 2.0f, renderer::LAYER_10), size, "white.png"); contextPointer_ = context; @@ -64,10 +64,9 @@ Level::Load(Application* context, const std::string& pathToLevel) // const auto weapons = json[key]["weapons"]; const auto& name = player["name"]; - player_.Setup(context, glm::vec3(position[0], position[1], 0.0f), + player_.Setup(context, glm::vec3(position[0], position[1], renderer::LAYER_1), glm::ivec2(size[0], size[1]), texture, name); player_.Rotate(player["rotation"]); - player_.GetSprite().ChangeRenderLayer(player["render_layer"]); } // ENEMIES @@ -87,11 +86,10 @@ Level::Load(Application* context, const std::string& pathToLevel) const auto& name = enemy["name"]; auto& object = enemies_.at(i); - object.Setup(context, glm::vec3(position[0], position[1], 0.0f), + object.Setup(context, glm::vec3(position[0], position[1], renderer::LAYER_1), glm::ivec2(size[0], size[1]), texture, std::vector< AnimationPoint >{}); object.SetName(name); object.Rotate(enemy["rotation"]); - object.GetSprite().ChangeRenderLayer(enemy["render_layer"]); std::vector< AnimationPoint > keypointsPositions = {}; glm::vec2 beginPoint = glm::vec2(position[0], position[1]); @@ -127,12 +125,13 @@ Level::Load(Application* context, const std::string& pathToLevel) const auto& name = object["name"]; auto& gameObject = objects_.at(i); - gameObject.Setup(context, glm::vec3(position[0], position[1], 0.0f), - glm::ivec2(size[0], size[1]), texture, ObjectType::OBJECT); + gameObject.Setup( + context, + glm::vec3(position[0], position[1], renderer::LAYERS.at(object["render_layer"])), + glm::ivec2(size[0], size[1]), texture, ObjectType::OBJECT); objectToIdx_[gameObject.GetID()] = i; gameObject.SetName(name); gameObject.Rotate(object["rotation"]); - gameObject.GetSprite().ChangeRenderLayer(object["render_layer"]); gameObject.SetHasCollision(object["has collision"]); } } @@ -564,7 +563,8 @@ Level::LoadPremade(const std::string& fileName, const glm::ivec2& size) levelSize_ = size; background_.SetSpriteTextured(glm::vec3(static_cast< float >(levelSize_.x) / 2.0f, - static_cast< float >(levelSize_.y) / 2.0f, 0.5f), + static_cast< float >(levelSize_.y) / 2.0f, + renderer::LAYER_10), size, fileName); baseTexture_ = background_.GetTexture()->GetID(); diff --git a/engine/game/player.cpp b/engine/game/player.cpp index 998f67a1..34e31f2b 100644 --- a/engine/game/player.cpp +++ b/engine/game/player.cpp @@ -24,7 +24,7 @@ Player::Player(Application* game, const glm::vec3& position, const glm::ivec2& s Player::Player(Application* game, const glm::vec2& position, const glm::ivec2& size, const std::string& sprite, const std::string& name) - : Player(game, glm::vec3{position, 0.0f}, size, sprite, name) + : Player(game, glm::vec3{position, 0.01f}, size, sprite, name) { } diff --git a/engine/renderer/camera/camera.cpp b/engine/renderer/camera/camera.cpp index 611080c9..f137b0e1 100644 --- a/engine/renderer/camera/camera.cpp +++ b/engine/renderer/camera/camera.cpp @@ -10,42 +10,41 @@ void Camera::Create(const glm::vec3& position, const glm::ivec2& windowSize, const glm::vec3& lookAt, const glm::vec3& upVec, float cameraSpeed) { - m_position = position; - m_lookAtDirection = lookAt; - m_upVector = upVec; - m_cameraSpeed = cameraSpeed; - m_windowSize = windowSize; + position_ = position; + lookAtDirection_ = lookAt; + upVector_ = upVec; + cameraSpeed_ = cameraSpeed; + windowSize_ = windowSize; - const auto left = -m_windowSize.x / 2.0f; - const auto right = m_windowSize.x / 2.0f; - const auto top = m_windowSize.y / 2.0f; - const auto bottom = -m_windowSize.y / 2.0f; + const auto left = -windowSize_.x / 2.0f; + const auto right = windowSize_.x / 2.0f; + const auto top = windowSize_.y / 2.0f; + const auto bottom = -windowSize_.y / 2.0f; - m_viewMatrix = glm::lookAt(m_position, m_position + m_lookAtDirection, m_upVector); + viewMatrix_ = glm::lookAt(position_, position_ + lookAtDirection_, upVector_); // NOLINTNEXTLINE top and bottom swapped intentionally - m_projectionMatrix = glm::ortho(left, right, top, bottom, nearPlane_, farPlane_); - - m_viewProjectionMatrix = m_projectionMatrix * m_viewMatrix; + projectionMatrix_ = glm::ortho(left, right, top, bottom, nearPlane_, farPlane_); + projectionWithoutZoom_ = projectionMatrix_; } void Camera::SetLevelSize(const glm::vec2& size) { - m_levelSize = size; + levelSize_ = size; } void Camera::SetProjection(float left, float right, float top, float bottom) { // NOLINTNEXTLINE top and bottom swapped intentionally - m_projectionMatrix = glm::ortho(left, right, top, bottom, nearPlane_, farPlane_); + projectionMatrix_ = glm::ortho(left, right, top, bottom, nearPlane_, farPlane_); } void Camera::SetCameraAtPosition(const glm::vec3& globalPosition) { - m_position = globalPosition; + position_ = globalPosition; UpdateViewMatrix(); } @@ -58,10 +57,10 @@ Camera::SetCameraAtPosition(const glm::vec2& globalPosition) void Camera::Move(const glm::vec3& conventionalVector) { - m_position += ConvertToCameraVector(conventionalVector) * m_cameraSpeed; + position_ += ConvertToCameraVector(conventionalVector) * cameraSpeed_; - m_position = glm::vec3(glm::clamp(m_position.x, -m_levelSize.x, m_levelSize.x), - glm::clamp(m_position.y, -m_levelSize.y, m_levelSize.y), 0.0f); + position_ = glm::vec3(glm::clamp(position_.x, -levelSize_.x, levelSize_.x), + glm::clamp(position_.y, -levelSize_.y, levelSize_.y), 0.0f); UpdateViewMatrix(); } @@ -71,17 +70,17 @@ Camera::Rotate(float angle, bool cumulative) { if (cumulative) { - m_rotationValue += angle; + rotationValue_ += angle; } else { - m_upVector = glm::rotateZ(m_upVector, -m_rotationValue); + upVector_ = glm::rotateZ(upVector_, -rotationValue_); UpdateViewMatrix(); - m_rotationValue = angle; + rotationValue_ = angle; } - - m_upVector = glm::rotateZ(m_upVector, angle); + + upVector_ = glm::rotateZ(upVector_, angle); UpdateViewMatrix(); } @@ -89,64 +88,56 @@ Camera::Rotate(float angle, bool cumulative) void Camera::Zoom(float value) { - const auto oldZoom = m_zoomScale; - m_zoomScale = glm::clamp(m_zoomScale + (value * m_zoomSpeed), m_maxZoomOut, m_maxZoomIn); + const auto oldZoom = zoomScale_; + zoomScale_ = glm::clamp(zoomScale_ + (value * zoomSpeed_), maxZoomOut_, maxZoomIn_); - if (oldZoom != m_zoomScale) + if (oldZoom != zoomScale_) { - const auto left = -m_windowSize.x / (2.0f + m_zoomScale); - const auto right = m_windowSize.x / (2.0f + m_zoomScale); - const auto top = m_windowSize.y / (2.0f + m_zoomScale); - const auto bottom = -m_windowSize.y / (2.0f + m_zoomScale); + const auto left = -windowSize_.x / (2.0f + zoomScale_); + const auto right = windowSize_.x / (2.0f + zoomScale_); + const auto top = windowSize_.y / (2.0f + zoomScale_); + const auto bottom = -windowSize_.y / (2.0f + zoomScale_); // NOLINTNEXTLINE top and bottom swapped intentionally - m_projectionMatrix = glm::ortho(left, right, top, bottom, nearPlane_, farPlane_); - - m_viewProjectionMatrix = m_projectionMatrix * m_viewMatrix; + projectionMatrix_ = glm::ortho(left, right, top, bottom, nearPlane_, farPlane_); } } float Camera::GetZoomLevel() const { - return m_zoomScale; + return zoomScale_; } const glm::mat4& Camera::GetViewMatrix() const { - return m_viewMatrix; + return viewMatrix_; } const glm::mat4& Camera::GetProjectionMatrix() const { - return m_projectionMatrix; -} - -const glm::mat4& -Camera::GetViewProjectionMatrix() const -{ - return m_viewProjectionMatrix; + return projectionMatrix_; } const glm::vec3& Camera::GetPosition() const { - return m_position; + return position_; } float Camera::GetRotation() const { - return m_rotationValue; + return rotationValue_; } glm::vec3 Camera::ConvertToCameraVector(const glm::vec3& conventionalVector) const { - const auto xAxesVector = glm::cross(m_lookAtDirection, m_upVector); - const auto yAxesVector = m_upVector; + const auto xAxesVector = glm::cross(lookAtDirection_, upVector_); + const auto yAxesVector = upVector_; return conventionalVector.x * xAxesVector + conventionalVector.y * -yAxesVector; } @@ -154,8 +145,7 @@ Camera::ConvertToCameraVector(const glm::vec3& conventionalVector) const void Camera::UpdateViewMatrix() { - m_viewMatrix = glm::lookAt(m_position, m_position + m_lookAtDirection, m_upVector); - m_viewProjectionMatrix = m_projectionMatrix * m_viewMatrix; + viewMatrix_ = glm::lookAt(position_, position_ + lookAtDirection_, upVector_); } } // namespace looper::renderer \ No newline at end of file diff --git a/engine/renderer/camera/camera.hpp b/engine/renderer/camera/camera.hpp index e27d3f20..9226dc6a 100644 --- a/engine/renderer/camera/camera.hpp +++ b/engine/renderer/camera/camera.hpp @@ -68,30 +68,31 @@ class Camera [[nodiscard]] glm::vec3 ConvertToCameraVector(const glm::vec3& conventionalVector) const; + public: + glm::mat4 viewMatrix_ = {}; + glm::mat4 projectionMatrix_ = {}; + glm::mat4 projectionWithoutZoom_ = {}; + private: void UpdateViewMatrix(); - float m_cameraSpeed = 0.0f; - float m_rotationValue = 0.0f; - float m_zoomSpeed = 0.10f; - float m_zoomScale = 0.0f; - float m_maxZoomIn = 2.5f; - float m_maxZoomOut = -1.9f; + float cameraSpeed_ = 0.0f; + float rotationValue_ = 0.0f; + float zoomSpeed_ = 0.10f; + float zoomScale_ = 0.0f; + float maxZoomIn_ = 2.5f; + float maxZoomOut_ = -1.9f; const float nearPlane_ = 1.0f; const float farPlane_ = -1.0f; - glm::vec2 m_levelSize = {}; - glm::vec2 m_windowSize = {}; - - glm::vec3 m_position = {}; - glm::vec3 m_upVector = {}; - glm::vec3 m_lookAtDirection = {}; + glm::vec2 levelSize_ = {}; + glm::vec2 windowSize_ = {}; - glm::mat4 m_viewMatrix = {}; - glm::mat4 m_projectionMatrix = {}; - glm::mat4 m_viewProjectionMatrix = {}; + glm::vec3 position_ = {}; + glm::vec3 upVector_ = {}; + glm::vec3 lookAtDirection_ = {}; }; } // namespace looper::renderer \ No newline at end of file diff --git a/engine/renderer/renderer.cpp b/engine/renderer/renderer.cpp index d48e08f5..8806729f 100644 --- a/engine/renderer/renderer.cpp +++ b/engine/renderer/renderer.cpp @@ -26,10 +26,12 @@ namespace looper::renderer { namespace { bool initialized_ = false; bool updateDescriptors_ = false; -bool updatePerInstanceBuffer_ = false; + bool updateVertexBuffer_ = false; std::vector< uint32_t > updatedObjects_ = {}; std::vector< int32_t > renderLayersChanged_ = {}; +bool updatePerInstanceBuffer_ = false; + VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo_ = {}; VkQueue presentQueue_ = {}; @@ -856,15 +858,16 @@ CreatePerInstanceBuffer() for (size_t frame = 0; frame < MAX_FRAMES_IN_FLIGHT; frame++) { - auto& sbo = renderData.ssbo.at(frame); - if (sbo.buffer_ != VK_NULL_HANDLE) - { - sbo.Destroy(); - } + auto& sbo = renderData.ssbo.at(frame); + if (sbo.buffer_ != VK_NULL_HANDLE) + { + sbo.Destroy(); + } - sbo = Buffer::CreateBuffer(SSBObufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, - VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT - | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); + sbo = Buffer::CreateBuffer(SSBObufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT + | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); + } } @@ -881,6 +884,7 @@ UpdateUniformBuffer() tmpUBO.view = renderData.viewMat; tmpUBO.proj = renderData.projMat; + tmpUBO.projNoZoom = renderData.projNoZoomMat; auto& ubo = renderData.uniformBuffers.at(frame); @@ -1033,7 +1037,7 @@ MeshLoaded(const std::vector< Vertex >& vertices_in, const TextureIDs& textures_ { auto* renderData = &Data::renderData_[boundApplication_]; // convert from depth value to render layer - const auto layer = static_cast< int32_t >(vertices_in.front().m_position.z * 20.0f); + const auto layer = static_cast< int32_t >(vertices_in.front().position_.z * 20.0f); int32_t idx = 0; auto& vertices = renderData->vertices.at(static_cast< size_t >(layer)); @@ -1059,7 +1063,7 @@ MeshLoaded(const std::vector< Vertex >& vertices_in, const TextureIDs& textures_ auto& vertex = vertices.at(offset + vertexIdx); vertex = vertices_in.at(vertexIdx); - vertex.m_texCoordsDraw.z = static_cast< float >(idx); + vertex.texCoordsDraw_.z = static_cast< float >(idx); } UpdateDescriptors(); @@ -1083,20 +1087,18 @@ void SubmitMeshData(const uint32_t idx, const TextureIDs& ids, const glm::mat4& modelMat, const glm::vec4& color) { - auto& object = Data::renderData_[boundApplication_].perInstance.at(idx); - - object.model = modelMat; - object.color = color; - object.texSamples.x = static_cast< float >(ids.at(0)); - object.texSamples.y = static_cast< float >(ids.at(1)); - object.texSamples.z = static_cast< float >(ids.at(2)); - object.texSamples.w = static_cast< float >(ids.at(3)); - - updatePerInstanceBuffer_ = true; - updatedObjects_.push_back(idx); + auto& object = Data::renderData_[boundApplication_].perInstance.at(idx); + object.model = modelMat; + object.color = color; + object.texSamples.x = static_cast< float >(ids.at(0)); + object.texSamples.y = static_cast< float >(ids.at(1)); + object.texSamples.z = static_cast< float >(ids.at(2)); + object.texSamples.w = static_cast< float >(ids.at(3)); + + updatePerInstanceBuffer_ = true; + updatedObjects_.push_back(idx); } - void CreateLinePipeline() { @@ -1203,6 +1205,7 @@ RecreateQuadPipeline() CreateRenderPipeline(); CreateQuadBuffers(); + QuadShader::CreateDescriptorPool(); QuadShader::CreateDescriptorSets(); QuadShader::UpdateDescriptorSets(); diff --git a/engine/renderer/shader.cpp b/engine/renderer/shader.cpp index d1fa882c..4e2a7c4d 100644 --- a/engine/renderer/shader.cpp +++ b/engine/renderer/shader.cpp @@ -11,6 +11,7 @@ namespace looper::renderer { namespace { + VkShaderModule CreateShaderModule(VkDevice device, std::vector< char >&& shaderByteCode) { @@ -52,6 +53,9 @@ VulkanShader::CreateShader(VkDevice device, std::string_view vertex, std::string return {{device, vertShaderStageInfo}, {device, fragShaderStageInfo}}; } +/////////////////////////////////////////////////////////////////// +/////////////////////// QUAD SHADER /////////////////////// +/////////////////////////////////////////////////////////////////// void QuadShader::CreateDescriptorPool() { diff --git a/engine/renderer/shader.hpp b/engine/renderer/shader.hpp index ebf56cb6..85261cfe 100644 --- a/engine/renderer/shader.hpp +++ b/engine/renderer/shader.hpp @@ -35,15 +35,6 @@ struct VulkanShader static std::pair< VertexShaderInfo, FragmentShaderInfo > CreateShader(VkDevice device, std::string_view vertex, std::string_view fragment); - - static void - CreateDescriptorPool(); - - static void - CreateDescriptorSetLayout(); - - static void - CreateDescriptorSets(); }; struct QuadShader @@ -55,6 +46,7 @@ struct QuadShader struct PushConstants { float selectedIdx = {}; + float meshType = {}; }; static void diff --git a/engine/renderer/sprite.cpp b/engine/renderer/sprite.cpp index dd382157..73af5872 100644 --- a/engine/renderer/sprite.cpp +++ b/engine/renderer/sprite.cpp @@ -35,7 +35,7 @@ Sprite::ChangeRenderLayer(int32_t newLayer) for (auto& vertex : vertices_) { - vertex.m_position.z = static_cast< float >(newLayer) / 20.0f; + vertex.position_.z = LAYERS.at(newLayer); } const auto transformMat = ComputeModelMat(); @@ -45,11 +45,12 @@ Sprite::ChangeRenderLayer(int32_t newLayer) } void -Sprite::SetSprite(const glm::vec3& position, const glm::vec2& size) +Sprite::SetSprite(const glm::vec3& position, const glm::vec2& size, SpriteType t) { // m_texture = std::make_shared< Texture >(); // m_texture->CreateColorTexture(size, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); + type_ = t; currentState_.currentPosition_ = position; initialPosition_ = position; currentState_.translateVal_ = position; @@ -61,9 +62,10 @@ Sprite::SetSprite(const glm::vec3& position, const glm::vec2& size) void Sprite::SetSpriteTextured(const glm::vec3& position, const glm::vec2& size, - const std::string& fileName) + const std::string& fileName, SpriteType t) { changed_ = true; + type_ = t; initialPosition_ = position; currentState_.currentPosition_ = position; @@ -101,14 +103,15 @@ Sprite::SetSpriteTextured(const glm::vec3& position, const glm::vec2& size, TextureLibrary::GetTexture(fileName)->GetID(), TextureLibrary::GetTexture(fileName)->GetID()}; + renderInfo_ = MeshLoaded(vertices_, textures_, transformMat, currentState_.color_); } void Sprite::SetSpriteTextured(const glm::vec2& position, const glm::vec2& size, - const std::string& fileName) + const std::string& fileName, SpriteType t) { - SetSpriteTextured(glm::vec3{position, 0.0f}, size, fileName); + SetSpriteTextured(glm::vec3{position, 0.0f}, size, fileName, t); } void @@ -193,7 +196,7 @@ Sprite::GetTranslation() const float Sprite::GetRotation(RotationType type) const { - return type == RotationType::DEGREES ? glm::degrees(currentState_.angle_) : currentState_.angle_; + return type == RotationType::degrees ? glm::degrees(currentState_.angle_) : currentState_.angle_; } glm::vec2& @@ -287,7 +290,7 @@ Sprite::GetTexture() const void Sprite::Rotate(float angle, RotationType type) { - currentState_.angle_ = type == RotationType::DEGREES ? glm::radians(angle) : angle; + currentState_.angle_ = type == RotationType::degrees ? glm::radians(angle) : angle; currentState_.angle_ = glm::clamp(currentState_.angle_, glm::radians(-360.0f), glm::radians(360.0f)); @@ -297,7 +300,7 @@ Sprite::Rotate(float angle, RotationType type) void Sprite::RotateCumulative(float angle, RotationType type) { - currentState_.angle_ += type == RotationType::DEGREES ? glm::radians(angle) : angle; + currentState_.angle_ += type == RotationType::degrees ? glm::radians(angle) : angle; currentState_.angle_ = glm::clamp(currentState_.angle_, glm::radians(-360.0f), glm::radians(360.0f)); @@ -357,10 +360,10 @@ Sprite::ComputeBoundingBox() * glm::rotate(glm::mat4(1.0f), currentState_.angle_, {0.0f, 0.0f, 1.0f}) * glm::scale(glm::mat4(1.0f), {size_, 1.0f}); - boundingBox_.at(1) = transformMat * glm::vec4(vertices_[0].m_position, 1.0f); - boundingBox_.at(2) = transformMat * glm::vec4(vertices_[3].m_position, 1.0f); - boundingBox_.at(0) = transformMat * glm::vec4(vertices_[1].m_position, 1.0f); - boundingBox_.at(3) = transformMat * glm::vec4(vertices_[2].m_position, 1.0f); + boundingBox_.at(1) = transformMat * glm::vec4(vertices_[0].position_, 1.0f); + boundingBox_.at(2) = transformMat * glm::vec4(vertices_[3].position_, 1.0f); + boundingBox_.at(0) = transformMat * glm::vec4(vertices_[1].position_, 1.0f); + boundingBox_.at(3) = transformMat * glm::vec4(vertices_[2].position_, 1.0f); } std::array< glm::vec2, 4 > diff --git a/engine/renderer/sprite.hpp b/engine/renderer/sprite.hpp index 80bd5bf0..d876a0e6 100644 --- a/engine/renderer/sprite.hpp +++ b/engine/renderer/sprite.hpp @@ -15,12 +15,6 @@ namespace looper::renderer { class Sprite { public: - enum class RotationType - { - DEGREES, - RADIANS - }; - struct Modifiers { glm::vec2 scale = {1.0f, 1.0f}; @@ -32,18 +26,19 @@ class Sprite // Create sprite without texture void SetSprite(const glm::vec3& position = glm::vec3(0.0f, 0.0f, 0.0f), - const glm::vec2& size = glm::vec2(128, 128)); + const glm::vec2& size = glm::vec2(128, 128), SpriteType = SpriteType::regular); // Create sprite with texture void SetSpriteTextured(const glm::vec2& position = glm::vec2(0.0f, 0.0f), const glm::vec2& size = glm::vec2(128, 128), - const std::string& fileName = "Default128.png"); - + const std::string& fileName = "Default128.png", + SpriteType = SpriteType::regular); void SetSpriteTextured(const glm::vec3& position = glm::vec3(0.0f, 0.0f, 0.0f), const glm::vec2& size = glm::vec2(128, 128), - const std::string& fileName = "Default128.png"); + const std::string& fileName = "Default128.png", + SpriteType = SpriteType::regular); void SetColor(const glm::vec4& color); @@ -63,7 +58,8 @@ class Sprite [[nodiscard]] glm::vec2 GetSize() const; - void SetSize(const glm::vec2& newSize); + void + SetSize(const glm::vec2& newSize); [[nodiscard]] glm::vec2 GetOriginalSize() const; @@ -75,7 +71,7 @@ class Sprite GetTranslation() const; [[nodiscard]] float - GetRotation(RotationType type = RotationType::RADIANS) const; + GetRotation(RotationType type = RotationType::radians) const; [[nodiscard]] glm::mat4 ComputeModelMat() const; @@ -97,11 +93,11 @@ class Sprite // Set rotation angle void - Rotate(float angle, RotationType type = RotationType::RADIANS); + Rotate(float angle, RotationType type = RotationType::radians); // Add 'angle' value to current rotation angle void - RotateCumulative(float angle, RotationType type = RotationType::RADIANS); + RotateCumulative(float angle, RotationType type = RotationType::radians); void Scale(const glm::vec2& scaleValue); @@ -193,9 +189,10 @@ class Sprite StateList< State > statesQueue_ = {}; State currentState_ = {}; + SpriteType type_ = SpriteType::regular; // sprite's texture - TextureIDs textures_ = {}; + TextureIDs textures_ = {}; // width and height glm::vec2 size_ = {}; diff --git a/engine/renderer/types.hpp b/engine/renderer/types.hpp index 20bd3de7..f99a6e5a 100644 --- a/engine/renderer/types.hpp +++ b/engine/renderer/types.hpp @@ -39,6 +39,18 @@ enum class PrimitiveType LINE }; +enum class RotationType +{ + degrees, + radians +}; + +enum class SpriteType +{ + regular, + alwaysOnTop +}; + enum class ApplicationType { EDITOR, @@ -61,6 +73,7 @@ using TextureIDs = std::array< TextureID, 4 >; struct UniformBufferObject { alignas(16) glm::mat4 proj = {}; + alignas(16) glm::mat4 projNoZoom = {}; alignas(16) glm::mat4 view = {}; glm::vec4 cameraPos = {}; }; diff --git a/engine/renderer/vertex.cpp b/engine/renderer/vertex.cpp index 4522c809..26cd5713 100644 --- a/engine/renderer/vertex.cpp +++ b/engine/renderer/vertex.cpp @@ -22,7 +22,7 @@ LineVertex::getAttributeDescriptions() attributeDescriptions[0].binding = 0; attributeDescriptions[0].location = 0; attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT; - attributeDescriptions[0].offset = offsetof(LineVertex, m_position); + attributeDescriptions[0].offset = offsetof(LineVertex, position_); return attributeDescriptions; } @@ -46,12 +46,12 @@ Vertex::getAttributeDescriptions() attributeDescriptions[0].binding = 0; attributeDescriptions[0].location = 0; attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT; - attributeDescriptions[0].offset = offsetof(Vertex, m_position); + attributeDescriptions[0].offset = offsetof(Vertex, position_); attributeDescriptions[1].binding = 0; attributeDescriptions[1].location = 1; attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT; - attributeDescriptions[1].offset = offsetof(Vertex, m_texCoordsDraw); + attributeDescriptions[1].offset = offsetof(Vertex, texCoordsDraw_); return attributeDescriptions; } diff --git a/engine/renderer/vertex.hpp b/engine/renderer/vertex.hpp index c1c57b48..4ad0f2db 100644 --- a/engine/renderer/vertex.hpp +++ b/engine/renderer/vertex.hpp @@ -8,7 +8,7 @@ namespace looper::renderer { struct LineVertex { - glm::vec3 m_position; + glm::vec3 position_; static VkVertexInputBindingDescription getBindingDescription(); @@ -19,8 +19,8 @@ struct LineVertex struct Vertex { - glm::vec3 m_position; - glm::vec3 m_texCoordsDraw; // texcoords and drawID + glm::vec3 position_; + glm::vec3 texCoordsDraw_; // texcoords and drawID static VkVertexInputBindingDescription getBindingDescription(); diff --git a/engine/renderer/vulkan_common.hpp b/engine/renderer/vulkan_common.hpp index acd8de2f..08f7d00b 100644 --- a/engine/renderer/vulkan_common.hpp +++ b/engine/renderer/vulkan_common.hpp @@ -103,12 +103,28 @@ vk_check_error(VkResult vkResult, std::string_view errorMessage) } } + +inline constexpr uint32_t NUM_LAYERS = 11; +inline constexpr float LAYER_0 = 0.00f; +inline constexpr float LAYER_1 = 0.05f; +inline constexpr float LAYER_2 = 0.10f; +inline constexpr float LAYER_3 = 0.15f; +inline constexpr float LAYER_4 = 0.20f; +inline constexpr float LAYER_5 = 0.25f; +inline constexpr float LAYER_6 = 0.30f; +inline constexpr float LAYER_7 = 0.35f; +inline constexpr float LAYER_8 = 0.40f; +inline constexpr float LAYER_9 = 0.45f; +inline constexpr float LAYER_10 = 0.50f; +inline constexpr std::array< float, NUM_LAYERS > LAYERS = {LAYER_0, LAYER_1, LAYER_2, LAYER_3, + LAYER_4, LAYER_5, LAYER_6, LAYER_7, + LAYER_8, LAYER_9, LAYER_10}; + static constexpr uint32_t VERTICES_PER_SPRITE = 4; static constexpr uint32_t INDICES_PER_SPRITE = 6; static constexpr uint32_t MAX_SPRITES_PER_LAYER = 100000; static constexpr size_t MAX_NUM_VERTICES_PER_LAYER = MAX_SPRITES_PER_LAYER * VERTICES_PER_SPRITE; static constexpr size_t MAX_NUM_INDICES_PER_LAYER = MAX_SPRITES_PER_LAYER * INDICES_PER_SPRITE; -inline constexpr uint32_t NUM_LAYERS = 11; static constexpr size_t MAX_NUM_SPRITES = MAX_SPRITES_PER_LAYER * NUM_LAYERS; static constexpr uint32_t MAX_NUM_TEXTURES = 256; static constexpr uint32_t INDICES_PER_LINE = 2; @@ -148,7 +164,7 @@ struct RenderData std::array< std::vector< uint32_t >, NUM_LAYERS > deletedObjs_ = {}; //////////////////////////////////////////////////////////////////////// - // RENDER LAYERS (from near to far (values ranging from 0.0 to -0.9)) + // RENDER LAYERS (from near 0.0 to far -0.9 // //////////////////////////////////////////////////////////////////////// // Vertex @@ -164,16 +180,14 @@ struct RenderData std::array< uint32_t, NUM_LAYERS > numMeshes = {}; uint32_t totalNumMeshes = {}; - - //////////////////////////////////// - // // - //////////////////////////////////// + ///////////////////////////////////// + // VULKAN DATA // + ///////////////////////////////////// // SSBO (PerInstanceBuffer) std::vector< PerInstanceBuffer > perInstance = {}; std::array< Buffer, MAX_FRAMES_IN_FLIGHT > ssbo = {}; - // UBO (UniformBufferObject) std::vector< Buffer > uniformBuffers{MAX_FRAMES_IN_FLIGHT}; @@ -209,6 +223,7 @@ struct RenderData glm::mat4 viewMat = {}; glm::mat4 projMat = {}; + glm::mat4 projNoZoomMat = {}; }; /*