12#ifndef JAU_MATH_UTIL_PMVMAT4f_HPP_
13#define JAU_MATH_UTIL_PMVMAT4f_HPP_
79 std::enable_if_t<std::is_floating_point_v<Value_type>,
bool> =
true>
105 : m_mat(m), m_sync( std::move(s) )
108 PMVSync1(
const Mat4& m) noexcept
113 const Mat4& matrix()
const noexcept override {
return m_mat; }
124 : m_mat(m), m_count(count), m_sync( std::move(s) )
126 PMVSyncN(
const Mat4& m,
size_t count) noexcept
131 const Mat4* matrices() const noexcept
override {
return &m_mat; }
132 size_t matrixCount() const noexcept
override {
return m_count; }
143 MatrixStack<value_type> m_stackMv, m_stackP, m_stackTex;
145 uint32_t m_requestBits;
147 PMVSync1 m_syncP = PMVSync1(m_matP);
148 PMVSync1 m_syncMv = PMVSync1(m_matMv);
149 PMVSync1 m_syncTex = PMVSync1(m_matTex);
151 PMVSync1 m_syncMvi = PMVSync1(m_matMvi,
jau::bind_member(
this, &PMVMatrix4::updateImpl0));
152 PMVSync1 m_syncMvit = PMVSync1(m_matMvit,
jau::bind_member(
this, &PMVMatrix4::updateImpl0));
154 PMVSyncN m_syncP_Mv = PMVSyncN(m_matP, 2);
155 PMVSyncN m_syncP_Mv_Mvi = PMVSyncN(m_matP, 3,
jau::bind_member(
this, &PMVMatrix4::updateImpl0));
156 PMVSyncN m_syncP_Mv_Mvi_Mvit = PMVSyncN(m_matP, 4,
jau::bind_member(
this, &PMVMatrix4::updateImpl0));
159 uint32_t m_dirtyBits = 0;
163 geom::Frustum m_frustum;
165 constexpr static uint32_t matToReq(uint32_t req)
noexcept {
196 constexpr static const uint32_t
FRUSTUM = 1 << 4;
232 : m_requestBits( matToReq(derivedMatrices) )
242 m_matP.loadIdentity();
243 m_matMv.loadIdentity();
244 m_matTex.loadIdentity();
263 constexpr Mat4&
getT() noexcept {
return m_matTex; }
264 constexpr const Mat4&
getT() const noexcept {
return m_matTex; }
285 constexpr const Mat4&
getP() const noexcept {
return m_matP; }
306 constexpr const Mat4&
getMv() const noexcept {
return m_matMv; }
410 return m_syncP_Mv_Mvi;
424 return m_syncP_Mv_Mvi_Mvit;
440 return result.mul(m_matP, m_matMv);
452 return result.mul(m_matMv, m_matP);
462 return m_matMv.mulVec4(v_in, v_out);
471 return m_matMv.mulVec4(v_inout);
484 return m_matMv.mulVec3(v_in, v_out);
495 m_matMv.load(values);
511 quat.toMatrix(m_matMv);
536 quat.toMatrix(m_matP);
545 m_matTex.load(values);
561 quat.toMatrix(m_matTex);
570 m_matMv.loadIdentity();
579 m_matP.loadIdentity();
588 m_matTex.loadIdentity();
745 return mulMv( quat.toMatrix(mat4Tmp1) );
783 return mulP( quat.toMatrix(mat4Tmp1) );
788 m_stackMv.pop(m_matMv);
794 m_stackP.pop(m_matP);
800 m_stackTex.pop(m_matTex);
806 m_stackMv.push(m_matMv);
811 m_stackP.push(m_matP);
816 m_stackTex.push(m_matTex);
830 constexpr void orthoP(
const float left,
const float right,
const float bottom,
const float top,
const float zNear,
const float zFar)
noexcept {
832 mulP( mat4Tmp1.
setToOrtho(left, right, bottom, top, zNear, zFar) );
842 void frustumP(
const float left,
const float right,
const float bottom,
const float top,
const float zNear,
const float zFar) {
935 bool mapWinToObj(
const float winx,
const float winy,
const float winz,
936 const Recti& viewport,
Vec3& objPos)
noexcept {
961 const Mat4& matVi,
const Recti& viewport,
Vec3& objPos)
noexcept {
981 bool mapWinToView(
const float winx,
const float winy,
const float winz,
982 const Recti& viewport,
Vec3& objPos)
noexcept {
1006 bool mapWinToObj4(
const float winx,
const float winy,
const float winz,
const float clipw,
1007 const Recti& viewport,
const float near,
const float far,
Vec4& objPos)
const noexcept {
1035 bool mapWinToObjRay(
const float winx,
const float winy,
const float winz0,
const float winz1,
1036 const Recti& viewport,
Ray3f& ray)
noexcept {
1069 bool mapWinToWorldRay(
const float winx,
const float winy,
const float winz0,
const float winz1,
1096 bool mapWinToViewRay(
const float winx,
const float winy,
const float winz0,
const float winz1,
1097 const Recti& viewport,
Ray3f& ray)
noexcept {
1101 std::string&
toString(std::string& sb,
const std::string& f)
const noexcept {
1102 const bool pmvDirty = 0 != (
PREMUL_PMV & m_dirtyBits);
1104 const bool pmviDirty = 0 != (
PREMUL_PMVI & m_dirtyBits);
1106 const bool frustumDirty = 0 != (
FRUSTUM & m_dirtyBits);
1122 sb.append(
"PMVMatrix4[modified[P ").append(std::to_string(modP)).append(
", Mv ").append(std::to_string(modMv)).append(
", T ").append(std::to_string(modT));
1123 sb.append(
"], dirty[PMv ").append(std::to_string(pmvDirty))
1124 .append(
", Pmvi ").append(std::to_string(pmviDirty))
1125 .append(
", Frustum ").append(std::to_string(frustumDirty));
1126 sb.append(
"], dirty/req[Mvi ").append(std::to_string(mviDirty)).append(
"/").append(std::to_string(mviReq))
1127 .append(
", Pi ").append(std::to_string(piDirty)).append(
"/").append(std::to_string(piReq))
1128 .append(
", Mvit ").append(std::to_string(mvitDirty)).append(
"/").append(std::to_string(mvitReq)).append(
"]\n");
1129 sb.append(
", Projection\n");
1130 m_matP.toString(sb, f);
1131 sb.append(
", Modelview\n");
1132 m_matMv.toString(sb, f);
1133 sb.append(
", Texture\n");
1134 m_matTex.toString(sb, f);
1136 sb.append(
", P * Mv\n");
1137 m_matPMv.toString(sb, f);
1141 sb.append(
", P * Mv\n");
1142 m_matPMvi.toString(sb, f);
1146 sb.append(
", Inverse Modelview\n");
1147 m_matMvi.toString(sb, f);
1151 sb.append(
", Inverse Transposed Modelview\n");
1152 m_matMvit.toString(sb, f);
1159 sb.append(
", matrices "+std::to_string(count)+
" + "+std::to_string(tmpCount)+
" temp = "+std::to_string(count+tmpCount)+
"]");
1184 const uint32_t r = m_modifiedBits;
1246 return 0 != ( m_requestBits & m_dirtyBits );
1296 return m_requestBits;
1314 m_matPMv.mul(m_matP, m_matMv);
1337 m_matPMviOK = m_matPMvi.invert(mPMv);
1357 if( 0 != ( m_dirtyBits &
FRUSTUM ) ) {
1358 m_frustum.setFromMat(
getPMv());
1413 return updateImpl(
true);
1421 void updateImpl0() noexcept { updateImpl(
false); }
1422 bool updateImpl(
bool clearModBits)
noexcept {
1423 bool mod = 0 != m_modifiedBits;
1424 if( clearModBits ) {
1429 if( !m_matPi.invert(m_matP) ) {
1438 if( !m_matMvi.invert(m_matMv) ) {
1447 m_matMvit.transpose(m_matMvi);
1456 std::enable_if_t<std::is_floating_point_v<Value_type>,
bool> =
true>
1458 return out << v.toString();
Basic 4x4 value_type matrix implementation using fields for intensive use-cases (host operations).
constexpr Matrix4 & setToScale(const value_type x, const value_type y, const value_type z) noexcept
Set this matrix to scale.
static bool mapViewToWin(const Vec3 &view, const Matrix4 &mP, const Recti &viewport, Vec3 &winPos) noexcept
constexpr_cxx26 Matrix4 & setToRotationAxis(const value_type ang_rad, value_type x, value_type y, value_type z) noexcept
Set this matrix to rotation from the given axis and angle in radians.
static bool mapWinToObj4(const value_type winx, const value_type winy, const value_type winz, const value_type clipw, const Matrix4 &mMv, const Matrix4 &mP, const Recti &viewport, const value_type near, const value_type far, Vec4 &objPos) noexcept
Map window coordinates to object coordinates.
Matrix4 & setToPerspective(const value_type fovy_rad, const value_type aspect, const value_type zNear, const value_type zFar)
Set this matrix to perspective frustum projection.
static bool mapObjToWin(const Vec3 &obj, const Matrix4 &mPMv, const Recti &viewport, Vec3 &winPos) noexcept
Matrix4 & setToFrustum(const value_type left, const value_type right, const value_type bottom, const value_type top, const value_type zNear, const value_type zFar)
Set this matrix to frustum.
static bool mapWinToAny(const value_type winx, const value_type winy, const value_type winz, const Matrix4 &invAny, const Recti &viewport, Vec3 &objPos) noexcept
Map window coordinates to object, world or view coordinates, depending on invAny argument.
static bool mapWorldToWin(const Vec3 &world, const Matrix4 &mV, const Matrix4 &mP, const Recti &viewport, Vec3 &winPos) noexcept
constexpr Matrix4 & setToOrtho(const value_type left, const value_type right, const value_type bottom, const value_type top, const value_type zNear, const value_type zFar) noexcept
Set this matrix to orthogonal projection.
static bool mapWinToAnyRay(const value_type winx, const value_type winy, const value_type winz0, const value_type winz1, const Matrix4 &invAny, const Recti &viewport, Ray3 &ray) noexcept
constexpr Matrix4 & mul(const Matrix4 &b) noexcept
Multiply matrix: [this] = [this] x [b].
constexpr Matrix4 & setToTranslation(const value_type x, const value_type y, const value_type z) noexcept
Set this matrix to translation.
constexpr Matrix4 & setToLookAt(const Vec3 &eye, const Vec3 ¢er, const Vec3 &up) noexcept
Set this matrix to the look-at matrix based on given parameters.
Simple compound denoting a ray.
3D vector using three value_type components.
4D vector using four value_type components.
Providing frustum planes derived by different inputs (P*MV, ..) used to classify objects.
PMVMatrix4 implements the basic computer graphics Matrix4 pack using projection (P),...
constexpr PMVMatrix4 & loadPIdentity() noexcept
Load the projection matrix with the values of the given Mat4.
bool mapWinToObj(const float winx, const float winy, const float winz, const Recti &viewport, Vec3 &objPos) noexcept
Map window coordinates to object coordinates.
const Mat4 & getMvi()
Returns the inverse modelview matrix (Mvi) if requested.
constexpr PMVMatrix4 & loadMvIdentity() noexcept
Load the modelview matrix with the values of the given Mat4.
constexpr void orthoP(const float left, const float right, const float bottom, const float top, const float zNear, const float zFar) noexcept
Multiply the projection matrix with the orthogonal matrix.
std::string toString() const noexcept
constexpr SyncMat4 & getSyncT() noexcept
Returns the SyncMatrix of texture matrix (T).
constexpr PMVMatrix4 & mulT(const Mat4 &m) noexcept
Multiply the texture matrix: [c] = [c] x [m].
jau::math::geom::Frustum getFrustum() noexcept
Returns the frustum, derived from projection x modelview.
static constexpr const uint32_t INVERSE_TRANSPOSED_MODELVIEW
Bit value for inverse transposed modelview matrix (Mvit), updated via update().
const value_type * const_pointer
constexpr PMVMatrix4 & translateP(float x, float y, float z) noexcept
Translate the projection matrix.
constexpr bool isReqDirty() noexcept
Returns true if the one of the requested bits are are set dirty due to mutable operations,...
constexpr Mat4 & getPMv() noexcept
Returns the pre-multiplied projection x modelview, P x Mv.
constexpr PMVMatrix4 & mulP(const Mat4 &m) noexcept
Multiply the projection matrix: [c] = [c] x [m].
SyncMats4f & getSyncPMvMviMvit()
Returns SyncMatrices4f of 4 matrices within one FloatBuffer: P, Mv, Mvi and Mvit if requested.
PMVMatrix4(int derivedMatrices) noexcept
Creates an instance of PMVMatrix4.
constexpr const SyncMats4f & getSyncPMv() const noexcept
constexpr PMVMatrix4 & loadMv(float values[]) noexcept
Load the modelview matrix with the provided values.
constexpr PMVMatrix4 & loadP(const Quat4f &quat) noexcept
Load the projection matrix with the values of the given Quaternion's rotation Quaternion::toMatrix() ...
bool mapViewToWin(const Vec3 &view, const Recti &viewport, Vec3 &winPos) const noexcept
Map view coordinates ( Mv x object ) to window coordinates.
const value_type & const_reference
const Mat4 & getMvit()
Returns the inverse transposed modelview matrix (Mvit) if requested.
constexpr PMVMatrix4 & scaleP(const Vec3 &s) noexcept
Scale the projection matrix.
static constexpr const uint32_t INVERSE_PROJECTION
Bit value for inverse projection matrix (Pi), updated via update().
constexpr_cxx20 PMVMatrix4 & popT() noexcept
Pop the texture matrix from its stack.
constexpr const SyncMats4 & getSyncP() const noexcept
SyncMats4f & getSyncPMvMvi()
Returns SyncMatrices4f of 3 matrices within one FloatBuffer: P, Mv and Mvi if requested.
constexpr Mat4 & getP() noexcept
Returns the projection matrix (P).
static constexpr const uint32_t INVERSE_MODELVIEW
Bit value for inverse modelview matrix (Mvi), updated via update().
constexpr SyncMats4f & getSyncPMv() noexcept
Returns SyncMatrices4f of 2 matrices within one FloatBuffer: P and Mv.
constexpr Mat4 & getMv() noexcept
Returns the modelview matrix (Mv).
constexpr PMVMatrix4 & scaleMv(const Vec3 &s) noexcept
Scale the modelview matrix.
constexpr_cxx26 PMVMatrix4 & rotateP(const float ang_rad, const Vec3 &axis) noexcept
Rotate the projection matrix by the given axis and angle in radians.
bool mapWinToView(const float winx, const float winy, const float winz, const Recti &viewport, Vec3 &objPos) noexcept
Map window coordinates to view coordinates.
constexpr Vec3 & mulWithMv(const Vec3 &v_in, Vec3 &v_out) noexcept
v_out = Mv x v_in
constexpr const SyncMats4 & getSyncT() const noexcept
constexpr const SyncMats4 & getSyncMv() const noexcept
constexpr uint32_t getDirtyBits() noexcept
Returns the dirty bits due to mutable operations, i.e.
constexpr Mat4 & getPMvi() noexcept
Returns the pre-multiplied inverse projection x modelview, if Mat4#invert(Mat4) succeeded,...
constexpr PMVMatrix4 & loadT(float values[]) noexcept
Load the texture matrix with the provided values.
Vector4F< value_type, std::is_floating_point_v< Value_type > > Vec4
Matrix4< value_type, std::is_floating_point_v< Value_type > > Mat4
constexpr uint32_t getModifiedBits(const bool clear) noexcept
Returns the modified bits due to mutable operations.
static constexpr const uint32_t FRUSTUM
std::string & toString(std::string &sb, const std::string &f) const noexcept
constexpr_cxx26 PMVMatrix4 & rotateMv(const float ang_rad, const float x, const float y, const float z) noexcept
Rotate the modelview matrix by the given axis and angle in radians.
SyncMatrix4< value_type, std::is_floating_point_v< Value_type > > SyncMat4
constexpr PMVMatrix4 & mulMv(const Mat4 &m) noexcept
Multiply the modelview matrix: [c] = [c] x [m].
constexpr PMVMatrix4 & loadMv(const Quat4f &quat) noexcept
Load the modelview matrix with the values of the given Quaternion's rotation Quaternion::toMatrix() r...
constexpr_cxx20 PMVMatrix4 & popMv() noexcept
Pop the modelview matrix from its stack.
bool mapWinToObjRay(const float winx, const float winy, const float winz0, const float winz1, const Recti &viewport, Ray3f &ray) noexcept
Map two window coordinates w/ shared X/Y and distinctive Z to a Ray in object space.
constexpr_cxx20 PMVMatrix4 & pushP() noexcept
Push the projection matrix to its stack, while preserving its values.
constexpr PMVMatrix4 & loadTIdentity() noexcept
Load the texture matrix with the values of the given Mat4.
constexpr SyncMat4 & getSyncP() noexcept
Returns the SyncMatrix of projection matrix (P).
constexpr PMVMatrix4 & scaleP(float x, float y, float z) noexcept
Scale the projection matrix.
Vector3F< value_type, std::is_floating_point_v< Value_type > > Vec3
SyncMat4 & getSyncMvi()
Returns the SyncMatrix of inverse modelview matrix (Mvi) if requested.
constexpr void setModelviewDirty() noexcept
Sets the Modelview (Mv) matrix dirty and modified, i.e.
bool update() noexcept
Update the derived inverse modelview (Mvi), inverse transposed modelview (Mvit) matrices if they are ...
constexpr_cxx20 PMVMatrix4 & pushMv() noexcept
Push the modelview matrix to its stack, while preserving its values.
constexpr PMVMatrix4 & translateMv(float x, float y, float z) noexcept
Translate the modelview matrix.
constexpr_cxx26 PMVMatrix4 & rotateP(const float ang_rad, const float x, const float y, const float z) noexcept
Rotate the projection matrix by the given axis and angle in radians.
const Mat4 & getPi()
Returns the inverse modelview matrix (Pi) if requested.
constexpr const Mat4 & getMv() const noexcept
constexpr PMVMatrix4 & lookAtP(const Vec3 &eye, const Vec3 ¢er, const Vec3 &up) noexcept
Multiply the projection matrix with the eye, object and orientation, i.e.
constexpr PMVMatrix4 & loadT(Mat4 &m) noexcept
Load the texture matrix with the values of the given Mat4.
PMVMatrix4 & perspectiveP(const float fovy_rad, const float aspect, const float zNear, const float zFar)
Multiply the projection matrix with the perspective/frustum matrix.
SyncMatrices4< value_type, std::is_floating_point_v< Value_type > > SyncMats4
static constexpr const uint32_t MODIFIED_TEXTURE
void frustumP(const float left, const float right, const float bottom, const float top, const float zNear, const float zFar)
Multiply the projection matrix with the frustum matrix.
constexpr PMVMatrix4 & translateMv(const Vec3 &t) noexcept
Translate the modelview matrix.
constexpr SyncMat4 & getSyncMv() noexcept
Returns the SyncMatrix of modelview matrix (Mv).
constexpr const Mat4 & getT() const noexcept
static constexpr const uint32_t MANUAL_BITS
constexpr void setTextureDirty() noexcept
Sets the Texture (T) matrix modified.
constexpr PMVMatrix4 & rotateP(const Quat4f &quat) noexcept
Rotate the projection matrix with the given Quaternion's rotation matrix representation.
constexpr_cxx20 PMVMatrix4 & popP() noexcept
Pop the projection matrix from its stack.
bool mapWinToWorldRay(const float winx, const float winy, const float winz0, const float winz1, const Mat4 &matVi, const Recti &viewport, Ray3f &ray) noexcept
Map two window coordinates w/ shared X/Y and distinctive Z to a Ray in world space.
constexpr void setProjectionDirty() noexcept
Sets the Projection (P) matrix dirty and modified, i.e.
bool mapWinToWorld(const float winx, const float winy, const float winz, const Mat4 &matVi, const Recti &viewport, Vec3 &objPos) noexcept
Map window coordinates to object coordinates.
PMVMatrix4() noexcept
Creates an instance of PMVMatrix4.
constexpr PMVMatrix4 & rotateMv(const Quat4f &quat) noexcept
Rotate the modelview matrix with the given Quaternion's rotation matrix representation.
static constexpr const uint32_t PREMUL_PMVI
constexpr Mat4 & getMulPMv(Mat4 &result) noexcept
Returns multiplication result of P and Mv matrix, i.e.
constexpr PMVMatrix4 & translateP(const Vec3 &t) noexcept
Translate the projection matrix.
constexpr Mat4 & getT() noexcept
Returns the texture matrix (T).
constexpr PMVMatrix4 & scaleMv(float x, float y, float z) noexcept
Scale the modelview matrix.
constexpr Vec4 & mulWithMv(const Vec4 &v_in, Vec4 &v_out) noexcept
v_out = Mv x v_in
constexpr PMVMatrix4 & loadMv(const Mat4 &m) noexcept
Load the modelview matrix with the values of the given Mat4.
const value_type * const_iterator
static constexpr const uint32_t PREMUL_PMV
bool mapWinToViewRay(const float winx, const float winy, const float winz0, const float winz1, const Recti &viewport, Ray3f &ray) noexcept
Map two window coordinates w/ shared X/Y and distinctive Z to a Ray in view space.
constexpr Mat4 & getMulMvP(Mat4 &result) noexcept
Returns multiplication result of Mv and P matrix, i.e.
constexpr PMVMatrix4 & loadP(const Mat4 &m)
Load the projection matrix with the values of the given Mat4.
bool mapWorldToWin(const Vec3 &objPos, const Mat4 &matV, const Recti &viewport, Vec3 &winPos) const noexcept
Map world coordinates to window coordinates.
static constexpr const uint32_t MODIFIED_PROJECTION
static constexpr const uint32_t MODIFIED_MODELVIEW
constexpr void reset() noexcept
Issues Mat4#loadIdentity() on all matrices and resets all internal states.
constexpr const Mat4 & getP() const noexcept
constexpr_cxx20 PMVMatrix4 & pushT() noexcept
Push the texture matrix to its stack, while preserving its values.
constexpr PMVMatrix4 & loadP(float values[]) noexcept
Load the projection matrix with the provided values.
constexpr Vec4 & mulWithMv(Vec4 &v_inout) noexcept
v_inout = Mv x v_inout
SyncMat4 & getSyncMvit()
Returns the SyncMatrix of inverse transposed modelview matrix (Mvit) if requested.
constexpr_cxx26 PMVMatrix4 & rotateMv(const float ang_rad, const Vec3 &axis) noexcept
Rotate the modelview matrix by the given axis and angle in radians.
Ray3F< value_type, std::is_floating_point_v< Value_type > > Ray3
bool mapWinToObj4(const float winx, const float winy, const float winz, const float clipw, const Recti &viewport, const float near, const float far, Vec4 &objPos) const noexcept
Map window coordinates to object coordinates.
static constexpr const uint32_t MODIFIED_ALL
Bit value stating all is modified.
constexpr PMVMatrix4 & loadT(const Quat4f &quat) noexcept
Load the texture matrix with the values of the given Quaternion's rotation Quaternion::toMatrix() rep...
constexpr uint32_t getReqBits() noexcept
Returns the request bit mask, which uses bit values equal to the dirty mask and may contain.
bool mapObjToWin(const Vec3 &objPos, const Recti &viewport, Vec3 &winPos) const noexcept
Map object coordinates to window coordinates.
SyncBuffer interface with multiple underlying Matrix4.
Matrix4< value_type, std::is_floating_point_v< value_type > > Mat4
SyncBuffer interface with a single underlying Matrix4.
#define DBG_ERR_PRINT(...)
Use for environment-variable environment::DEBUG conditional error messages, prefix '[elapsed_time] Wa...
#define constexpr_cxx20
constexpr qualifier replacement for C++20 constexpr.
jau::function< R(A...)> bind_member(C1 *base, R(C0::*mfunc)(A...)) noexcept
Bind given class instance and non-void member function to an anonymous function using func_member_tar...
SyncMatrices4< float > SyncMats4f
jau::function< void()> sync_action_t
Specific data synchronization action implemented by the data provider to update the buffer with the u...
Quaternion< float > Quat4f
PMVMatrix4< float > PMVMat4f
std::ostream & operator<<(std::ostream &out, const PMVMatrix4< Value_type > &v) noexcept