136 lines
3.1 KiB
C++
136 lines
3.1 KiB
C++
#pragma once
|
|
#ifndef _NB_CAMERA
|
|
#define _NB_CAMERA
|
|
|
|
#include <GLLoad.h>
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_inverse.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
namespace NB {
|
|
|
|
class Camera {
|
|
public:
|
|
typedef glm::vec3 Vec3;
|
|
typedef glm::mat4 Mat4;
|
|
|
|
enum CameraTypeEnum : unsigned char {
|
|
None,
|
|
Orthographic,
|
|
Perspective
|
|
};
|
|
|
|
const CameraTypeEnum CameraType = CameraTypeEnum::None;
|
|
|
|
static Vec3 normCross(const Vec3&, const Vec3&);
|
|
static Vec3 transformVec(const Mat4&, const Vec3&);
|
|
|
|
Camera(
|
|
const Vec3& pos=Vec3(1.0f, 0.0f, 0.0f),
|
|
const Vec3& tar=Vec3(0.0f, 0.0f, 0.0f),
|
|
const Vec3& up =Vec3(0.0f, 1.0f, 0.0f)
|
|
);
|
|
|
|
|
|
Vec3 getPos() const;
|
|
Vec3 getTarget() const;
|
|
Vec3 getDirection() const;
|
|
Vec3 getWorldUp() const;
|
|
Vec3 getUp() const;
|
|
Vec3 getRight() const;
|
|
Mat4 getLookMatrix() const;
|
|
Mat4 getProjectionMatrix() const;
|
|
Mat4 getFlattenedMatrices() const;
|
|
bool isUpLocked() const;
|
|
|
|
void setProjection(const Mat4&);
|
|
void setPos(const Vec3&);
|
|
void setTarget(const Vec3&);
|
|
void setDirection(const Vec3&);
|
|
void setWorldUp(const Vec3&);
|
|
void lockToWorldUp(bool lock);
|
|
void resetUp();
|
|
void addPos(const Vec3&);
|
|
void panPos(const Vec3&);
|
|
void panPosRelative(const Vec3&);
|
|
void addTarget(const Vec3&);
|
|
void roll(float);
|
|
void pitch(float);
|
|
void yaw(float);
|
|
|
|
protected:
|
|
Vec3 _pos;
|
|
Vec3 _tar;
|
|
Vec3 _dir;
|
|
Vec3 _world_up;
|
|
Vec3 _up;
|
|
Vec3 _right;
|
|
Mat4 _look;
|
|
Mat4 _proj;
|
|
bool _lock_world_up = true;
|
|
|
|
void _calcLook();
|
|
|
|
};
|
|
|
|
class OrthographicCamera : public Camera {
|
|
public:
|
|
const CameraTypeEnum CameraType = CameraTypeEnum::Orthographic;
|
|
|
|
OrthographicCamera(
|
|
const Vec3& pos=Vec3(1.0f, 0.0f, 0.0f),
|
|
const Vec3& tar=Vec3(0.0f, 0.0f, 0.0f),
|
|
const Vec3& up =Vec3(0.0f, 1.0f, 0.0f),
|
|
float left = -1.0f,
|
|
float right = 1.0f,
|
|
float bottom = -1.0f,
|
|
float top = 1.0f,
|
|
float near = -1.0f,
|
|
float far = 1.0f
|
|
);
|
|
|
|
float getLeftPlane() const;
|
|
float getRightPlane() const;
|
|
float getBottomPlane() const;
|
|
float getTopPlane() const;
|
|
float getNearPlane() const;
|
|
float getFarPlane() const;
|
|
|
|
void setLeftPlane(float);
|
|
void setRightPlane(float);
|
|
void setBottomPlane(float);
|
|
void setTopPlane(float);
|
|
void setNearPlane(float);
|
|
void setFarPlane(float);
|
|
|
|
private:
|
|
float _leftPlane, _rightPlane, _topPlane, _bottomPlane, _nearPlane, _farPlane;
|
|
|
|
using Camera::setProjection;
|
|
|
|
};
|
|
|
|
class PerspectiveCamera : public Camera {
|
|
public:
|
|
const CameraTypeEnum CameraType = CameraTypeEnum::Perspective;
|
|
|
|
PerspectiveCamera(
|
|
const Vec3& pos=Vec3(1.0f, 0.0f, 0.0f),
|
|
const Vec3& tar=Vec3(0.0f, 0.0f, 0.0f),
|
|
const Vec3& up =Vec3(0.0f, 1.0f, 0.0f),
|
|
float fov = 45.0f,
|
|
float aspectRatio = 1.0f,
|
|
float near = -1.0f,
|
|
float far = 1.0f
|
|
);
|
|
|
|
private:
|
|
float _fov, _ratio, _nearPlane, _farPlane;
|
|
|
|
using Camera::setProjection;
|
|
|
|
};
|
|
|
|
}
|
|
#endif // _NB_CAMERA
|