Skip to content

Commit

Permalink
Connected the physics to the renderer. (#71)
Browse files Browse the repository at this point in the history
Co-authored-by: ZacharyH777 <[email protected]>
  • Loading branch information
ZacharyH777 and ZHowe1 authored Nov 24, 2024
1 parent 403996f commit 524e8a2
Show file tree
Hide file tree
Showing 22 changed files with 622 additions and 79 deletions.
41 changes: 22 additions & 19 deletions Editor/Editor/EditorScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "Math/Math.hpp"
#include <Core/SceneManagment/Components/SPComps/Transform.hpp>
#include <Core/EngineLogger.hpp>
#include <Core/SceneManagment/Components/SPComps/EditorCamera.hpp>
#include <Core/SceneManagment/Components/SPComps/Camera.hpp>
#include <Core/TimeManagement/UpdateManagers/SyncUpdateManager.hpp>
#include <Math/Interpolation.hpp>
#include <Core/ApplicationInstance.hpp>
Expand All @@ -22,7 +22,7 @@ namespace engine3d{
// auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/smooth_vase.obj");
// auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/FinalBaseMesh.obj");
// auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/Castelia City.obj");
auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/bugatti.obj");
auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/smooth_vase.obj");
// auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/colored_cube.obj");
// auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/sphere.obj");
//! @note Make this scene object as part of our current scene.
Expand All @@ -31,30 +31,33 @@ namespace engine3d{
// Camera Scene Object Creation
// -----------------------------
SceneObject* m_CameraObject = new SceneObject(m_Scene);
m_CameraObject->AddComponent<EditorCamera>();
auto& camera_transform = m_CameraObject->SceneGetComponent<Transform>();
camera_transform.m_Position = {-1.f, -2.f, -20.f};
m_CameraObject->AddComponent<Camera>();
auto& camera_transform = m_CameraObject->GetComponent<Transform>();
camera_transform.m_Position = {-1.f, 5.f, -20.f};
// camera_transform.m_AxisRotation = {glm::radians(180.0f), 0.f, 0.f};
auto camera = m_CameraObject->SceneGetComponent<EditorCamera>();
auto camera = m_CameraObject->GetComponent<Camera>();
m_CameraObjects.push_back(m_CameraObject);


// -----------------------------
// Cube 1 Scene object Creation
// -----------------------------
SceneObject* cube1 = new SceneObject(m_Scene);
auto& cube1_transform = cube1->SceneGetComponent<Transform>();
auto& cube1_transform = cube1->GetComponent<Transform>();
cube1_transform.m_Position = {.0f, .0f, 2.5};
cube1_transform.m_Scale = {.5f, .5f, .5f};
cube1_transform.m_Scale = {10.5f, 10.5f, 10.5f};
// cube1_transform.m_AxisRotation = ToQuat(glm::vec3(glm::radians(180.0f), 0.f, 0.f));
cube1_transform.m_AxisRotation = {glm::radians(180.0f), 0.0f, 0.0f};
cube1_transform.m_AxisRotation = {
glm::radians(0.0f),
glm::radians(0.0f),
glm::radians(0.0f)};
cube1->SetMesh(cube_mesh);

// -----------------------------
// Cube 2 Scene object Creation
// -----------------------------
SceneObject* cube2 = new SceneObject(m_Scene);
auto& cube2_transform = cube2->SceneGetComponent<Transform>();
auto& cube2_transform = cube2->GetComponent<Transform>();
// auto aspect_ratio = ApplicationInstance::GetWindow().GetAspectRatio();
cube2_transform.m_Position = {5.f, .0f, -7.f};
cube2_transform.m_Scale = {5.5f, 5.5f, 5.5};
Expand All @@ -63,35 +66,35 @@ namespace engine3d{

SceneObject* sphere_point_light = new SceneObject(m_Scene);
Mesh mesh = Mesh::LoadModel("3d_models/tutorial/sphere.obj");
auto& sphere_transform = sphere_point_light->SceneGetComponent<Transform>();
auto& sphere_transform = sphere_point_light->GetComponent<Transform>();
sphere_transform.m_Position = {-10.0, 3.0, -1.0};
sphere_transform.m_Scale = {1.f, 1.f, 1.f};
sphere_point_light->SetMesh(mesh);


//! @note Then we add them to our vector.
m_SceneObjects.push_back(cube1);
// m_SceneObjects.push_back(cube2);
m_SceneObjects.push_back(cube2);
m_PointLightObjects.push_back(sphere_point_light);

m_AllSceneObjecs.insert({"SceneObjects", m_SceneObjects});
m_AllSceneObjecs.insert({"RenderedObjects", m_SceneObjects});
m_AllSceneObjecs.insert({"PointLights", m_PointLightObjects});
m_AllSceneObjecs.insert({"PointRadioLights", m_PointRadioLights});
m_AllSceneObjecs.insert({"PointRadiusLights", m_PointRadioLights});
m_AllSceneObjecs.insert({"Cameras", m_CameraObjects});

}

void EditorScene::OnMoveCamUpdate(){

auto& cameraObject = m_AllSceneObjecs["Cameras"].at(0);
auto& transform = cameraObject->SceneGetComponent<Transform>();
auto& camera = cameraObject->SceneGetComponent<EditorCamera>();
auto cube_transform = m_SceneObjects[0]->SceneGetComponent<Transform>();
auto& transform = cameraObject->GetComponent<Transform>();
auto& camera = cameraObject->GetComponent<Camera>();
auto cube_transform = m_SceneObjects[0]->GetComponent<Transform>();
// float tempDt_Y;
glm::vec2 temp_position = {0.f, 0.f};
constexpr float sensitivity = 2.0f;
float pos_sensitivity = 2.f;
constexpr glm::vec2 invert_pos = {1, -1};
constexpr glm::vec2 invert_pos = {-1, -1};
glm::vec3 rotate{0};

//! @note Make sure that our mouse controls how camera rotates.
Expand Down Expand Up @@ -138,7 +141,7 @@ namespace engine3d{
camera.SetViewXYZ(transform.m_Position, transform.m_AxisRotation);


camera.SetPerspectiveProjection(glm::radians(50.f), ApplicationInstance::GetWindow().GetAspectRatio(), 0.1f, 1000.f);
camera.SetPerspectiveProjection(glm::radians(50.f), ApplicationInstance::GetWindow().GetAspectRatio(), 1.f, 1000.f);

}
};
9 changes: 6 additions & 3 deletions TestApp/Application.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#include "Core/EngineLogger.hpp"
#include <engine3d/Core/ApplicationInstance.hpp>
#include "Scenes/Assets/SceneInstances/ShowCaseSceneInstance.hpp"
#include <Scenes/Assets/WorldInstances/ShowCaseWorldInstance.hpp>
#include <string>

namespace engine3d{
class TestbedApplication : public ApplicationInstance{
public:
// TestbedApplication() = default;
TestbedApplication(const std::string& p_DebugName="Show Case") : ApplicationInstance(p_DebugName) {}
TestbedApplication(const std::string& p_DebugName="Show Case") : ApplicationInstance(p_DebugName)
{
m_WorldManager = new ShowCaseWorldInstance();
}
private:
ShowCaseSceneInstance m_SceneManager;
ShowCaseWorldInstance* m_WorldManager;
};

ApplicationInstance* InitializeApplication(){
Expand Down
6 changes: 3 additions & 3 deletions TestApp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ build_demos(
Scenes/Assets/Components/Bodies/Shapes/SphereShaper.hpp
src/Scenes/Assets/Components/Bodies/Shapes/SphereShaper.cpp

#Scenes
Scenes/Assets/SceneInstances/ShowCaseSceneInstance.hpp
src/Scenes/Assets/SceneInstances/ShowCaseSceneInstance.cpp
#Worlds
Scenes/Assets/WorldInstances/ShowCaseWorldInstance.hpp
src/Scenes/Assets/WorldInstances/ShowCaseWorldInstance.cpp

#Graphics
Scenes/Assets/Components/Graphics/SpriteRender3D.hpp
Expand Down
15 changes: 8 additions & 7 deletions TestApp/Scenes/Assets/Components/Graphics/SpriteRender3D.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@

#include <engine3d/Core/SceneManagment/Components/GameComponent.hpp>
#include <Scenes/Assets/Components/Physics/PhysicsBody3D.hpp>
#include <Core/SceneManagment/SceneObjects/SceneObject.hpp>
#include <Scenes/Assets/Components/Graphics/Meshes/MeshContainer.hpp>

#include <Core/GraphicDrivers/Mesh.hpp>
class SpriteRender3D : public engine3d::GameComponent
{
public:
SpriteRender3D(MeshContainer* meshBody);
void OnIntegrate();
void Update();
void LateUpdate();
void PhysicsUpdate();
SpriteRender3D() = default;
SpriteRender3D(const engine3d::Mesh& p_MeshBody, engine3d::SceneObject* p_sceneObj);
SpriteRender3D(const std::string& p_model, engine3d::SceneObject* p_sceneObj);
void OnIntegrate();
void RenderObject();
std::vector<glm::vec3> vertices;
std::vector<glm::vec3> normals;
std::vector<glm::vec2> texCoords;

private:
MeshContainer* m_MeshContainer;
engine3d::Mesh m_Mesh;
engine3d::Transform m_Transform;
glm::vec3* m_TransformPosition;
};
30 changes: 30 additions & 0 deletions TestApp/Scenes/Assets/WorldInstances/ShowCaseWorldInstance.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <Core/SceneManagment/SceneObjects/SceneObject.hpp>
#include <Core/ApplicationManager/Scene.hpp>


#include <vector>

// #include "Core/SceneManagment/SceneObjects/SceneObject.hpp"
class ShowCaseWorldInstance
{
public:
ShowCaseWorldInstance();
~ShowCaseWorldInstance();
std::vector<engine3d::Scene*> m_Scenes;
engine3d::Scene* GetScene();
void RenderScenes();


private:
void CreateObjects();
std::unordered_map<std::string, std::vector<engine3d::SceneObject *>> m_AllSceneObjecs;
std::vector<engine3d::SceneObject*> m_SceneObjectList;
std::vector<engine3d::SceneObject*> m_RenderedObjectList;
std::vector<engine3d::SceneObject*> m_PointLightObjectList;
std::vector<engine3d::SceneObject*> m_CameraObjectList;
std::vector<engine3d::SceneObject*> m_PointRadiusLightList;

glm::vec2 m_MousePosition;
float m_MoveSpeed = {5.f};
float m_LookSpeed = {1.5f};
};
39 changes: 19 additions & 20 deletions TestApp/src/Scenes/Assets/Components/Graphics/SpriteRender3D.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
#include "Core/EngineLogger.hpp"
#include "Core/GraphicDrivers/Mesh.hpp"
#include <Scenes/Assets/Components/Graphics/SpriteRender3D.hpp>
#include <Core/SceneManagment/SceneObjects/SceneObject.hpp>
using namespace engine3d;

const int Radius = 1;
const int SectorCount = 10;
const int StackCount = 10;

SpriteRender3D::SpriteRender3D(MeshContainer* meshBody) : m_MeshContainer(meshBody){}
SpriteRender3D::SpriteRender3D(const engine3d::Mesh& p_MeshBody, SceneObject* p_sceneObj)
{
m_Mesh = p_MeshBody;
p_sceneObj->SetMesh(m_Mesh);
}

void SpriteRender3D::OnIntegrate()
SpriteRender3D::SpriteRender3D(const std::string& p_model, SceneObject* p_sceneObj)
{

SyncUpdateManager::GetInstance()->Subscribe(
this, &SpriteRender3D::Update);
SyncUpdateManager::GetInstance()->Subscribe
(this, &SpriteRender3D::LateUpdate);
SyncUpdateManager::GetInstance()->Subscribe(
this, &SpriteRender3D::PhysicsUpdate);
m_Mesh = Mesh::LoadModel(p_model);
p_sceneObj->SetMesh(m_Mesh);
}


void SpriteRender3D::OnIntegrate()
{
// Need an activation and start fuction
m_Transform = m_GameObjectRef->SceneGetComponent<Transform>();
m_Transform = m_GameObjectRef->GetComponent<Transform>();
m_TransformPosition = &m_Transform.m_Position;
};
}

void SpriteRender3D::Update()
void SpriteRender3D::RenderObject()
{
//! @note needs to be sent to the vulkanmodal
m_MeshContainer->GetVertices();
};

void SpriteRender3D::LateUpdate() {};

void SpriteRender3D::PhysicsUpdate() {};
//! @note for special renderer/task based rendering
}
16 changes: 10 additions & 6 deletions TestApp/src/Scenes/Assets/Components/Physics/PhysicsBody3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,11 @@ void PhysicsBody3D::OnIntegrate()
void PhysicsBody3D::Begin()
{
m_interface = engine3d::JoltHandler::GetInstance()->getInterface();
m_Transform = &m_GameObjectRef->SceneGetComponent<Transform>();
m_Transform = &m_GameObjectRef->GetComponent<Transform>();
}

void PhysicsBody3D::Update()
{
m_Transform->m_Position.x = m_interface->GetCenterOfMassPosition(bodyType->m_BodyID).GetX();
m_Transform->m_Position.y = m_interface->GetCenterOfMassPosition(bodyType->m_BodyID).GetY();
m_Transform->m_Position.z = m_interface->GetCenterOfMassPosition(bodyType->m_BodyID).GetZ();


//Convert Posiitons
m_Transform->m_Position.x = m_interface->
GetCenterOfMassPosition(bodyType->m_BodyID).GetX();
Expand Down Expand Up @@ -68,11 +63,13 @@ void PhysicsBody3D::Update()

void PhysicsBody3D::SetScale(float x, float y, float z)
{
m_Transform->m_Scale = glm::vec3(x,y,z);
m_interface->GetShape(bodyType->m_BodyID)->ScaleShape(RVec3(x,y,z));
}

void PhysicsBody3D::SetPosition(float x, float y, float z)
{
m_Transform->m_Position = glm::vec3(x,y,z);
m_interface->SetPosition(
bodyType->m_BodyID,
RVec3(x,y,z),
Expand All @@ -85,6 +82,13 @@ void PhysicsBody3D::SetRotation(Quat quaternion)
bodyType->m_BodyID,
quaternion,
JPH::EActivation::Activate);

m_Transform->m_AxisRotation.x = m_interface->GetRotation(
bodyType->m_BodyID).GetEulerAngles().GetX();
m_Transform->m_AxisRotation.y = m_interface->GetRotation(
bodyType->m_BodyID).GetEulerAngles().GetY();
m_Transform->m_AxisRotation.z = m_interface->GetRotation(
bodyType->m_BodyID).GetEulerAngles().GetZ();
}

void PhysicsBody3D::LateUpdate()
Expand Down
2 changes: 1 addition & 1 deletion TestApp/src/Scenes/Assets/Components/testComp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using namespace engine3d;
(this, &testComp::PhysicsUpdate);

// Need an activation and start funciton
m_rb = m_GameObjectRef->SceneGetComponent<PhysicsBody3D>().GetBody();
m_rb = m_GameObjectRef->GetComponent<PhysicsBody3D>().GetBody();

}

Expand Down
Loading

0 comments on commit 524e8a2

Please sign in to comment.