Skip to content

Commit

Permalink
Merge pull request #2 from FraserConnolly/Componentisation
Browse files Browse the repository at this point in the history
Componentisation
  • Loading branch information
FraserConnolly authored May 6, 2024
2 parents c2a4526 + a12a8b8 commit 6d0eb0e
Show file tree
Hide file tree
Showing 43 changed files with 1,957 additions and 490 deletions.
20 changes: 10 additions & 10 deletions Lab1/Camera.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include "Camera.h"

Camera::Camera ( )
: _mode ( CameraMode::PERSPECTIVE )
#include "GameObject.h"
#include "Transform.h"

Camera::Camera ( GameObject & hostObject )
: Component( ComponentTypes::CAMERA, hostObject ),
_mode ( CameraMode::PERSPECTIVE )
{ }

Camera::~Camera ( )
Expand All @@ -10,25 +14,21 @@ Camera::~Camera ( )
void Camera::SetCameraTarget ( const glm::vec3 & target )
{
_cameraTarget = target;
_cameraDirection = glm::normalize ( _transform.GetPosition ( ) - _cameraTarget );
_cameraDirection = glm::normalize ( m_gameObject.GetTransform( ).GetPosition ( ) - _cameraTarget );
glm::vec3 up = glm::vec3 ( 0.0f, 1.0f, 0.0f );
glm::vec3 right = glm::normalize ( glm::cross ( up, _cameraDirection ) );
_cameraUp = glm::cross ( _cameraDirection, right );
}



Transform & Camera::GetTransform ( )
{
return _transform;
}

glm::mat4 Camera::GetViewMatrix ( )
{
// in lab 4 Bryan multiples the View Matrix with the Projection matrix here instead of passing the projection matrix seperatly as a unform.
// in lab 4 Bryan multiples the View Matrix with the Projection matrix here instead of passing the projection matrix separately as a uniform.
// Bryan also multiplies the combined View Project matrix with the Model matrix in the application before sending the MVP (Model View Matrix)
// as a whole to the vertex shader.
// I have opeted to pass the three matrixes to the vertex shader seperatly so that the GPU can do this calculation.
// I have opted to pass the three matrices to the vertex shader separately so that the GPU can do this calculation.


// these lines of code will result in a camera that is always looking at the target position in world space.
Expand All @@ -37,7 +37,7 @@ glm::mat4 Camera::GetViewMatrix ( )


// this line will result in a matrix where the camera will always look forward rather than at a defined target position in world space.
return glm::lookAt ( _transform.GetPosition ( ), _transform.GetPosition ( ) + _cameraForward, _cameraUp );
return glm::lookAt ( m_gameObject.GetTransform( ).GetPosition ( ), m_gameObject.GetTransform ( ).GetPosition ( ) + _cameraForward, _cameraUp );

}

Expand Down
26 changes: 20 additions & 6 deletions Lab1/Camera.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once

#include "Component.h"

#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>

#include "Transform.h"
class Transform;

class Camera
class Camera : public Component
{
public:
enum CameraMode
Expand All @@ -14,9 +16,15 @@ class Camera
ORTHOGRAPHIC
};

Camera ( );

friend class GameObject;

private:
Camera ( GameObject & hostObject );
~Camera ( );

public:

#pragma region Getters and Setters


Expand All @@ -26,6 +34,15 @@ class Camera
_projectionMatrixIsDirty = true;
}

/// <summary>
/// Gets the camera's field of view
/// </summary>
/// <returns>In degrees.</returns>
const float GetFoV ( )
{
return glm::degrees( _fov );
}

/// <summary>
/// Set the camera's field of view.
/// </summary>
Expand Down Expand Up @@ -98,8 +115,6 @@ class Camera

#pragma endregion

Transform & GetTransform ( );

glm::mat4 GetViewMatrix ( );
glm::mat4 GetProjectionMatrix ( ) ;

Expand All @@ -112,7 +127,6 @@ class Camera
glm::vec2 _clippingPlanes = glm::vec2 ( 0.1f, 100.0f );
glm::vec4 _orthoRectangle = glm::vec4 ( -10.0f, 10.0f, -10.0f, 10.0f );

Transform _transform;
glm::vec3 _cameraTarget;
glm::vec3 _cameraDirection;
glm::vec3 _cameraForward = glm::vec3 ( 0.0f, 0.0f, -1.0f );
Expand Down
115 changes: 92 additions & 23 deletions Lab1/CameraFlyController.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#pragma once
#include <GL\glew.h>

#include "GameObject.h"
#include "Component.h"
#include "Camera.h"
#include "Transform.h"
#include "Input.h"
#include "Time.h"

#include <SDL/SDL_keycode.h>

// Reference: https://learnopengl.com/code_viewer_gh.php?code=includes/learnopengl/camera.h

Expand All @@ -24,15 +31,17 @@ const float SPEED = 2.5f;
const float SENSITIVITY = 0.1f;
const float ZOOM = 45.0f;

class CameraFlyController
class CameraFlyController : public Component
{

public:
friend class GameObject;

private:

CameraFlyController ( )
{
_camera = nullptr;
CameraFlyController ( GameObject & hostObject )
: Component ( ComponentTypes::CAMERA_FLY_CONTROLLER, hostObject )
{
m_camera = nullptr;
WorldUp = glm::vec3 ( 0.0f, 1.0f, 0.0f );
Yaw = YAW;
Pitch = PITCH;
Expand All @@ -41,22 +50,30 @@ class CameraFlyController
updateCameraVectors ( );
}

void UpdateCamera ( )
{
_camera->SetCameraForward ( Front );
_camera->SetCameraUp ( Up );
_camera->SetFoV ( Zoom );
public:

void Awake ( ) override
{
// Technically these only need to be called on the first script.
// But there currently isn't away of doing that.
Input::RegisterKey ( SDLK_a ); // left
Input::RegisterKey ( SDLK_d ); // right
Input::RegisterKey ( SDLK_w ); // forward
Input::RegisterKey ( SDLK_s ); // back
Input::RegisterKey ( SDLK_q ); // down
Input::RegisterKey ( SDLK_e ); // up
}

void SetCamera ( Camera & camera )
{
_camera = &camera;
m_camera = &camera;
Zoom = camera.GetFoV ( );
}

// processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems)
void ProcessKeyboard ( Camera_Movement direction, float deltaTime )
{
glm::vec3 Position = _camera->GetTransform ( ).GetPosition ( );
glm::vec3 Position = m_camera->GetGameObject( ).GetTransform ( ).GetPosition ( );

float velocity = MovementSpeed * deltaTime;

Expand All @@ -73,7 +90,7 @@ class CameraFlyController
if ( direction == DOWN )
Position -= Up * velocity;

_camera->GetTransform ( ).SetPosition ( Position );
m_camera->GetGameObject ( ).GetTransform ( ).SetPosition ( Position );
}

// processes input received from a mouse input system. Expects the offset value in both the x and y direction.
Expand Down Expand Up @@ -101,7 +118,7 @@ class CameraFlyController
}

// processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis
void ProcessMouseScroll ( float yoffset )
void ProcessMouseScroll ( int yoffset )
{
Zoom -= ( float ) yoffset;
if ( Zoom < 1.0f )
Expand All @@ -110,22 +127,68 @@ class CameraFlyController
Zoom = 45.0f;
}

void Update ( ) override
{
Component::Update ( );

#pragma region Camera controls

float _deltaTime = Time::GetDeltaTime ( );

if ( Input::IsKeyPressed ( SDLK_a ) )
{
ProcessKeyboard ( Camera_Movement::LEFT, _deltaTime );
}
if ( Input::IsKeyPressed ( SDLK_d ) )
{
ProcessKeyboard ( Camera_Movement::RIGHT, _deltaTime );
}
if ( Input::IsKeyPressed ( SDLK_w ) )
{
ProcessKeyboard ( Camera_Movement::FORWARD, _deltaTime );
}
if ( Input::IsKeyPressed ( SDLK_s ) )
{
ProcessKeyboard ( Camera_Movement::BACKWARD, _deltaTime );
}
if ( Input::IsKeyPressed ( SDLK_e ) )
{
ProcessKeyboard ( Camera_Movement::UP, _deltaTime );
}
if ( Input::IsKeyPressed ( SDLK_q ) )
{
ProcessKeyboard ( Camera_Movement::DOWN, _deltaTime );
}

auto & mouseDelta = Input::GetMouseDelta ( );
ProcessMouseMovement ( (float) mouseDelta.x, (float) mouseDelta.y, true );

auto & wheelDelta = Input::GetMouseWheelDelta ( );
ProcessMouseScroll ( wheelDelta.y );

UpdateCamera ( );

#pragma endregion

}

private:

// euler Angles
float Yaw;
float Pitch;
// euler Angles
float Yaw;
float Pitch;

// camera options
float MovementSpeed;
float MouseSensitivity;
float Zoom;
// camera options
float MovementSpeed;
float MouseSensitivity;
float Zoom;

glm::vec3 Front;
glm::vec3 Up;
glm::vec3 Right;
glm::vec3 WorldUp;

private:
Camera * m_camera;

// calculates the front vector from the Camera's (updated) Euler Angles
void updateCameraVectors ( )
Expand All @@ -141,7 +204,13 @@ class CameraFlyController
Up = glm::normalize ( glm::cross ( Right, Front ) );
}

Camera * _camera;
void UpdateCamera ( )
{
m_camera->SetCameraForward ( Front );
m_camera->SetCameraUp ( Up );
m_camera->SetFoV ( Zoom );
}


};

63 changes: 63 additions & 0 deletions Lab1/Component.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "Component.h"
#include "GameObject.h"

Component::Component ( const ComponentTypes type, GameObject & hostObject ) :
m_isEnabled ( true ), m_componentType ( type ), m_gameObject ( hostObject )
{
}

Component::Component ( const ComponentTypes type, GameObject & hostObject, const bool enable ) :
m_isEnabled ( enable ), m_componentType ( type ), m_gameObject ( hostObject )
{
}

GameObject & Component::GetGameObject ( ) const
{
return m_gameObject;
}

bool Component::IsDead ( ) const
{
return m_isDead;
}

void Component::Kill ( )
{
SetActive ( false );
m_isDead = true;
}

bool Component::IsEnabled ( ) const
{
return m_isEnabled;
}

bool Component::IsActiveAndEnabled ( ) const
{
if ( ! m_isEnabled )
{
return false;
}

return m_gameObject.IsActiveInHierarchy ( );
}

void Component::SetActive ( const bool enable )
{
if ( m_isEnabled == enable )
{
return;
}

m_isEnabled = enable;

if ( enable )
{
OnEnable();
}
else
{
OnDisable ( );
}

}
Loading

0 comments on commit 6d0eb0e

Please sign in to comment.