Completed persp camera I hope

This commit is contained in:
NaifBanana 2024-11-23 01:35:55 -06:00
parent b1daa922ef
commit 9ff0d9edb4
5 changed files with 101 additions and 11 deletions

View File

@ -153,6 +153,10 @@ OrthographicCamera::OrthographicCamera(
float near,
float far
) : Camera(pos, tar, up), _leftPlane(left), _rightPlane(right), _bottomPlane(bottom), _topPlane(top), _nearPlane(near), _farPlane(far) {
_calcView();
}
void OrthographicCamera::_calcView() {
setProjection(glm::ortho(_leftPlane, _rightPlane, _bottomPlane, _topPlane, _nearPlane, _farPlane));
}
@ -168,6 +172,46 @@ float OrthographicCamera::getNearPlane() const { return _nearPlane; }
float OrthographicCamera::getFarPlane() const { return _farPlane; }
void OrthographicCamera::setLeftPlane(float left) {
_leftPlane = left;
_calcView();
}
void OrthographicCamera::setRightPlane(float right) {
_rightPlane = right;
_calcView();
}
void OrthographicCamera::setBottomPlane(float bottom) {
_bottomPlane = bottom;
_calcView();
}
void OrthographicCamera::setTopPlane(float top) {
_topPlane = top;
_calcView();
}
void OrthographicCamera::setNearPlane(float near) {
_nearPlane = near;
_calcView();
}
void OrthographicCamera::setFarPlane(float far) {
_farPlane = far;
_calcView();
}
void OrthographicCamera::setOrthographicProjection(float left, float right, float bottom, float top, float near, float far) {
_leftPlane = left;
_rightPlane = right;
_bottomPlane = bottom;
_topPlane = top;
_nearPlane = near;
_farPlane = far;
_calcView();
}
// PerspectiveCamera class
PerspectiveCamera::PerspectiveCamera(
const Vec3& pos,
@ -178,7 +222,47 @@ PerspectiveCamera::PerspectiveCamera(
float near,
float far
) : Camera(pos, tar, up), _fov(fov), _ratio(aspectRatio), _nearPlane(near), _farPlane(far) {
_calcView();
}
void PerspectiveCamera::_calcView() {
setProjection(glm::perspective(glm::radians(_fov), _ratio, _nearPlane, _farPlane));
}
float PerspectiveCamera::getFOV() const { return _fov; }
float PerspectiveCamera::getAspectRatio() const { return _ratio; }
float PerspectiveCamera::getNearPlane() const { return _nearPlane; }
float PerspectiveCamera::getFarPlane() const { return _farPlane; }
void PerspectiveCamera::setFOV(float fov) {
_fov = fov;
_calcView();
}
void PerspectiveCamera::setAspectRatio(float aspectRatio) {
_ratio = aspectRatio;
_calcView();
}
void PerspectiveCamera::setNearPlane(float near) {
_nearPlane = near;
_calcView();
}
void PerspectiveCamera::setFarPlane(float far) {
_farPlane = far;
_calcView();
}
void PerspectiveCamera::setPerspectiveProjection(float fov, float aspectRatio, float near, float far) {
_fov = fov;
_ratio = aspectRatio;
_nearPlane = near;
_farPlane = far;
_calcView();
}
}

View File

@ -2,6 +2,7 @@
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
cam->setAspectRatio((float)width / (float)height);
}
void processInput(GLFWwindow* window) {

View File

@ -108,6 +108,7 @@ public:
private:
float _leftPlane, _rightPlane, _topPlane, _bottomPlane, _nearPlane, _farPlane;
void _calcView();
using Camera::setProjection;
};
@ -126,9 +127,21 @@ public:
float far = 1.0f
);
float getFOV() const;
float getAspectRatio() const;
float getNearPlane() const;
float getFarPlane() const;
void setFOV(float);
void setAspectRatio(float);
void setNearPlane(float);
void setFarPlane(float);
void setPerspectiveProjection(float fov=45, float aspectRatio=1.0f, float near=-1.0f, float far=1.0f);
private:
float _fov, _ratio, _nearPlane, _farPlane;
void _calcView();
using Camera::setProjection;
};

View File

@ -7,7 +7,7 @@
#include "Draw.h"
#include "Camera.h"
extern NB::Camera* cam;
extern NB::PerspectiveCamera* cam;
extern float deltaTime;
void framebuffer_size_callback(GLFWwindow*, int, int);

View File

@ -14,7 +14,7 @@
#include "Draw.h"
#include "Camera.h"
NB::Camera* cam;
NB::PerspectiveCamera* cam;
float deltaTime = 0.0f;
float lastFrame = 0.0f;
@ -52,7 +52,7 @@ int main() {
myCube.generateVBO(NB::vectorToRaw(vert_data));
myCube.generateEBO(elmt_data);
NB::OrthographicCamera myCam(glm::vec3(0.0f, 0.0f, -2.0f), glm::vec3(0.0), glm::vec3(0.0, 1.0, 0.0), -2, 2, -2, 2, -2, 2);
NB::PerspectiveCamera myCam(glm::vec3(0.0f, 0.0f, -2.0f), glm::vec3(0.0), glm::vec3(0.0, 1.0, 0.0), 45, 4.0/3.0, 0.01, 100);
cam = &myCam;
// myCam.setProjection(glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f));
glm::mat4 lookMat;
@ -66,14 +66,6 @@ int main() {
NB_GL_DEBUG_THROW("nothing", "during");
// Input checking
processInput(window);
std::cout << "xy: " << glm::dot(myCam.getRight(), myCam.getDirection());
std::cout << "\n\tyz : " << glm::dot(myCam.getDirection(), myCam.getUp());
std::cout << "\n\tzx : " << glm::dot(myCam.getUp(), myCam.getRight());
std::cout << "\n\tdir : " << glm::length(myCam.getDirection());
std::cout << "\n\tright: " << glm::length(myCam.getRight());
std::cout << "\n\tup : " << glm::length(myCam.getUp());
std::cout << "\n\n";
// Rendering Loop
myShader.use();