Moving projection to viewport

This commit is contained in:
Dynamitos
2022-11-30 10:19:45 +01:00
parent 1e04da963d
commit 3c7346cf7b
18 changed files with 87 additions and 86 deletions
+2 -2
View File
@@ -13,9 +13,9 @@ public:
virtual ~Entity();
template<typename Component, typename... Args>
void attachComponent(Args... args)
Component& attachComponent(Args... args)
{
scene->attachComponent<Component>(identifier, args...);
return scene->attachComponent<Component>(identifier, args...);
}
protected:
PScene scene;
+3 -3
View File
@@ -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<PMesh> MeshAsset::getMeshes()
const Array<PMesh> MeshAsset::getMeshes()
{
std::scoped_lock lck(lock);
return meshes;
+3 -4
View File
@@ -15,11 +15,10 @@ public:
virtual void save() override;
virtual void load() override;
void addMesh(PMesh mesh);
const std::vector<PMesh> getMeshes();
const Array<PMesh> getMeshes();
//Workaround while no editor
std::vector<PMaterialAsset> referencedMaterials;
private:
std::vector<PMesh> meshes;
Array<PMaterialAsset> referencedMaterials;
Array<PMesh> meshes;
};
DEFINE_REF(MeshAsset)
} // namespace Seele
+22 -1
View File
@@ -61,6 +61,10 @@ void MeshLoader::loadMaterials(const aiScene* scene, Array<PMaterialAsset>& 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<Math::Vector4> 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<PMesh>& globalMeshes, const Array<PMaterialAsset>& materials)
{
for (uint32 meshIndex = 0; meshIndex < scene->mNumMeshes; ++meshIndex)
@@ -172,7 +193,7 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, Array<PMesh>& 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);
+1 -28
View File
@@ -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;
}
-13
View File
@@ -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
+2 -4
View File
@@ -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
@@ -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
+13
View File
@@ -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<float>(sizeY), 0.1f, 1000.0f);
}
else
{
return glm::ortho(0.0f, (float)sizeX, (float)sizeY, 0.0f);
}
}
+7 -5
View File
@@ -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)
+1 -1
View File
@@ -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<float>(viewport->getSizeX()), static_cast<float>(viewport->getSizeY()));
uniformUpdate.size = sizeof(ViewParameter);
@@ -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<float>(viewport->getSizeX()), static_cast<float>(viewport->getSizeY()));
uniformUpdate.size = sizeof(ViewParameter);
@@ -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<float>(viewportWidth), static_cast<float>(viewportHeight));
uniformUpdate.size = sizeof(ViewParameter);
+3 -2
View File
@@ -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);
@@ -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);
+1
View File
@@ -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<std::string>();
+24 -19
View File
@@ -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<StaticMeshBatch> 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<uint32>(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<uint32>(m->indexBuffer->getNumIndices() / 3); //TODO: hardcoded
batch.elements.add(batchElement);
}
}
return result;
}
+1 -1
View File
@@ -24,7 +24,7 @@ void System::update()
void System::updateViewport(Gfx::PViewport viewport)
{
virtualCamera.projectionMatrix = glm::ortho<float>(0.0f, static_cast<float>(viewport->getSizeX()), 0.0f, static_cast<float>(viewport->getSizeY()));
//TODO set viewport FoV to 0
}
UIPassData System::getUIPassData()