Files
Re3D/include/types/camera.h
2024-01-03 09:26:21 -05:00

142 lines
4.6 KiB
C++

#pragma once
#include "moby.h"
#include "player.h"
#include "../engine/engine.h"
#include "entityList.h"
#include <GL/glu.h>
enum class CameraMode: uint8_t {
THIRD_PERSON = 0,
FREECAM = 1,
SCRIPTED_MOVE = 2
};
const LinearAlgebra::Vector3 UP = {0, 1, 0};
class Camera : public Moby {
protected:
public:
LinearAlgebra::Matrix4x4 GetViewMatrix()
{
//return glm::lookAt(position, position + angle, UP);
}
void MoveForward(float dist);
void StrafeLeft(float dist);
void StrafeRight(float dist);
void TurnLeft(float deg = 90);
void TurnRight(float deg = 90);
void LookAt(LinearAlgebra::Vector3 pos, LinearAlgebra::Vector3 target, LinearAlgebra::Vector3 upaxis = UP) {}
void Rotate(float amt, LinearAlgebra::Vector3 axis) { }
void Translate(LinearAlgebra::Vector3 dir) { }
float fov;
Camera() : Moby(), fov(60)
{
}
CameraMode cameraMode = CameraMode::FREECAM;
bool takingScreenshot = false;
void update();
void pre_render() {
if (engine->window->keyDown(SCANCODE::ZERO)) {
this->takingScreenshot = true;
engine->debugInfo();
}
if (engine->window->keyDown(SCANCODE::ONE))
this->cameraMode = CameraMode::FREECAM;
if (engine->window->keyDown(SCANCODE::TWO))
this->cameraMode = CameraMode::THIRD_PERSON;
if (engine->window->keyDown(SCANCODE::THREE))
this->cameraMode = CameraMode::SCRIPTED_MOVE;
if (cameraMode == CameraMode::SCRIPTED_MOVE) {
doScriptedMovement();
}
if (cameraMode == CameraMode::THIRD_PERSON) {
if (engine->debug)
std::cout << "Calculated Pitch: " << VectorMath::calcAngle(position,getPlayer()->position).pitch << " Calculated Yaw: " << VectorMath::calcAngle(position,getPlayer()->position).yaw << std::endl;
this->position = getPlayer()->cameraPoint(2);
//Make the camera pitch down a little bit.
this->position.y += 0.5;
this->angle.pitch = VectorMath::calcAngle(position,getPlayer()->position).pitch;
this->angle.yaw = VectorMath::calcAngle(position,getPlayer()->position).yaw;
}
//if (engine->frameCount == 5000)
//getPlayer()->thirdPersonCameraPoints();
if (cameraMode == CameraMode::FREECAM) {
if (engine->window->keyDown(SCANCODE::S)) {
move(bAngle(),2);
}
if (engine->window->keyDown(SCANCODE::W)) {
move(fAngle(),2);
}
if (engine->window->keyDown(SCANCODE::A)) {
move(lAngle(), 2);
}
if (engine->window->keyDown(SCANCODE::D)) {
move(rAngle(),2);
}
if (engine->window->keyDown(SCANCODE::SPACE)) {
this->position.y += 5*engine->frameDelta;
}
if (engine->window->keyDown(SCANCODE::LEFT_SHIFT)) {
this->position.y -= 5*engine->frameDelta;
}
if (engine->window->keyDown(SCANCODE::LEFT)) {
this->angle.yaw +=75.0f*engine->frameDelta;
}
if (engine->window->keyDown(SCANCODE::RIGHT)) {
this->angle.yaw -=75.0f*engine->frameDelta;
}
if (engine->window->keyDown(SCANCODE::UP)) {
this->angle.pitch -=75.0f*engine->frameDelta;
}
if (engine->window->keyDown(SCANCODE::DOWN)) {
this->angle.pitch +=75.0f*engine->frameDelta;
}
}
}
void render() {
// Preferrably: Camera would return a coordinate system that GameEngine
// would set gluLookAt() with, this helps keep objects self contained
this->angle.clamp();
glRotatef(angle.pitch,1.0f, 0.0f, 0.0f);
glRotatef(-angle.yaw,0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, 0.0f);
gluLookAt(position.x, position.y, position.z, // camera position
position.x, position.y, engine->farPlane+position.z, // target position, We're always *looking at* the far plane straight ahead so the camera never turns around.
upVector.x,upVector.y,upVector.z);
}
void post_render() {
if (this->takingScreenshot) {
engine->takeScreenshot();
takingScreenshot = false;
}
this->ticksAlive++;
}
};
inline Camera* getCamera() {
for (auto& e : entityList)
if (auto* c = dynamic_cast<Camera*>(e))
return c;
std::cerr << "Attempt to get Camera pointer while not in scene." << std::endl;
engine->quit();
}