From 3c7346cf7b26cedca3a84375a8659dd71eb58410 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Wed, 30 Nov 2022 10:19:45 +0100 Subject: [PATCH] Moving projection to viewport --- src/Engine/Actor/Entity.h | 4 +- src/Engine/Asset/MeshAsset.cpp | 6 +-- src/Engine/Asset/MeshAsset.h | 7 ++- src/Engine/Asset/MeshLoader.cpp | 23 +++++++++- src/Engine/Component/Camera.cpp | 29 +------------ src/Engine/Component/Camera.h | 13 ------ src/Engine/Component/StaticMesh.h | 6 +-- src/Engine/Graphics/GraphicsInitializer.h | 1 + src/Engine/Graphics/GraphicsResources.cpp | 13 ++++++ src/Engine/Graphics/GraphicsResources.h | 12 +++--- src/Engine/Graphics/RenderPass/BasePass.cpp | 2 +- .../Graphics/RenderPass/DepthPrepass.cpp | 2 +- .../Graphics/RenderPass/LightCullingPass.cpp | 2 +- src/Engine/Graphics/RenderPass/TextPass.cpp | 5 ++- src/Engine/Graphics/Vulkan/VulkanViewport.cpp | 2 +- src/Engine/Material/MaterialAsset.cpp | 1 + src/Engine/Scene/Scene.cpp | 43 +++++++++++-------- src/Engine/UI/System.cpp | 2 +- 18 files changed, 87 insertions(+), 86 deletions(-) diff --git a/src/Engine/Actor/Entity.h b/src/Engine/Actor/Entity.h index 0bbac17..431bed9 100644 --- a/src/Engine/Actor/Entity.h +++ b/src/Engine/Actor/Entity.h @@ -13,9 +13,9 @@ public: virtual ~Entity(); template - void attachComponent(Args... args) + Component& attachComponent(Args... args) { - scene->attachComponent(identifier, args...); + return scene->attachComponent(identifier, args...); } protected: PScene scene; diff --git a/src/Engine/Asset/MeshAsset.cpp b/src/Engine/Asset/MeshAsset.cpp index da07387..c9743b0 100644 --- a/src/Engine/Asset/MeshAsset.cpp +++ b/src/Engine/Asset/MeshAsset.cpp @@ -30,11 +30,11 @@ void MeshAsset::load() void MeshAsset::addMesh(PMesh mesh) { std::scoped_lock lck(lock); - meshes.push_back(mesh); - referencedMaterials.push_back(mesh->referencedMaterial); + meshes.add(mesh); + referencedMaterials.add(mesh->referencedMaterial); } -const std::vector MeshAsset::getMeshes() +const Array MeshAsset::getMeshes() { std::scoped_lock lck(lock); return meshes; diff --git a/src/Engine/Asset/MeshAsset.h b/src/Engine/Asset/MeshAsset.h index 39e8033..99cd912 100644 --- a/src/Engine/Asset/MeshAsset.h +++ b/src/Engine/Asset/MeshAsset.h @@ -15,11 +15,10 @@ public: virtual void save() override; virtual void load() override; void addMesh(PMesh mesh); - const std::vector getMeshes(); + const Array getMeshes(); //Workaround while no editor - std::vector referencedMaterials; -private: - std::vector meshes; + Array referencedMaterials; + Array meshes; }; DEFINE_REF(MeshAsset) } // namespace Seele \ No newline at end of file diff --git a/src/Engine/Asset/MeshLoader.cpp b/src/Engine/Asset/MeshLoader.cpp index 48fd5c4..0861398 100644 --- a/src/Engine/Asset/MeshLoader.cpp +++ b/src/Engine/Asset/MeshLoader.cpp @@ -61,6 +61,10 @@ void MeshLoader::loadMaterials(const aiScene* scene, Array& glob }; matCode["code"]["baseColor"] = "return diffuseTexture.Sample(textureSampler, input.texCoords[0]).xyz;"; } + else + { + matCode["code"]["baseColor"] = "return input.vertexColor.xyz;"; + } if(material->GetTexture(aiTextureType_SPECULAR, 0, &texPath) == AI_SUCCESS) { std::string texFilename = std::filesystem::path(texPath.C_Str()).replace_extension("asset").stem().string(); @@ -143,6 +147,23 @@ VertexStreamComponent createVertexStream(uint32 size, aiVector2D* sourceData, Gf vertexBuffer->transferOwnership(Gfx::QueueType::GRAPHICS); return VertexStreamComponent(vertexBuffer, 0, vbInfo.vertexSize, Gfx::SE_FORMAT_R32G32_SFLOAT); } +VertexStreamComponent createVertexStream(uint32 size, aiColor4D* sourceData, Gfx::PGraphics graphics) +{ + Array buffer(size); + for(uint32 i = 0; i < size; ++i) + { + buffer[i] = Math::Vector4(sourceData[i].r, sourceData[i].g, sourceData[i].b, sourceData[i].a); + } + VertexBufferCreateInfo vbInfo; + vbInfo.numVertices = size; + vbInfo.vertexSize = sizeof(Math::Vector4); + vbInfo.resourceData.data = (uint8 *)buffer.data(); + vbInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER; + vbInfo.resourceData.size = sizeof(Math::Vector4) * buffer.size(); + Gfx::PVertexBuffer vertexBuffer = graphics->createVertexBuffer(vbInfo); + vertexBuffer->transferOwnership(Gfx::QueueType::GRAPHICS); + return VertexStreamComponent(vertexBuffer, 0, vbInfo.vertexSize, Gfx::SE_FORMAT_R32G32_SFLOAT); +} void MeshLoader::loadGlobalMeshes(const aiScene* scene, Array& globalMeshes, const Array& materials) { for (uint32 meshIndex = 0; meshIndex < scene->mNumMeshes; ++meshIndex) @@ -172,7 +193,7 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, Array& globalMesh } if(mesh->HasVertexColors(0)) { - //data.colorComponent = createVertexStream(mesh->mNumVertices, mesh->mColors[0], graphics); + data.colorComponent = createVertexStream(mesh->mNumVertices, mesh->mColors[0], graphics); } vertexShaderInput->setData(std::move(data)); vertexShaderInput->init(graphics); diff --git a/src/Engine/Component/Camera.cpp b/src/Engine/Component/Camera.cpp index 79b2c73..6cefc83 100644 --- a/src/Engine/Component/Camera.cpp +++ b/src/Engine/Component/Camera.cpp @@ -8,12 +8,8 @@ using namespace Seele::Component; using namespace Seele::Math; Camera::Camera() - : aspectRatio(0) - , fieldOfView(glm::radians(70.f)) - , bNeedsViewBuild(false) - , bNeedsProjectionBuild(false) + : bNeedsViewBuild(false) , viewMatrix(Matrix4()) - , projectionMatrix(Matrix4()) { yaw = 0; pitch = 0; @@ -54,14 +50,6 @@ void Camera::moveY(float amount) bNeedsViewBuild = true; } -void Camera::setViewport(Gfx::PViewport newViewport) -{ - viewport = newViewport; - aspectRatio = viewport->getSizeX() / (float)viewport->getSizeY(); - - bNeedsProjectionBuild = true; -} - void Camera::buildViewMatrix() { Vector eyePos = getTransform().getPosition();//getAbsoluteTransform().getPosition(); @@ -71,18 +59,3 @@ void Camera::buildViewMatrix() bNeedsViewBuild = false; } - -void Camera::buildProjectionMatrix() -{ - projectionMatrix = glm::perspective(fieldOfView, aspectRatio, 1.0f, 1000.f); - static Matrix4 correctionMatrix = - Matrix4( - Vector4(1, 0, 0, 0), - Vector4(0, 1, 0, 0), - Vector4(0, 0, 0.5f, 0), - Vector4(0, 0, 0.5f, 1)); - projectionMatrix = correctionMatrix * projectionMatrix; - bNeedsProjectionBuild = false; -} - - diff --git a/src/Engine/Component/Camera.h b/src/Engine/Component/Camera.h index 0004c47..ef3bff2 100644 --- a/src/Engine/Component/Camera.h +++ b/src/Engine/Component/Camera.h @@ -20,11 +20,6 @@ struct Camera assert (!bNeedsViewBuild); return viewMatrix; } - Math::Matrix4 getProjectionMatrix() const - { - assert (!bNeedsProjectionBuild); - return projectionMatrix; - } Math::Vector getCameraPosition() const { return Math::Vector(viewMatrix[3]); @@ -34,21 +29,13 @@ struct Camera void mouseScroll(float x); void moveX(float amount); void moveY(float amount); - float aspectRatio; - float fieldOfView; void buildViewMatrix(); - void buildProjectionMatrix(); Math::Matrix4 viewMatrix; - Math::Matrix4 projectionMatrix; //Transforms relative to actor float yaw; float pitch; private: bool bNeedsViewBuild; - bool bNeedsProjectionBuild; - - Gfx::PViewport viewport; - }; } // namespace Component } // namespace Seele diff --git a/src/Engine/Component/StaticMesh.h b/src/Engine/Component/StaticMesh.h index 4db2ab8..2b72b5e 100644 --- a/src/Engine/Component/StaticMesh.h +++ b/src/Engine/Component/StaticMesh.h @@ -1,5 +1,5 @@ #pragma once -#include "Graphics/GraphicsResources.h" +#include "Asset/MeshAsset.h" namespace Seele { @@ -7,9 +7,7 @@ namespace Component { struct StaticMesh { - PVertexShaderInput vertexBuffer; - Gfx::PIndexBuffer indexBuffer; - PMaterialAsset material; + PMeshAsset mesh; }; } // namespace Component } // namespace Seele diff --git a/src/Engine/Graphics/GraphicsInitializer.h b/src/Engine/Graphics/GraphicsInitializer.h index 0c41c5a..8b33c91 100644 --- a/src/Engine/Graphics/GraphicsInitializer.h +++ b/src/Engine/Graphics/GraphicsInitializer.h @@ -43,6 +43,7 @@ struct WindowCreateInfo struct ViewportCreateInfo { Math::URect dimensions; + float fieldOfView = 1.222f; // 70 deg }; // doesnt own the data, only proxy it struct BulkResourceData diff --git a/src/Engine/Graphics/GraphicsResources.cpp b/src/Engine/Graphics/GraphicsResources.cpp index ae06da4..e30d18e 100644 --- a/src/Engine/Graphics/GraphicsResources.cpp +++ b/src/Engine/Graphics/GraphicsResources.cpp @@ -506,6 +506,7 @@ Viewport::Viewport(PWindow owner, const ViewportCreateInfo &viewportInfo) , sizeY(viewportInfo.dimensions.size.y) , offsetX(viewportInfo.dimensions.offset.x) , offsetY(viewportInfo.dimensions.offset.y) + , fieldOfView(viewportInfo.fieldOfView) , owner(owner) { } @@ -514,3 +515,15 @@ Viewport::~Viewport() { } +Math::Matrix4 Viewport::getProjectionMatrix() const +{ + if(fieldOfView > 0.0f) + { + return glm::perspective(fieldOfView, sizeX / static_cast(sizeY), 0.1f, 1000.0f); + } + else + { + return glm::ortho(0.0f, (float)sizeX, (float)sizeY, 0.0f); + } +} + diff --git a/src/Engine/Graphics/GraphicsResources.h b/src/Engine/Graphics/GraphicsResources.h index efe9834..b6224fb 100644 --- a/src/Engine/Graphics/GraphicsResources.h +++ b/src/Engine/Graphics/GraphicsResources.h @@ -650,16 +650,18 @@ public: virtual ~Viewport(); virtual void resize(uint32 newX, uint32 newY) = 0; virtual void move(uint32 newOffsetX, uint32 newOffsetY) = 0; - inline PWindow getOwner() const {return owner;} - inline uint32 getSizeX() const {return sizeX;} - inline uint32 getSizeY() const {return sizeY;} - inline uint32 getOffsetX() const {return offsetX;} - inline uint32 getOffsetY() const {return offsetY;} + constexpr PWindow getOwner() const {return owner;} + constexpr uint32 getSizeX() const {return sizeX;} + constexpr uint32 getSizeY() const {return sizeY;} + constexpr uint32 getOffsetX() const {return offsetX;} + constexpr uint32 getOffsetY() const {return offsetY;} + Math::Matrix4 getProjectionMatrix() const; protected: uint32 sizeX; uint32 sizeY; uint32 offsetX; uint32 offsetY; + float fieldOfView; PWindow owner; }; DEFINE_REF(Viewport) diff --git a/src/Engine/Graphics/RenderPass/BasePass.cpp b/src/Engine/Graphics/RenderPass/BasePass.cpp index 1fd1658..6033a83 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.cpp +++ b/src/Engine/Graphics/RenderPass/BasePass.cpp @@ -120,7 +120,7 @@ void BasePass::beginFrame(const Component::Camera& cam) BulkResourceData uniformUpdate; viewParams.viewMatrix = cam.getViewMatrix(); - viewParams.projectionMatrix = cam.getProjectionMatrix(); + viewParams.projectionMatrix = viewport->getProjectionMatrix(); viewParams.cameraPosition = Math::Vector4(cam.getCameraPosition(), 0); viewParams.screenDimensions = Math::Vector2(static_cast(viewport->getSizeX()), static_cast(viewport->getSizeY())); uniformUpdate.size = sizeof(ViewParameter); diff --git a/src/Engine/Graphics/RenderPass/DepthPrepass.cpp b/src/Engine/Graphics/RenderPass/DepthPrepass.cpp index 06bb5ef..759ecab 100644 --- a/src/Engine/Graphics/RenderPass/DepthPrepass.cpp +++ b/src/Engine/Graphics/RenderPass/DepthPrepass.cpp @@ -103,7 +103,7 @@ void DepthPrepass::beginFrame(const Component::Camera& cam) BulkResourceData uniformUpdate; viewParams.viewMatrix = cam.getViewMatrix(); - viewParams.projectionMatrix = cam.getProjectionMatrix(); + viewParams.projectionMatrix = viewport->getProjectionMatrix(); viewParams.cameraPosition = Math::Vector4(cam.getCameraPosition(), 0); viewParams.screenDimensions = Math::Vector2(static_cast(viewport->getSizeX()), static_cast(viewport->getSizeY())); uniformUpdate.size = sizeof(ViewParameter); diff --git a/src/Engine/Graphics/RenderPass/LightCullingPass.cpp b/src/Engine/Graphics/RenderPass/LightCullingPass.cpp index 275bdd2..9f9de52 100644 --- a/src/Engine/Graphics/RenderPass/LightCullingPass.cpp +++ b/src/Engine/Graphics/RenderPass/LightCullingPass.cpp @@ -24,7 +24,7 @@ void LightCullingPass::beginFrame(const Component::Camera& cam) BulkResourceData uniformUpdate; viewParams.viewMatrix = cam.getViewMatrix(); - viewParams.projectionMatrix = cam.getProjectionMatrix(); + viewParams.projectionMatrix = viewport->getProjectionMatrix(); viewParams.cameraPosition = Math::Vector4(cam.getCameraPosition(), 0); viewParams.screenDimensions = Math::Vector2(static_cast(viewportWidth), static_cast(viewportHeight)); uniformUpdate.size = sizeof(ViewParameter); diff --git a/src/Engine/Graphics/RenderPass/TextPass.cpp b/src/Engine/Graphics/RenderPass/TextPass.cpp index 941c052..9514ed4 100644 --- a/src/Engine/Graphics/RenderPass/TextPass.cpp +++ b/src/Engine/Graphics/RenderPass/TextPass.cpp @@ -15,7 +15,7 @@ TextPass::~TextPass() } -void TextPass::beginFrame(const Component::Camera& cam) +void TextPass::beginFrame(const Component::Camera&) { for(TextRender& render : passData.texts) { @@ -59,9 +59,10 @@ void TextPass::beginFrame(const Component::Camera& cam) .scale = render.scale, }; } + auto proj = viewport->getProjectionMatrix(); BulkResourceData projectionUpdate = { .size = sizeof(Math::Matrix4), - .data = (uint8*)&cam.projectionMatrix, + .data = (uint8*)&proj, }; projectionBuffer->updateContents(projectionUpdate); generalSet->updateBuffer(1, projectionBuffer); diff --git a/src/Engine/Graphics/Vulkan/VulkanViewport.cpp b/src/Engine/Graphics/Vulkan/VulkanViewport.cpp index 6a75320..d708a99 100644 --- a/src/Engine/Graphics/Vulkan/VulkanViewport.cpp +++ b/src/Engine/Graphics/Vulkan/VulkanViewport.cpp @@ -8,7 +8,7 @@ using namespace Seele; using namespace Seele::Vulkan; -void glfwKeyCallback(GLFWwindow* handle, int key, int action, int, int modifier) +void glfwKeyCallback(GLFWwindow* handle, int key, int, int action, int modifier) { Window* window = (Window*)glfwGetWindowUserPointer(handle); window->keyCallback((KeyCode)key, (InputAction)action, (KeyModifier)modifier); diff --git a/src/Engine/Material/MaterialAsset.cpp b/src/Engine/Material/MaterialAsset.cpp index 173e960..9bbd725 100644 --- a/src/Engine/Material/MaterialAsset.cpp +++ b/src/Engine/Material/MaterialAsset.cpp @@ -50,6 +50,7 @@ void MaterialAsset::load() layout = WindowManager::getGraphics()->createDescriptorLayout(materialName + "Layout"); //Shader file needs to conform to the slang standard, which prohibits _ materialName.erase(std::remove(materialName.begin(), materialName.end(), '_'), materialName.end()); + materialName.erase(std::remove(materialName.begin(), materialName.end(), '.'), materialName.end()); std::ofstream codeStream("./shaders/generated/"+materialName+".slang"); std::string profile = j["profile"].get(); diff --git a/src/Engine/Scene/Scene.cpp b/src/Engine/Scene/Scene.cpp index 4d4724d..a78b5a1 100644 --- a/src/Engine/Scene/Scene.cpp +++ b/src/Engine/Scene/Scene.cpp @@ -1,6 +1,7 @@ #include "Scene.h" #include "Material/MaterialAsset.h" #include "Graphics/Graphics.h" +#include "Graphics/Mesh.h" #include "Component/StaticMesh.h" #include "Component/Transform.h" @@ -82,25 +83,29 @@ Array Scene::getStaticMeshes() } }; Gfx::PUniformBuffer transformBuf = graphics->createUniformBuffer(info); - auto& batch = result.add(); - batch.material = mesh.material; - batch.isBackfaceCullingDisabled = false; - batch.isCastingShadow = true; - batch.topology = Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; - batch.useReverseCulling = false; - batch.useWireframe = false; - batch.vertexInput = mesh.vertexBuffer; - MeshBatchElement batchElement; - batchElement.baseVertexIndex = 0; - batchElement.firstIndex = 0; - batchElement.indexBuffer = mesh.indexBuffer; - batchElement.indirectArgsBuffer = nullptr; - batchElement.instanceRuns = nullptr; - batchElement.isInstanced = false; - batchElement.numInstances = 1; - batchElement.uniformBuffer = transformBuf; - batchElement.numPrimitives = static_cast(mesh.indexBuffer->getNumIndices() / 3); //TODO: hardcoded - batch.elements.add(batchElement); + // TODO + for(auto& m : mesh.mesh->meshes) + { + auto& batch = result.add(); + batch.material = m->referencedMaterial; + batch.isBackfaceCullingDisabled = false; + batch.isCastingShadow = true; + batch.topology = Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; + batch.useReverseCulling = false; + batch.useWireframe = false; + batch.vertexInput = m->vertexInput; + MeshBatchElement batchElement; + batchElement.baseVertexIndex = 0; + batchElement.firstIndex = 0; + batchElement.indexBuffer = m->indexBuffer; + batchElement.indirectArgsBuffer = nullptr; + batchElement.instanceRuns = nullptr; + batchElement.isInstanced = false; + batchElement.numInstances = 1; + batchElement.uniformBuffer = transformBuf; + batchElement.numPrimitives = static_cast(m->indexBuffer->getNumIndices() / 3); //TODO: hardcoded + batch.elements.add(batchElement); + } } return result; } diff --git a/src/Engine/UI/System.cpp b/src/Engine/UI/System.cpp index f909e83..a12c4ff 100644 --- a/src/Engine/UI/System.cpp +++ b/src/Engine/UI/System.cpp @@ -24,7 +24,7 @@ void System::update() void System::updateViewport(Gfx::PViewport viewport) { - virtualCamera.projectionMatrix = glm::ortho(0.0f, static_cast(viewport->getSizeX()), 0.0f, static_cast(viewport->getSizeY())); + //TODO set viewport FoV to 0 } UIPassData System::getUIPassData()