Skip to content

Commit

Permalink
Working EditorScene to test the ECS and custom Scene objects intsead …
Browse files Browse the repository at this point in the history
…of the ones from the tutorials
  • Loading branch information
SpinnerX committed Nov 21, 2024
1 parent 0cdc76a commit 97573bc
Show file tree
Hide file tree
Showing 31 changed files with 647 additions and 220 deletions.
3 changes: 3 additions & 0 deletions Editor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ build_demos(
SOURCES
Editor/Editor.hpp
Editor/Editor.cpp

Editor/EditorScene.hpp
Editor/EditorScene.cpp
LINK_LIBRARIES engine3d
)
76 changes: 19 additions & 57 deletions Editor/Editor/Editor.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,14 @@
#include "Editor.hpp"
#include <Core/Core.hpp>
#include "Core/Renderer/Renderer.hpp"
#include "Core/Scene/SceneObject.hpp"
// #include "internal/VulkanCpp/helper_functions.hpp"
#include <Core/internal/Vulkan2Showcase/helper_functions.hpp>
#include <Core/internal/Vulkan2Showcase/VulkanContext.hpp>
#include <Core/Event/InputPoll.hpp>
#include <Core/Event/KeyCodes.hpp>
#include <Core/EngineLogger.hpp>
#include <Core/Timestep.hpp>
#include <glm/common.hpp>
#include <glm/fwd.hpp>
#include <glm/gtc/constants.hpp>
#include <vulkan/vulkan_core.h>
#include "EditorScene.hpp"
#include <Core/internal/Vulkan2Showcase/VulkanModel.hpp>
#include <Core/Scene/SceneTest.hpp>
#include <Core/TimeManagement/GlobalUpdateManager.hpp>
#include <Core/Renderer/Renderer.hpp>

namespace engine3d{
/*
Ref<vk::VulkanModel> CreateSquareMesh(glm::vec2 offset) {
std::vector<vk::VulkanModel::Vertex> vertices = {
{{-0.5f, -0.5f}},
{{0.5f, 0.5f}},
{{-0.5f, 0.5f}},
{{-0.5f, -0.5f}},
{{0.5f, -0.5f}},
{{0.5f, 0.5f}}, //
};
for (auto& v : vertices) {
v.Position += offset;
}
return CreateRef<vk::VulkanModel>(vertices, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
}
Ref<vk::VulkanModel> CreateCircleMesh(unsigned int numSides) {
std::vector<vk::VulkanModel::Vertex> uniqueVertices{};
for (int i = 0; i < numSides; i++) {
float angle = i * glm::two_pi<float>() / numSides;
uniqueVertices.push_back({{glm::cos(angle), glm::sin(angle)}});
}
uniqueVertices.push_back({}); // adds center vertex at 0, 0
std::vector<vk::VulkanModel::Vertex> vertices{};
for (int i = 0; i < numSides; i++) {
vertices.push_back(uniqueVertices[i]);
vertices.push_back(uniqueVertices[(i + 1) % numSides]);
vertices.push_back(uniqueVertices[numSides]);
}
return CreateRef<vk::VulkanModel>(vertices, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
}
*/


/*
Ref<vk::VulkanModel> CreateCubeMesh(glm::vec3 offset){
std::vector<vk::VulkanModel::Vertex> vertices{
// left face (white)
Expand Down Expand Up @@ -106,22 +65,23 @@ namespace engine3d{
}
return CreateRef<vk::VulkanModel>(vertices, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
}
*/


EditorApplication::EditorApplication(const std::string& p_DebugName) : ApplicationInstance(p_DebugName) {
GlobalUpdateManager::GetInstance()->SubscribeApplicationUpdate(this, &EditorApplication::OnApplicationUpdate);
m_CubeMesh = CreateCubeMesh({.0f, .0f, .0f});
// Ref<vk::VulkanModel> m_CubeMesh = CreateCubeMesh({.0f, .0f, .0f});


auto cube = SceneObject::Create();
cube.SetModel(m_CubeMesh);
cube.GetTransform().Translation = {.0f, .0f, .5f};
cube.GetTransform().scale = {.5f, .5f, .5f};
// auto cube = SceneObjectTutorial::Create();
// cube.SetModel(m_CubeMesh);
// cube.GetTransform().Translation = {.0f, .0f, .5f};
// cube.GetTransform().scale = {.5f, .5f, .5f};

m_SceneGameObjects.push_back(cube);
// m_SceneGameObjects.push_back(cube);

ConsoleLogError("m_SceneGameObjects.size() === {}", m_SceneGameObjects.size());

// ConsoleLogError("m_SceneGameObjects.size() === {}", m_SceneGameObjects.size());
m_EditorScene = new EditorScene();


// m_GravitySystem = GravityPhysicsSystem(0.81f);
Expand Down Expand Up @@ -176,7 +136,9 @@ namespace engine3d{
//! @note Handle Events.
// m_GravitySystem.update(m_SceneGameObjects, 5);
// m_VectorFieldSystem.update(m_GravitySystem, m_SceneGameObjects, m_VectorFields);
Renderer::RecordGameObjects(m_SceneGameObjects);
auto& objects = m_EditorScene->GetSceneObjects();
Renderer::RecordSceneGameObjects(objects);
// Renderer::RecordGameObjects(m_SceneGameObjects);
// Renderer::RecordGameObjects(m_VectorFields);


Expand Down
94 changes: 10 additions & 84 deletions Editor/Editor/Editor.hpp
Original file line number Diff line number Diff line change
@@ -1,85 +1,11 @@
#pragma once
#include "Core/internal/Vulkan2Showcase/VulkanModel.hpp"
// #include <Core/internal/Vulkan2Showcase/VulkanModel.hpp>
#include <Core/ApplicationInstance.hpp>
#include <Core/Scene/SceneObject.hpp>
#include <Core/TimeManagement/GlobalUpdateManager.hpp>
// #include <Core/Scene/SceneObject.hpp>
// #include <Core/TimeManagement/GlobalUpdateManager.hpp>
#include "EditorScene.hpp"

namespace engine3d{

/*
class GravityPhysicsSystem{
public:
GravityPhysicsSystem() = default;
GravityPhysicsSystem(float strength) : m_Strength(strength){}
void update(std::vector<SceneObject>& p_Objects, unsigned int substeps = 1){
const float step_data = SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime / substeps;
for(int i = 0; i < substeps; i++){
step_simulation(p_Objects, step_data);
}
}
glm::vec2 ComputeForce(SceneObject& p_FromObject, SceneObject& p_ToObject) const{
auto offset = p_FromObject.GetTransform().Translation - p_ToObject.GetTransform().Translation;
float dist_squared = glm::dot(offset, offset);
if(glm::abs(dist_squared) < 1e-10f){
return {.0f, .0f};
}
float force = m_Strength * p_ToObject.RigidBodyComponent.mass * p_FromObject.RigidBodyComponent.mass / dist_squared;
return force * offset / glm::sqrt(dist_squared);
}
private:
void step_simulation(std::vector<SceneObject>& physicsObjs, float dt){
// Loops through all pairs of objects and applies attractive force between them
for (auto iterA = physicsObjs.begin(); iterA != physicsObjs.end(); ++iterA) {
auto& objA = *iterA;
for (auto iterB = iterA; iterB != physicsObjs.end(); ++iterB) {
if (iterA == iterB) continue;
auto& objB = *iterB;
auto force = ComputeForce(objA, objB);
objA.RigidBodyComponent.velocity += dt * -force / objA.RigidBodyComponent.mass;
objB.RigidBodyComponent.velocity += dt * force / objB.RigidBodyComponent.mass;
}
}
// update each objects position based on its final velocity
for (auto& obj : physicsObjs) {
obj.GetTransform().Translation += dt * obj.RigidBodyComponent.velocity;
}
}
private:
float m_Strength;
};
class Vec2FieldSystem{
public:
Vec2FieldSystem() = default;
void update(const GravityPhysicsSystem& physicsSystem, std::vector<SceneObject>& physicsObjs, std::vector<SceneObject>& vectorField){
// For each field line we caluclate the net graviation force for that point in space
for (auto& vf : vectorField) {
glm::vec2 direction{};
for (auto& obj : physicsObjs) {
direction += physicsSystem.ComputeForce(obj, vf);
}
// This scales the length of the field line based on the log of the length
// values were chosen just through trial and error based on what i liked the look
// of and then the field line is rotated to point in the direction of the field
vf.GetTransform().scale.x = 0.005f + 0.045f * glm::clamp(glm::log(glm::length(direction) + 1) / 3.f, 0.f, 1.f);
vf.GetTransform().rotation = atan2(direction.y, direction.x);
}
}
};
*/

//! @note Our actual editor application
//! @note TODO -- Having a cenrtal application that will be used across both editor and the game(being developed)
Expand All @@ -96,15 +22,15 @@ namespace engine3d{
void OnApplicationUpdate();

private:
Ref<vk::VulkanModel> m_SquareMesh;
Ref<vk::VulkanModel> m_CircleMesh;

std::vector<SceneObject> m_SceneGameObjects;
std::vector<SceneObject> m_VectorFields;
// Ref<vk::VulkanModel> m_SquareMesh;
// Ref<vk::VulkanModel> m_CircleMesh;
EditorScene* m_EditorScene;
// std::vector<SceneObject> m_SceneGameObjects;
// std::vector<SceneObject> m_VectorFields;

// GravityPhysicsSystem m_GravitySystem;
// Vec2FieldSystem m_VectorFieldSystem;
Ref<vk::VulkanModel> m_CubeMesh;
// Ref<vk::VulkanModel> m_CubeMesh;



Expand Down
128 changes: 128 additions & 0 deletions Editor/Editor/EditorComponents/EditorCamera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include "EditorCamera.hpp"
#include "Event/InputPoll.hpp"
#include "TimeManagement/UpdateManagers/SyncUpdateManager.hpp"
#include <Core/ApplicationInstance.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/quaternion.hpp>

#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/matrix_decompose.hpp>
#include <glm/gtc/type_ptr.hpp>

namespace engine3d{
EditorCamera::EditorCamera(const std::string& p_Tag) : m_Tag(p_Tag) {}

void EditorCamera::OnIntegrate(float p_Fov, float p_NearClip, float p_FarClip){
m_Fov = p_Fov;
m_AspectRatio = ((float)ApplicationInstance::GetWindow().GetWidth() / (float)ApplicationInstance::GetWindow().GetHeight());
m_NearClip = p_NearClip;
m_FarClip = p_FarClip;

m_ViewportSize.x = (float)ApplicationInstance::GetWindow().GetWidth();
m_ViewportSize.y = (float)ApplicationInstance::GetWindow().GetHeight();

RecalculateView();
}

void EditorCamera::OnUpdate(){
if(InputPoll::IsKeyPressed(ENGINE_KEY_LEFT_ALT)){
glm::vec2 delta = (InputPoll::GetMousePosition() - m_InitialMousePosition) * 0.003f;
m_InitialMousePosition = InputPoll::GetMousePosition();

if(InputPoll::IsMousePressed(Mouse::ButtonMiddle)){
CameraPan(delta);
}
else if(InputPoll::IsMousePressed(Mouse::ButtonLast)){
CameraRotate(delta);
}
else if(InputPoll::IsMousePressed(Mouse::ButtonRight)){
CameraZoom(delta.y);
}
}

RecalculateView();
}

glm::quat EditorCamera::GetOrientation() const {
return glm::quat(glm::vec3(-m_Pitch, -m_Yaw, 1.0f));
}

glm::vec3 EditorCamera::GetUpDirection() const{
return glm::rotate(GetOrientation(), glm::vec3{0.0f, 1.0f, 0.0f});
}

glm::vec3 EditorCamera::GetRightDirection() const {
return glm::rotate(GetOrientation(), glm::vec3(1.0f, 0.0f, 0.0f));
}

glm::vec3 EditorCamera::GetForwardDirection() const {
return glm::rotate(GetOrientation(), glm::vec3(0.0f, 0.0f, -1.0f));
}


//! @note Getting pans.
void EditorCamera::CameraPan(const glm::vec2& p_Data){}

void EditorCamera::CameraZoom(float delta) {
// float dt = SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime;
m_Distance -= (delta * ZoomSpeed());

if(m_Distance < 1.0f){
m_FocalPoint += GetForwardDirection();
m_Distance = 1.0f;
}
}

void EditorCamera::CameraRotate(const glm::vec2& p_Position) {
float yawSign = GetUpDirection().y < 0 ? -1.0f : 1.0f;
m_Yaw += yawSign * p_Position.x * RotationSpeed();
m_Pitch += p_Position.y * RotationSpeed();
}

float EditorCamera::RotationSpeed() const {
return 0.8f;
}

glm::vec3 EditorCamera::CalculateNewPosition() const{
return m_FocalPoint - GetForwardDirection() * m_Distance;
}

float EditorCamera::ZoomSpeed() const{
float distance = m_Distance * 0.2f;
distance = std::max(distance, 0.0f);

float speed = distance * distance;
speed = std::min(speed, 100.0f);
return speed;
}

void EditorCamera::RecalculateProjection() {
m_ProjectionMatrix = glm::perspective(glm::radians(m_Fov), m_AspectRatio, m_NearClip, m_FarClip);
RecalculateView();
}

void EditorCamera::RecalculateView() {
m_Position = CalculateNewPosition();
glm::quat orientation = GetOrientation();
m_ViewMatrix = glm::translate(glm::mat4(1.0f), m_Position) * glm::toMat4(orientation);
m_ViewMatrix = glm::inverse(m_ViewMatrix);
}

glm::vec3 EditorCamera::RecalculatePosition() const {
return m_FocalPoint * GetForwardDirection() * m_Distance;

}

glm::vec2 EditorCamera::PanSpeed() const{
float x = std::min(m_ViewportSize.x / 1000.0f, 2.4f); // max = 2.4.f
float xFactor = 0.0366f * (x * x) - 0.1778f * x + 0.3021f;

float y = std::min(m_ViewportSize.y / 1000.0f, 2.4f); // max = 2.4f
float yFactor = 0.0366f * (y * y) - 0.1778f * y + 0.3021f;

return {xFactor, yFactor};
}
};
Loading

0 comments on commit 97573bc

Please sign in to comment.