Skip to content

Commit

Permalink
Working editor scene that utilizes our scene structure with basic ECS…
Browse files Browse the repository at this point in the history
… system working.
  • Loading branch information
SpinnerX committed Nov 22, 2024
1 parent a13fee5 commit e163e8a
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 12 deletions.
1 change: 1 addition & 0 deletions Editor/Editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace engine3d{

void EditorApplication::OnApplicationUpdate(){
//! @note Handle Events.
// m_EditorScene->OnCameraUpdate();
auto& objects = m_EditorScene->GetSceneObjects();
Renderer::RecordSceneGameObjects(objects);
}
Expand Down
12 changes: 10 additions & 2 deletions Editor/Editor/EditorScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ namespace engine3d{
//! @note Setting our properties
auto& transform = cube->SceneGetComponent<Transform>();

transform.m_Position = {.0f, .0f, .5f};
transform.m_Scale = {.5f, .5f, .5f};
// negative-float closer, positive-float z further
transform.m_Position = {.0f, .0f, .75f};
// transform.m_Scale = {.5f, .5f, 0.5};
transform.m_Scale = {.5f, .5f, 0.5};
// cube->SetModel(cube_mesh);
cube->SetModal(cube_mesh);

Expand All @@ -89,4 +91,10 @@ namespace engine3d{

void EditorScene::OnCreate(){
}

// void EditorScene::OnCameraUpdate(){
// for(const auto& obj : m_SceneObjects){
// obj->SceneGetComponent<EditorCamera>().OnUpdate();
// }
// }
};
4 changes: 2 additions & 2 deletions engine3d/Core/Renderer/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace engine3d{
public:
static void Initialize(const std::string& p_DebugName);

static VkCommandBuffer BeginFrame();
static void BeginFrame();

//! @note Currently the record command buffers just does what "simple_render_system.hpp/.cpp" does already
//! @note In the future I'll add API's for submitting draw calls.
static void RecordCommandBuffers(VkCommandBuffer p_CommandBuffer);
// static void RecordCommandBuffers(VkCommandBuffer p_CommandBuffer);
static void RecordGameObjects(std::vector<SceneObjectTutorial>& p_Objects);
static void RecordSceneGameObjects(std::vector<SceneObject*>& p_SceneObjects);
static void EndFrame();
Expand Down
79 changes: 79 additions & 0 deletions engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#pragma once

#include <Core/SceneManagment/Components/GameComponent.hpp>
#include <glm/glm.hpp>
#include <string>

namespace engine3d{
class EditorCamera : public GameComponent{
public:
struct CameraData{
float PerspectiveFOV = glm::radians(45.0f);
float PerspectiveNear = 0.01;
float PerspectiveFar = 1000.0f;
float OrthographicSize = 10.0f;
float OrthographicNear = 1.0f;
float OrthographicFar = 1.0f;
};

enum CameraProjectionType{
UNDEFINED = -1,
ORTHOGRAPHIC = 0,
PERSPECTIVE = 1
};


EditorCamera() = default;
EditorCamera(const std::string& p_Tag);

void SetInitialProperties(float p_Fov, float p_NearClip, float p_FarClip);
void OnUpdate();

//! @note Eventually we will refer to OnIntegrate as OnCreate or OnPlay or something.
//! @note Come up with a better name for this for accurate API.
void OnIntegrate();

glm::quat GetOrientation() const;
glm::vec3 GetUpDirection() const;
glm::vec3 GetRightDirection() const;
glm::vec3 GetForwardDirection() const;

glm::mat4 GetProjection() const { return m_ProjectionMatrix; }
glm::mat4 GetView() const { return m_ViewMatrix; }


//! @note Getting pans.
void CameraPan(const glm::vec2& p_Data);
void CameraZoom(float delta);
void CameraRotate(const glm::vec2& p_Data);

private:
glm::vec3 CalculateNewPosition() const;
void RecalculateView();

void RecalculateProjection();

glm::vec3 RecalculatePosition() const;

glm::vec2 PanSpeed() const;
float ZoomSpeed() const;

float RotationSpeed() const;

private:
std::string m_Tag;
glm::vec2 m_InitialMousePosition = {0.0f, 0.0f};
float m_Fov = 45.0f, m_AspectRatio = 1.778f, m_NearClip=0.1f, m_FarClip = 1000.0f;

glm::mat4 m_ViewMatrix;
glm::mat4 m_ProjectionMatrix;
glm::vec3 m_Position;
glm::vec3 m_FocalPoint;
//! @note Moving this out of Editor Camera.
// glm::vec2 m_InitialMousePosition
float m_Distance = 10.0f;
float m_Pitch = 0.f, m_Yaw = 0.f;
glm::vec2 m_ViewportSize;

};
};
4 changes: 2 additions & 2 deletions sim_shader_transforms/simple_shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ void main(){
// gl_Position = vec4(p * Position + push.offset, 0.0, 1.0);
mat4 p = push.transform;
p[0] /= (push.iResolution.x / push.iResolution.y);
p[1] /= (push.iResolution.x / push.iResolution.y);
p[2] /= (push.iResolution.x / push.iResolution.y);
// p[1] /= (push.iResolution.x / push.iResolution.y);
// p[2] /= (push.iResolution.x / push.iResolution.y);
gl_Position = p * vec4(Position, 1.0);
fragColor = Color;
}
Expand Down
Binary file modified sim_shader_transforms/simple_shader.vert.spv
Binary file not shown.
3 changes: 2 additions & 1 deletion src/engine3d/Core/ApplicationInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ namespace engine3d{

// Renderer::EndFrame();
// Renderer::BeginFrame();
Renderer::RecordCommandBuffers(Renderer::BeginFrame());
Renderer::BeginFrame();
// Renderer::RecordCommandBuffers(Renderer::BeginFrame());
m_Window->OnUpdateAllFrames();
Renderer::EndFrame();
}
Expand Down
21 changes: 16 additions & 5 deletions src/engine3d/Core/Renderer/Renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// #include "internal/VulkanCpp/Vulkan.hpp"
#include "EngineLogger.hpp"
#include "GraphicDrivers/Shader.hpp"
#include "TimeManagement/UpdateManagers/SyncUpdateManager.hpp"
#include "internal/Vulkan2Showcase/Shaders/VulkanShader.hpp"
#include "internal/Vulkan2Showcase/VulkanContext.hpp"
#include "internal/Vulkan2Showcase/VulkanModel.hpp"
#include "internal/Vulkan2Showcase/helper_functions.hpp"
#include <Core/ApplicationInstance.hpp>
#include <Core/Renderer/Renderer.hpp>
#include <glm/geometric.hpp>
#include <vulkan/vulkan_core.h>
#include <vector>

Expand Down Expand Up @@ -105,7 +107,7 @@ namespace engine3d{
VkClearValue clearColorValue = {{p_Rgba[0], p_Rgba[1], p_Rgba[2], p_Rgba[3]}};
}

VkCommandBuffer Renderer::BeginFrame(){
void Renderer::BeginFrame(){
g_CurrentFrameIndex = ApplicationInstance::GetWindow().GetCurrentSwapchain()->AcquireNextImage();
VkCommandBufferBeginInfo cmd_buffer_begin_info = {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
Expand Down Expand Up @@ -134,7 +136,8 @@ namespace engine3d{

vkCmdBeginRenderPass(cmd_buffer, &rp_begin_info, VK_SUBPASS_CONTENTS_INLINE);
g_IsFrameStarted = true;
return cmd_buffer;
// return cmd_buffer;
g_CommandBuffers[g_CurrentFrameIndex] = cmd_buffer;
}

void Renderer::RecordGameObjects(std::vector<SceneObjectTutorial>& p_Objects){
Expand All @@ -146,6 +149,7 @@ namespace engine3d{
//! @note Only for testing purposes for mesh data.
for(auto& obj : p_Objects){
obj.m_Transform2D.rotation.y = glm::mod(obj.GetTransform().rotation.y + 0.001f, glm::two_pi<float>());
obj.m_Transform2D.rotation.x = glm::mod(obj.GetTransform().rotation.x + 0.001f, glm::two_pi<float>());

SimplePushConstantData push = {
.Transform = obj.GetTransform().mat4(),
Expand All @@ -171,21 +175,28 @@ namespace engine3d{
//! @note Essentially doing m_Pipeline->Bind(m_CommandBuffer[i])
//! @note Starts when to start rendering!!
vkCmdBindPipeline(current_cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_Shader->GetGraphicsPipeline());
float delta_time = SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime;
// ConsoleLogWarn("Delta Time = {:.7}", delta_time);

//! @note Only for testing purposes for mesh data.
for(auto& obj : p_Objects){
// obj.m_Transform2D.rotation.y = glm::mod(obj.GetTransform().rotation.y + 0.001f, glm::two_pi<float>());
float x = obj->GetRotation().x;
float y = glm::mod(obj->GetRotation().y + 0.001f, glm::two_pi<float>());
// float x = obj->GetRotation().x;
float y = glm::mod(obj->GetRotation().y + (0.1f * delta_time), glm::two_pi<float>());
float x = glm::mod(obj->GetRotation().x + (0.05f * delta_time), glm::two_pi<float>());
float z = obj->GetRotation().z;
obj->SetRotation({x, y, z});

glm::vec3 new_position = {x, y, z};
// new_position = glm::normalize(new_position);
obj->SetRotation(new_position);

SimplePushConstantData push = {
// .Transform = obj.GetTransform().mat4(),
.Transform = obj->toMat4(),
.iResolution = {ApplicationInstance::GetWindow().GetWidth(), ApplicationInstance::GetWindow().GetHeight()},
// .Color = obj.GetColor(),
};

vkCmdPushConstants(
current_cmd_buffer,
g_PipelineLayout,
Expand Down
131 changes: 131 additions & 0 deletions src/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// #include "EditorCamera.hpp"
#include <Core/SceneManagment/Components/SPComps/EditorCamera.hpp>
#include <Core/Event/InputPoll.hpp>
#include <Core/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(){
}

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();
}

void EditorCamera::SetInitialProperties(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();
}

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};
}
};
4 changes: 4 additions & 0 deletions src/engine3d/Core/SceneManagment/SceneObjects/SceneObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ namespace engine3d {
auto transform_component = SceneGetComponent<Transform>();

auto transform = glm::translate(glm::mat4{1.f}, transform_component.m_Position);
transform[0][0] *= transform_component.m_Scale.x;
transform[1][1] *= transform_component.m_Scale.y;
transform[2][2] *= transform_component.m_Scale.z;

transform = glm::rotate(transform, transform_component.m_AxisRotation.y, {0.f, 1.f, 0.f});
transform = glm::rotate(transform, transform_component.m_AxisRotation.x, {1.f, 0.f, 0.f});
transform = glm::rotate(transform, transform_component.m_AxisRotation.z, {0.f, 0.f, 1.f});
Expand Down
1 change: 1 addition & 0 deletions src/engine3d/Core/TimeManagement/GlobalUpdateManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace engine3d

m_GlobalDeltaTime = 0.0;
m_UpdateTime = m_GlobalTimer->GetCurrentTime();
// m_MaxFPS = 5000;
m_MaxFPS = 500;
m_FPSCounter = 1;

Expand Down
1 change: 1 addition & 0 deletions src/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace engine3d::vk{
vkBindBufferMemory(VulkanContext::GetDriver(), m_VertexBuffer, m_VertexBufferDeviceMemory, 0);

//! @note Mapping memory data.
//! @note THIS is how we map our vertices data to our VkBuffer (essentially it is our vertex buffer)
void* data;
vkMapMemory(VulkanContext::GetDriver(), m_VertexBufferDeviceMemory, 0, buffer_size, 0, &data);
memcpy(data, p_Vertices.data(), static_cast<size_t>(buffer_size));
Expand Down

0 comments on commit e163e8a

Please sign in to comment.