From 47810fed5550c18955b76900a7383d2915998c8d Mon Sep 17 00:00:00 2001 From: PR3C14D0 Date: Wed, 12 Apr 2023 00:26:59 +0200 Subject: [PATCH] Editor camera created (Without delta time) --- src/CMakeLists.txt | 2 +- src/private/Engine/Camera/Camera.cpp | 13 +++--- src/private/Engine/Camera/EditorCamera.cpp | 43 ++++++++++++++++++++ src/private/Engine/GameObject/GameObject.cpp | 1 + src/private/Engine/Scene/SceneManager.cpp | 4 +- src/private/Math/Transform.cpp | 23 +++++++++++ src/public/Engine/Camera/EditorCamera.h | 10 +++++ src/public/Engine/GameObject/GameObject.h | 5 ++- src/public/Engine/Scene/Scene.h | 1 + src/public/Math/Transform.h | 5 +++ 10 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 src/private/Engine/Camera/EditorCamera.cpp create mode 100644 src/public/Engine/Camera/EditorCamera.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index df0e002..5457105 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ -add_executable (Lumina "main.cpp" "public/Engine/Core.h" "private/Engine/Core.cpp" "public/Engine/Util.h" "public/Engine/ScreenQuad.h" "public/DirectXIncludes.h" "private/Engine/ScreenQuad.cpp" "public/Engine/Vertex.h" "public/Engine/Shader/Shader.h" "private/Engine/Shader/Shader.cpp" "public/Engine/Scene/Scene.h" "private/Engine/Scene/Scene.cpp" "public/Engine/GameObject/GameObject.h" "private/Engine/GameObject/GameObject.cpp" "public/Math/Vector3.h" "private/Math/Vector3.cpp" "public/Math/Transform.h" "private/Math/Transform.cpp" "public/Engine/Camera/Camera.h" "private/Engine/Camera/Camera.cpp" "public/Engine/Scene/SceneManager.h" "private/Engine/Scene/SceneManager.cpp" "public/Engine/ConstantBuffers.h" "public/Engine/GameObject/Component/Component.h" "public/Engine/GameObject/Component/Mesh.h" "private/Engine/GameObject/Component/Mesh.cpp" "private/Engine/GameObject/Component/Component.cpp" "public/Engine/Input.h" "private/Engine/Input.cpp") +add_executable (Lumina "main.cpp" "public/Engine/Core.h" "private/Engine/Core.cpp" "public/Engine/Util.h" "public/Engine/ScreenQuad.h" "public/DirectXIncludes.h" "private/Engine/ScreenQuad.cpp" "public/Engine/Vertex.h" "public/Engine/Shader/Shader.h" "private/Engine/Shader/Shader.cpp" "public/Engine/Scene/Scene.h" "private/Engine/Scene/Scene.cpp" "public/Engine/GameObject/GameObject.h" "private/Engine/GameObject/GameObject.cpp" "public/Math/Vector3.h" "private/Math/Vector3.cpp" "public/Math/Transform.h" "private/Math/Transform.cpp" "public/Engine/Camera/Camera.h" "private/Engine/Camera/Camera.cpp" "public/Engine/Scene/SceneManager.h" "private/Engine/Scene/SceneManager.cpp" "public/Engine/ConstantBuffers.h" "public/Engine/GameObject/Component/Component.h" "public/Engine/GameObject/Component/Mesh.h" "private/Engine/GameObject/Component/Mesh.cpp" "private/Engine/GameObject/Component/Component.cpp" "public/Engine/Input.h" "private/Engine/Input.cpp" "private/Engine/Camera/EditorCamera.cpp" "public/Engine/Camera/EditorCamera.h") if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET Lumina PROPERTY CXX_STANDARD 20) diff --git a/src/private/Engine/Camera/Camera.cpp b/src/private/Engine/Camera/Camera.cpp index 668f5b2..7ec5226 100644 --- a/src/private/Engine/Camera/Camera.cpp +++ b/src/private/Engine/Camera/Camera.cpp @@ -6,19 +6,20 @@ Camera::Camera(std::string name) : GameObject::GameObject(name) { int width, height; core->GetWindowSize(width, height); - this->transform.translate(0.f, -1.f, 0.f); - this->transform.rotate(-45.f, 0.f, 0.f); + this->input = Input::GetInstance(); + this->View = XMMatrixTranspose(XMMatrixIdentity() * XMMatrixTranslation(this->transform.location.x, this->transform.location.y, this->transform.location.z)); this->Projection = XMMatrixTranspose(XMMatrixPerspectiveFovLH(XMConvertToRadians(90.f), (float)width / (float)height, 0.001f, 300.f)); } void Camera::Update() { // GameObject::Update(); We'll simply comment this because we want to override our GameObject::Update method. - - this->View = XMMatrixTranspose(XMMatrixIdentity() * XMMatrixTranslation(this->transform.location.x, this->transform.location.y, this->transform.location.z)); + + this->View = XMMatrixTranspose(XMMatrixIdentity()); this->View *= XMMatrixTranspose(XMMatrixRotationX(XMConvertToRadians(this->transform.rotation.x))); - this->View *= XMMatrixTranspose(XMMatrixRotationX(XMConvertToRadians(this->transform.rotation.y))); - this->View *= XMMatrixTranspose(XMMatrixRotationX(XMConvertToRadians(this->transform.rotation.z))); + this->View *= XMMatrixTranspose(XMMatrixRotationY(XMConvertToRadians(this->transform.rotation.y))); + this->View *= XMMatrixTranspose(XMMatrixRotationZ(XMConvertToRadians(this->transform.rotation.z))); + this->View *= XMMatrixTranspose(XMMatrixTranslation(this->transform.location.x, this->transform.location.y, this->transform.location.z)); } void Camera::GetMatrices(XMMATRIX& View, XMMATRIX& Projection) { diff --git a/src/private/Engine/Camera/EditorCamera.cpp b/src/private/Engine/Camera/EditorCamera.cpp new file mode 100644 index 0000000..cff3d54 --- /dev/null +++ b/src/private/Engine/Camera/EditorCamera.cpp @@ -0,0 +1,43 @@ +#include "Engine/Camera/EditorCamera.h" + +EditorCamera::EditorCamera(std::string name) : Camera::Camera(name) { + +} + +void EditorCamera::Start() { + return; +} + +void EditorCamera::Update() { + Camera::Update(); + std::cout << "[X] " << this->input->deltaX << " [Y] " << this->input->deltaY << std::endl; + if (this->input->GetButtonDown(RIGHT)) { + this->input->ShowCursor(false); + float deltaX = this->input->deltaX; + float deltaY = this->input->deltaY; + + this->transform.rotate(deltaY, deltaX, 0.f); + + if (this->input->GetKeyDown('w')) { + Vector3 translation = this->transform.Forward() * 0.1f; + this->transform.translate(translation); + } + if (this->input->GetKeyDown('s')) { + Vector3 translation = this->transform.Forward() * -0.1f; + this->transform.translate(translation); + } + if (this->input->GetKeyDown('d')) { + Vector3 translation = this->transform.Right() * 0.1f; + this->transform.translate(translation); + } + if (this->input->GetKeyDown('a')) { + Vector3 translation = this->transform.Right() * -0.1f; + this->transform.translate(translation); + } + } + else { + this->input->ShowCursor(true); + } + + return; +} \ No newline at end of file diff --git a/src/private/Engine/GameObject/GameObject.cpp b/src/private/Engine/GameObject/GameObject.cpp index afd0d79..d661c51 100644 --- a/src/private/Engine/GameObject/GameObject.cpp +++ b/src/private/Engine/GameObject/GameObject.cpp @@ -3,6 +3,7 @@ GameObject::GameObject(std::string name) { this->name = name; this->transform = Transform{}; + this->input = Input::GetInstance(); } void GameObject::Start() { diff --git a/src/private/Engine/Scene/SceneManager.cpp b/src/private/Engine/Scene/SceneManager.cpp index 70e13ea..f0f1c4e 100644 --- a/src/private/Engine/Scene/SceneManager.cpp +++ b/src/private/Engine/Scene/SceneManager.cpp @@ -9,8 +9,8 @@ SceneManager::SceneManager() { void SceneManager::Start() { GameObject* sampleObj = new GameObject("Test"); Scene* sampleScene = new Scene("asd"); - Camera* sampleCamera = new Camera("Camera"); - sampleScene->gameObjects["Camera"] = sampleCamera; + EditorCamera* sampleCamera = new EditorCamera("Camera"); + sampleScene->gameObjects["Editor Camera"] = sampleCamera; sampleScene->actualCamera = sampleCamera; Mesh* mesh = new Mesh(&sampleObj->transform); sampleScene->gameObjects["Test"] = sampleObj; diff --git a/src/private/Math/Transform.cpp b/src/private/Math/Transform.cpp index 2bb6e75..4f4b046 100644 --- a/src/private/Math/Transform.cpp +++ b/src/private/Math/Transform.cpp @@ -26,4 +26,27 @@ void Transform::rotate(float x, float y, float z) { Vector3 v = Vector3{ x, y, z }; this->rotate(v); return; +} + +Vector3 Transform::Forward() { + Vector3 v = Vector3{ 0.f, 0.f, -1.f }; + return this->RotatePoint(v); +} + +Vector3 Transform::Right() { + Vector3 v = Vector3{ -1.f, 0.f, 0.f }; + return this->RotatePoint(v); +} + +Vector3 Transform::RotatePoint(Vector3 v) { + XMMATRIX rot = XMMatrixTranspose(XMMatrixIdentity()); + rot *= XMMatrixTranspose(XMMatrixRotationX(XMConvertToRadians(this->rotation.x))); + rot *= XMMatrixTranspose(XMMatrixRotationY(XMConvertToRadians(this->rotation.y))); + rot *= XMMatrixTranspose(XMMatrixRotationZ(XMConvertToRadians(this->rotation.z))); + + XMVECTOR rotToPoint = XMVector4Transform(XMVectorSet(v.x, v.y, v.z, 1.f), rot); + XMFLOAT4 rotToPointFloat; + XMStoreFloat4(&rotToPointFloat, rotToPoint); + + return Vector3(rotToPointFloat.x, rotToPointFloat.y, rotToPointFloat.z); } \ No newline at end of file diff --git a/src/public/Engine/Camera/EditorCamera.h b/src/public/Engine/Camera/EditorCamera.h new file mode 100644 index 0000000..1e2fef0 --- /dev/null +++ b/src/public/Engine/Camera/EditorCamera.h @@ -0,0 +1,10 @@ +#pragma once +#include "Engine/Camera/Camera.h" + +class EditorCamera : public Camera { +public: + EditorCamera(std::string name); + + void Start() override; + void Update() override; +}; \ No newline at end of file diff --git a/src/public/Engine/GameObject/GameObject.h b/src/public/Engine/GameObject/GameObject.h index e7b3e95..b504ca6 100644 --- a/src/public/Engine/GameObject/GameObject.h +++ b/src/public/Engine/GameObject/GameObject.h @@ -4,12 +4,13 @@ #include "Math/Transform.h" #include "DirectXIncludes.h" #include "Engine/GameObject/Component/Mesh.h" - +#include "Engine/Input.h" class GameObject { private: std::vector components; - +protected: + Input* input; public: std::string name; GameObject(std::string name); diff --git a/src/public/Engine/Scene/Scene.h b/src/public/Engine/Scene/Scene.h index a192b67..8170b31 100644 --- a/src/public/Engine/Scene/Scene.h +++ b/src/public/Engine/Scene/Scene.h @@ -3,6 +3,7 @@ #include #include #include "Engine/Camera/Camera.h" +#include "Engine/Camera/EditorCamera.h" class Core; /* Core class forward declaration */ diff --git a/src/public/Math/Transform.h b/src/public/Math/Transform.h index 854f028..a9b517d 100644 --- a/src/public/Math/Transform.h +++ b/src/public/Math/Transform.h @@ -1,5 +1,6 @@ #pragma once #include "Math/Vector3.h" +#include "DirectXIncludes.h" class Transform { public: @@ -14,4 +15,8 @@ class Transform { void rotate(Vector3 rotation); void rotate(float x, float y, float z); + + Vector3 Forward(); + Vector3 Right(); + Vector3 RotatePoint(Vector3 v); }; \ No newline at end of file