28package com.jogamp.math.util;
30import java.nio.Buffer;
31import java.nio.FloatBuffer;
33import com.jogamp.common.nio.Buffers;
34import com.jogamp.math.FloatUtil;
35import com.jogamp.math.Matrix4f;
36import com.jogamp.math.Quaternion;
37import com.jogamp.math.Ray;
38import com.jogamp.math.Recti;
39import com.jogamp.math.Vec3f;
40import com.jogamp.math.Vec4f;
41import com.jogamp.math.geom.AABBox;
42import com.jogamp.math.geom.Frustum;
207 mTex_offset = (mcount-1)*16;
210 matrixStore =
new float[mcount*16];
213 final FloatBuffer bufP = Buffers.slice2Float(matrixStore, mP_offset, 1*16);
214 syncP =
new SyncBuffer0(
matP, bufP);
216 final FloatBuffer bufMv = Buffers.slice2Float(matrixStore, mMv_offset, 1*16);
217 syncMv =
new SyncBuffer1(
matMv, bufMv, mMv_offset);
219 final FloatBuffer bufP_Mv = Buffers.slice2Float(matrixStore, mP_offset, 2*16);
222 final FloatBuffer bufTex = Buffers.slice2Float(matrixStore, mTex_offset, 1*16);
223 syncT =
new SyncBuffer1(
matTex, bufTex, mTex_offset);
225 if(
null != matMvi ) {
226 final FloatBuffer bufMvi = Buffers.slice2Float(matrixStore, mMvi_offset, 1*16);
227 final FloatBuffer bufP_Mv_Mvi = Buffers.slice2Float(matrixStore, mP_offset, 3*16);
228 syncMvi =
new SyncBuffer1U(matMvi, bufMvi, mMvi_offset);
229 syncP_Mv_Mvi =
new SyncBufferNU(
new Matrix4f[] {
matP,
matMv, matMvi }, bufP_Mv_Mvi, mP_offset);
234 if(
null != matMvit ) {
235 final FloatBuffer bufMvit = Buffers.slice2Float(matrixStore, mMvit_offset, 1*16);
236 final FloatBuffer bufP_Mv_Mvi_Mvit = Buffers.slice2Float(matrixStore, mP_offset, 4*16);
237 syncMvit =
new SyncBuffer1U(matMvit, bufMvit, mMvit_offset);
238 syncP_Mv_Mvi_Mvit =
new SyncBufferNU(
new Matrix4f[] {
matP,
matMv, matMvi, matMvit }, bufP_Mv_Mvi_Mvit, mP_offset);
241 syncP_Mv_Mvi_Mvit =
null;
280 if(
null == mat4Tmp2 ) {
378 throw new IllegalArgumentException(
"Not requested in ctor");
393 throw new IllegalArgumentException(
"Not requested in ctor");
407 throw new IllegalArgumentException(
"Not requested in ctor");
422 throw new IllegalArgumentException(
"Not requested in ctor");
436 throw new IllegalArgumentException(
"Not requested in ctor");
450 throw new IllegalArgumentException(
"Not requested in ctor");
452 return syncP_Mv_Mvi_Mvit;
531 final int spos = m.position();
566 final int spos = m.position();
601 final int spos = m.position();
874 public final void orthoP(
final float left,
final float right,
final float bottom,
final float top,
final float zNear,
final float zFar) {
885 public final void frustumP(
final float left,
final float right,
final float bottom,
final float top,
final float zNear,
final float zFar)
throws IllegalArgumentException {
903 public final PMVMatrix4f perspectiveP(
final float fovy_rad,
final float aspect,
final float zNear,
final float zFar)
throws IllegalArgumentException {
945 public final boolean mapWinToObj(
final float winx,
final float winy,
final float winz,
970 public boolean mapWinToObj4(
final float winx,
final float winy,
final float winz,
final float clipw,
971 final Recti viewport,
final float near,
final float far,
final Vec4f objPos) {
999 public final boolean mapWinToRay(
final float winx,
final float winy,
final float winz0,
final float winz1,
1000 final Recti viewport,
final Ray ray) {
1004 public StringBuilder
toString(StringBuilder sb,
final String f) {
1006 sb =
new StringBuilder();
1008 final boolean pmvDirty = 0 != (
PREMUL_PMV & dirtyBits);
1009 final boolean pmvUsed =
null != matPMv;
1011 final boolean pmviDirty = 0 != (
PREMUL_PMVI & dirtyBits);
1012 final boolean pmviUsed =
null != matPMvi;
1014 final boolean frustumDirty = 0 != (
FRUSTUM & dirtyBits);
1015 final boolean frustumUsed =
null != frustum;
1028 sb.append(
"PMVMatrix4f[modified[P ").append(modP).append(
", Mv ").append(modMv).append(
", T ").append(modT);
1029 sb.append(
"], dirty/used[PMv ").append(pmvDirty).append(
"/").append(pmvUsed).append(
", Pmvi ").append(pmviDirty).append(
"/").append(pmviUsed).append(
", Frustum ").append(frustumDirty).append(
"/").append(frustumUsed);
1030 sb.append(
"], dirty/req[Mvi ").append(mviDirty).append(
"/").append(mviReq).append(
", Mvit ").append(mvitDirty).append(
"/").append(mvitReq).append(
"]").append(System.lineSeparator());
1031 sb.append(
", Projection").append(System.lineSeparator());
1033 sb.append(
", Modelview").append(System.lineSeparator());
1035 sb.append(
", Texture").append(System.lineSeparator());
1037 if(
null != matPMv ) {
1038 sb.append(
", P * Mv").append(System.lineSeparator());
1042 if(
null != matPMvi ) {
1043 sb.append(
", P * Mv").append(System.lineSeparator());
1048 sb.append(
", Inverse Modelview").append(System.lineSeparator());
1053 sb.append(
", Inverse Transposed Modelview").append(System.lineSeparator());
1058 if(
null != mat4Tmp2 ) {
1061 sb.append(
", matrices "+count+
" + "+tmpCount+
" temp = "+(count+tmpCount)+
"]");
1067 return toString(
null,
"%10.5f").toString();
1085 final int r = modifiedBits;
1143 return 0 != ( requestBits & dirtyBits );
1209 if(
null == matPMv ) {
1213 dirtyBits &= ~PREMUL_PMV;
1234 if(
null == matPMvi ) {
1238 matPMviOK = matPMvi.
invert(mPMv);
1239 dirtyBits &= ~PREMUL_PMVI;
1241 return matPMviOK ? matPMvi :
null;
1258 if( 0 != ( dirtyBits &
FRUSTUM ) ) {
1259 if(
null == frustum ) {
1263 dirtyBits &= ~FRUSTUM;
1315 return updateImpl(
true);
1322 private final boolean updateImpl(
final boolean clearModBits) {
1323 boolean mod = 0 != modifiedBits;
1324 if( clearModBits ) {
1329 throw new RuntimeException(
"Invalid source Mv matrix, can't compute inverse");
1331 dirtyBits &= ~INVERSE_MODELVIEW;
1336 dirtyBits &= ~INVERSE_TRANSPOSED_MODELVIEW;
1349 private static final int mP_offset = 0*16;
1350 private static final int mMv_offset = 1*16;
1351 private final int mMvi_offset;
1352 private final int mMvit_offset;
1353 private final int mTex_offset;
1355 private final float[] matrixStore;
1359 private final SyncMatrices4f syncP_Mv, syncP_Mv_Mvi, syncP_Mv_Mvi_Mvit;
1365 private int dirtyBits = 0;
1366 private final int requestBits;
1369 private boolean matPMviOK;
1372 private abstract class PMVSyncBuffer
implements SyncMatrix4f {
1374 private final FloatBuffer fbuf;
1376 public PMVSyncBuffer(
final Matrix4f m,
final FloatBuffer fbuf) {
1382 public final Buffer getBuffer() {
return fbuf; }
1385 public final SyncBuffer sync() { getAction().
sync();
return this; }
1388 public final Buffer getSyncBuffer() { getAction().
sync();
return fbuf; }
1391 public final Matrix4f getMatrix() {
return mat; }
1394 public final FloatBuffer getSyncFloats() { getAction().
sync();
return fbuf; }
1396 private final class SyncBuffer0
extends PMVSyncBuffer {
1399 public void sync() { mat.get(matrixStore); }
1402 public SyncBuffer0(
final Matrix4f m,
final FloatBuffer fbuf) { super(m, fbuf); }
1405 public SyncAction getAction() {
return action; }
1408 private final class SyncBuffer1
extends PMVSyncBuffer {
1409 private final int offset;
1410 private final SyncAction action =
new SyncAction() {
1412 public void sync() { mat.get(matrixStore, offset); }
1415 public SyncBuffer1(
final Matrix4f m,
final FloatBuffer fbuf,
final int offset) {
1417 this.offset = offset;
1421 public SyncAction getAction() {
return action; }
1423 private final class SyncBuffer1U
extends PMVSyncBuffer {
1424 private final int offset;
1425 private final SyncAction action =
new SyncAction() {
1427 public void sync() {
1429 mat.get(matrixStore, offset);
1433 public SyncBuffer1U(
final Matrix4f m,
final FloatBuffer fbuf,
final int offset) {
1435 this.offset = offset;
1439 public SyncAction getAction() {
return action; }
1442 private abstract class PMVSyncBufferN
implements SyncMatrices4f {
1443 protected final Matrix4f[] mats;
1444 private final FloatBuffer fbuf;
1446 public PMVSyncBufferN(
final Matrix4f[] ms,
final FloatBuffer fbuf) {
1452 public final Buffer getBuffer() {
return fbuf; }
1455 public final SyncBuffer sync() {
getAction().
sync();
return this; }
1458 public final Buffer getSyncBuffer() {
getAction().
sync();
return fbuf; }
1461 public Matrix4f[] getMatrices() {
return mats; }
1464 public final FloatBuffer getSyncFloats() {
getAction().
sync();
return fbuf; }
1466 private final class SyncBufferN
extends PMVSyncBufferN {
1467 private final int offset;
1468 private final SyncAction action =
new SyncAction() {
1470 public void sync() {
1472 for(
int i=0; i<mats.length; ++i, ioff+=16) {
1473 mats[i].get(matrixStore, ioff);
1478 public SyncBufferN(
final Matrix4f[] ms,
final FloatBuffer fbuf,
final int offset) {
1480 this.offset = offset;
1484 public SyncAction getAction() {
return action; }
1486 private final class SyncBufferNU
extends PMVSyncBufferN {
1487 private final int offset;
1488 private final SyncAction action =
new SyncAction() {
1490 public void sync() {
1493 for(
int i=0; i<mats.length; ++i, ioff+=16) {
1494 mats[i].get(matrixStore, ioff);
1499 public SyncBufferNU(
final Matrix4f[] ms,
final FloatBuffer fbuf,
final int offset) {
1501 this.offset = offset;
1505 public SyncAction getAction() {
return action; }
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
final Matrix4f mul(final Matrix4f b)
Multiply matrix: [this] = [this] x [b].
static boolean mapObjToWin(final Vec3f obj, final Matrix4f mMv, final Matrix4f mP, final Recti viewport, final Vec3f winPos)
Map object coordinates to window coordinates.
Matrix4f load(final Matrix4f src)
Load the values of the given matrix src to this matrix.
final Matrix4f loadIdentity()
Set this matrix to identity.
static boolean mapWinToObj4(final float winx, final float winy, final float winz, final float clipw, final Matrix4f mMv, final Matrix4f mP, final Recti viewport, final float near, final float far, final Vec4f objPos, final Matrix4f mat4Tmp)
Map window coordinates to object coordinates.
final Vec3f mulVec3f(final Vec3f v_in, final Vec3f v_out)
Affine 3f-vector transformation by 4x4 matrix.
final Matrix4f transpose()
Transpose this matrix.
Matrix4f setToPerspective(final float fovy_rad, final float aspect, final float zNear, final float zFar)
Set this matrix to perspective frustum projection.
boolean invert()
Invert this matrix.
final Matrix4f setToRotation(final Quaternion q)
Set this matrix to rotation using the given Quaternion.
final void pop()
Pop the current matrix from it's stack, replacing this matrix values.
StringBuilder toString(final StringBuilder sb, final String rowPrefix, final String f)
final Matrix4f setToRotationAxis(final float ang_rad, float x, float y, float z)
Set this matrix to rotation from the given axis and angle in radians.
Matrix4f setToOrtho(final float left, final float right, final float bottom, final float top, final float zNear, final float zFar)
Set this matrix to orthogonal projection.
Matrix4f setToLookAt(final Vec3f eye, final Vec3f center, final Vec3f up, final Matrix4f tmp)
Set this matrix to the look-at matrix based on given parameters.
static boolean mapWinToRay(final float winx, final float winy, final float winz0, final float winz1, final Matrix4f mMv, final Matrix4f mP, final Recti viewport, final Ray ray, final Matrix4f mat4Tmp1)
Map two window coordinates w/ shared X/Y and distinctive Z to a Ray.
final void push()
Push the matrix to it's stack, while preserving this matrix values.
final Vec4f mulVec4f(final Vec4f v_in, final Vec4f v_out)
final Matrix4f setToScale(final float x, final float y, final float z)
Set this matrix to scale.
Matrix4f setToFrustum(final float left, final float right, final float bottom, final float top, final float zNear, final float zFar)
Set this matrix to frustum.
static boolean mapWinToObj(final float winx, final float winy, final float winz, final Matrix4f mMv, final Matrix4f mP, final Recti viewport, final Vec3f objPos, final Matrix4f mat4Tmp)
Map window coordinates to object coordinates.
final Matrix4f setToTranslation(final float x, final float y, final float z)
Set this matrix to translation.
Quaternion implementation supporting Gimbal-Lock free rotations.
Simple compound denoting a ray.
Rectangle with x, y, width and height integer components.
3D Vector based upon three float components.
4D Vector based upon four float components.
Providing frustum planes derived by different inputs (P*MV, ..) used to classify objects.
Frustum setFromMat(final Matrix4f pmv)
Calculate the frustum planes in world coordinates using the passed column major order matrix,...
PMVMatrix4f implements the basic computer graphics Matrix4f pack using projection (P),...
final PMVMatrix4f mulMv(final Matrix4f m)
Multiply the modelview matrix: [c] = [c] x [m].
final PMVMatrix4f translateMv(final float x, final float y, final float z)
Translate the modelview matrix.
final Matrix4f getMulPMv(final Matrix4f result)
Returns multiplication result of P and Mv matrix, i.e.
final PMVMatrix4f loadP(final Matrix4f m)
Load the projection matrix with the values of the given Matrix4f.
final int getReqBits()
Returns the request bit mask, which uses bit values equal to the dirty mask and may contain.
final PMVMatrix4f scaleMv(final float x, final float y, final float z)
Scale the modelview matrix.
final PMVMatrix4f scaleMv(final Vec3f s)
Scale the modelview matrix.
static final int MANUAL_BITS
Manual bits not covered by update() but getFrustum(), FRUSTUM, getPMv(), PREMUL_PMV,...
final SyncMatrix4f getSyncMvi()
Returns the SyncMatrix of inverse modelview matrix (Mvi) if requested.
final Vec4f mulWithMv(final Vec4f v_in, final Vec4f v_out)
v_out = Mv * v_in
final Matrix4f getMvi()
Returns the inverse modelview matrix (Mvi) if requested.
final SyncMatrix4f getSyncMvit()
Returns the SyncMatrix of inverse transposed modelview matrix (Mvit) if requested.
final PMVMatrix4f rotateMv(final float ang_rad, final float x, final float y, final float z)
Rotate the modelview matrix by the given axis and angle in radians.
final PMVMatrix4f loadTIdentity()
Load the texture matrix with the values of the given Matrix4f.
final PMVMatrix4f loadMv(final float[] values, final int offset)
Load the modelview matrix with the provided values.
final SyncMatrix4f getSyncT()
Returns the SyncMatrix of texture matrix (T).
final PMVMatrix4f loadMv(final Quaternion quat)
Load the modelview matrix with the values of the given Quaternion's rotation matrix representation.
final PMVMatrix4f loadMvIdentity()
Load the modelview matrix with the values of the given Matrix4f.
final SyncMatrix4f getSyncMv()
Returns the SyncMatrix of modelview matrix (Mv).
static final int MODIFIED_PROJECTION
Bit value stating a modified projection matrix (P), since last update() call.
final PMVMatrix4f pushP()
Push the projection matrix to its stack, while preserving its values.
final PMVMatrix4f translateP(final Vec3f t)
Translate the projection matrix.
PMVMatrix4f(final int derivedMatrices)
Creates an instance of PMVMatrix4f.
final PMVMatrix4f rotateP(final float ang_rad, final Vec3f axis)
Rotate the projection matrix by the given axis and angle in radians.
final PMVMatrix4f loadMv(final java.nio.FloatBuffer m)
Load the modelview matrix with the provided values.
static final int FRUSTUM
Bit value for frustum and updated by getFrustum().
final PMVMatrix4f loadP(final float[] values, final int offset)
Load the projection matrix with the provided values.
final Vec3f mulWithMv(final Vec3f v_in, final Vec3f v_out)
v_out = Mv * v_in
final boolean mapWinToRay(final float winx, final float winy, final float winz0, final float winz1, final Recti viewport, final Ray ray)
Map two window coordinates w/ shared X/Y and distinctive Z to a Ray.
final PMVMatrix4f mulT(final Matrix4f m)
Multiply the texture matrix: [c] = [c] x [m].
final PMVMatrix4f mulP(final Matrix4f m)
Multiply the projection matrix: [c] = [c] x [m].
void reset()
Issues Matrix4f#loadIdentity() on all matrices and resets all internal states.
final int getDirtyBits()
Returns the dirty bits due to mutable operations, i.e.
final PMVMatrix4f loadT(final Quaternion quat)
Load the texture matrix with the values of the given Quaternion's rotation matrix representation.
final int getModifiedBits(final boolean clear)
Returns the modified bits due to mutable operations.
final SyncMatrix4f getSyncP()
Returns the SyncMatrix of projection matrix (P).
final Matrix4f getT()
Returns the texture matrix (T).
final PMVMatrix4f loadT(final java.nio.FloatBuffer m)
Load the texture matrix with the provided values.
final Matrix4f getMulMvP(final Matrix4f result)
Returns multiplication result of Mv and P matrix, i.e.
final PMVMatrix4f popP()
Pop the projection matrix from its stack.
final PMVMatrix4f loadP(final java.nio.FloatBuffer m)
Load the projection matrix with the provided values.
final PMVMatrix4f rotateMv(final Quaternion quat)
Rotate the modelview matrix with the given Quaternion's rotation matrix representation.
final Matrix4f getMv()
Returns the modelview matrix (Mv).
final SyncMatrices4f getSyncPMvMvi()
Returns SyncMatrices4f of 3 matrices within one FloatBuffer: P, Mv and Mvi if requested.
StringBuilder toString(StringBuilder sb, final String f)
final PMVMatrix4f loadP(final Quaternion quat)
Load the projection matrix with the values of the given Quaternion's rotation matrix representation.
static final int MODIFIED_ALL
Bit value stating all is modified.
static final int MODIFIED_MODELVIEW
Bit value stating a modified modelview matrix (Mv), since last update() call.
final PMVMatrix4f translateP(final float x, final float y, final float z)
Translate the projection matrix.
final PMVMatrix4f lookAtP(final Vec3f eye, final Vec3f center, final Vec3f up)
Multiply the projection matrix with the eye, object and orientation, i.e.
final PMVMatrix4f perspectiveP(final float fovy_rad, final float aspect, final float zNear, final float zFar)
Multiply the projection matrix with the perspective/frustum matrix.
final PMVMatrix4f rotateMv(final float ang_rad, final Vec3f axis)
Rotate the modelview matrix by the given axis and angle in radians.
static final int INVERSE_TRANSPOSED_MODELVIEW
Bit value for inverse transposed modelview matrix (Mvit), updated via update().
final Matrix4f getPMv()
Returns the pre-multiplied projection x modelview, P x Mv.
final PMVMatrix4f loadT(final Matrix4f m)
Load the texture matrix with the values of the given Matrix4f.
final PMVMatrix4f rotateP(final Quaternion quat)
Rotate the projection matrix with the given Quaternion's rotation matrix representation.
final Vec4f mulWithMv(final Vec4f v_inout)
v_inout = Mv * v_inout
final void orthoP(final float left, final float right, final float bottom, final float top, final float zNear, final float zFar)
Multiply the projection matrix with the orthogonal matrix.
final PMVMatrix4f popT()
Pop the texture matrix from its stack.
boolean mapWinToObj4(final float winx, final float winy, final float winz, final float clipw, final Recti viewport, final float near, final float far, final Vec4f objPos)
Map window coordinates to object coordinates.
final PMVMatrix4f scaleP(final float x, final float y, final float z)
Scale the projection matrix.
static final int INVERSE_MODELVIEW
Bit value for inverse modelview matrix (Mvi), updated via update().
PMVMatrix4f()
Creates an instance of PMVMatrix4f.
final boolean update()
Update the derived inverse modelview (Mvi), inverse transposed modelview (Mvit) matrices if they are ...
final SyncMatrices4f getSyncPMv()
Returns SyncMatrices4f of 2 matrices within one FloatBuffer: P and Mv.
final PMVMatrix4f scaleP(final Vec3f s)
Scale the projection matrix.
final PMVMatrix4f loadPIdentity()
Load the projection matrix with the values of the given Matrix4f.
final Frustum getFrustum()
Returns the frustum, derived from projection x modelview.
final Matrix4f getP()
Returns the projection matrix (P).
final Matrix4f getTmp2Mat()
Return the second temporary Matrix4f exposed to be reused for efficiency.
static final int MODIFIED_TEXTURE
Bit value stating a modified texture matrix (T), since last update() call.
final boolean mapObjToWin(final Vec3f objPos, final Recti viewport, final Vec3f winPos)
Map object coordinates to window coordinates.
final PMVMatrix4f popMv()
Pop the modelview matrix from its stack.
final PMVMatrix4f loadT(final float[] values, final int offset)
Load the texture matrix with the provided values.
final boolean mapWinToObj(final float winx, final float winy, final float winz, final Recti viewport, final Vec3f objPos)
Map window coordinates to object coordinates.
static final int PREMUL_PMVI
Bit value for pre-multiplied invert(P * Mv), updated by getPMvi().
final Matrix4f getPMvi()
Returns the pre-multiplied inverse projection x modelview, if Matrix4f#invert(Matrix4f) succeeded,...
final void setModelviewDirty()
Sets the Modelview (Mv) matrix dirty and modified, i.e.
final PMVMatrix4f loadMv(final Matrix4f m)
Load the modelview matrix with the values of the given Matrix4f.
final void setProjectionDirty()
Sets the Projection (P) matrix dirty and modified, i.e.
final boolean isReqDirty()
Returns true if the one of the requested bits are are set dirty due to mutable operations,...
final PMVMatrix4f pushMv()
Push the modelview matrix to its stack, while preserving its values.
final PMVMatrix4f rotateP(final float ang_rad, final float x, final float y, final float z)
Rotate the projection matrix by the given axis and angle in radians.
final PMVMatrix4f pushT()
Push the texture matrix to its stack, while preserving its values.
final PMVMatrix4f translateMv(final Vec3f t)
Translate the modelview matrix.
static final int PREMUL_PMV
Bit value for pre-multiplied P * Mv, updated by getPMv().
final Matrix4f getMvit()
Returns the inverse transposed modelview matrix (Mvit) if requested.
final void frustumP(final float left, final float right, final float bottom, final float top, final float zNear, final float zFar)
Multiply the projection matrix with the frustum matrix.
final SyncMatrices4f getSyncPMvMviMvit()
Returns SyncMatrices4f of 4 matrices within one FloatBuffer: P, Mv, Mvi and Mvit if requested.
final void setTextureDirty()
Sets the Texture (T) matrix modified.
Specific data synchronization action implemented by the data provider to update the buffer with the u...
void sync()
Synchronizes the buffer with the underlying data before usage.
Convenient tuple of a SyncAction and Buffer.
SyncAction getAction()
Return the SyncAction.
SyncBuffer with a multiple underlying Matrix4f, used in SyncMatrices4f16 and PMVMatrix4f
SyncBuffer interface with a single underlying Matrix4f, used in SyncMatrix4f16 and PMVMatrix4f.