Added Base Components
This commit is contained in:
parent
73e39559cb
commit
81ec55a229
62
Engine/Components/Camera.cpp
Normal file
62
Engine/Components/Camera.cpp
Normal file
@ -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;
|
||||||
|
}
|
47
Engine/Components/Camera.h
Normal file
47
Engine/Components/Camera.h
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#ifndef CAMERA_H
|
||||||
|
#define CAMERA_H
|
||||||
|
|
||||||
|
#include "Component.h"
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
|
// 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
|
12
Engine/Components/Component.h
Normal file
12
Engine/Components/Component.h
Normal file
@ -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
|
@ -11,6 +11,10 @@
|
|||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
|
#include "Components/Component.h"
|
||||||
|
#include "Components/Camera.h"
|
||||||
|
|
||||||
|
|
||||||
class Engine {
|
class Engine {
|
||||||
public:
|
public:
|
||||||
// Main window.
|
// Main window.
|
||||||
|
@ -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));
|
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
|
||||||
glUniform3f(glGetUniformLocation(shaderProgram, "viewPos"), viewPos.x, viewPos.y, viewPos.z);
|
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));
|
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));
|
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(model));
|
||||||
|
|
||||||
|
2
Makefile
2
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
|
LDFLAGS := -Llib -lglfw3 -lopengl32 -lglew32 -lglu32
|
||||||
|
|
||||||
# Source and build directories (including vendor folder)
|
# 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
|
BUILD_DIR := build
|
||||||
|
|
||||||
# Find all source files
|
# Find all source files
|
||||||
|
BIN
Three-Labs.exe
BIN
Three-Labs.exe
Binary file not shown.
BIN
build/Engine/Components/Camera.o
Normal file
BIN
build/Engine/Components/Camera.o
Normal file
Binary file not shown.
Binary file not shown.
12
imgui.ini
12
imgui.ini
@ -10,13 +10,13 @@ Collapsed=0
|
|||||||
|
|
||||||
[Window][Editor Panel]
|
[Window][Editor Panel]
|
||||||
Pos=0,0
|
Pos=0,0
|
||||||
Size=334,800
|
Size=333,800
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
DockId=0x00000001,0
|
DockId=0x00000001,0
|
||||||
|
|
||||||
[Window][Rendered Output]
|
[Window][Rendered Output]
|
||||||
Pos=336,0
|
Pos=335,0
|
||||||
Size=944,800
|
Size=945,800
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
DockId=0x00000002,0
|
DockId=0x00000002,0
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ Size=680,444
|
|||||||
Collapsed=0
|
Collapsed=0
|
||||||
|
|
||||||
[Docking][Data]
|
[Docking][Data]
|
||||||
DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,0 Size=1280,800 Split=X Selected=0x5098C5B2
|
DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,0 Size=1280,800 Split=X Selected=0xB6999AB4
|
||||||
DockNode ID=0x00000001 Parent=0x08BD597D SizeRef=334,800 Selected=0x5098C5B2
|
DockNode ID=0x00000001 Parent=0x08BD597D SizeRef=333,800 Selected=0x5098C5B2
|
||||||
DockNode ID=0x00000002 Parent=0x08BD597D SizeRef=944,800 CentralNode=1 Selected=0xB6999AB4
|
DockNode ID=0x00000002 Parent=0x08BD597D SizeRef=945,800 CentralNode=1 Selected=0xB6999AB4
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user