24#ifndef JAU_PMVMAT4f_HPP_
25#define JAU_PMVMAT4f_HPP_
34#include <initializer_list>
83 std::enable_if_t<std::is_floating_point_v<Value_type>,
bool> =
true>
109 : m_mat(m), m_sync( std::move(s) )
112 PMVSync1(
Mat4& m) noexcept
117 const Mat4& matrix()
const noexcept override {
return m_mat; }
128 : m_mat(m), m_count(count), m_sync( std::move(s) )
130 PMVSyncN(
Mat4* m,
size_t count) noexcept
135 const Mat4* matrices() const noexcept
override {
return m_mat; }
136 size_t matrixCount() const noexcept
override {
return m_count; }
146 MatrixStack<value_type> stackMv, stackP, stackTex;
148 uint32_t requestBits;
150 PMVSync1 syncP = PMVSync1(matP);
151 PMVSync1 syncMv = PMVSync1(matMv);
152 PMVSync1 syncTex = PMVSync1(matTex);
154 PMVSync1 syncMvi = PMVSync1(matMvi,
jau::bind_member(
this, &PMVMatrix4::updateImpl0));
155 PMVSync1 syncMvit = PMVSync1(matMvit,
jau::bind_member(
this, &PMVMatrix4::updateImpl0));
157 PMVSyncN syncP_Mv = PMVSyncN(&matP, 2);
158 PMVSyncN syncP_Mv_Mvi = PMVSyncN(&matP, 3,
jau::bind_member(
this, &PMVMatrix4::updateImpl0));
159 PMVSyncN syncP_Mv_Mvi_Mvit = PMVSyncN(&matP, 4,
jau::bind_member(
this, &PMVMatrix4::updateImpl0));
162 uint32_t dirtyBits = 0;
166 geom::Frustum frustum;
168 constexpr static uint32_t matToReq(uint32_t req)
noexcept {
194 constexpr static const uint32_t
FRUSTUM = 1 << 3;
229 : requestBits( matToReq(derivedMatrices) )
240 matMv.loadIdentity();
241 matTex.loadIdentity();
261 constexpr const Mat4&
getT() const noexcept {
return matTex; }
282 constexpr const Mat4&
getP() const noexcept {
return matP; }
303 constexpr const Mat4&
getMv() const noexcept {
return matMv; }
406 return syncP_Mv_Mvi_Mvit;
422 return result.
mul(matP, matMv);
434 return result.
mul(matMv, matP);
444 return matMv.mulVec4(v_in, v_out);
453 return matMv.mulVec4(v_inout);
466 return matMv.mulVec3(v_in, v_out);
493 quat.toMatrix(matMv);
543 quat.toMatrix(matTex);
552 matMv.loadIdentity();
570 matTex.loadIdentity();
727 return mulMv( quat.toMatrix(mat4Tmp1) );
765 return mulP( quat.toMatrix(mat4Tmp1) );
782 stackTex.pop(matTex);
798 stackTex.push(matTex);
812 constexpr void orthoP(
const float left,
const float right,
const float bottom,
const float top,
const float zNear,
const float zFar)
noexcept {
814 mulP( mat4Tmp1.
setToOrtho(left, right, bottom, top, zNear, zFar) );
824 void frustumP(
const float left,
const float right,
const float bottom,
const float top,
const float zNear,
const float zFar) {
888 bool mapWinToObj(
const float winx,
const float winy,
const float winz,
889 const Recti& viewport,
Vec3& objPos)
noexcept {
913 bool mapWinToObj4(
const float winx,
const float winy,
const float winz,
const float clipw,
914 const Recti& viewport,
const float near,
const float far,
Vec4& objPos)
noexcept {
942 bool mapWinToRay(
const float winx,
const float winy,
const float winz0,
const float winz1,
947 std::string&
toString(std::string& sb,
const std::string& f)
const noexcept {
948 const bool pmvDirty = 0 != (
PREMUL_PMV & dirtyBits);
949 const bool pmvUsed =
true;
951 const bool pmviDirty = 0 != (
PREMUL_PMVI & dirtyBits);
952 const bool pmviUsed =
true;
954 const bool frustumDirty = 0 != (
FRUSTUM & dirtyBits);
955 const bool frustumUsed =
true;
974 sb.append(
", Projection\n");
975 matP.toString(sb, f);
976 sb.append(
", Modelview\n");
977 matMv.toString(sb, f);
978 sb.append(
", Texture\n");
979 matTex.toString(sb, f);
981 sb.append(
", P * Mv\n");
982 matPMv.toString(sb, f);
986 sb.append(
", P * Mv\n");
987 matPMvi.toString(sb, f);
991 sb.append(
", Inverse Modelview\n");
992 matMvi.toString(sb, f);
996 sb.append(
", Inverse Transposed Modelview\n");
997 matMvit.toString(sb, f);
1029 const uint32_t r = modifiedBits;
1087 return 0 != ( requestBits & dirtyBits );
1153 matPMv.
mul(matP, matMv);
1154 dirtyBits &= ~PREMUL_PMV;
1176 matPMviOK = matPMvi.invert(mPMv);
1177 dirtyBits &= ~PREMUL_PMVI;
1196 if( 0 != ( dirtyBits &
FRUSTUM ) ) {
1198 dirtyBits &= ~FRUSTUM;
1250 return updateImpl(
true);
1258 void updateImpl0() { updateImpl(
false); }
1259 bool updateImpl(
bool clearModBits) {
1260 bool mod = 0 != modifiedBits;
1261 if( clearModBits ) {
1265 if( !matMvi.invert(matMv) ) {
1268 dirtyBits &= ~INVERSE_MODELVIEW;
1272 matMvit.transpose(matMvi);
1273 dirtyBits &= ~INVERSE_TRANSPOSED_MODELVIEW;
1281 std::enable_if_t<std::is_floating_point_v<Value_type>,
bool> =
true>
1283 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.
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, Matrix4 &mat4Tmp) noexcept
Map window coordinates to object coordinates.
static bool mapWinToRay(const value_type winx, const value_type winy, const value_type winz0, const value_type winz1, const Matrix4 &mMv, const Matrix4 &mP, const Recti &viewport, Ray3 &ray, Matrix4 &mat4Tmp1) noexcept
Map two window coordinates w/ shared X/Y and distinctive Z to a Ray.
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.
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 mapObjToWin(const Vec3 &obj, const Matrix4 &mMv, const Matrix4 &mP, const Recti &viewport, Vec3 &winPos) noexcept
Map object coordinates to window coordinates.
constexpr Matrix4 & setToLookAt(const Vec3 &eye, const Vec3 ¢er, const Vec3 &up, Matrix4 &tmp) noexcept
Set this matrix to the look-at matrix based on given parameters.
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 mapWinToObj(const value_type winx, const value_type winy, const value_type winz, const Matrix4 &mMv, const Matrix4 &mP, const Recti &viewport, Vec3 &objPos, Matrix4 &mat4Tmp) noexcept
Map window coordinates to object coordinates.
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.
Quaternion implementation supporting Gimbal-Lock free rotations.
Rectangle with x, y, width and height integer components.
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.
Frustum & setFromMat(const jau::math::Mat4f &m) noexcept
Calculate the frustum planes in world coordinates using the given column major order matrix,...
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.
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.
bool update()
Update the derived inverse modelview (Mvi), inverse transposed modelview (Mvit) matrices if they are ...
constexpr PMVMatrix4 & loadP(const Quat4f &quat) noexcept
Load the projection matrix with the values of the given Quaternion's rotation Quaternion::toMatrix() ...
const value_type & const_reference
constexpr PMVMatrix4 & scaleP(const Vec3 &s) noexcept
Scale the projection matrix.
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).
Mat4 & getMvit()
Returns the inverse transposed modelview matrix (Mvit) if requested.
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.
constexpr Vec3 & mulWithMv(const Vec3 &v_in, Vec3 &v_out) noexcept
v_out = Mv * 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
Bit value for frustum and updated by getFrustum().
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.
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.
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.
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
Bit value stating a modified texture matrix (T), since last update() call.
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
Manual bits not covered by update() but getFrustum(), FRUSTUM, getPMv(), PREMUL_PMV,...
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.
constexpr void setProjectionDirty() noexcept
Sets the Projection (P) matrix dirty and modified, i.e.
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
Bit value for pre-multiplied invert(P * Mv), updated by getPMvi().
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 * 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
Bit value for pre-multiplied P * Mv, updated by getPMv().
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 mapWinToObj4(const float winx, const float winy, const float winz, const float clipw, const Recti &viewport, const float near, const float far, Vec4 &objPos) noexcept
Map window coordinates to object coordinates.
static constexpr const uint32_t MODIFIED_PROJECTION
Bit value stating a modified projection matrix (P), since last update() call.
static constexpr const uint32_t MODIFIED_MODELVIEW
Bit value stating a modified modelview matrix (Mv), since last update() call.
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 * 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 mapObjToWin(const Vec3 &objPos, const Recti &viewport, Vec3 &winPos) noexcept
Map object coordinates to window coordinates.
Mat4 & getMvi()
Returns the inverse modelview matrix (Mvi) if requested.
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...
bool mapWinToRay(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.
constexpr uint32_t getReqBits() noexcept
Returns the request bit mask, which uses bit values equal to the dirty mask and may contain.
SyncBuffer interface with multiple underlying Matrix4.
SyncBuffer interface with a single underlying Matrix4.
std::string to_string(const alphabet &v) noexcept
#define constexpr_cxx20
constexpr qualifier replacement for C++20 constexpr.
void clear() noexcept
Clears internal list.
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...
jau::function< R(A...)> bind_free(R(*func)(A...)) noexcept
Bind given non-void free-function to an anonymous function using func::free_target_t.
jau::function< void()> sync_action_t
Specific data synchronization action implemented by the data provider to update the buffer with the u...
PMVMatrix4< float > PMVMat4f
void(* sync_action_fptr)()
Plain function pointer type matching sync_action_t.
std::ostream & operator<<(std::ostream &out, const PMVMatrix4< Value_type > &v) noexcept
Simple compound denoting a ray.