2022-11-15 12:19:11 +01:00
|
|
|
#include "Camera.h"
|
2022-11-17 16:47:42 +01:00
|
|
|
#include "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-11-15 12:19:11 +01:00
|
|
|
using namespace Seele::Component;
|
|
|
|
|
using namespace Seele::Math;
|
2020-09-30 13:48:41 +02:00
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
Camera::Camera()
|
2023-11-06 14:47:21 +01:00
|
|
|
: viewMatrix(Matrix4())
|
|
|
|
|
, bNeedsViewBuild(false)
|
2020-09-30 13:48:41 +02:00
|
|
|
{
|
2022-03-26 12:55:04 +01:00
|
|
|
yaw = 0;
|
2023-11-13 13:48:01 +01:00
|
|
|
pitch = 0.2;
|
2020-09-30 13:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
Camera::~Camera()
|
2020-09-30 13:48:41 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
void Camera::mouseMove(float deltaYaw, float deltaPitch)
|
2020-09-30 13:48:41 +02:00
|
|
|
{
|
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;
|
2022-11-15 12:19:11 +01:00
|
|
|
getTransform().setRelativeRotation(result);
|
2022-03-19 22:45:30 +01:00
|
|
|
bNeedsViewBuild = true;
|
2020-09-30 13:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
void Camera::mouseScroll(float x)
|
2020-09-30 13:48:41 +02:00
|
|
|
{
|
2022-11-15 12:19:11 +01:00
|
|
|
getTransform().addRelativeLocation(getTransform().getForward()*x);
|
2022-03-19 22:45:30 +01:00
|
|
|
bNeedsViewBuild = true;
|
2020-09-30 13:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
void Camera::moveX(float amount)
|
2020-09-30 13:48:41 +02:00
|
|
|
{
|
2022-11-15 12:19:11 +01:00
|
|
|
getTransform().addRelativeLocation(getTransform().getForward()*amount);
|
2022-03-19 22:45:30 +01:00
|
|
|
bNeedsViewBuild = true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
void Camera::moveY(float amount)
|
2022-03-19 22:45:30 +01:00
|
|
|
{
|
2022-11-15 12:19:11 +01:00
|
|
|
getTransform().addRelativeLocation(getTransform().getRight()*amount);
|
2022-03-19 22:45:30 +01:00
|
|
|
bNeedsViewBuild = true;
|
2020-09-30 13:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
void Camera::buildViewMatrix()
|
2020-09-30 13:48:41 +02:00
|
|
|
{
|
2022-11-15 12:19:11 +01:00
|
|
|
Vector eyePos = getTransform().getPosition();//getAbsoluteTransform().getPosition();
|
2022-11-17 16:47:42 +01:00
|
|
|
Vector lookAt = eyePos + glm::normalize(Vector(cos(yaw) * cos(pitch), sin(pitch), sin(yaw) * cos(pitch)));
|
2023-11-13 13:23:16 +01:00
|
|
|
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
|
|
|
}
|