zeus/include/zeus/CProjection.hpp

113 lines
2.6 KiB
C++
Raw Normal View History

2018-10-06 20:39:40 -07:00
#pragma once
2015-04-19 13:39:16 -07:00
2016-03-04 15:03:26 -08:00
#include <cstdio>
#include <cstdlib>
#include "zeus/CMatrix4f.hpp"
2015-04-19 13:39:16 -07:00
2018-12-07 17:16:50 -08:00
namespace zeus {
2018-12-07 21:23:50 -08:00
enum class EProjType { None = 0, Orthographic = 1, Perspective = 2 };
2016-05-20 19:59:26 -07:00
2018-12-07 17:16:50 -08:00
class SProjOrtho {
2016-05-20 19:59:26 -07:00
public:
2018-12-07 17:16:50 -08:00
float top, bottom, left, right, znear, zfar;
explicit SProjOrtho(float p_top = 1.0f, float p_bottom = -1.0f, float p_left = -1.0f, float p_right = 1.0f,
float p_near = 1.0f, float p_far = -1.0f)
2018-12-07 21:23:50 -08:00
: top(p_top), bottom(p_bottom), left(p_left), right(p_right), znear(p_near), zfar(p_far) {}
2015-04-19 13:39:16 -07:00
};
2018-12-07 17:16:50 -08:00
struct SProjPersp {
float fov, aspect, znear, zfar;
SProjPersp(float p_fov = degToRad(55.0f), float p_aspect = 1.0f, float p_near = 0.1f, float p_far = 4096.f)
2018-12-07 21:23:50 -08:00
: fov(p_fov), aspect(p_aspect), znear(p_near), zfar(p_far) {}
2015-04-19 13:39:16 -07:00
};
2018-12-07 17:16:50 -08:00
2015-04-19 13:39:16 -07:00
extern const SProjOrtho kOrthoIdentity;
2018-12-07 17:16:50 -08:00
class CProjection {
void _updateCachedMatrix();
2016-07-08 11:42:42 -07:00
2015-04-19 13:39:16 -07:00
public:
2018-12-07 17:16:50 -08:00
CProjection() {
m_projType = EProjType::Orthographic;
m_ortho = SProjOrtho();
2019-02-23 23:15:32 -08:00
m_mtx = CMatrix4f();
2018-12-07 17:16:50 -08:00
}
2015-04-19 13:39:16 -07:00
2018-12-07 17:16:50 -08:00
CProjection(const CProjection& other) { *this = other; }
2016-07-08 11:42:42 -07:00
2018-12-07 17:16:50 -08:00
CProjection(const SProjOrtho& ortho) { setOrtho(ortho); }
2016-07-08 11:42:42 -07:00
2018-12-07 17:16:50 -08:00
CProjection(const SProjPersp& persp) { setPersp(persp); }
CProjection& operator=(const CProjection& other) {
if (this != &other) {
m_projType = other.m_projType;
m_ortho = other.m_ortho;
m_mtx = other.m_mtx;
2016-07-08 11:42:42 -07:00
}
2018-12-07 17:16:50 -08:00
return *this;
}
2016-07-08 11:42:42 -07:00
2018-12-07 17:16:50 -08:00
void setOrtho(const SProjOrtho& ortho) {
m_projType = EProjType::Orthographic;
m_ortho = ortho;
_updateCachedMatrix();
}
void setPersp(const SProjPersp& persp) {
m_projType = EProjType::Perspective;
m_persp = persp;
_updateCachedMatrix();
}
[[nodiscard]] EProjType getType() const { return m_projType; }
2018-12-07 17:16:50 -08:00
[[nodiscard]] const SProjOrtho& getOrtho() const {
2018-12-07 17:16:50 -08:00
#ifndef NDEBUG
if (m_projType != EProjType::Orthographic) {
std::fprintf(stderr, "attempted to access orthographic structure of non-ortho projection");
std::abort();
2015-04-19 13:39:16 -07:00
}
2018-12-07 17:16:50 -08:00
#endif
return m_ortho;
}
[[nodiscard]] const SProjPersp& getPersp() const {
2018-12-07 17:16:50 -08:00
#ifndef NDEBUG
if (m_projType != EProjType::Perspective) {
std::fprintf(stderr, "attempted to access perspective structure of non-persp projection");
std::abort();
2015-04-19 13:39:16 -07:00
}
2018-12-07 17:16:50 -08:00
#endif
return m_persp;
}
2015-04-19 13:39:16 -07:00
[[nodiscard]] const CMatrix4f& getCachedMatrix() const { return m_mtx; }
2016-07-08 11:42:42 -07:00
protected:
2018-12-07 17:16:50 -08:00
/* Projection type */
EProjType m_projType;
2016-07-08 11:42:42 -07:00
2018-12-07 17:16:50 -08:00
/* Projection intermediate */
union {
2015-04-19 13:39:16 -07:00
#ifdef _MSC_VER
2018-12-07 21:23:50 -08:00
struct {
SProjOrtho m_ortho;
2018-12-07 17:16:50 -08:00
};
2018-12-07 21:23:50 -08:00
struct {
SProjPersp m_persp;
2015-04-19 13:39:16 -07:00
};
2018-12-07 17:16:50 -08:00
#else
SProjOrtho m_ortho;
SProjPersp m_persp;
#endif
};
2015-04-19 13:39:16 -07:00
2018-12-07 17:16:50 -08:00
/* Cached projection matrix */
CMatrix4f m_mtx;
2015-04-19 13:39:16 -07:00
};
2018-12-07 21:23:50 -08:00
} // namespace zeus