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

87 lines
2.5 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
, viewMatrix(Matrix4())
2022-03-26 16:15:50 +01:00
, projectionMatrix(Matrix4())
{
2022-03-26 12:55:04 +01:00
yaw = 0;
pitch = 0;
2022-03-19 22:45:30 +01:00
setRelativeLocation(Vector(0, 10, -50));
}
CameraComponent::~CameraComponent()
{
}
2022-03-26 12:55:04 +01:00
void CameraComponent::mouseMove(float deltaYaw, float deltaPitch)
{
2022-03-26 12:55:04 +01:00
yaw -= deltaYaw / 500.f;
pitch += deltaPitch / 500.f;
//std::cout << "Yaw: " << yaw << " Pitch: " << pitch << std::endl;
Vector cameraDirection = glm::normalize(Vector(cos(yaw) * cos(pitch), sin(pitch), sin(yaw) * cos(pitch)));
Vector xyz = glm::cross(cameraDirection, Vector(0, 0, 1));
Quaternion result = Quaternion(glm::dot(cameraDirection, Vector(0, 0, 1)) + 1, xyz.x, xyz.y, xyz.z);
//std::cout << "Result " << Vector(0, 0, 1) * glm::normalize(result) << " cameraDirection: " << cameraDirection << std::endl;
setRelativeRotation(result);
2022-03-19 22:45:30 +01:00
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;
}
2022-04-17 09:10:20 +02:00
void CameraComponent::setViewport(Gfx::PViewport newViewport)
{
viewport = newViewport;
aspectRatio = viewport->getSizeX() / (float)viewport->getSizeY();
bNeedsProjectionBuild = true;
}
void CameraComponent::buildViewMatrix()
{
2022-03-26 12:55:04 +01:00
Vector eyePos = getAbsoluteTransform().getPosition();
Vector lookAt = eyePos + glm::normalize(Vector(cos(yaw) * cos(pitch), sin(pitch), sin(yaw) * cos(pitch)));
//std::cout << "Eye: " << eyePos << " lookAt: " << lookAt << std::endl;
2022-03-19 22:45:30 +01:00
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;
}