diff --git a/Engine/Components/Camera.cpp b/Engine/Components/Camera.cpp new file mode 100644 index 0000000..a1672ba --- /dev/null +++ b/Engine/Components/Camera.cpp @@ -0,0 +1,62 @@ +#include "Camera.h" + +Camera::Camera() + : position(0.0f, 0.0f, 5.0f), + yaw(-90.0f), + pitch(0.0f), + fov(45.0f), + aspect(4.0f / 3.0f), + nearPlane(0.1f), + farPlane(100.0f) +{ +} + +Camera::~Camera() { +} + +void Camera::Update(float deltaTime) { + // For now, we do not modify the camera over time. + // Input or smoothing could be handled here. +} + +void Camera::SetPosition(const glm::vec3& pos) { + position = pos; +} + +void Camera::SetRotation(float newYaw, float newPitch) { + yaw = newYaw; + pitch = newPitch; +} + +glm::mat4 Camera::GetViewMatrix() const { + // Compute the forward vector from yaw and pitch. + glm::vec3 front; + front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); + front.y = sin(glm::radians(pitch)); + front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch)); + front = glm::normalize(front); + return glm::lookAt(position, position + front, glm::vec3(0, 1, 0)); +} + +glm::mat4 Camera::GetProjectionMatrix() const { + return glm::perspective(glm::radians(fov), aspect, nearPlane, farPlane); +} + +void Camera::SetPerspective(float fov, float aspect, float nearPlane, float farPlane) { + this->fov = fov; + this->aspect = aspect; + this->nearPlane = nearPlane; + this->farPlane = farPlane; +} + +glm::vec3 Camera::GetPosition() const { + return position; +} + +float Camera::GetYaw() const { + return yaw; +} + +float Camera::GetPitch() const { + return pitch; +} diff --git a/Engine/Components/Camera.h b/Engine/Components/Camera.h new file mode 100644 index 0000000..251fbff --- /dev/null +++ b/Engine/Components/Camera.h @@ -0,0 +1,47 @@ +#ifndef CAMERA_H +#define CAMERA_H + +#include "Component.h" +#include +#include + +// A simple Camera component that inherits from Component. +class Camera : public Component { +public: + Camera(); + virtual ~Camera(); + + // Override update (if needed, e.g. for smoothing or input handling). + virtual void Update(float deltaTime) override; + + // Set the camera's position. + void SetPosition(const glm::vec3& pos); + // Set the camera's rotation in degrees. + void SetRotation(float yaw, float pitch); + + // Get the view matrix calculated from position and rotation. + glm::mat4 GetViewMatrix() const; + // Get the projection matrix. + glm::mat4 GetProjectionMatrix() const; + + // Set perspective projection parameters. + void SetPerspective(float fov, float aspect, float nearPlane, float farPlane); + + // Getters. + glm::vec3 GetPosition() const; + float GetYaw() const; + float GetPitch() const; + +private: + glm::vec3 position; + float yaw; + float pitch; + + // Perspective projection parameters. + float fov; + float aspect; + float nearPlane; + float farPlane; +}; + +#endif // CAMERA_H diff --git a/Engine/Components/Component.h b/Engine/Components/Component.h new file mode 100644 index 0000000..6077fcf --- /dev/null +++ b/Engine/Components/Component.h @@ -0,0 +1,12 @@ +#ifndef COMPONENT_H +#define COMPONENT_H + +// Base Component class for all components. +class Component { +public: + virtual ~Component() {} + // Update the component each frame. + virtual void Update(float deltaTime) = 0; +}; + +#endif // COMPONENT_H diff --git a/Engine/Components/Componet.h b/Engine/Components/Componet.h deleted file mode 100644 index e69de29..0000000 diff --git a/Engine/Engine.h b/Engine/Engine.h index 2f73d93..9de0b24 100644 --- a/Engine/Engine.h +++ b/Engine/Engine.h @@ -11,6 +11,10 @@ #include #include +#include "Components/Component.h" +#include "Components/Camera.h" + + class Engine { public: // Main window. diff --git a/Engine/engine.cpp b/Engine/engine.cpp index d1ad752..c9142cf 100644 --- a/Engine/engine.cpp +++ b/Engine/engine.cpp @@ -289,7 +289,7 @@ ImTextureID Engine::RenderScene(const glm::mat4 &view, const glm::mat4 &projecti glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); glUniform3f(glGetUniformLocation(shaderProgram, "viewPos"), viewPos.x, viewPos.y, viewPos.z); - rotationAngle += 0.01f; + rotationAngle += 0.001f; glm::mat4 model = glm::rotate(glm::mat4(1.0f), rotationAngle, glm::vec3(1,1,0)); glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(model)); diff --git a/Makefile b/Makefile index e552ad7..432634f 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ CXXFLAGS := -std=c++20 -Wall -Wextra -O2 -I/c/msys64/mingw64/include -Ivendor/im LDFLAGS := -Llib -lglfw3 -lopengl32 -lglew32 -lglu32 # Source and build directories (including vendor folder) -SRC_DIRS := . Editor Engine vendor/imgui-docking +SRC_DIRS := . Editor Engine vendor/imgui-docking Engine/Components BUILD_DIR := build # Find all source files diff --git a/Three-Labs.exe b/Three-Labs.exe index 6a2f19a..ca11185 100644 Binary files a/Three-Labs.exe and b/Three-Labs.exe differ diff --git a/build/Engine/Components/Camera.o b/build/Engine/Components/Camera.o new file mode 100644 index 0000000..0d1d5b3 Binary files /dev/null and b/build/Engine/Components/Camera.o differ diff --git a/build/Engine/engine.o b/build/Engine/engine.o index e905e5d..cd0cfc7 100644 Binary files a/build/Engine/engine.o and b/build/Engine/engine.o differ diff --git a/imgui.ini b/imgui.ini index f17b88a..07f66b7 100644 --- a/imgui.ini +++ b/imgui.ini @@ -10,13 +10,13 @@ Collapsed=0 [Window][Editor Panel] Pos=0,0 -Size=334,800 +Size=333,800 Collapsed=0 DockId=0x00000001,0 [Window][Rendered Output] -Pos=336,0 -Size=944,800 +Pos=335,0 +Size=945,800 Collapsed=0 DockId=0x00000002,0 @@ -26,7 +26,7 @@ Size=680,444 Collapsed=0 [Docking][Data] -DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,0 Size=1280,800 Split=X Selected=0x5098C5B2 - DockNode ID=0x00000001 Parent=0x08BD597D SizeRef=334,800 Selected=0x5098C5B2 - DockNode ID=0x00000002 Parent=0x08BD597D SizeRef=944,800 CentralNode=1 Selected=0xB6999AB4 +DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,0 Size=1280,800 Split=X Selected=0xB6999AB4 + DockNode ID=0x00000001 Parent=0x08BD597D SizeRef=333,800 Selected=0x5098C5B2 + DockNode ID=0x00000002 Parent=0x08BD597D SizeRef=945,800 CentralNode=1 Selected=0xB6999AB4