Add some projection functions to math namespace. Break projection into their own header
This commit is contained in:
parent
6fe392258b
commit
b514cd2e87
@ -33,8 +33,10 @@ namespace egn{
|
||||
PROJ_UPDATE = 16,
|
||||
};
|
||||
protected:
|
||||
math::mat4<GLfloat> m_projection_matrix; //camera-to-sceen matrix
|
||||
math::mat4<GLfloat> m_view_matrix; //world-to-camera matrix
|
||||
//mutable because they're only really a cached value representation of the data
|
||||
//in position, orientation, near, far, etc
|
||||
mutable math::mat4<GLfloat> m_projection_matrix; //camera-to-sceen matrix
|
||||
mutable math::mat4<GLfloat> m_view_matrix; //world-to-camera matrix
|
||||
GLfloat m_near = 1; //near clipping plane in camera space
|
||||
GLfloat m_far = 100; //far clipping plane in camera space
|
||||
|
||||
@ -52,8 +54,8 @@ namespace egn{
|
||||
void set_orientation(const math::quaternion<GLfloat>& distance)override;
|
||||
|
||||
//getters
|
||||
const math::mat4<GLfloat>& get_projection_matrix();
|
||||
const math::mat4<GLfloat>& get_view_matrix();
|
||||
const math::mat4<GLfloat>& get_projection_matrix()const;
|
||||
const math::mat4<GLfloat>& get_view_matrix()const;
|
||||
GLfloat get_near_plane()const;
|
||||
GLfloat get_far_plane()const;
|
||||
|
||||
@ -62,8 +64,8 @@ namespace egn{
|
||||
void set_far_plane(GLfloat f);
|
||||
|
||||
protected:
|
||||
void recalc_view_matrix();
|
||||
virtual void recalc_projection_matrix() = 0;
|
||||
void recalc_view_matrix()const;
|
||||
virtual void recalc_projection_matrix()const = 0;
|
||||
};
|
||||
|
||||
class ortho_camera : public camera_iface
|
||||
@ -87,7 +89,7 @@ namespace egn{
|
||||
void set_projection_height(GLfloat h);
|
||||
void set_projection_box(GLfloat w, GLfloat h);
|
||||
protected:
|
||||
void recalc_projection_matrix()override;
|
||||
void recalc_projection_matrix()const override;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@ -37,8 +37,8 @@ namespace egn{
|
||||
math::vec3<GLfloat> m_position; //track current positon in world space
|
||||
math::quaternion<GLfloat> m_orientation; //track current model space rotation
|
||||
math::vec3<GLfloat> m_scale{1.0f, 1.0f, 1.0f}; //track model space scale
|
||||
math::mat4<GLfloat> m_model_matrix; //compile all the above info into a matrix
|
||||
int m_update_flag = NO_UPDATE; //whether or not to update the matrix upon access
|
||||
mutable math::mat4<GLfloat> m_model_matrix; //compile all the above info into a matrix
|
||||
mutable int m_update_flag = NO_UPDATE; //whether or not to update the matrix upon access
|
||||
|
||||
public:
|
||||
object() = default;
|
||||
@ -59,10 +59,14 @@ namespace egn{
|
||||
virtual void set_position(const math::vec3<GLfloat>& pos);
|
||||
virtual void set_orientation(const math::quaternion<GLfloat>& orient);
|
||||
virtual void set_scale(const math::vec3<GLfloat>& scale);
|
||||
const math::mat4<GLfloat>& get_model_matrix();
|
||||
|
||||
const math::mat4<GLfloat>& get_model_matrix()const;
|
||||
const math::vec3<GLfloat>& get_position()const;
|
||||
const math::vec3<GLfloat>& get_scale()const;
|
||||
const math::quaternion<GLfloat>& get_orientation()const;
|
||||
|
||||
protected:
|
||||
void recalc_model_matrix();
|
||||
void recalc_model_matrix()const;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@ -176,14 +176,6 @@ namespace math{
|
||||
template<typename T>
|
||||
matrix<T,3,3> rotation2d(T x, T y, T z);
|
||||
|
||||
template<typename T>
|
||||
matrix<T,4,4> fov_projection(T fov, T asp, T near, T far);
|
||||
template<typename T>
|
||||
matrix<T,4,4> fov_asymetric_projection(T fovl, T fovr, T fovb, T fovt, T asp, T near, T far);
|
||||
template<typename T>
|
||||
matrix<T,4,4> ortho_projection(T w, T h, T n, T f);
|
||||
template<typename T>
|
||||
matrix<T,4,4> ortho_asymetric_projection(T l, T r, T b, T t, T n, T f);
|
||||
template<typename T>
|
||||
constexpr matrix<T,4,4> rotation3d(T angle_x, T angle_y, T angle_z);
|
||||
template<typename T>
|
||||
|
||||
@ -208,40 +208,6 @@ namespace math{
|
||||
return q.to_mat3();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
matrix<T,4,4> fov_projection(T fov, T asp, T near, T far){
|
||||
T r = near * std::tan(fov / T{2.0});
|
||||
return matrix<T,4,4>((near / r) / asp, T{0}, T{0}, T{0},
|
||||
T{0}, (near / r), T{0}, T{0},
|
||||
T{0}, T{0}, (far + near) / (near - far), -T{1},
|
||||
T{0}, T{0}, (T{2} * near * far) / (near - far), T{0});
|
||||
}
|
||||
template<typename T>
|
||||
matrix<T,4,4> fov_asymetric_projection(T fovl, T fovr, T fovb, T fovt, T asp, T n, T f){
|
||||
T l = n * std::tan(fovl);
|
||||
T r = n * std::tan(fovr);
|
||||
T b = n * std::tan(fovb);
|
||||
T t = n * std::tan(fovt);
|
||||
|
||||
return matrix<T,4,4>(((T{2} * n) / (r - l)) * asp, T{0}, T{0}, T{0},
|
||||
T{0}, (T{2} * n) / (t - b), T{0}, T{0},
|
||||
(r + l) / (r - l), (t + b) / (t - b), (f + n) / (n - f), -T{1},
|
||||
T{0}, T{0}, (T{2} * n * f) / (n - f), T{0});
|
||||
}
|
||||
template<typename T>
|
||||
matrix<T,4,4> ortho_projection(T w, T h, T n, T f){
|
||||
return matrix<T,4,4>(T{2} / w, T{0}, T{0}, T{0},
|
||||
T{0}, T{2} / h, T{0}, T{0},
|
||||
T{0}, T{0}, T{2} / (n - f), T{0},
|
||||
T{0}, T{0}, (n + f) / (n - f), T{1});
|
||||
}
|
||||
template<typename T>
|
||||
matrix<T,4,4> ortho_asymetric_projection(T l, T r, T b, T t, T n, T f){
|
||||
return matrix<T,4,4>(T{2} / (r - l), T{0}, T{0}, T{0},
|
||||
T{0}, T{2} / (t - b), T{0}, T{0},
|
||||
T{0}, T{0}, T{2} / (n - f), T{0},
|
||||
(r + l) / (l - r), (t + b) / (b - t), (n + f) / (n - f), T{1});
|
||||
}
|
||||
template<typename T>
|
||||
constexpr matrix<T,4,4> rotation3d(T angle_x, T angle_y, T angle_z){
|
||||
quaternion<T> q(angle_x, angle_y, angle_z);
|
||||
|
||||
@ -26,5 +26,6 @@
|
||||
#include "vec.hpp"
|
||||
#include "mat.hpp"
|
||||
#include "quat.hpp"
|
||||
#include "projection.hpp"
|
||||
|
||||
#endif
|
||||
|
||||
45
include/math/projection.hpp
Normal file
45
include/math/projection.hpp
Normal file
@ -0,0 +1,45 @@
|
||||
/**
|
||||
This file is a part of our_dick
|
||||
Copyright (C) 2020 rexy712
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef OUR_DICK_MATH_PROJECTION_HPP
|
||||
#define OUR_DICK_MATH_PROJECTION_HPP
|
||||
|
||||
#include "mat.hpp"
|
||||
#include "vec.hpp"
|
||||
|
||||
namespace math{
|
||||
|
||||
template<typename T>
|
||||
matrix<T,4,4> fov_projection(T fov, T asp, T near, T far);
|
||||
template<typename T>
|
||||
matrix<T,4,4> fov_asymetric_projection(T fovl, T fovr, T fovb, T fovt, T asp, T near, T far);
|
||||
template<typename T>
|
||||
matrix<T,4,4> ortho_projection(T w, T h, T n, T f);
|
||||
template<typename T>
|
||||
matrix<T,4,4> ortho_asymetric_projection(T l, T r, T b, T t, T n, T f);
|
||||
|
||||
template<typename T>
|
||||
vec3<T> project(const mat4<T>& viewproj_mat, const vec3<T>& world_coords, const vec4<T>& viewport);
|
||||
template<typename T>
|
||||
vec3<T> unproject(const mat4<T>& viewproj_mat, const vec3<T>& viewport_coords, const vec4<T>& viewport);
|
||||
|
||||
}
|
||||
|
||||
#include "projection.tpp"
|
||||
|
||||
#endif
|
||||
98
include/math/projection.tpp
Normal file
98
include/math/projection.tpp
Normal file
@ -0,0 +1,98 @@
|
||||
/**
|
||||
This file is a part of our_dick
|
||||
Copyright (C) 2020 rexy712
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef OUR_DICK_MATH_PROJECTION_TPP
|
||||
#define OUR_DICK_MATH_PROJECTION_TPP
|
||||
|
||||
#include "mat.hpp"
|
||||
#include "vec.hpp"
|
||||
#include <cmath> //sin, cos, tan
|
||||
|
||||
namespace math{
|
||||
|
||||
template<typename T>
|
||||
matrix<T,4,4> fov_projection(T fov, T asp, T near, T far){
|
||||
T r = near * std::tan(fov / T{2.0});
|
||||
return matrix<T,4,4>((near / r) / asp, T{0}, T{0}, T{0},
|
||||
T{0}, (near / r), T{0}, T{0},
|
||||
T{0}, T{0}, (far + near) / (near - far), -T{1},
|
||||
T{0}, T{0}, (T{2} * near * far) / (near - far), T{0});
|
||||
}
|
||||
template<typename T>
|
||||
matrix<T,4,4> fov_asymetric_projection(T fovl, T fovr, T fovb, T fovt, T asp, T n, T f){
|
||||
T l = n * std::tan(fovl);
|
||||
T r = n * std::tan(fovr);
|
||||
T b = n * std::tan(fovb);
|
||||
T t = n * std::tan(fovt);
|
||||
|
||||
return matrix<T,4,4>(((T{2} * n) / (r - l)) * asp, T{0}, T{0}, T{0},
|
||||
T{0}, (T{2} * n) / (t - b), T{0}, T{0},
|
||||
(r + l) / (r - l), (t + b) / (t - b), (f + n) / (n - f), -T{1},
|
||||
T{0}, T{0}, (T{2} * n * f) / (n - f), T{0});
|
||||
}
|
||||
template<typename T>
|
||||
matrix<T,4,4> ortho_projection(T w, T h, T n, T f){
|
||||
return matrix<T,4,4>(T{2} / w, T{0}, T{0}, T{0},
|
||||
T{0}, T{2} / h, T{0}, T{0},
|
||||
T{0}, T{0}, T{2} / (n - f), T{0},
|
||||
T{0}, T{0}, (n + f) / (n - f), T{1});
|
||||
}
|
||||
template<typename T>
|
||||
matrix<T,4,4> ortho_asymetric_projection(T l, T r, T b, T t, T n, T f){
|
||||
return matrix<T,4,4>(T{2} / (r - l), T{0}, T{0}, T{0},
|
||||
T{0}, T{2} / (t - b), T{0}, T{0},
|
||||
T{0}, T{0}, T{2} / (n - f), T{0},
|
||||
(r + l) / (l - r), (t + b) / (b - t), (n + f) / (n - f), T{1});
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
vec3<T> project(const mat4<T>& viewproj_mat, const vec3<T>& w_coords, const vec4<T>& viewport){
|
||||
//project world coordinates to ndc coordinates
|
||||
vec4<T> world_coords{w_coords[0], w_coords[1], w_coords[2], T{1.0}};
|
||||
vec4<T> ndc_coords = viewproj_mat * world_coords;
|
||||
|
||||
//perspective_division
|
||||
ndc_coords /= ndc_coords[3];
|
||||
|
||||
//project ndc coordinates to viewport coordinates
|
||||
return vec3<T>{((ndc_coords[0] + T{1.0}) * viewport[2] * T{0.5}) + viewport[0],
|
||||
((ndc_coords[1] + T{1.0}) * viewport[3] * T{0.5}) + viewport[1],
|
||||
ndc_coords[2]
|
||||
};
|
||||
}
|
||||
template<typename T>
|
||||
vec3<T> unproject(const mat4<T>& viewproj_mat, const vec3<T>& viewport_coords, const vec4<T>& viewport){
|
||||
//project viewport coordinates to ndc coordinates
|
||||
vec4<T> ndc_coords{((viewport_coords[0] - viewport[0]) * T{2.0} / viewport[2]) - T{1.0},
|
||||
((viewport_coords[1] - viewport[1]) * T{2.0} / viewport[3]) - T{1.0},
|
||||
viewport_coords[2],
|
||||
T{1.0}};
|
||||
|
||||
//project ndc coordinates to world coordinates
|
||||
mat4<T> inv_viewproj_mat = viewproj_mat.inverse();
|
||||
vec4<T> world_coords = inv_viewproj_mat * ndc_coords;
|
||||
|
||||
//perspective division
|
||||
world_coords /= world_coords[3];
|
||||
|
||||
return {world_coords[0], world_coords[1], world_coords[2]};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -18,6 +18,7 @@
|
||||
|
||||
#include "engine/camera.hpp"
|
||||
#include "config.hpp"
|
||||
#include "math/projection.hpp"
|
||||
|
||||
namespace egn{
|
||||
|
||||
@ -30,17 +31,18 @@ namespace egn{
|
||||
m_update_flag |= VIEW_UPDATE;
|
||||
}
|
||||
void camera_iface::set_orientation(const math::quaternion<GLfloat>& orient){
|
||||
debug_print("%f, %f, %f, %f\n", m_orientation.w(), m_orientation.x(), m_orientation.y(), m_orientation.z());
|
||||
object::set_orientation(orient);
|
||||
m_update_flag |= VIEW_UPDATE;
|
||||
}
|
||||
const math::mat4<GLfloat>& camera_iface::get_projection_matrix(){
|
||||
const math::mat4<GLfloat>& camera_iface::get_projection_matrix()const{
|
||||
if(m_update_flag & PROJ_UPDATE){
|
||||
recalc_projection_matrix();
|
||||
m_update_flag ^= PROJ_UPDATE;
|
||||
}
|
||||
return m_projection_matrix;
|
||||
}
|
||||
const math::mat4<GLfloat>& camera_iface::get_view_matrix(){
|
||||
const math::mat4<GLfloat>& camera_iface::get_view_matrix()const{
|
||||
if(m_update_flag & VIEW_UPDATE){
|
||||
recalc_view_matrix();
|
||||
m_update_flag ^= VIEW_UPDATE;
|
||||
@ -63,7 +65,7 @@ namespace egn{
|
||||
m_far = f;
|
||||
}
|
||||
|
||||
void camera_iface::recalc_view_matrix(){
|
||||
void camera_iface::recalc_view_matrix()const{
|
||||
debug_print_verbose("Rebuilding view matrix\n");
|
||||
m_view_matrix = m_orientation.to_mat4();
|
||||
m_view_matrix.get(3, 0) = -m_position[0];
|
||||
@ -97,7 +99,7 @@ namespace egn{
|
||||
m_height = h;
|
||||
}
|
||||
|
||||
void ortho_camera::recalc_projection_matrix(){
|
||||
void ortho_camera::recalc_projection_matrix()const{
|
||||
debug_print_verbose("Rebuilding orthographic projection matrix\n");
|
||||
m_projection_matrix = math::ortho_projection(m_width, m_height, m_near, m_far);
|
||||
}
|
||||
|
||||
@ -58,14 +58,24 @@ namespace egn{
|
||||
m_update_flag |= SCALE_UPDATE;
|
||||
m_scale = scale;
|
||||
}
|
||||
const math::mat4<GLfloat>& object::get_model_matrix(){
|
||||
const math::mat4<GLfloat>& object::get_model_matrix()const{
|
||||
if(m_update_flag & (SCALE_UPDATE | ROTATION_UPDATE | TRANSLATION_UPDATE)){
|
||||
recalc_model_matrix();
|
||||
m_update_flag &= (~(SCALE_UPDATE | ROTATION_UPDATE | TRANSLATION_UPDATE));
|
||||
}
|
||||
return m_model_matrix;
|
||||
}
|
||||
void object::recalc_model_matrix(){
|
||||
const math::vec3<GLfloat>& object::get_position()const{
|
||||
return m_position;
|
||||
}
|
||||
const math::vec3<GLfloat>& object::get_scale()const{
|
||||
return m_scale;
|
||||
}
|
||||
const math::quaternion<GLfloat>& object::get_orientation()const{
|
||||
return m_orientation;
|
||||
}
|
||||
|
||||
void object::recalc_model_matrix()const{
|
||||
debug_print_verbose("Rebuilding model matrix\n");
|
||||
m_model_matrix = m_orientation.to_mat4();
|
||||
m_model_matrix.get(3, 0) = m_position[0];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user