Skip to content

Commit

Permalink
Merge pull request #580 from sdslabs/transform-system
Browse files Browse the repository at this point in the history
Transform System
  • Loading branch information
Bashar-Ahmed authored Aug 26, 2022
2 parents 602e180 + 337653f commit 0eabdfc
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void BasicMaterialResourceFile::bindSamplers()

void BasicMaterialResourceFile::bindVSCB()
{
RenderingDevice::GetSingleton()->editBuffer(PerModelVSCBData(RenderSystem::GetSingleton()->getCurrentMatrix()), m_VSCB.Get());
RenderingDevice::GetSingleton()->editBuffer(PerModelVSCBData(TransformSystem::GetSingleton()->getCurrentMatrix()), m_VSCB.Get());
RenderingDevice::GetSingleton()->setVSCB(PER_OBJECT_VS_CPP, 1, m_VSCB.GetAddressOf());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ void CustomMaterialResourceFile::bindSamplers()

void CustomMaterialResourceFile::bindVSCB()
{
RenderingDevice::GetSingleton()->editBuffer(PerModelVSCBData(RenderSystem::GetSingleton()->getCurrentMatrix()), m_VSCB.Get());
RenderingDevice::GetSingleton()->editBuffer(PerModelVSCBData(TransformSystem::GetSingleton()->getCurrentMatrix()), m_VSCB.Get());
RenderingDevice::GetSingleton()->setVSCB(PER_OBJECT_VS_CPP, 1, m_VSCB.GetAddressOf());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void DecalMaterialResourceFile::bindSamplers()

void DecalMaterialResourceFile::bindVSCB()
{
RenderingDevice::GetSingleton()->editBuffer(PerModelVSCBData(RenderSystem::GetSingleton()->getCurrentMatrix()), m_VSCB.Get());
RenderingDevice::GetSingleton()->editBuffer(PerModelVSCBData(TransformSystem::GetSingleton()->getCurrentMatrix()), m_VSCB.Get());
RenderingDevice::GetSingleton()->setVSCB(PER_OBJECT_VS_CPP, 1, m_VSCB.GetAddressOf());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bool RenderableComponent::setupEntities()

bool RenderableComponent::preRender(float deltaMilliseconds)
{
RenderSystem::GetSingleton()->pushMatrixOverride(getTransformComponent()->getAbsoluteTransform());
TransformSystem::GetSingleton()->pushMatrixOverride(getTransformComponent()->getAbsoluteTransform());
return true;
}

Expand All @@ -62,7 +62,7 @@ void RenderableComponent::render(float viewDistance)

void RenderableComponent::postRender()
{
RenderSystem::GetSingleton()->popMatrix();
TransformSystem::GetSingleton()->popMatrix();
}

bool RenderableComponent::addAffectingStaticLight(SceneID ID)
Expand Down
4 changes: 2 additions & 2 deletions rootex/framework/components/visual/model/sprite_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ bool SpriteComponent::preRender(float deltaMilliseconds)
billboardMatrix.Up(billboardMatrix.Up() * scaling.y);
billboardMatrix.Backward(billboardMatrix.Backward() * scaling.z);

RenderSystem::GetSingleton()->pushMatrixOverride(billboardMatrix);
TransformSystem::GetSingleton()->pushMatrixOverride(billboardMatrix);
}

return true;
Expand All @@ -77,7 +77,7 @@ void SpriteComponent::postRender()
{
if (m_IsBillboarded)
{
RenderSystem::GetSingleton()->popMatrix();
TransformSystem::GetSingleton()->popMatrix();
}
ModelComponent::postRender();
}
Expand Down
74 changes: 3 additions & 71 deletions rootex/framework/systems/render_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ RenderSystem::RenderSystem()
m_Binder.bind(RootexEvents::OpenedScene, this, &RenderSystem::onOpenedScene);

m_Camera = SceneLoader::GetSingleton()->getRootScene()->getEntity().getComponent<CameraComponent>();
m_TransformationStack.push_back(Matrix::Identity);

m_LineMaterial = ResourceLoader::CreateBasicMaterialResourceFile("rootex/assets/materials/line.basic.rmat");
m_CurrentFrameLines.m_Endpoints.reserve(LINE_MAX_VERTEX_COUNT * LINE_VERTEX_COUNT * 3);
Expand Down Expand Up @@ -68,54 +67,7 @@ void RenderSystem::setConfig(const SceneSettings& sceneSettings)

setCamera(camera);

calculateTransforms(SceneLoader::GetSingleton()->getRootScene());
}

void RenderSystem::calculateTransforms(Scene* scene)
{
Entity& entity = scene->getEntity();
if (TransformComponent* transform = entity.getComponent<TransformComponent>())
{
int passDown = transform->getPassDowns();

if (passDown == (int)TransformPassDown::All)
{
pushMatrix(transform->getLocalTransform());
}
else
{
Matrix matrix = Matrix::Identity;
if (passDown & (int)TransformPassDown::Position)
{
matrix = Matrix::CreateTranslation(transform->getPosition()) * matrix;
}
if (passDown & (int)TransformPassDown::Rotation)
{
matrix = Matrix::CreateFromQuaternion(transform->getRotation()) * matrix;
}
if (passDown & (int)TransformPassDown::Scale)
{
matrix = Matrix::CreateScale(transform->getScale()) * matrix;
}
pushMatrix(matrix);
}
}
else
{
pushMatrix(Matrix::Identity);
}

for (auto& child : scene->getChildren())
{
Entity& childEntity = child->getEntity();
if (TransformComponent* childTransform = childEntity.getComponent<TransformComponent>())
{
childTransform->setParentAbsoluteTransform(getCurrentMatrix());
}

calculateTransforms(child.get());
}
popMatrix();
TransformSystem::GetSingleton()->calculateTransforms(SceneLoader::GetSingleton()->getRootScene());
}

void RenderSystem::renderPassRender(float deltaMilliseconds, RenderPass renderPass)
Expand Down Expand Up @@ -219,7 +171,7 @@ void RenderSystem::update(float deltaMilliseconds)
}
{
ZoneNamedN(absoluteTransform, "Absolute Transformations", true);
calculateTransforms(SceneLoader::GetSingleton()->getRootScene());
TransformSystem::GetSingleton()->calculateTransforms(SceneLoader::GetSingleton()->getRootScene());
}
{
ZoneNamedN(stateSet, "Render PlayerState Reset", true);
Expand Down Expand Up @@ -423,21 +375,6 @@ void RenderSystem::submitCone(const Matrix& transform, const float& height, cons
submitLine(center, end - right);
}

void RenderSystem::pushMatrix(const Matrix& transform)
{
m_TransformationStack.push_back(transform * m_TransformationStack.back());
}

void RenderSystem::pushMatrixOverride(const Matrix& transform)
{
m_TransformationStack.push_back(transform);
}

void RenderSystem::popMatrix()
{
m_TransformationStack.pop_back();
}

void RenderSystem::enableWireframeRasterizer()
{
RenderingDevice::GetSingleton()->setRSType(RenderingDevice::RasterizerState::Wireframe);
Expand Down Expand Up @@ -510,7 +447,7 @@ void RenderSystem::setPerFramePSCBs(const Color& fogColor)

void RenderSystem::setPerScenePSCBs()
{
calculateTransforms(SceneLoader::GetSingleton()->getRootScene());
TransformSystem::GetSingleton()->calculateTransforms(SceneLoader::GetSingleton()->getRootScene());
updateStaticLights();
}

Expand Down Expand Up @@ -556,11 +493,6 @@ void RenderSystem::restoreCamera()
}
}

const Matrix& RenderSystem::getCurrentMatrix() const
{
return m_TransformationStack.back();
}

Variant RenderSystem::onOpenedScene(const Event* event)
{
updatePerSceneBinds();
Expand Down
185 changes: 88 additions & 97 deletions rootex/framework/systems/render_system.h
Original file line number Diff line number Diff line change
@@ -1,97 +1,88 @@
#pragma once

#include "core/renderer/renderer.h"
#include "core/renderer/render_pass.h"
#include "core/resource_files/basic_material_resource_file.h"
#include "main/window.h"
#include "framework/ecs_factory.h"
#include "framework/scene.h"
#include "framework/system.h"
#include "framework/components/visual/camera_component.h"
#include "components/visual/model/model_component.h"
#include "components/visual/model/animated_model_component.h"
#include "components/visual/model/sprite_component.h"

#include "ASSAO/ASSAO.h"

/// Responsible for handling all the rendering in the editor.
class RenderSystem : public System
{
EventBinder<RenderSystem> m_Binder;

struct LineRequests
{
Vector<float> m_Endpoints;
Vector<unsigned short> m_Indices;
};

CameraComponent* m_Camera;

Ptr<Renderer> m_Renderer;
Vector<Matrix> m_TransformationStack;

Ref<BasicMaterialResourceFile> m_LineMaterial;
LineRequests m_CurrentFrameLines;

Microsoft::WRL::ComPtr<ID3D11Buffer> m_PerFrameVSCB;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_PerCameraChangeVSCB;

Microsoft::WRL::ComPtr<ID3D11Buffer> m_PerCameraChangePSCB;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_PerFrameCustomPSCB;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_PerFramePSCB;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_PerScenePSCB;

bool m_IsEditorRenderPassEnabled;

RenderSystem();
RenderSystem(RenderSystem&) = delete;

void renderPassRender(float deltaMilliseconds, RenderPass renderPass);

Variant onOpenedScene(const Event* event);

public:
static RenderSystem* GetSingleton();

void setConfig(const SceneSettings& sceneSettings) override;
void update(float deltaMilliseconds) override;
void renderLines();

void submitLine(const Vector3& from, const Vector3& to);

void submitBox(const Vector3& min, const Vector3& max);
void submitSphere(const Vector3& center, const float& radius);
void submitCone(const Matrix& transform, const float& height, const float& radius);

void recoverLostDevice();

void setCamera(CameraComponent* camera);
void restoreCamera();

void calculateTransforms(Scene* scene);
void pushMatrix(const Matrix& transform);
void pushMatrixOverride(const Matrix& transform);
void popMatrix();

void enableWireframeRasterizer();
void resetDefaultRasterizer();

void setPerCameraVSCBs();
void setPerFrameVSCBs(float fogStart, float fogEnd);
void setPerCameraChangePSCBs();
void setPerFramePSCBs(const Color& fogColor);
void setPerScenePSCBs();
void updateStaticLights();
void updatePerSceneBinds();

void setIsEditorRenderPass(bool enabled) { m_IsEditorRenderPassEnabled = enabled; }

void enableLineRenderMode();
void resetRenderMode();

CameraComponent* getCamera() const { return m_Camera; }
const Matrix& getCurrentMatrix() const;
Renderer* getRenderer() const { return m_Renderer.get(); }

void draw() override;
};
#pragma once

#include "core/renderer/renderer.h"
#include "core/renderer/render_pass.h"
#include "core/resource_files/basic_material_resource_file.h"
#include "main/window.h"
#include "framework/components/visual/camera_component.h"
#include "components/visual/model/model_component.h"
#include "components/visual/model/animated_model_component.h"
#include "components/visual/model/sprite_component.h"
#include "transform_system.h"

#include "ASSAO/ASSAO.h"

/// Responsible for handling all the rendering in the editor.
class RenderSystem : public System
{
EventBinder<RenderSystem> m_Binder;

struct LineRequests
{
Vector<float> m_Endpoints;
Vector<unsigned short> m_Indices;
};

CameraComponent* m_Camera;

Ptr<Renderer> m_Renderer;

Ref<BasicMaterialResourceFile> m_LineMaterial;
LineRequests m_CurrentFrameLines;

Microsoft::WRL::ComPtr<ID3D11Buffer> m_PerFrameVSCB;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_PerCameraChangeVSCB;

Microsoft::WRL::ComPtr<ID3D11Buffer> m_PerCameraChangePSCB;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_PerFrameCustomPSCB;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_PerFramePSCB;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_PerScenePSCB;

bool m_IsEditorRenderPassEnabled;

RenderSystem();
RenderSystem(RenderSystem&) = delete;

void renderPassRender(float deltaMilliseconds, RenderPass renderPass);

Variant onOpenedScene(const Event* event);

public:
static RenderSystem* GetSingleton();

void setConfig(const SceneSettings& sceneSettings) override;
void update(float deltaMilliseconds) override;
void renderLines();

void submitLine(const Vector3& from, const Vector3& to);

void submitBox(const Vector3& min, const Vector3& max);
void submitSphere(const Vector3& center, const float& radius);
void submitCone(const Matrix& transform, const float& height, const float& radius);

void recoverLostDevice();

void setCamera(CameraComponent* camera);
void restoreCamera();

void enableWireframeRasterizer();
void resetDefaultRasterizer();

void setPerCameraVSCBs();
void setPerFrameVSCBs(float fogStart, float fogEnd);
void setPerCameraChangePSCBs();
void setPerFramePSCBs(const Color& fogColor);
void setPerScenePSCBs();
void updateStaticLights();
void updatePerSceneBinds();

void setIsEditorRenderPass(bool enabled) { m_IsEditorRenderPassEnabled = enabled; }

void enableLineRenderMode();
void resetRenderMode();

CameraComponent* getCamera() const { return m_Camera; }
Renderer* getRenderer() const { return m_Renderer.get(); }

void draw() override;
};
Loading

0 comments on commit 0eabdfc

Please sign in to comment.