Files
Seele/src/Engine/Scene/Components/CameraComponent.cpp
T

75 lines
1.9 KiB
C++
Raw Normal View History

#include "CameraComponent.h"
2021-01-19 15:30:00 +01:00
#include "Scene/Actor/Actor.h"
#include <algorithm>
2021-01-19 15:30:00 +01:00
#include <iostream>
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())
{
rotationX = 0;
2022-03-19 22:45:30 +01:00
rotationY = 0;
setRelativeLocation(Vector(0, 10, -50));
}
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;
}
2022-03-19 22:45:30 +01:00
void CameraComponent::mouseScroll(float x)
{
2022-03-19 22:45:30 +01:00
addRelativeLocation(getTransform().getForward()*x);
bNeedsViewBuild = true;
}
2022-03-19 22:45:30 +01:00
void CameraComponent::moveX(float amount)
{
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;
}
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));
2022-03-19 22:45:30 +01:00
bNeedsViewBuild = false;
}
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;
}