24#ifndef JAU_PMVMAT4f_HPP_
25#define JAU_PMVMAT4f_HPP_
80 std::enable_if_t<std::is_floating_point_v<Value_type>,
bool> =
true>
106 : m_mat(m), m_sync( std::move(s) )
109 PMVSync1(
const Mat4& m) noexcept
114 const Mat4& matrix()
const noexcept override {
return m_mat; }
125 : m_mat(m), m_count(count), m_sync( std::move(s) )
127 PMVSyncN(
const Mat4& m,
size_t count) noexcept
132 const Mat4* matrices() const noexcept
override {
return &m_mat; }
133 size_t matrixCount() const noexcept
override {
return m_count; }
143 MatrixStack<value_type> stackMv, stackP, stackTex;
145 uint32_t requestBits;
147 PMVSync1 syncP = PMVSync1(matP);
148 PMVSync1 syncMv = PMVSync1(matMv);
149 PMVSync1 syncTex = PMVSync1(matTex);
151 PMVSync1 syncMvi = PMVSync1(matMvi,
jau::bind_member(
this, &PMVMatrix4::updateImpl0));
152 PMVSync1 syncMvit = PMVSync1(matMvit,
jau::bind_member(
this, &PMVMatrix4::updateImpl0));
154 PMVSyncN syncP_Mv = PMVSyncN(matP, 2);
155 PMVSyncN syncP_Mv_Mvi = PMVSyncN(matP, 3,
jau::bind_member(
this, &PMVMatrix4::updateImpl0));
156 PMVSyncN syncP_Mv_Mvi_Mvit = PMVSyncN(matP, 4,
jau::bind_member(
this, &PMVMatrix4::updateImpl0));
159 uint32_t dirtyBits = 0;
163 geom::Frustum frustum;
165 constexpr static uint32_t matToReq(uint32_t req)
noexcept {
191 constexpr static const uint32_t
FRUSTUM = 1 << 3;
226 : requestBits( matToReq(derivedMatrices) )
237 matMv.loadIdentity();
238 matTex.loadIdentity();
258 constexpr const Mat4&
getT() const noexcept {
return matTex; }
279 constexpr const Mat4&
getP() const noexcept {
return matP; }
300 constexpr const Mat4&
getMv() const noexcept {
return matMv; }
403 return syncP_Mv_Mvi_Mvit;
419 return result.mul(matP, matMv);
431 return result.mul(matMv, matP);
441 return matMv.mulVec4(v_in, v_out);
450 return matMv.mulVec4(v_inout);
463 return matMv.mulVec3(v_in, v_out);
490 quat.toMatrix(matMv);
540 quat.toMatrix(matTex);
549 matMv.loadIdentity();
567 matTex.loadIdentity();
724 return mulMv( quat.toMatrix(mat4Tmp1) );
762 return mulP( quat.toMatrix(mat4Tmp1) );
779 stackTex.pop(matTex);
795 stackTex.push(matTex);
809 constexpr void orthoP(
const float left,
const float right,
const float bottom,
const float top,
const float zNear,
const float zFar)
noexcept {
811 mulP( mat4Tmp1.
setToOrtho(left, right, bottom, top, zNear, zFar) );
821 void frustumP(
const float left,
const float right,
const float bottom,
const float top,
const float zNear,
const float zFar) {
885 bool mapWinToObj(
const float winx,
const float winy,
const float winz,
886 const Recti& viewport,
Vec3& objPos)
noexcept {
910 bool mapWinToObj4(
const float winx,
const float winy,
const float winz,
const float clipw,
911 const Recti& viewport,
const float near,
const float far,
Vec4& objPos)
noexcept {
939 bool mapWinToRay(
const float winx,
const float winy,
const float winz0,
const float winz1,
944 std::string&
toString(std::string& sb,
const std::string& f)
const noexcept {
945 const bool pmvDirty = 0 != (
PREMUL_PMV & dirtyBits);
946 const bool pmvUsed =
true;
948 const bool pmviDirty = 0 != (
PREMUL_PMVI & dirtyBits);
949 const bool pmviUsed =
true;
951 const bool frustumDirty = 0 != (
FRUSTUM & dirtyBits);
952 const bool frustumUsed =
true;
965 sb.append(
"PMVMatrix4[modified[P ").append(std::to_string(modP)).append(
", Mv ").append(std::to_string(modMv)).append(
", T ").append(std::to_string(modT));
966 sb.append(
"], dirty/used[PMv ").append(std::to_string(pmvDirty)).append(
"/").append(std::to_string(pmvUsed))
967 .append(
", Pmvi ").append(std::to_string(pmviDirty)).append(
"/").append(std::to_string(pmviUsed))
968 .append(
", Frustum ").append(std::to_string(frustumDirty)).append(
"/").append(std::to_string(frustumUsed));
969 sb.append(
"], dirty/req[Mvi ").append(std::to_string(mviDirty)).append(
"/").append(std::to_string(mviReq))
970 .append(
", Mvit ").append(std::to_string(mvitDirty)).append(
"/").append(std::to_string(mvitReq)).append(
"]\n");
971 sb.append(
", Projection\n");
972 matP.toString(sb, f);
973 sb.append(
", Modelview\n");
974 matMv.toString(sb, f);
975 sb.append(
", Texture\n");
976 matTex.toString(sb, f);
978 sb.append(
", P * Mv\n");
979 matPMv.toString(sb, f);
983 sb.append(
", P * Mv\n");
984 matPMvi.toString(sb, f);
988 sb.append(
", Inverse Modelview\n");
989 matMvi.toString(sb, f);
993 sb.append(
", Inverse Transposed Modelview\n");
994 matMvit.toString(sb, f);
1001 sb.append(
", matrices "+std::to_string(count)+
" + "+std::to_string(tmpCount)+
" temp = "+std::to_string(count+tmpCount)+
"]");
1026 const uint32_t r = modifiedBits;
1084 return 0 != ( requestBits & dirtyBits );
1150 matPMv.mul(matP, matMv);
1173 matPMviOK = matPMvi.invert(mPMv);
1193 if( 0 != ( dirtyBits &
FRUSTUM ) ) {
1194 frustum.setFromMat(
getPMv());
1247 return updateImpl(
true);
1255 void updateImpl0() { updateImpl(
false); }
1256 bool updateImpl(
bool clearModBits) {
1257 bool mod = 0 != modifiedBits;
1258 if( clearModBits ) {
1262 if( !matMvi.invert(matMv) ) {
1265 dirtyBits &= ~INVERSE_MODELVIEW;
1269 matMvit.transpose(matMvi);
1278 std::enable_if_t<std::is_floating_point_v<Value_type>,
bool> =
true>
1280 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
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
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 & setToTranslation(const value_type x, const value_type y, const value_type z) noexcept
Set this matrix to translation.
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.
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
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
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.
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
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
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
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 * 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.
Matrix4< value_type, std::is_floating_point_v< value_type > > Mat4
SyncBuffer interface with a single underlying Matrix4.
#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...
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.
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
void(* sync_action_fptr)()
Plain function pointer type matching sync_action_t.
std::ostream & operator<<(std::ostream &out, const PMVMatrix4< Value_type > &v) noexcept