Skip to content

Commit

Permalink
Camera component works!!
Browse files Browse the repository at this point in the history
  • Loading branch information
SpinnerX committed Nov 23, 2024
1 parent ae1c6ab commit 1b71378
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 19 deletions.
7 changes: 3 additions & 4 deletions Editor/Editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ namespace engine3d{

void EditorApplication::OnApplicationUpdate(){
//! @note Handle Events.
m_EditorScene->OnUpdate();
// m_EditorScene->OnMoveCamUpdate();
auto& objects = m_EditorScene->GetSceneObjects();
Renderer::RecordSceneGameObjects(objects);
// m_EditorScene->OnUpdate();
m_EditorScene->OnMoveCamUpdate();
Renderer::RecordSceneGameObjects(m_EditorScene->GetSceneObjects(), m_EditorScene->GetCameraObject());
}

ApplicationInstance* InitializeApplication(){
Expand Down
142 changes: 133 additions & 9 deletions Editor/Editor/EditorScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,41 @@ namespace engine3d{
//! @note Make this scene object as part of our current scene.
// SceneObject* cube = new SceneObject(m_Scene);
SceneObject* cube = new SceneObject(m_Scene);
m_CameraObject = new SceneObject(m_Scene);

m_CameraObject->AddComponent<EditorCamera>();

// auto cube = SceneObject::Create();
// cube.SetModel(m_CubeMesh);
// cube.GetTransform().Translation = {.0f, .0f, .5f};
// cube.GetTransform().scale = {.5f, .5f, .5f};
auto& camera_transform = m_CameraObject->SceneGetComponent<Transform>();
camera_transform.m_Position = {-1.f, -2.f, -20.f};

auto camera = m_CameraObject->SceneGetComponent<EditorCamera>();

//! @note Setting our properties
auto& transform = cube->SceneGetComponent<Transform>();

cube->AddComponent<EditorCamera>();
// cube->AddComponent<EditorCamera>();

auto& camera = cube->SceneGetComponent<EditorCamera>();
// auto& camera = cube->SceneGetComponent<EditorCamera>();
// camera.SetInitialProperties(30.0f, 0.1f, 1000.0f);
// camera.SetView(glm::vec3(0.f), glm::vec3(0.5f, 0.f, 1.f));
auto aspect_ratio = ApplicationInstance::GetWindow().GetAspectRatio();
// camera.SetOrthoProjection(-1, 1, -1, 1, -1, 1);

//! @note Shows the actual cube in center of screen that works
//! @note Because z = -20.f your gonna want to increase the near clip by 100.0f
camera.SetViewTarget({-1.f, -2.f, -20.f}, {0.f, 0.f, 2.5f});
// camera.SetViewTarget({-1.f, -2.f, -20.f}, {0.f, 0.f, 2.5f});
// camera.SetViewTarget(glm::vec3(-1.f, -2.f, -20.f), glm::vec3(0.f, 0.f, 2.5f));

// negative-float closer, positive-float z further
// transform.m_Position = {.0f, .0f, .75f};
transform.m_Position = {.0f, .0f, 2.5f};
// transform.m_Position = {.0f, .0f, 2.5f};
transform.m_Position = {.0f, .0f, 2.5};
// transform.m_Position.z = transform.m_Position.z - 2.5f;
// camera.SetViewXYZ(transform.m_Position, transform.m_AxisRotation);
// transform.m_Scale = {.5f, .5f, 0.5};
transform.m_Scale = {.5f, .5f, 0.5};
// cube->SetModel(cube_mesh);
Expand All @@ -115,14 +127,126 @@ namespace engine3d{
// glm::vec3 m_MoveDirection{0.f};
// glm::vec3 m_Rotation{0};

// for(const auto& obj : m_SceneObjects){
// // auto& transform_compoent = obj->SceneGetComponent<Transform>();
// auto& camera_component = obj->SceneGetComponent<EditorCamera>();
// camera_component.SetPerspectiveProjection(glm::radians(50.f), ApplicationInstance::GetWindow().GetAspectRatio(), 0.1f, 50.f);
// }
}

void EditorScene::OnMoveCamUpdate(){
auto& transform = m_CameraObject->SceneGetComponent<Transform>();
auto& camera = m_CameraObject->SceneGetComponent<EditorCamera>();
auto cube_transform = m_SceneObjects[0]->SceneGetComponent<Transform>();
// float tempDt_Y;
glm::vec2 temp_position = {0.f, 0.f};
constexpr float sensitivity = 5.0f;
constexpr glm::vec2 invert_pos = {-1, 1};
// ConsoleLogInfo("x = {}, y = {}, z = {}", transform.m_Position.x, transform.m_Position.y, transform.m_Position.z);
// ConsoleLogInfo("x = {}, y = {}, z = {}\n", cube_transform.m_Position.x, cube_transform.m_Position.y, cube_transform.m_Position.z);

/*
move-right = D
move-left = A
move-forward = W
move-backward = S
move-up = E
move-down = Q
look-left = LEFT
look-right = RIGHT
look-up = UP
look-down = DOWN
*/

/*
for(const auto& obj : m_SceneObjects){
// auto& transform_compoent = obj->SceneGetComponent<Transform>();
auto& camera_component = obj->SceneGetComponent<EditorCamera>();
camera_component.SetPerspectiveProjection(glm::radians(50.f), ApplicationInstance::GetWindow().GetAspectRatio(), 0.1f, 100.f);
glm::vec3 rotate{0};
if(InputPoll::IsKeyPressed(ENGINE_KEY_LEFT)) rotate.y += 1.f;
if(InputPoll::IsKeyPressed(ENGINE_KEY_RIGHT)) rotate.y -= 1.f;
if(InputPoll::IsKeyPressed(ENGINE_KEY_UP)) rotate.x += 1.f;
if(InputPoll::IsKeyPressed(ENGINE_KEY_DOWN)) rotate.x -= 1.f;
auto& transform = obj->SceneGetComponent<Transform>();
auto& camera = obj->SceneGetComponent<EditorCamera>();
if(glm::dot(rotate, rotate) > std::numeric_limits<float>::epsilon()){
transform.m_AxisRotation += m_LookSpeed * SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime * glm::normalize(rotate);
}
transform.m_AxisRotation.x = glm::clamp(transform.m_AxisRotation.x, -1.5f, 1.5f);
transform.m_AxisRotation.y = glm::mod(transform.m_AxisRotation.y, glm::two_pi<float>());
float yaw = transform.m_AxisRotation.y;
const glm::vec3 forward_dir{sin(yaw), 0.f, cos(yaw)};
const glm::vec3 right_dir{forward_dir.z, 0.f, -forward_dir.y};
const glm::vec3 up_dir{0.f, -1.f, 0.f};
glm::vec3 move_dir{0.f};
if(InputPoll::IsKeyPressed(ENGINE_KEY_W)) move_dir += forward_dir; // FORWARD
if(InputPoll::IsKeyPressed(ENGINE_KEY_S)) move_dir -= forward_dir; // BACKWARD
if(InputPoll::IsKeyPressed(ENGINE_KEY_D)) move_dir += right_dir; // RIGHT
if(InputPoll::IsKeyPressed(ENGINE_KEY_A)) move_dir -= right_dir; // LEFT
if(InputPoll::IsKeyPressed(ENGINE_KEY_E)) move_dir += up_dir; // UP
if(InputPoll::IsKeyPressed(ENGINE_KEY_Q)) move_dir -= up_dir; // DOWN
if(glm::dot(move_dir, move_dir) > std::numeric_limits<float>::epsilon()){
transform.m_Position += m_MoveSpeed * (SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime) * glm::normalize(move_dir);
}
// camera.SetViewTarget({-1.f, -2.f, -20.f}, transform.m_Position);
// camera.SetViewTarget({0.f, 0.f, 0.f}, transform.m_Position);
camera.SetViewXYZ(transform.m_Position, transform.m_AxisRotation);
}
}
*/

glm::vec3 rotate{0};

if(InputPoll::IsMousePressed(Mouse::ButtonRight)){
// temp_position.x = m_MousePosition.x - InputPoll::GetMouseX();
rotate.y += (m_MousePosition.x - InputPoll::GetMouseX()) * invert_pos.y;
rotate.x += (m_MousePosition.y - InputPoll::GetMouseY()) * invert_pos.x;
}
// if(InputPoll::IsKeyPressed(ENGINE_KEY_LEFT)) rotate.y += 1.f;
// if(InputPoll::IsKeyPressed(ENGINE_KEY_RIGHT)) rotate.y -= 1.f;
// if(InputPoll::IsKeyPressed(ENGINE_KEY_UP)) rotate.x += 1.f;
// if(InputPoll::IsKeyPressed(ENGINE_KEY_DOWN)) rotate.x -= 1.f;

m_MousePosition = InputPoll::GetMousePosition();

if(glm::dot(rotate, rotate) > std::numeric_limits<float>::epsilon()){
transform.m_AxisRotation += m_LookSpeed * SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime * glm::normalize(rotate) * sensitivity;
}

transform.m_AxisRotation.x = glm::clamp(transform.m_AxisRotation.x, -1.5f, 1.5f);
transform.m_AxisRotation.y = glm::mod(transform.m_AxisRotation.y, glm::two_pi<float>());

float yaw = transform.m_AxisRotation.y;
const glm::vec3 forward_dir{sin(yaw), 0.f, cos(yaw)};
const glm::vec3 right_dir{forward_dir.z, 0.f, -forward_dir.y};
const glm::vec3 up_dir{0.f, -1.f, 0.f};

void EditorScene::OnMoveCamUpdate(){}
glm::vec3 move_dir{0.f};

if(InputPoll::IsKeyPressed(ENGINE_KEY_W)) move_dir += forward_dir; // FORWARD
if(InputPoll::IsKeyPressed(ENGINE_KEY_S)) move_dir -= forward_dir; // BACKWARD
if(InputPoll::IsKeyPressed(ENGINE_KEY_D)) move_dir += right_dir; // RIGHT
if(InputPoll::IsKeyPressed(ENGINE_KEY_A)) move_dir -= right_dir; // LEFT
if(InputPoll::IsKeyPressed(ENGINE_KEY_E)) move_dir += up_dir; // UP
if(InputPoll::IsKeyPressed(ENGINE_KEY_Q)) move_dir -= up_dir; // DOWN

if(glm::dot(move_dir, move_dir) > std::numeric_limits<float>::epsilon()){
transform.m_Position += m_MoveSpeed * (SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime) * glm::normalize(move_dir);
}

camera.SetViewXYZ(transform.m_Position, transform.m_AxisRotation);


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

}

// void EditorScene::OnCameraUpdate(){
// for(const auto& obj : m_SceneObjects){
Expand Down
4 changes: 4 additions & 0 deletions Editor/Editor/EditorScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ namespace engine3d{

std::vector<SceneObject*>& GetSceneObjects() { return m_SceneObjects; }

SceneObject* GetCameraObject() const { return m_CameraObject; }

private:
std::vector<SceneObject*> m_SceneObjects;
Scene* m_Scene;
SceneObject* m_CameraObject;
glm::vec2 m_MousePosition;
float m_MoveSpeed = {3.f};
float m_LookSpeed = {1.5f};
};
Expand Down
2 changes: 1 addition & 1 deletion engine3d/Core/Renderer/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace engine3d{
//! @note In the future I'll add API's for submitting draw calls.
// static void RecordCommandBuffers(VkCommandBuffer p_CommandBuffer);
static void RecordGameObjects(std::vector<SceneObjectTutorial>& p_Objects);
static void RecordSceneGameObjects(std::vector<SceneObject*>& p_SceneObjects);
static void RecordSceneGameObjects(std::vector<SceneObject*>& p_SceneObjects, SceneObject* p_CameraObject);
static void EndFrame();

template<typename UFunction>
Expand Down
8 changes: 5 additions & 3 deletions sim_shader_transforms/simple_shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ layout(location = 0) out vec3 fragColor;

layout(push_constant) uniform Push {
mat4 transform;
mat4 Projection;
vec2 iResolution;
vec3 color;
} push;
Expand All @@ -18,11 +19,12 @@ void main(){
// p[0] *= (push.iResolution.y / push.iResolution.x);
// p[1] *= (push.iResolution.y / push.iResolution.x);
// gl_Position = vec4(p * Position + push.offset, 0.0, 1.0);
mat4 p = push.transform;
p[0] /= (push.iResolution.x / push.iResolution.y);
// 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);
gl_Position = p * vec4(Position, 1.0);
// gl_Position = p * vec4(Position, 1.0);
gl_Position = push.transform * vec4(Position, 1.0);
fragColor = Color;
}

Expand Down
Binary file modified sim_shader_transforms/simple_shader.vert.spv
Binary file not shown.
6 changes: 4 additions & 2 deletions src/engine3d/Core/Renderer/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ namespace engine3d{
}
}

void Renderer::RecordSceneGameObjects(std::vector<SceneObject*>& p_Objects){
void Renderer::RecordSceneGameObjects(std::vector<SceneObject*>& p_Objects, SceneObject* p_CameraObject){
auto current_cmd_buffer = GetCurrentCommandBuffer();

//! @note Essentially doing m_Pipeline->Bind(m_CommandBuffer[i])
Expand All @@ -201,9 +201,11 @@ namespace engine3d{
// ConsoleLogWarn("Delta Time = {:.7}", delta_time);
// auto projection_view =

auto camera_component = p_CameraObject->SceneGetComponent<EditorCamera>();

//! @note Only for testing purposes for mesh data.
for(auto& obj : p_Objects){
auto camera_component = obj->SceneGetComponent<EditorCamera>();
// auto camera_component = obj->SceneGetComponent<EditorCamera>();
auto proj_view = camera_component.GetProjection() * camera_component.GetView();

// obj.m_Transform2D.rotation.y = glm::mod(obj.GetTransform().rotation.y + 0.001f, glm::two_pi<float>());
Expand Down

0 comments on commit 1b71378

Please sign in to comment.