Completed persp camera I hope
This commit is contained in:
parent
b1daa922ef
commit
9ff0d9edb4
84
Camera.cpp
84
Camera.cpp
@ -153,6 +153,10 @@ OrthographicCamera::OrthographicCamera(
|
|||||||
float near,
|
float near,
|
||||||
float far
|
float far
|
||||||
) : Camera(pos, tar, up), _leftPlane(left), _rightPlane(right), _bottomPlane(bottom), _topPlane(top), _nearPlane(near), _farPlane(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));
|
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; }
|
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 class
|
||||||
PerspectiveCamera::PerspectiveCamera(
|
PerspectiveCamera::PerspectiveCamera(
|
||||||
const Vec3& pos,
|
const Vec3& pos,
|
||||||
@ -178,7 +222,47 @@ PerspectiveCamera::PerspectiveCamera(
|
|||||||
float near,
|
float near,
|
||||||
float far
|
float far
|
||||||
) : Camera(pos, tar, up), _fov(fov), _ratio(aspectRatio), _nearPlane(near), _farPlane(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));
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
||||||
glViewport(0, 0, width, height);
|
glViewport(0, 0, width, height);
|
||||||
|
cam->setAspectRatio((float)width / (float)height);
|
||||||
}
|
}
|
||||||
|
|
||||||
void processInput(GLFWwindow* window) {
|
void processInput(GLFWwindow* window) {
|
||||||
|
|||||||
@ -108,6 +108,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
float _leftPlane, _rightPlane, _topPlane, _bottomPlane, _nearPlane, _farPlane;
|
float _leftPlane, _rightPlane, _topPlane, _bottomPlane, _nearPlane, _farPlane;
|
||||||
|
|
||||||
|
void _calcView();
|
||||||
using Camera::setProjection;
|
using Camera::setProjection;
|
||||||
|
|
||||||
};
|
};
|
||||||
@ -126,9 +127,21 @@ public:
|
|||||||
float far = 1.0f
|
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:
|
private:
|
||||||
float _fov, _ratio, _nearPlane, _farPlane;
|
float _fov, _ratio, _nearPlane, _farPlane;
|
||||||
|
|
||||||
|
void _calcView();
|
||||||
using Camera::setProjection;
|
using Camera::setProjection;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
#include "Draw.h"
|
#include "Draw.h"
|
||||||
#include "Camera.h"
|
#include "Camera.h"
|
||||||
|
|
||||||
extern NB::Camera* cam;
|
extern NB::PerspectiveCamera* cam;
|
||||||
extern float deltaTime;
|
extern float deltaTime;
|
||||||
|
|
||||||
void framebuffer_size_callback(GLFWwindow*, int, int);
|
void framebuffer_size_callback(GLFWwindow*, int, int);
|
||||||
|
|||||||
12
main.cpp
12
main.cpp
@ -14,7 +14,7 @@
|
|||||||
#include "Draw.h"
|
#include "Draw.h"
|
||||||
#include "Camera.h"
|
#include "Camera.h"
|
||||||
|
|
||||||
NB::Camera* cam;
|
NB::PerspectiveCamera* cam;
|
||||||
float deltaTime = 0.0f;
|
float deltaTime = 0.0f;
|
||||||
float lastFrame = 0.0f;
|
float lastFrame = 0.0f;
|
||||||
|
|
||||||
@ -52,7 +52,7 @@ int main() {
|
|||||||
myCube.generateVBO(NB::vectorToRaw(vert_data));
|
myCube.generateVBO(NB::vectorToRaw(vert_data));
|
||||||
myCube.generateEBO(elmt_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;
|
cam = &myCam;
|
||||||
// myCam.setProjection(glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f));
|
// myCam.setProjection(glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f));
|
||||||
glm::mat4 lookMat;
|
glm::mat4 lookMat;
|
||||||
@ -66,14 +66,6 @@ int main() {
|
|||||||
NB_GL_DEBUG_THROW("nothing", "during");
|
NB_GL_DEBUG_THROW("nothing", "during");
|
||||||
// Input checking
|
// Input checking
|
||||||
processInput(window);
|
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
|
// Rendering Loop
|
||||||
myShader.use();
|
myShader.use();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user