2020-09-30 13:48:41 +02:00
|
|
|
#include "CameraComponent.h"
|
2021-01-19 15:30:00 +01:00
|
|
|
#include "Scene/Actor/Actor.h"
|
2020-09-30 13:48:41 +02:00
|
|
|
#include <algorithm>
|
2021-01-19 15:30:00 +01:00
|
|
|
#include <iostream>
|
2020-09-30 13:48:41 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
2022-01-12 14:40:26 +01:00
|
|
|
CameraComponent::CameraComponent()
|
2022-03-19 22:45:30 +01:00
|
|
|
: aspectRatio(0)
|
|
|
|
|
, fieldOfView(0)
|
2022-01-12 14:40:26 +01:00
|
|
|
, bNeedsViewBuild(true)
|
2021-10-15 23:12:29 +02:00
|
|
|
, bNeedsProjectionBuild(true)
|
2022-03-19 22:45:30 +01:00
|
|
|
, projectionMatrix(Matrix4())
|
|
|
|
|
, viewMatrix(Matrix4())
|
2020-09-30 13:48:41 +02:00
|
|
|
{
|
|
|
|
|
rotationX = 0;
|
2022-03-19 22:45:30 +01:00
|
|
|
rotationY = 0;
|
|
|
|
|
setRelativeLocation(Vector(0, 10, -50));
|
2020-09-30 13:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CameraComponent::~CameraComponent()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CameraComponent::mouseMove(float deltaX, float deltaY)
|
|
|
|
|
{
|
2022-03-19 22:45:30 +01:00
|
|
|
rotationX -= deltaX / 1000.f;
|
|
|
|
|
rotationY += deltaY / 1000.f;
|
|
|
|
|
//std::cout << "X:" << rotationX << " Y: " << rotationY << std::endl;
|
|
|
|
|
setRelativeRotation(Vector(rotationY, rotationX, 0));
|
|
|
|
|
bNeedsViewBuild = true;
|
2020-09-30 13:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-19 22:45:30 +01:00
|
|
|
void CameraComponent::mouseScroll(float x)
|
2020-09-30 13:48:41 +02:00
|
|
|
{
|
2022-03-19 22:45:30 +01:00
|
|
|
addRelativeLocation(getTransform().getForward()*x);
|
|
|
|
|
bNeedsViewBuild = true;
|
2020-09-30 13:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-19 22:45:30 +01:00
|
|
|
void CameraComponent::moveX(float amount)
|
2020-09-30 13:48:41 +02:00
|
|
|
{
|
2022-03-19 22:45:30 +01:00
|
|
|
addRelativeLocation(getTransform().getForward()*amount);
|
|
|
|
|
bNeedsViewBuild = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CameraComponent::moveY(float amount)
|
|
|
|
|
{
|
|
|
|
|
addRelativeLocation(getTransform().getRight()*amount);
|
|
|
|
|
bNeedsViewBuild = true;
|
2020-09-30 13:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CameraComponent::buildViewMatrix()
|
|
|
|
|
{
|
2022-03-19 22:45:30 +01:00
|
|
|
Vector eyePos = getTransform().getPosition();
|
|
|
|
|
Vector lookAt = eyePos + getTransform().getForward();
|
|
|
|
|
std::cout << "Eye: " << eyePos << " lookAt: " << lookAt << std::endl;
|
|
|
|
|
viewMatrix = glm::lookAt(eyePos, lookAt, Vector(0, 1, 0));
|
2020-09-30 13:48:41 +02:00
|
|
|
|
2022-03-19 22:45:30 +01:00
|
|
|
bNeedsViewBuild = false;
|
2020-09-30 13:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CameraComponent::buildProjectionMatrix()
|
|
|
|
|
{
|
|
|
|
|
projectionMatrix = glm::perspective(fieldOfView, aspectRatio, 1.0f, 1000.f);
|
2022-03-19 22:45:30 +01:00
|
|
|
static Matrix4 correctionMatrix =
|
|
|
|
|
Matrix4(
|
|
|
|
|
Vector4(1, 0, 0, 0),
|
|
|
|
|
Vector4(0, 1, 0, 0),
|
|
|
|
|
Vector4(0, 0, 0.5f, 0),
|
|
|
|
|
Vector4(0, 0, 0.5f, 1));
|
|
|
|
|
projectionMatrix = correctionMatrix * projectionMatrix;
|
|
|
|
|
bNeedsProjectionBuild = false;
|
2020-09-30 13:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|