Fixed Stupid Compilation errors.

This commit is contained in:
OusmBlueNinja 2025-01-06 13:52:46 -06:00
parent 24a5a4fbbe
commit 95b15827d6
5 changed files with 58 additions and 19 deletions

View File

@ -32,6 +32,32 @@ void TransformComponent::Update(float _deltaTime)
return; return;
} }
void TransformComponent::LookAt(const glm::vec3& target, const glm::vec3& up) {
// Calculate the direction vector from position to target
glm::vec3 direction = glm::normalize(target - position);
// Handle the case when direction is parallel to up vector
if (glm::length(direction) == 0.0f) {
// Cannot look at the same position; no rotation needed
return;
}
// Create a lookAt matrix
glm::mat4 lookAtMatrix = glm::lookAt(position, target, up);
// Extract the rotation part of the matrix
glm::mat4 rotationMatrix = glm::mat4(glm::mat3(lookAtMatrix));
// Convert the rotation matrix to a quaternion
glm::quat rotationQuat = glm::quat_cast(rotationMatrix);
// Extract Euler angles from the quaternion
glm::vec3 eulerRadians = glm::eulerAngles(rotationQuat);
// Convert radians to degrees and set the rotation
rotation = glm::degrees(eulerRadians);
}
// New Methods // New Methods
glm::mat4 TransformComponent::GetTransformMatrix() const glm::mat4 TransformComponent::GetTransformMatrix() const
{ {

View File

@ -1,10 +1,18 @@
// Transform.h // Transform.h
#pragma once #pragma once
#define GLM_ENABLE_EXPERIMENTAL
#include "Component.h" #include "Component.h"
#include <glm/glm.hpp>
#include <yaml-cpp/yaml.h> #include <yaml-cpp/yaml.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> // For glm::lookAt
#include <glm/gtc/quaternion.hpp> // For glm::quat
#include <glm/gtx/quaternion.hpp> // For glm::quat_cast
#include <glm/gtc/constants.hpp> // For glm::pi
#include <glm/gtc/matrix_access.hpp> // For glm::eulerAngles
class TransformComponent : public Component class TransformComponent : public Component
{ {
public: public:
@ -32,9 +40,13 @@ public:
rotation = {x, y, z}; rotation = {x, y, z};
} }
void LookAt(const glm::vec3& target, const glm::vec3& up) { /**
viewMatrix = glm::lookAt(position, target, up); * @brief Orients the transform to face the target position.
} *
* @param target The target position to look at.
* @param up The up vector to use for orientation.
*/
void LookAt(const glm::vec3& target, const glm::vec3& up);
TransformComponent(); TransformComponent();
virtual const std::string &GetName() const override; virtual const std::string &GetName() const override;
@ -48,7 +60,6 @@ public:
glm::mat4 GetTransformMatrix() const; glm::mat4 GetTransformMatrix() const;
void SetTransformMatrix(const glm::mat4& transform); void SetTransformMatrix(const glm::mat4& transform);
private: private:
static const std::string name; static const std::string name;
}; };

View File

@ -304,7 +304,7 @@ void MyEngine::Run()
{ {
ScopedTimer timer("RenderGame"); ScopedTimer timer("RenderGame");
m_RenderWindow->Show(&m_GameRunning); // The spinning triangle as ImGui::Image m_RenderWindow->Show(&m_GameRunning, m_Ms); // The spinning triangle as ImGui::Image
} }
{ {
ScopedTimer timer("ShowEditor"); ScopedTimer timer("ShowEditor");

View File

@ -331,7 +331,7 @@ void RenderWindow::Show(bool *GameRunning, double deltaTime)
} }
else else
{ {
m_ActiveCamera = m_EditorCamera.GetComponent<CameraComponent>(); m_ActiveCamera = m_EditorCamera->GetComponent<CameraComponent>();
// 1. Mouse Input for Rotation // 1. Mouse Input for Rotation
if (g_InputManager.IsMouseButtonPressed(MouseButton::RIGHT)) if (g_InputManager.IsMouseButtonPressed(MouseButton::RIGHT))
@ -355,20 +355,20 @@ void RenderWindow::Show(bool *GameRunning, double deltaTime)
editorDistance = glm::clamp(editorDistance, minZoom, maxZoom); editorDistance = glm::clamp(editorDistance, minZoom, maxZoom);
// 3. Keyboard Input for Panning (WASD) // 3. Keyboard Input for Panning (WASD)
glm::vec3 forward = glm::normalize(editorTarget - m_EditorCamera.GetComponent<TransformComponent>()->GetPosition()); glm::vec3 forward = glm::normalize(editorTarget - m_EditorCamera->GetComponent<TransformComponent>()->GetPosition());
glm::vec3 right = glm::normalize(glm::cross(forward, glm::vec3(0.0f, 1.0f, 0.0f))); glm::vec3 right = glm::normalize(glm::cross(forward, glm::vec3(0.0f, 1.0f, 0.0f)));
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f); glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
deltaTime = static_cast<float>(deltaTime); deltaTime = static_cast<float>(deltaTime);
if (g_InputManager.IsKeyPressed(KeyCode::W)) if (g_InputManager.IsKeyPressed(KeyCode::W))
editorTarget += up * movementSpeed * deltaTime; editorTarget += up.y * movementSpeed * deltaTime;
if (g_InputManager.IsKeyPressed(KeyCode::S)) if (g_InputManager.IsKeyPressed(KeyCode::S))
editorTarget -= up * movementSpeed * deltaTime; editorTarget -= up.y * movementSpeed * deltaTime;
if (g_InputManager.IsKeyPressed(KeyCode::A)) if (g_InputManager.IsKeyPressed(KeyCode::A))
editorTarget -= right * movementSpeed * deltaTime; editorTarget -= right.x * movementSpeed * deltaTime;
if (g_InputManager.IsKeyPressed(KeyCode::D)) if (g_InputManager.IsKeyPressed(KeyCode::D))
editorTarget += right * movementSpeed * deltaTime; editorTarget += right.x * movementSpeed * deltaTime;
// 4. Calculate the New Camera Position // 4. Calculate the New Camera Position
glm::vec3 direction; glm::vec3 direction;
@ -380,8 +380,8 @@ void RenderWindow::Show(bool *GameRunning, double deltaTime)
glm::vec3 newPosition = editorTarget - direction * editorDistance; glm::vec3 newPosition = editorTarget - direction * editorDistance;
// 5. Update the Editor Camera's Transform // 5. Update the Editor Camera's Transform
auto editorTransform = m_EditorCamera.GetComponent<TransformComponent>(); auto editorTransform = m_EditorCamera->GetComponent<TransformComponent>();
editorTransform->SetPosition(newPosition); editorTransform->SetPosition(newPosition.x,newPosition.y,newPosition.z);
editorTransform->LookAt(editorTarget, up); editorTransform->LookAt(editorTarget, up);
// 6. Retrieve Updated Matrices // 6. Retrieve Updated Matrices
@ -458,9 +458,11 @@ void RenderWindow::InitGLResources()
// throw this in here cus we dont have a constructor // throw this in here cus we dont have a constructor
m_ActiveCamera = nullptr; m_ActiveCamera = nullptr;
m_EditorCamera = std::make_shared<GameObject>(-1, "EditorCamera");
// Setup the editor camera // Setup the editor camera
m_EditorCamera.AddComponent(std::make_shared<CameraComponent>()); m_EditorCamera->AddComponent(std::make_shared<CameraComponent>());
m_EditorCamera.AddComponent(std::make_shared<TransformComponent>()); m_EditorCamera->AddComponent(std::make_shared<TransformComponent>());
{ {
std::shared_ptr<Shader> shaderAsset = g_AssetManager.loadAsset<Shader>(AssetType::SHADER, "assets/shaders/UnlitMaterial"); std::shared_ptr<Shader> shaderAsset = g_AssetManager.loadAsset<Shader>(AssetType::SHADER, "assets/shaders/UnlitMaterial");

View File

@ -42,7 +42,7 @@ private:
Shader* m_ShaderPtr = nullptr; Shader* m_ShaderPtr = nullptr;
Shader* m_LineShaderPtr = nullptr; Shader* m_LineShaderPtr = nullptr;
GameObject m_EditorCamera; std::shared_ptr<GameObject> m_EditorCamera;
}; };