Skip to content

Commit

Permalink
Editor camera created (Without delta time)
Browse files Browse the repository at this point in the history
  • Loading branch information
PR3C14D0 committed Apr 11, 2023
1 parent 5a1b732 commit 47810fe
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
13 changes: 7 additions & 6 deletions src/private/Engine/Camera/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
43 changes: 43 additions & 0 deletions src/private/Engine/Camera/EditorCamera.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions src/private/Engine/GameObject/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
GameObject::GameObject(std::string name) {
this->name = name;
this->transform = Transform{};
this->input = Input::GetInstance();
}

void GameObject::Start() {
Expand Down
4 changes: 2 additions & 2 deletions src/private/Engine/Scene/SceneManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions src/private/Math/Transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
10 changes: 10 additions & 0 deletions src/public/Engine/Camera/EditorCamera.h
Original file line number Diff line number Diff line change
@@ -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;
};
5 changes: 3 additions & 2 deletions src/public/Engine/GameObject/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Component*> components;

protected:
Input* input;
public:
std::string name;
GameObject(std::string name);
Expand Down
1 change: 1 addition & 0 deletions src/public/Engine/Scene/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Windows.h>
#include <map>
#include "Engine/Camera/Camera.h"
#include "Engine/Camera/EditorCamera.h"

class Core; /* Core class forward declaration */

Expand Down
5 changes: 5 additions & 0 deletions src/public/Math/Transform.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "Math/Vector3.h"
#include "DirectXIncludes.h"

class Transform {
public:
Expand All @@ -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);
};

0 comments on commit 47810fe

Please sign in to comment.