Skip to content

Commit

Permalink
feat: editor camera
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanbaburajan committed Oct 25, 2023
1 parent f291e00 commit 0562e7c
Show file tree
Hide file tree
Showing 21 changed files with 498 additions and 147 deletions.
3 changes: 0 additions & 3 deletions Editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ namespace DT
ImGui::EndFrame();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

renderer->BindFrameBuffer(renderer->GetRenderFBO());
}


void Editor::Close()
{
Expand Down
66 changes: 66 additions & 0 deletions Editor/EditorCamera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
MIT License
Copyright (c) 2021 - 2023 Aryan Baburajan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#pragma once

#include <Renderer/BaseCamera.h>

namespace DT
{
class EditorCamera : public BaseCamera
{
public:
Transform transform;

using BaseCamera::BaseCamera;

void Recalculate(Window *window)
{
PROFILE();

view = glm::lookAtLH(transform.translation, transform.translation + transform.Forward(), transform.Up());

glm::vec2 winSize = window->GetWindowSize();
float aspect = (float)winSize.x/winSize.y;
if (isOrthographic)
projection = glm::orthoLH(-aspect, aspect, -1.0f, 1.0f, nearPlane, farPlane);
else
projection = glm::perspectiveLH(glm::radians(fieldOfView), window->GetWindowSize().x / window->GetWindowSize().y, nearPlane, farPlane);
}

glm::vec3 GetPosition() override
{
PROFILE();

return transform.translation;
}

glm::quat GetRotation() override
{
PROFILE();

return transform.rotation;
}
};
}
77 changes: 40 additions & 37 deletions Editor/Panels/GameView.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,52 @@ namespace DT
void Init(Context &ctx) override
{
PROFILE();

isOpen = false;

renderer = ctx.GetService<Renderer>().Fatal("WorldViewPanel::Init()");
window = ctx.GetService<Window>().Fatal("WorldViewPanel::Init()");
renderer = ctx.GetService<Renderer>().Fatal("GameViewPanel::Init()");
window = ctx.GetService<Window>().Fatal("GameViewPanel::Init()");
}

void Tick(Context &ctx, const float &dt) override
{
PROFILE();

ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.f, 0.f));
ImGui::Begin("World View", &isOpen);

sceneViewActive = ImGui::IsWindowFocused() || ImGui::IsWindowHovered();
ImVec2 vMin = ImGui::GetWindowContentRegionMin();
ImVec2 vMax = ImGui::GetWindowContentRegionMax();

vMin.x += ImGui::GetWindowPos().x;
vMin.y += ImGui::GetWindowPos().y;
vMax.x += ImGui::GetWindowPos().x;
vMax.y += ImGui::GetWindowPos().y;

windowPos = vMin;

ImVec2 windowSize = ImVec2(vMax.x - vMin.x, vMax.y - vMin.y);

if (window->GetWindowSize() != glm::vec2(windowSize.x, windowSize.y))
{
if (glm::vec2(windowSize.x, windowSize.y) != glm::vec2(0.f, 0.f))
{
window->SetWindowSize({windowSize.x, windowSize.y});
renderer->SetViewport({windowSize.x, windowSize.y});
}
}

ImGui::GetWindowDrawList()->AddImage(
(ImTextureID)(uintptr_t)renderer->GetRenderFBO(),
vMin,
vMax,
ImVec2(0, 1),
ImVec2(1, 0));

ImGui::End();
ImGui::PopStyleVar();

/// Proper Game Runtime to be implemented in the future
// ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.f, 0.f));
// ImGui::Begin("Game View", &isOpen);

// sceneViewActive = ImGui::IsWindowFocused() || ImGui::IsWindowHovered();
// ImVec2 vMin = ImGui::GetWindowContentRegionMin();
// ImVec2 vMax = ImGui::GetWindowContentRegionMax();

// vMin.x += ImGui::GetWindowPos().x;
// vMin.y += ImGui::GetWindowPos().y;
// vMax.x += ImGui::GetWindowPos().x;
// vMax.y += ImGui::GetWindowPos().y;

// windowPos = vMin;

// ImVec2 windowSize = ImVec2(vMax.x - vMin.x, vMax.y - vMin.y);

// if (window->GetWindowSize() != glm::vec2(windowSize.x, windowSize.y))
// {
// if (glm::vec2(windowSize.x, windowSize.y) != glm::vec2(0.f, 0.f))
// {
// window->SetWindowSize({windowSize.x, windowSize.y});
// renderer->SetViewport({windowSize.x, windowSize.y});
// }
// }

// ImGui::GetWindowDrawList()->AddImage(
// (ImTextureID)(uintptr_t)Camera::GetActiveCamera()->renderTexture,
// vMin,
// vMax,
// ImVec2(0, 1),
// ImVec2(1, 0));

// ImGui::End();
// ImGui::PopStyleVar();
}

private:
Expand Down
5 changes: 5 additions & 0 deletions Editor/Panels/WorldOutliner.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ SOFTWARE.

#include <ECS/SceneManager.h>
#include <Panels/Panel.h>
#include <Panels/WorldView.h>

namespace DT
{
Expand All @@ -37,6 +38,7 @@ namespace DT
PROFILE();

sceneManager = ctx.GetService<SceneManager>().Fatal("WorldOutlinerPanel::Init()");
worldView = editor->GetPanel<WorldViewPanel>().Fatal("InspectorPanel::Init()");
}

void Tick(Context &ctx, const float &dt) override
Expand All @@ -45,6 +47,8 @@ namespace DT

ImGui::Begin("World Outliner", &isOpen);

worldView->editorCamera.transform.InspectorMenu(ctx, dt);

for (Entity entity : sceneManager->activeScene.GetEntities())
{
std::string label;
Expand All @@ -67,6 +71,7 @@ namespace DT

private:
SceneManager *sceneManager;
WorldViewPanel *worldView;
Entity selectedEntity = 0;

friend class InspectorPanel;
Expand Down
115 changes: 115 additions & 0 deletions Editor/Panels/WorldView.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,133 @@ SOFTWARE.
#pragma once

#include <Panels/Panel.h>
#include <EditorCamera.h>

namespace DT
{
class WorldViewPanel : public Panel
{
public:
EditorCamera editorCamera;

WorldViewPanel(Context &ctx) : editorCamera(ctx) {}

void Init(Context &ctx) override
{
PROFILE();

renderer = ctx.GetService<Renderer>().Fatal("WorldViewPanel::Init()");
window = ctx.GetService<Window>().Fatal("WorldViewPanel::Init()");
sceneManager = ctx.GetService<SceneManager>().Fatal("WorldViewPanel::Init()");
input = ctx.GetService<InputManager>().Fatal("WorldViewPanel::Init()");

editorCamera.transform.translation = glm::vec3(0.f, 0.f, -3.f);
}

void Tick(Context &ctx, const float &dt) override
{
PROFILE();

ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.f, 0.f));
ImGui::Begin("World View", &isOpen);

sceneViewActive = ImGui::IsWindowFocused() || ImGui::IsWindowHovered();
ImVec2 vMin = ImGui::GetWindowContentRegionMin();
ImVec2 vMax = ImGui::GetWindowContentRegionMax();

vMin.x += ImGui::GetWindowPos().x;
vMin.y += ImGui::GetWindowPos().y;
vMax.x += ImGui::GetWindowPos().x;
vMax.y += ImGui::GetWindowPos().y;

windowPos = vMin;

ImVec2 windowSize = ImVec2(vMax.x - vMin.x, vMax.y - vMin.y);

if (window->GetWindowSize() != glm::vec2(windowSize.x, windowSize.y))
{
if (glm::vec2(windowSize.x, windowSize.y) != glm::vec2(0.f, 0.f))
{
window->SetWindowSize({windowSize.x, windowSize.y});
renderer->SetViewport({windowSize.x, windowSize.y});
}
}

// Freelook
if (sceneViewActive)
{
if (editorCamera.isOrthographic)
{
if (input->IsKeyHeld(KEY_W))
editorCamera.transform.translation += speed * dt * editorCamera.transform.Up();
if (input->IsKeyHeld(KEY_A))
editorCamera.transform.translation -= speed * dt * editorCamera.transform.Right();
if (input->IsKeyHeld(KEY_S))
editorCamera.transform.translation -= speed * dt * editorCamera.transform.Up();
if (input->IsKeyHeld(KEY_D))
editorCamera.transform.translation += speed * dt * editorCamera.transform.Right();

editorCamera.transform.translation.z = -3.f;
editorCamera.transform.SetEulerRotation({0.f, 180.f, 0.f});
}
else
{
if (input->IsKeyHeld(KEY_W))
editorCamera.transform.translation += speed * dt * editorCamera.transform.Forward();
if (input->IsKeyHeld(KEY_A))
editorCamera.transform.translation -= speed * dt * editorCamera.transform.Right();
if (input->IsKeyHeld(KEY_S))
editorCamera.transform.translation -= speed * dt * editorCamera.transform.Forward();
if (input->IsKeyHeld(KEY_D))
editorCamera.transform.translation += speed * dt * editorCamera.transform.Right();
if (input->IsKeyHeld(KEY_SPACE))
editorCamera.transform.translation += speed * dt * glm::vec3(0.f, 1.f, 0.f);
if (input->IsKeyHeld(KEY_LEFT_SHIFT))
editorCamera.transform.translation -= speed * dt * glm::vec3(0.f, 1.f, 0.f);
if (input->IsKeyHeld(KEY_E))
orientation.z += sensitivity * dt;
if (input->IsKeyHeld(KEY_Q))
orientation.z -= sensitivity * dt;

// Look
orientation.y += input->GetMouseDelta().x * sensitivity * dt;
orientation.x += -input->GetMouseDelta().y * sensitivity * dt;

if (orientation.x < -90.0f)
orientation.x = -90.0f;
if (orientation.x > 90.0f)
orientation.x = 90.0f;

orientation = glm::angNormalize(orientation);
editorCamera.transform.SetEulerRotation(orientation);
}
}

// Render
editorCamera.transform.LookAt(glm::vec3(1.f, 0.f, 0.f));

ImGui::GetWindowDrawList()->AddImage(
(ImTextureID)(uintptr_t)editorCamera.renderTexture,
vMin,
vMax,
ImVec2(0, 1),
ImVec2(1, 0));

ImGui::End();
ImGui::PopStyleVar();
}

private:
bool sceneViewActive;
ImVec2 windowPos, windowSize;

glm::vec3 orientation;
float speed = 2.5f, sensitivity = 75.f;
float lastx;

Renderer *renderer;
Window *window;
SceneManager *sceneManager;
InputManager *input;
};
}
1 change: 0 additions & 1 deletion Engine/Components/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@ namespace DT
projection = glm::orthoLH(-aspect, aspect, -1.0f, 1.0f, nearPlane, farPlane);
else
projection = glm::perspectiveLH(glm::radians(fieldOfView), window->GetWindowSize().x / window->GetWindowSize().y, nearPlane, farPlane);

}
}
22 changes: 15 additions & 7 deletions Engine/Components/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,43 @@ SOFTWARE.
#include <ECS/SceneManager.h>
#include <Core/Window.h>
#include <Core/InputManager.h>
#include <Renderer/BaseCamera.h>

namespace DT
{
class Camera : public Component
class Camera : public Component, public BaseCamera
{
public:
float fieldOfView = 75.f, nearPlane = 0.1f, farPlane = 100.f;
glm::mat4 projection, view;
bool isOrthographic = false;
Camera(Context &ctx) : BaseCamera(ctx)
{
activeCamera = std::shared_ptr<Camera>(this);
}

void Init(Context &ctx) override;
void Tick(Context &ctx, const float &dt) override;
inline glm::vec3 GetPosition()

glm::vec3 GetPosition() override
{
PROFILE();

return transform->translation;
}

inline glm::quat GetRotation()
glm::quat GetRotation() override
{
PROFILE();

return transform->rotation;
}

static std::shared_ptr<Camera> GetActiveCamera()
{
return activeCamera;
}

private:
std::shared_ptr<Transform> transform;
static inline std::shared_ptr<Camera> activeCamera;
Window *window;
};
}
Loading

0 comments on commit 0562e7c

Please sign in to comment.