Make camera a cpp.

This commit is contained in:
2024-01-05 06:37:34 -05:00
parent 9eb314cb73
commit 8e7d1a2b80
3 changed files with 118 additions and 114 deletions

View File

@@ -9,7 +9,7 @@
#include "../entityList.h"
//A "scripted move" is a vector of points and angles that function as keyframes.
//A moby will follow *and interpolate* between those points.
//TODO Refactor into cpp.
class ScriptedMove {
public:
std::vector<LinearAlgebra::Vector3> positions;

View File

@@ -14,130 +14,30 @@ enum class CameraMode: uint8_t {
const LinearAlgebra::Vector3 UP = {0, 1, 0};
class Camera : public Moby {
private:
void modeBinds();
void thirdPerson();
void freeCam();
protected:
public:
LinearAlgebra::Matrix4x4 GetViewMatrix()
{
//return glm::lookAt(position, position + angle, UP);
}
CameraMode cameraMode = CameraMode::FREECAM;
bool takingScreenshot = false;
LinearAlgebra::Matrix4x4 GetViewMatrix();
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).x << " Calculated Yaw: " << VectorMath::calcAngle(position,getPlayer()->position).y << std::endl;
this->position = getPlayer()->cameraPoint(2);
//Make the camera pitch down a little bit.
this->position.y += 0.5;
this->angle.x = VectorMath::calcAngle(position,getPlayer()->position).x;
this->angle.y = VectorMath::calcAngle(position,getPlayer()->position).y;
}
//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.y +=75.0f*engine->frameDelta;
}
if (engine->window->keyDown(SCANCODE::RIGHT)) {
this->angle.y -=75.0f*engine->frameDelta;
}
if (engine->window->keyDown(SCANCODE::UP)) {
this->angle.x -=75.0f*engine->frameDelta;
}
if (engine->window->keyDown(SCANCODE::DOWN)) {
this->angle.x +=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
//TODO
//this->angle.clamp();
glRotatef(angle.x,1.0f, 0.0f, 0.0f);
glRotatef(-angle.y,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++;
}
void pre_render() override;
void render() override;
void post_render() override;
};
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();
}
Camera* getCamera();

View File

@@ -1,7 +1,5 @@
#include <types/camera.h>
// Even tho CLion reports this as const-qualifiable
// It is not in "intent" (obviously the gametick changes gamestate)
void Camera::update()
@@ -11,4 +9,110 @@ void Camera::update()
std::cout << "X: " << position.x << " Y: " << position.y << " Z: " << position.z << std::endl;
std::cout << "Pitch: " << angle.x << " Yaw: " << angle.y << " Roll: " << angle.z << std::endl;
}
}
LinearAlgebra::Matrix4x4 Camera::GetViewMatrix() {
//return glm::lookAt(position, position + angle, UP);
}
void Camera::modeBinds() {
if (engine->window->keyDown(SCANCODE::ZERO)) {
takingScreenshot = true;
engine->debugInfo();
}
if (engine->window->keyDown(SCANCODE::ONE))
cameraMode = CameraMode::FREECAM;
if (engine->window->keyDown(SCANCODE::TWO))
cameraMode = CameraMode::THIRD_PERSON;
if (engine->window->keyDown(SCANCODE::THREE))
cameraMode = CameraMode::SCRIPTED_MOVE;
//if (cameraMode == CameraMode::SCRIPTED_MOVE) {
//doScriptedMovement();
//}
}
void Camera::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.y +=75.0f*engine->frameDelta;
}
if (engine->window->keyDown(SCANCODE::RIGHT)) {
this->angle.y -=75.0f*engine->frameDelta;
}
if (engine->window->keyDown(SCANCODE::UP)) {
this->angle.x -=75.0f*engine->frameDelta;
}
if (engine->window->keyDown(SCANCODE::DOWN)) {
this->angle.x +=75.0f*engine->frameDelta;
}
}
void Camera::thirdPerson() {
if (engine->debug)
std::cout << "Calculated Pitch: " << VectorMath::calcAngle(position,getPlayer()->position).x << " Calculated Yaw: " << VectorMath::calcAngle(position,getPlayer()->position).y << std::endl;
position = getPlayer()->cameraPoint(2);
//Make the camera pitch down a little bit.
this->position.y += 0.5;
this->angle.x = VectorMath::calcAngle(position,getPlayer()->position).x;
this->angle.y = VectorMath::calcAngle(position,getPlayer()->position).y;
}
void Camera::pre_render() {
modeBinds();
if (cameraMode == CameraMode::FREECAM)
freeCam();
if (cameraMode == CameraMode::THIRD_PERSON)
thirdPerson();
//if(cameraMode == CameraMode::SCRIPTED_MOVE)
//0doScriptedMovement();
}
void Camera::render() {
// Preferrably: Camera would return a coordinate system that GameEngine
// would set gluLookAt() with, this helps keep objects self contained
//TODO
//this->angle.clamp();
glRotatef(angle.x,1.0f, 0.0f, 0.0f);
glRotatef(-angle.y,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 Camera::post_render() {
if (this->takingScreenshot) {
engine->takeScreenshot();
takingScreenshot = false;
}
this->ticksAlive++;
}
//TODO: Make it such that we don't have to do this.
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();
}