Skip to content

Commit

Permalink
Remove unused code and refactor ACamera and descendants
Browse files Browse the repository at this point in the history
  • Loading branch information
aeris170 committed Sep 2, 2024
1 parent 324feb1 commit dfebd5e
Show file tree
Hide file tree
Showing 20 changed files with 275 additions and 446 deletions.
20 changes: 10 additions & 10 deletions Editor/ComponentWidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,9 @@ bool ResolutionWidget(const std::string& label, Resolution& resolution) {
}

bool OrthoCameraWidget(OrthoCamera& cameraData) {
glm::vec1 top{ cameraData._top };
glm::vec2 leftRight{ cameraData._left, cameraData._right };
glm::vec1 bottom{ cameraData._bottom };
glm::vec1 top{ cameraData.TopPlane };
glm::vec2 leftRight{ cameraData.LeftPlane, cameraData.RightPlane };
glm::vec1 bottom{ cameraData.BottomPlane };

FancyVectorWidgetSettings<Display::X> settingsOrthoTop;
settingsOrthoTop.resetTo = 1;
Expand Down Expand Up @@ -497,17 +497,17 @@ bool OrthoCameraWidget(OrthoCamera& cameraData) {
rv = rv | FancyVector2Widget("Left & Right", leftRight, settingsOrthoLeftRight); // no double pipe because of short circuit shadowing imgui call
rv = rv | FancyVector1Widget("Bottom", bottom, settingsOrthoBottom);

cameraData._top = top.x;
cameraData._left = leftRight.x;
cameraData._right = leftRight.y;
cameraData._bottom = bottom.x;
cameraData.TopPlane = top.x;
cameraData.LeftPlane = leftRight.x;
cameraData.RightPlane = leftRight.y;
cameraData.BottomPlane = bottom.x;

return rv;
}

bool PerspectiveCameraWidget(PerspectiveCamera& cameraData) {
glm::vec1 fov{ cameraData._fov };
glm::vec1 aspect{ cameraData._aspect };
glm::vec1 fov{ cameraData.FOV };
glm::vec1 aspect{ cameraData.AspectRatio };

FancyVectorWidgetSettings<Display::X> fovSettings;
fovSettings.resetTo = 110;
Expand All @@ -526,7 +526,7 @@ bool PerspectiveCameraWidget(PerspectiveCamera& cameraData) {
rv = FancyVector1Widget("Field Of View", fov, fovSettings);
rv = rv | FancyVector1Widget("Aspect Ratio", aspect, aspectSettings); // no double pipe because of short circuit shadowing imgui call

cameraData._fov = fov.x;
cameraData.FOV = fov.x;

return rv;
}
Expand Down
4 changes: 2 additions & 2 deletions Editor/Gizmos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ void Gizmos::Render(Scene& scene) {
ImGuizmo::SetOrthographic(true);
}
const auto& camera = sv.GetViewportCamera().GetActiveCamera();
glm::mat4 proj = camera._projectionMatrix;
glm::mat4 view = camera._viewMatrix;
glm::mat4 proj = camera.GetProjectionMatrix();
glm::mat4 view = camera.GetViewMatrix();

ImGuizmo::SetRect(
settings.viewportPosition.x,
Expand Down
3 changes: 3 additions & 0 deletions Editor/SceneSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ void SceneSettings::DrawStats(Scene& scene) const {
GUI& gui = this->gui;
ImGui::BeginGroup();

ImGui::TextUnformatted("TODO");
/*
auto stats = scene.GetRendererStats();
ImGuiFormattedText("Draw Calls: {}", stats.drawCalls);
ImGuiFormattedText("Vertices: {}", stats.vertices);
Expand All @@ -44,6 +46,7 @@ void SceneSettings::DrawStats(Scene& scene) const {
ImGuiFormattedText("Editor average {:.3f} ms/frame ({:.1f} FPS)", 1000.0f / fps, fps);
ImGui::ColorEdit3("Clear Color", &scene.ClearColor.r);
ImGui::ColorEdit3("Selection Outline Color", &scene.SelectionOutlineColor.r);
*/

ImGui::EndGroup();
}
22 changes: 11 additions & 11 deletions Editor/SceneViewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ ImVec2 SceneViewport::GetViewportCameraSettingsButtonPosition() const noexcept {
void SceneViewport::ReallocBufferIfNeeded(Resolution size) {
if (viewportSize == size) { return; }
viewportSize = size;
viewportCamera.GetPerspectiveCamera()._aspect = size.Aspect();
viewportCamera.GetPerspectiveCamera().AspectRatio = size.Aspect();

{ // Build Multisampled FB
GPURenderBufferBuilder rbBuilder;
Expand Down Expand Up @@ -288,8 +288,8 @@ void SceneViewport::RenderSceneToBuffer(Scene& scene) {
viewportCamera.GetPerspectiveCamera().UpdateView();
viewportCamera.GetPerspectiveCamera().UpdateProjection();
glm::mat4 matrices[2] {
viewportCamera.GetPerspectiveCamera()._projectionMatrix,
viewportCamera.GetPerspectiveCamera()._viewMatrix
viewportCamera.GetPerspectiveCamera().GetProjectionMatrix(),
viewportCamera.GetPerspectiveCamera().GetViewMatrix()
};
Graphics::BufferSubData(perFrameUniformBuffer, sizeof(matrices), reinterpret_cast<NonOwningPointerToConstRawData>(glm::value_ptr(matrices[0])));
Graphics::BindDescriptorSet(perFrame);
Expand Down Expand Up @@ -425,25 +425,25 @@ void SceneViewport::DrawViewportSettings(bool hasScene) {
void SceneViewport::DrawCubeControl() {
auto& camera = viewportCamera.GetActiveCamera();
camera.UpdateView();
glm::mat4 view = camera._viewMatrix;
glm::mat4 view = camera.GetViewMatrix();
ImGuizmo::SetDrawlist();
ImGuizmo::ViewManipulate(glm::value_ptr(view), 8, { viewportPosition.x + viewportSize.Width - 128 , viewportPosition.y }, { 128, 128 }, 0x10101080);
camera.forward = glm::normalize(glm::vec3(-view[0].z, -view[1].z, -view[2].z)); // forward is INVERTED!!!
camera.Forward = glm::normalize(glm::vec3(-view[0].z, -view[1].z, -view[2].z)); // forward is INVERTED!!!

// don't change up vector, fuck space sims. up being something other than 0, 1, 0 is VERBOTEN!
//ptr->_activeCamera->up = glm::normalize(glm::vec3(view[0].y, view[1].y, view[2].y));

controls.yaw = glm::degrees(atan2(camera.forward.z, camera.forward.x));
controls.pitch = glm::degrees(asin(camera.forward.y));
controls.yaw = glm::degrees(atan2(camera.Forward.z, camera.Forward.x));
controls.pitch = glm::degrees(asin(camera.Forward.y));
}

void SceneViewport::HandleMouseControls() {
GUI& gui = this->gui;
auto& camera = viewportCamera.GetActiveCamera();
glm::vec3& eye = camera.eye;
glm::vec3& forward = camera.forward;
glm::vec3& up = camera.up;
float& zoom = camera.zoom;
glm::vec3& eye = camera.Eye;
glm::vec3& forward = camera.Forward;
glm::vec3& up = camera.Up;
float& zoom = camera.Zoom;

if (ImGui::IsItemHovered()) {
zoom += ImGui::GetIO().MouseWheel / 100;
Expand Down
26 changes: 13 additions & 13 deletions Editor/SceneViewportCameraSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,29 @@ void SceneViewportCameraSettings::Render() noexcept {

if (viewportCamera.IsOrtho()) {
auto& ortho = viewportCamera.GetOrthoCamera();
FloatWidget("Left", ortho._left, 1);
FloatWidget("Right", ortho._right, 1);
FloatWidget("Bottom", ortho._bottom, 1);
FloatWidget("Top", ortho._top, 1);
FloatWidget("Near", ortho._near, 1);
FloatWidget("Far", ortho._far, 1);
FloatWidget(nameof(OrthoCamera::LeftPlane), ortho.LeftPlane, 1);
FloatWidget(nameof(OrthoCamera::RightPlane), ortho.RightPlane, 1);
FloatWidget(nameof(OrthoCamera::BottomPlane), ortho.BottomPlane, 1);
FloatWidget(nameof(OrthoCamera::TopPlane), ortho.TopPlane, 1);
FloatWidget(nameof(OrthoCamera::NearPlane), ortho.NearPlane, 1);
FloatWidget(nameof(OrthoCamera::FarPlane), ortho.FarPlane, 1);
} else if (viewportCamera.IsPerspective()) {
auto& perspective = viewportCamera.GetPerspectiveCamera();
FloatWidget("FOV", perspective._fov, 1, 45, 135);
FloatWidget("Near", perspective._near, 1);
FloatWidget("Far", perspective._far, 1);
FloatWidget(nameof(PerspectiveCamera::FOV), perspective.FOV, 1, 45, 135);
FloatWidget(nameof(PerspectiveCamera::NearPlane), perspective.NearPlane, 1);
FloatWidget(nameof(PerspectiveCamera::FarPlane), perspective.FarPlane, 1);
}
ImGui::NewLine();

auto& active = viewportCamera.GetActiveCamera();
FancyVectorWidgetSettings<Display::XYZ> settings = defaultFancyVectorSettingsXYZ;
settings.resetAllToSame = false;
FancyVector3Widget("Eye", active.eye);
FancyVector3Widget(nameof(ACamera::Eye), active.Eye);
settings.resetTos = { 0, 0, -1 };
FancyVector3Widget("Forward", active.forward, settings);
FancyVector3Widget(nameof(ACamera::Forward), active.Forward, settings);
settings.resetTos = { 0, 1, 0 };
FancyVector3Widget("Up", active.up, settings);
FloatWidget("Zoom", active.zoom);
FancyVector3Widget(nameof(ACamera::Up), active.Up, settings);
FloatWidget(nameof(ACamera::Zoom), active.Zoom);

ImGui::EndGroup();
}
Expand Down
7 changes: 7 additions & 0 deletions Engine/ACamera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <Engine/ACamera.hpp>

ACamera::~ACamera() = default;

const glm::mat4& ACamera::GetViewMatrix() const noexcept { return viewMatrix; }
const glm::mat4& ACamera::GetProjectionMatrix() const noexcept { return projectionMatrix; }
const glm::mat4& ACamera::GetViewProjectionMatrix() const noexcept { return viewProjectionMatrix; }
25 changes: 15 additions & 10 deletions Engine/ACamera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@

struct ACamera {

glm::vec3 eye{ 0, 0, 0 };
glm::vec3 forward{ 0, 0, -1 };
glm::vec3 up{ 0, 1, 0 };
float zoom{ 1 };

glm::mat4 _viewMatrix;
glm::mat4 _projectionMatrix;
glm::mat4 _viewProjectionMatrix;

virtual ~ACamera() = default;
virtual ~ACamera() = 0;

virtual void UpdateView() = 0;
virtual void UpdateProjection() = 0;
virtual void UpdateViewProjection() = 0;

const glm::mat4& GetViewMatrix() const noexcept;
const glm::mat4& GetProjectionMatrix() const noexcept;
const glm::mat4& GetViewProjectionMatrix() const noexcept;

glm::vec3 Eye{ 0, 0, 0 };
glm::vec3 Forward{ 0, 0, -1 };
glm::vec3 Up{ 0, 1, 0 };
float Zoom{ 1 };

protected:
glm::mat4 viewMatrix;
glm::mat4 projectionMatrix;
glm::mat4 viewProjectionMatrix;
};
1 change: 1 addition & 0 deletions Engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ set(GROUP_LIST
"Project/Serialize/ProjectSerializer.hpp"
"Project/Serialize/ProjectDeserializer.cpp"
"Project/Serialize/ProjectDeserializer.hpp"
"Project/Scene/ACamera.cpp"
"Project/Scene/ACamera.hpp"
"Project/Scene/OrthoCamera.cpp"
"Project/Scene/OrthoCamera.hpp"
Expand Down
34 changes: 14 additions & 20 deletions Engine/CameraComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@
#include <glm/gtc/matrix_transform.hpp>

/* Ortho Camera Below */
OrthoCameraComponent::OrthoCameraComponent(const Entity owner, OrthoCamera&& data) noexcept :
entity(owner),
data(std::move(data)) {
UpdateMatrices();
}

OrthoCameraComponent::OrthoCameraComponent(const Entity owner) noexcept :
entity(owner) {
UpdateMatrices();
}
OrthoCameraComponent::OrthoCameraComponent(const Entity owner, const OrthoCamera& data) noexcept :
entity(owner),
data(data) {
UpdateMatrices();
}

Entity OrthoCameraComponent::GetEntity() const { return entity; }

const OrthoCamera& OrthoCameraComponent::GetData() const { return data; }
void OrthoCameraComponent::SetData(OrthoCamera&& data) {
this->data = std::move(data);
}
const OrthoCamera& OrthoCameraComponent::GetData() const noexcept { return data; }
void OrthoCameraComponent::SetData(const OrthoCamera& data) noexcept { this->data = data; }

void OrthoCameraComponent::UpdateMatrices() {
data.UpdateView();
Expand All @@ -36,23 +33,20 @@ bool OrthoCameraComponent::IsActiveAndRendering() const { return isActiveAndRend
/* Ortho Camera Above */

/* Perpective Camera Below */
PerspectiveCameraComponent::PerspectiveCameraComponent(const Entity owner, PerspectiveCamera&& data) noexcept :
entity(owner),
data(std::move(data)) {
UpdateMatrices();
}

PerspectiveCameraComponent::PerspectiveCameraComponent(const Entity owner) noexcept :
entity(owner) {
UpdateMatrices();
}
PerspectiveCameraComponent::PerspectiveCameraComponent(const Entity owner, const PerspectiveCamera& data) noexcept :
entity(owner),
data(data) {
UpdateMatrices();
}

Entity PerspectiveCameraComponent::GetEntity() const { return entity; }

const PerspectiveCamera& PerspectiveCameraComponent::GetData() const { return data; }
void PerspectiveCameraComponent::SetData(PerspectiveCamera&& data) {
this->data = std::move(data);
}
const PerspectiveCamera& PerspectiveCameraComponent::GetData() const noexcept { return data; }
void PerspectiveCameraComponent::SetData(const PerspectiveCamera& data) noexcept { this->data = data; }

void PerspectiveCameraComponent::UpdateMatrices() {
data.UpdateView();
Expand Down
14 changes: 6 additions & 8 deletions Engine/CameraComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ struct OrthoCameraComponent {
-1.0f, 1.0f
};

OrthoCameraComponent(const Entity owner, OrthoCamera&& matrix) noexcept;

public:
OrthoCameraComponent(const Entity owner) noexcept;
OrthoCameraComponent(const Entity owner, const OrthoCamera& matrix) noexcept;

Entity GetEntity() const;

const OrthoCamera& GetData() const;
void SetData(OrthoCamera&& data);
const OrthoCamera& GetData() const noexcept;
void SetData(const OrthoCamera& data) noexcept;

void UpdateMatrices();

Expand All @@ -59,15 +58,14 @@ struct PerspectiveCameraComponent {
10000.0f
};

PerspectiveCameraComponent(const Entity owner, PerspectiveCamera&& data) noexcept;

public:
PerspectiveCameraComponent(const Entity owner) noexcept;
PerspectiveCameraComponent(const Entity owner, const PerspectiveCamera& data) noexcept;

Entity GetEntity() const;

const PerspectiveCamera& GetData() const;
void SetData(PerspectiveCamera&& data);
const PerspectiveCamera& GetData() const noexcept;
void SetData(const PerspectiveCamera& data) noexcept;

void UpdateMatrices();

Expand Down
57 changes: 29 additions & 28 deletions Engine/OrthoCamera.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
#include "OrthoCamera.hpp"
#include <Engine/OrthoCamera.hpp>

#include <glm/gtx/quaternion.hpp>
#include <glm/gtc/matrix_transform.hpp>

OrthoCamera::OrthoCamera(float left, float right, float bottom, float top, float near, float far) :
_left(left),
_right(right),
_bottom(bottom),
_top(top),
_near(near),
_far(far) {}
OrthoCamera::OrthoCamera(float left, float right, float bottom, float top, float near, float far) noexcept :
LeftPlane(left),
RightPlane(right),
BottomPlane(bottom),
TopPlane(top),
NearPlane(near),
FarPlane(far) {}

void OrthoCamera::Set(float left, float right, float bottom, float top, float near, float far) {
_left = left;
_right = right;
_bottom = bottom;
_top = top;
_near = near;
_far = far;
void OrthoCamera::Set(float left, float right, float bottom, float top, float near, float far) noexcept {
LeftPlane = left;
RightPlane = right;
BottomPlane = bottom;
TopPlane = top;
NearPlane = near;
FarPlane = far;
}

void OrthoCamera::UpdateView() {
forward = glm::normalize(forward);
up = glm::normalize(up);
_viewMatrix = glm::lookAt(eye, eye + forward, up);
_viewMatrix = glm::scale(_viewMatrix, { 1, 1, 1 / zoom });
}

void OrthoCamera::UpdateProjection() {
_projectionMatrix = glm::ortho(_left, _right, _bottom, _top, _near, _far);
void OrthoCamera::UpdateView() noexcept {
Forward = glm::normalize(Forward);
Up = glm::normalize(Up);
viewMatrix = glm::lookAt(Eye, Eye + Forward, Up);
viewMatrix = glm::scale(viewMatrix, { 1, 1, 1 / Zoom });
}

void OrthoCamera::UpdateViewProjection() {
_viewProjectionMatrix = _projectionMatrix * _viewMatrix;
void OrthoCamera::UpdateProjection() noexcept {
projectionMatrix = glm::ortho(
LeftPlane, RightPlane,
BottomPlane, TopPlane,
NearPlane, FarPlane
);
}
void OrthoCamera::UpdateViewProjection() noexcept {
viewProjectionMatrix = projectionMatrix * viewMatrix;
}
Loading

0 comments on commit dfebd5e

Please sign in to comment.