29package com.jogamp.math;
31import java.nio.FloatBuffer;
33import com.jogamp.math.geom.AABBox;
34import com.jogamp.math.geom.Frustum;
35import com.jogamp.math.geom.Frustum.Plane;
95 m00 = m11 = m22 = m33 = 1.0f;
119 public Matrix4f(
final float[] m,
final int m_off) {
136 public void set(
final int i,
final float v) {
138 case 0+4*0: m00 = v;
break;
139 case 1+4*0: m10 = v;
break;
140 case 2+4*0: m20 = v;
break;
141 case 3+4*0: m30 = v;
break;
143 case 0+4*1: m01 = v;
break;
144 case 1+4*1: m11 = v;
break;
145 case 2+4*1: m21 = v;
break;
146 case 3+4*1: m31 = v;
break;
148 case 0+4*2: m02 = v;
break;
149 case 1+4*2: m12 = v;
break;
150 case 2+4*2: m22 = v;
break;
151 case 3+4*2: m32 = v;
break;
153 case 0+4*3: m03 = v;
break;
154 case 1+4*3: m13 = v;
break;
155 case 2+4*3: m23 = v;
break;
156 case 3+4*3: m33 = v;
break;
157 default:
throw new IndexOutOfBoundsException();
173 m00 = m11 = m22 = m33 = 1.0f;
177 m30 = m31 = m32 = 0.0f;
187 m00 = src.m00; m10 = src.m10; m20 = src.m20; m30 = src.m30;
188 m01 = src.m01; m11 = src.m11; m21 = src.m21; m31 = src.m31;
189 m02 = src.m02; m12 = src.m12; m22 = src.m22; m32 = src.m32;
190 m03 = src.m03; m13 = src.m13; m23 = src.m23; m33 = src.m33;
226 m00 = src[src_off+0+0*4];
227 m10 = src[src_off+1+0*4];
228 m20 = src[src_off+2+0*4];
229 m30 = src[src_off+3+0*4];
230 m01 = src[src_off+0+1*4];
231 m11 = src[src_off+1+1*4];
232 m21 = src[src_off+2+1*4];
233 m31 = src[src_off+3+1*4];
234 m02 = src[src_off+0+2*4];
235 m12 = src[src_off+1+2*4];
236 m22 = src[src_off+2+2*4];
237 m32 = src[src_off+3+2*4];
238 m03 = src[src_off+0+3*4];
239 m13 = src[src_off+1+3*4];
240 m23 = src[src_off+2+3*4];
241 m33 = src[src_off+3+3*4];
279 public float get(
final int i) {
281 case 0+4*0:
return m00;
282 case 1+4*0:
return m10;
283 case 2+4*0:
return m20;
284 case 3+4*0:
return m30;
286 case 0+4*1:
return m01;
287 case 1+4*1:
return m11;
288 case 2+4*1:
return m21;
289 case 3+4*1:
return m31;
291 case 0+4*2:
return m02;
292 case 1+4*2:
return m12;
293 case 2+4*2:
return m22;
294 case 3+4*2:
return m32;
296 case 0+4*3:
return m03;
297 case 1+4*3:
return m13;
298 case 2+4*3:
return m23;
299 case 3+4*3:
return m33;
301 default:
throw new IndexOutOfBoundsException();
312 v_out.
set(
get(0+column*4),
326 v_out.
set(
get(0+column*4),
339 v_out.
set(
get(row+0*4),
353 v_out.
set(
get(row+0*4),
366 public float[]
get(
final float[] dst,
final int dst_off) {
367 dst[dst_off+0+0*4] = m00;
368 dst[dst_off+1+0*4] = m10;
369 dst[dst_off+2+0*4] = m20;
370 dst[dst_off+3+0*4] = m30;
371 dst[dst_off+0+1*4] = m01;
372 dst[dst_off+1+1*4] = m11;
373 dst[dst_off+2+1*4] = m21;
374 dst[dst_off+3+1*4] = m31;
375 dst[dst_off+0+2*4] = m02;
376 dst[dst_off+1+2*4] = m12;
377 dst[dst_off+2+2*4] = m22;
378 dst[dst_off+3+2*4] = m32;
379 dst[dst_off+0+3*4] = m03;
380 dst[dst_off+1+3*4] = m13;
381 dst[dst_off+2+3*4] = m23;
382 dst[dst_off+3+3*4] = m33;
392 public float[]
get(
final float[] dst) {
422 public FloatBuffer
get(
final FloatBuffer dst) {
452 ret += m00 * ( + m11*(m22*m33 - m23*m32) - m12*(m21*m33 - m23*m31) + m13*(m21*m32 - m22*m31));
453 ret -= m01 * ( + m10*(m22*m33 - m23*m32) - m12*(m20*m33 - m23*m30) + m13*(m20*m32 - m22*m30));
454 ret += m02 * ( + m10*(m21*m33 - m23*m31) - m11*(m20*m33 - m23*m30) + m13*(m20*m31 - m21*m30));
455 ret -= m03 * ( + m10*(m21*m32 - m22*m31) - m11*(m20*m32 - m22*m30) + m12*(m20*m31 - m21*m30));
534 }
catch(
final ArithmeticException aex) {
537 final float a00 = m00*
scale;
538 final float a10 = m10*
scale;
539 final float a20 = m20*
scale;
540 final float a30 = m30*
scale;
542 final float a01 = m01*
scale;
543 final float a11 = m11*
scale;
544 final float a21 = m21*
scale;
545 final float a31 = m31*
scale;
547 final float a02 = m02*
scale;
548 final float a12 = m12*
scale;
549 final float a22 = m22*
scale;
550 final float a32 = m32*
scale;
552 final float a03 = m03*
scale;
553 final float a13 = m13*
scale;
554 final float a23 = m23*
scale;
555 final float a33 = m33*
scale;
557 final float b00 = + a11*(a22*a33 - a23*a32) - a12*(a21*a33 - a23*a31) + a13*(a21*a32 - a22*a31);
558 final float b01 = -( + a10*(a22*a33 - a23*a32) - a12*(a20*a33 - a23*a30) + a13*(a20*a32 - a22*a30));
559 final float b02 = + a10*(a21*a33 - a23*a31) - a11*(a20*a33 - a23*a30) + a13*(a20*a31 - a21*a30);
560 final float b03 = -( + a10*(a21*a32 - a22*a31) - a11*(a20*a32 - a22*a30) + a12*(a20*a31 - a21*a30));
562 final float b10 = -( + a01*(a22*a33 - a23*a32) - a02*(a21*a33 - a23*a31) + a03*(a21*a32 - a22*a31));
563 final float b11 = + a00*(a22*a33 - a23*a32) - a02*(a20*a33 - a23*a30) + a03*(a20*a32 - a22*a30);
564 final float b12 = -( + a00*(a21*a33 - a23*a31) - a01*(a20*a33 - a23*a30) + a03*(a20*a31 - a21*a30));
565 final float b13 = + a00*(a21*a32 - a22*a31) - a01*(a20*a32 - a22*a30) + a02*(a20*a31 - a21*a30);
567 final float b20 = + a01*(a12*a33 - a13*a32) - a02*(a11*a33 - a13*a31) + a03*(a11*a32 - a12*a31);
568 final float b21 = -( + a00*(a12*a33 - a13*a32) - a02*(a10*a33 - a13*a30) + a03*(a10*a32 - a12*a30));
569 final float b22 = + a00*(a11*a33 - a13*a31) - a01*(a10*a33 - a13*a30) + a03*(a10*a31 - a11*a30);
570 final float b23 = -( + a00*(a11*a32 - a12*a31) - a01*(a10*a32 - a12*a30) + a02*(a10*a31 - a11*a30));
572 final float b30 = -( + a01*(a12*a23 - a13*a22) - a02*(a11*a23 - a13*a21) + a03*(a11*a22 - a12*a21));
573 final float b31 = + a00*(a12*a23 - a13*a22) - a02*(a10*a23 - a13*a20) + a03*(a10*a22 - a12*a20);
574 final float b32 = -( + a00*(a11*a23 - a13*a21) - a01*(a10*a23 - a13*a20) + a03*(a10*a21 - a11*a20));
575 final float b33 = + a00*(a11*a22 - a12*a21) - a01*(a10*a22 - a12*a20) + a02*(a10*a21 - a11*a20);
577 final float det = (a00*b00 + a01*b01 + a02*b02 + a03*b03) /
scale;
581 final float invdet = 1.0f / det;
613 scale = src.mulScale();
614 }
catch(
final ArithmeticException aex) {
617 final float a00 = src.m00*
scale;
618 final float a10 = src.m10*
scale;
619 final float a20 = src.m20*
scale;
620 final float a30 = src.m30*
scale;
622 final float a01 = src.m01*
scale;
623 final float a11 = src.m11*
scale;
624 final float a21 = src.m21*
scale;
625 final float a31 = src.m31*
scale;
627 final float a02 = src.m02*
scale;
628 final float a12 = src.m12*
scale;
629 final float a22 = src.m22*
scale;
630 final float a32 = src.m32*
scale;
632 final float a03 = src.m03*
scale;
633 final float a13 = src.m13*
scale;
634 final float a23 = src.m23*
scale;
635 final float a33 = src.m33*
scale;
637 final float b00 = + a11*(a22*a33 - a23*a32) - a12*(a21*a33 - a23*a31) + a13*(a21*a32 - a22*a31);
638 final float b01 = -( + a10*(a22*a33 - a23*a32) - a12*(a20*a33 - a23*a30) + a13*(a20*a32 - a22*a30));
639 final float b02 = + a10*(a21*a33 - a23*a31) - a11*(a20*a33 - a23*a30) + a13*(a20*a31 - a21*a30);
640 final float b03 = -( + a10*(a21*a32 - a22*a31) - a11*(a20*a32 - a22*a30) + a12*(a20*a31 - a21*a30));
642 final float b10 = -( + a01*(a22*a33 - a23*a32) - a02*(a21*a33 - a23*a31) + a03*(a21*a32 - a22*a31));
643 final float b11 = + a00*(a22*a33 - a23*a32) - a02*(a20*a33 - a23*a30) + a03*(a20*a32 - a22*a30);
644 final float b12 = -( + a00*(a21*a33 - a23*a31) - a01*(a20*a33 - a23*a30) + a03*(a20*a31 - a21*a30));
645 final float b13 = + a00*(a21*a32 - a22*a31) - a01*(a20*a32 - a22*a30) + a02*(a20*a31 - a21*a30);
647 final float b20 = + a01*(a12*a33 - a13*a32) - a02*(a11*a33 - a13*a31) + a03*(a11*a32 - a12*a31);
648 final float b21 = -( + a00*(a12*a33 - a13*a32) - a02*(a10*a33 - a13*a30) + a03*(a10*a32 - a12*a30));
649 final float b22 = + a00*(a11*a33 - a13*a31) - a01*(a10*a33 - a13*a30) + a03*(a10*a31 - a11*a30);
650 final float b23 = -( + a00*(a11*a32 - a12*a31) - a01*(a10*a32 - a12*a30) + a02*(a10*a31 - a11*a30));
652 final float b30 = -( + a01*(a12*a23 - a13*a22) - a02*(a11*a23 - a13*a21) + a03*(a11*a22 - a12*a21));
653 final float b31 = + a00*(a12*a23 - a13*a22) - a02*(a10*a23 - a13*a20) + a03*(a10*a22 - a12*a20);
654 final float b32 = -( + a00*(a11*a23 - a13*a21) - a01*(a10*a23 - a13*a20) + a03*(a10*a21 - a11*a20));
655 final float b33 = + a00*(a11*a22 - a12*a21) - a01*(a10*a22 - a12*a20) + a02*(a10*a21 - a11*a20);
657 final float det = (a00*b00 + a01*b01 + a02*b02 + a03*b03) /
scale;
662 final float invdet = 1.0f / det;
686 private final float mulScale() {
696 float a = Math.abs(m00);
698 a = Math.abs(m01);
if( a > max ) max = a;
699 a = Math.abs(m02);
if( a > max ) max = a;
700 a = Math.abs(m03);
if( a > max ) max = a;
702 a = Math.abs(m10);
if( a > max ) max = a;
703 a = Math.abs(m11);
if( a > max ) max = a;
704 a = Math.abs(m12);
if( a > max ) max = a;
705 a = Math.abs(m13);
if( a > max ) max = a;
707 a = Math.abs(m20);
if( a > max ) max = a;
708 a = Math.abs(m21);
if( a > max ) max = a;
709 a = Math.abs(m22);
if( a > max ) max = a;
710 a = Math.abs(m23);
if( a > max ) max = a;
712 a = Math.abs(m30);
if( a > max ) max = a;
713 a = Math.abs(m31);
if( a > max ) max = a;
714 a = Math.abs(m32);
if( a > max ) max = a;
715 a = Math.abs(m33);
if( a > max ) max = a;
732 m00 = ai0 * b.m00 + ai1 * b.m10 + ai2 * b.m20 + ai3 * b.m30 ;
733 m01 = ai0 * b.m01 + ai1 * b.m11 + ai2 * b.m21 + ai3 * b.m31 ;
734 m02 = ai0 * b.m02 + ai1 * b.m12 + ai2 * b.m22 + ai3 * b.m32 ;
735 m03 = ai0 * b.m03 + ai1 * b.m13 + ai2 * b.m23 + ai3 * b.m33 ;
741 m10 = ai0 * b.m00 + ai1 * b.m10 + ai2 * b.m20 + ai3 * b.m30 ;
742 m11 = ai0 * b.m01 + ai1 * b.m11 + ai2 * b.m21 + ai3 * b.m31 ;
743 m12 = ai0 * b.m02 + ai1 * b.m12 + ai2 * b.m22 + ai3 * b.m32 ;
744 m13 = ai0 * b.m03 + ai1 * b.m13 + ai2 * b.m23 + ai3 * b.m33 ;
750 m20 = ai0 * b.m00 + ai1 * b.m10 + ai2 * b.m20 + ai3 * b.m30 ;
751 m21 = ai0 * b.m01 + ai1 * b.m11 + ai2 * b.m21 + ai3 * b.m31 ;
752 m22 = ai0 * b.m02 + ai1 * b.m12 + ai2 * b.m22 + ai3 * b.m32 ;
753 m23 = ai0 * b.m03 + ai1 * b.m13 + ai2 * b.m23 + ai3 * b.m33 ;
759 m30 = ai0 * b.m00 + ai1 * b.m10 + ai2 * b.m20 + ai3 * b.m30 ;
760 m31 = ai0 * b.m01 + ai1 * b.m11 + ai2 * b.m21 + ai3 * b.m31 ;
761 m32 = ai0 * b.m02 + ai1 * b.m12 + ai2 * b.m22 + ai3 * b.m32 ;
762 m33 = ai0 * b.m03 + ai1 * b.m13 + ai2 * b.m23 + ai3 * b.m33 ;
775 m00 = a.m00 * b.m00 + a.m01 * b.m10 + a.m02 * b.m20 + a.m03 * b.m30 ;
776 m01 = a.m00 * b.m01 + a.m01 * b.m11 + a.m02 * b.m21 + a.m03 * b.m31 ;
777 m02 = a.m00 * b.m02 + a.m01 * b.m12 + a.m02 * b.m22 + a.m03 * b.m32 ;
778 m03 = a.m00 * b.m03 + a.m01 * b.m13 + a.m02 * b.m23 + a.m03 * b.m33 ;
781 m10 = a.m10 * b.m00 + a.m11 * b.m10 + a.m12 * b.m20 + a.m13 * b.m30 ;
782 m11 = a.m10 * b.m01 + a.m11 * b.m11 + a.m12 * b.m21 + a.m13 * b.m31 ;
783 m12 = a.m10 * b.m02 + a.m11 * b.m12 + a.m12 * b.m22 + a.m13 * b.m32 ;
784 m13 = a.m10 * b.m03 + a.m11 * b.m13 + a.m12 * b.m23 + a.m13 * b.m33 ;
787 m20 = a.m20 * b.m00 + a.m21 * b.m10 + a.m22 * b.m20 + a.m23 * b.m30 ;
788 m21 = a.m20 * b.m01 + a.m21 * b.m11 + a.m22 * b.m21 + a.m23 * b.m31 ;
789 m22 = a.m20 * b.m02 + a.m21 * b.m12 + a.m22 * b.m22 + a.m23 * b.m32 ;
790 m23 = a.m20 * b.m03 + a.m21 * b.m13 + a.m22 * b.m23 + a.m23 * b.m33 ;
793 m30 = a.m30 * b.m00 + a.m31 * b.m10 + a.m32 * b.m20 + a.m33 * b.m30 ;
794 m31 = a.m30 * b.m01 + a.m31 * b.m11 + a.m32 * b.m21 + a.m33 * b.m31 ;
795 m32 = a.m30 * b.m02 + a.m31 * b.m12 + a.m32 * b.m22 + a.m33 * b.m32 ;
796 m33 = a.m30 * b.m03 + a.m31 * b.m13 + a.m32 * b.m23 + a.m33 * b.m33 ;
808 final float x = v_in.x(), y = v_in.y(), z = v_in.z(), w = v_in.w();
809 v_out.
set( x * m00 + y * m01 + z * m02 + w * m03,
810 x * m10 + y * m11 + z * m12 + w * m13,
811 x * m20 + y * m21 + z * m22 + w * m23,
812 x * m30 + y * m31 + z * m32 + w * m33 );
822 final float x = v_inout.x(), y = v_inout.y(), z = v_inout.z(), w = v_inout.w();
823 v_inout.
set( x * m00 + y * m01 + z * m02 + w * m03,
824 x * m10 + y * m11 + z * m12 + w * m13,
825 x * m20 + y * m21 + z * m22 + w * m23,
826 x * m30 + y * m31 + z * m32 + w * m33 );
843 final float x = v_in.x(), y = v_in.y(), z = v_in.z();
844 v_out.
set( x * m00 + y * m01 + z * m02 + 1f * m03,
845 x * m10 + y * m11 + z * m12 + 1f * m13,
846 x * m20 + y * m21 + z * m22 + 1f * m23 );
862 final float x = v_inout.x(), y = v_inout.y(), z = v_inout.z();
863 v_inout.
set( x * m00 + y * m01 + z * m02 + 1f * m03,
864 x * m10 + y * m11 + z * m12 + 1f * m13,
865 x * m20 + y * m21 + z * m22 + 1f * m23 );
888 m00 = m11 = m22 = m33 = 1.0f;
895 m30 = m31 = m32 = 0.0f;
937 m30 = m31 = m32 = 0.0f;
975 final float ic= 1.0f - c;
979 x = tmp.x(); y = tmp.y(); z = tmp.z();
981 final float xy = x*y;
982 final float xz = x*z;
983 final float xs = x*s;
984 final float ys = y*s;
985 final float yz = y*z;
986 final float zs = z*s;
1065 m01 = sh*sb - ch*sa*cb;
1067 m21 = sh*sa*cb + ch*sb;
1070 m02 = ch*sa*sb + sh*cb;
1072 m22 = -sh*sa*sb + ch*cb;
1136 srecip = 2.0f / norm;
1139 final float x = q.x();
1140 final float y = q.y();
1141 final float z = q.z();
1142 final float w = q.w();
1144 final float xs = srecip * x;
1145 final float ys = srecip * y;
1146 final float zs = srecip * z;
1148 final float xx = x * xs;
1149 final float xy = x * ys;
1150 final float xz = x * zs;
1151 final float xw = xs * w;
1152 final float yy = y * ys;
1153 final float yz = y * zs;
1154 final float yw = ys * w;
1155 final float zz = z * zs;
1156 final float zw = zs * w;
1158 m00 = 1f - ( yy + zz );
1164 m11 = 1f - ( xx + zz );
1170 m22 = 1f - ( xx + yy );
1173 m30 = m31 = m32 = 0f;
1186 return res.
setFromMat(m00, m01, m02, m10, m11, m12, m20, m21, m22);
1207 final float bottom,
final float top,
1208 final float zNear,
final float zFar) {
1211 m10 = m20 = m30 = 0f;
1212 m01 = m21 = m31 = 0f;
1213 m02 = m12 = m32 = 0f;
1216 final float dx=right-left;
1217 final float dy=top-bottom;
1218 final float dz=zFar-zNear;
1219 final float tx=-1.0f*(right+left)/dx;
1220 final float ty=-1.0f*(top+bottom)/dy;
1221 final float tz=-1.0f*(zFar+zNear)/dz;
1255 final float bottom,
final float top,
1256 final float zNear,
final float zFar)
throws IllegalArgumentException {
1257 if( zNear <= 0.0f || zFar <= zNear ) {
1258 throw new IllegalArgumentException(
"Requirements zNear > 0 and zFar > zNear, but zNear "+zNear+
", zFar "+zFar);
1260 if( left == right || top == bottom) {
1261 throw new IllegalArgumentException(
"GL_INVALID_VALUE: top,bottom and left,right must not be equal");
1265 m10 = m20 = m30 = 0f;
1266 m01 = m21 = m31 = 0f;
1269 final float zNear2 = 2.0f*zNear;
1270 final float dx=right-left;
1271 final float dy=top-bottom;
1272 final float dz=zFar-zNear;
1273 final float A=(right+left)/dx;
1274 final float B=(top+bottom)/dy;
1275 final float C=-1.0f*(zFar+zNear)/dz;
1276 final float D=-2.0f*(zFar*zNear)/dz;
1303 public Matrix4f setToPerspective(
final float fovy_rad,
final float aspect,
final float zNear,
final float zFar)
throws IllegalArgumentException {
1305 final float bottom = -1.0f * top;
1306 final float left = aspect * bottom;
1307 final float right = aspect * top;
1308 return setToFrustum(left, right, bottom, top, zNear, zFar);
1324 final float top = fovhvTan.top * zNear;
1325 final float bottom = -1.0f * fovhvTan.bottom * zNear;
1326 final float left = -1.0f * fovhvTan.left * zNear;
1327 final float right = fovhvTan.right * zNear;
1328 return setToFrustum(left, right, bottom, top, zNear, zFar);
1402 for (
int i = 0; i < 6; ++i) {
1405 final float invLen = 1f / p_n.
length();
1434 final Vec3f fwd =
new Vec3f( center.x() - eye.x(),
1435 center.y() - eye.y(),
1500 if (deltaX <= 0 || deltaY <= 0) {
1504 setToTranslation( ( viewport.width() - 2 * ( x - viewport.x() ) ) / deltaX,
1505 ( viewport.height() - 2 * ( y - viewport.y() ) ) / deltaY,
1507 mat4Tmp.
setToScale( viewport.width() / deltaX, viewport.height() / deltaY, 1.0f );
1508 return mul(mat4Tmp);
1708 if (rawWinPos.w() == 0.0f) {
1712 final float s = ( 1.0f / rawWinPos.w() ) * 0.5f;
1715 rawWinPos.
scale(s).
add(0.5f, 0.5f, 0.5f, 0f);
1718 winPos.
set( rawWinPos.x() * viewport.width() + viewport.x(),
1719 rawWinPos.y() * viewport.height() + viewport.y(),
1745 if (rawWinPos.w() == 0.0f) {
1749 final float s = ( 1.0f / rawWinPos.w() ) * 0.5f;
1752 rawWinPos.
scale(s).
add(0.5f, 0.5f, 0.5f, 0f);
1755 winPos.
set( rawWinPos.x() * viewport.width() + viewport.x(),
1756 rawWinPos.y() * viewport.height() + viewport.y(),
1778 public static boolean mapWinToObj(
final float winx,
final float winy,
final float winz,
1780 final Recti viewport,
1790 final Vec4f winPos =
new Vec4f(winx, winy, winz, 1f);
1793 winPos.
add(-viewport.x(), -viewport.y(), 0f, 0f).
mul(1f/viewport.width(), 1f/viewport.height(), 1f, 1f);
1796 winPos.
mul(2f, 2f, 2f, 1f).
add(-1f, -1f, -1f, 0f);
1801 if ( rawObjPos.w() == 0.0f ) {
1804 objPos.
set( rawObjPos.
scale( 1f / rawObjPos.w() ) );
1823 public static boolean mapWinToObj(
final float winx,
final float winy,
final float winz,
1825 final Recti viewport,
1828 if(
null == invPMv ) {
1831 final Vec4f winPos =
new Vec4f(winx, winy, winz, 1f);
1834 winPos.
add(-viewport.x(), -viewport.y(), 0f, 0f).
mul(1f/viewport.width(), 1f/viewport.height(), 1f, 1f);
1837 winPos.
mul(2f, 2f, 2f, 1f).
add(-1f, -1f, -1f, 0f);
1842 if ( rawObjPos.w() == 0.0f ) {
1845 objPos.
set( rawObjPos.
scale( 1f / rawObjPos.w() ) );
1866 public static boolean mapWinToObj(
final float winx,
final float winy,
final float winz1,
final float winz2,
1868 final Recti viewport,
1871 if(
null == invPMv ) {
1874 final Vec4f winPos =
new Vec4f(winx, winy, winz1, 1f);
1877 winPos.
add(-viewport.x(), -viewport.y(), 0f, 0f).
mul(1f/viewport.width(), 1f/viewport.height(), 1f, 1f);
1880 winPos.
mul(2f, 2f, 2f, 1f).
add(-1f, -1f, -1f, 0f);
1885 if ( rawObjPos.w() == 0.0f ) {
1888 objPos1.
set( rawObjPos.
scale( 1f / rawObjPos.w() ) );
1894 winPos.
setZ( winz2 * 2f - 1f );
1897 invPMv.
mulVec4f(winPos, rawObjPos);
1899 if ( rawObjPos.w() == 0.0f ) {
1902 objPos2.
set( rawObjPos.
scale( 1f / rawObjPos.w() ) );
1926 public static boolean mapWinToObj4(
final float winx,
final float winy,
final float winz,
final float clipw,
1928 final Recti viewport,
1929 final float near,
final float far,
1939 final Vec4f winPos =
new Vec4f(winx, winy, winz, clipw);
1942 winPos.
add(-viewport.x(), -viewport.y(), -near, 0f).
mul(1f/viewport.width(), 1f/viewport.height(), 1f/(far-near), 1f);
1945 winPos.
mul(2f, 2f, 2f, 1f).
add(-1f, -1f, -1f, 0f);
1950 if ( objPos.w() == 0.0f ) {
1973 public static boolean mapWinToObj4(
final float winx,
final float winy,
final float winz,
final float clipw,
1975 final Recti viewport,
1976 final float near,
final float far,
1979 if(
null == invPMv ) {
1982 final Vec4f winPos =
new Vec4f(winx, winy, winz, clipw);
1985 winPos.
add(-viewport.x(), -viewport.y(), -near, 0f).
mul(1f/viewport.width(), 1f/viewport.height(), 1f/(far-near), 1f);
1988 winPos.
mul(2f, 2f, 2f, 1f).
add(-1f, -1f, -1f, 0f);
1993 if ( objPos.w() == 0.0f ) {
2023 public static boolean mapWinToRay(
final float winx,
final float winy,
final float winz0,
final float winz1,
2025 final Recti viewport,
2063 public static boolean mapWinToRay(
final float winx,
final float winy,
final float winz0,
final float winz1,
2065 final Recti viewport,
2085 public StringBuilder
toString(
final StringBuilder sb,
final String rowPrefix,
final String f) {
2086 final float[] tmp =
new float[16];
2093 return toString(
null,
null,
"%10.5f").toString();
2096 private float m00, m10, m20, m30;
2097 private float m01, m11, m21, m31;
2098 private float m02, m12, m22, m32;
2099 private float m03, m13, m23, m33;
2101 final Stack stack =
new Stack(0, 16*16);
2103 private static class Stack {
2104 private int position;
2105 private float[] buffer;
2106 private final int growSize;
2113 public Stack(
final int initialSize,
final int growSize) {
2115 this.growSize = growSize;
2116 this.buffer =
new float[initialSize];
2119 private final void growIfNecessary(
final int length)
throws IndexOutOfBoundsException {
2120 if( position + length > buffer.length ) {
2121 if( 0 >= growSize ) {
2122 throw new IndexOutOfBoundsException(
"Out of fixed stack size: "+
this);
2124 final float[] newBuffer =
new float[buffer.length + Math.max(length, growSize)];
2125 System.arraycopy(buffer, 0, newBuffer, 0, position);
2131 growIfNecessary(16);
2132 src.get(buffer, position);
2139 dest.load(buffer, position);
Basic Float math utility functions.
static float sin(final float a)
static final float EPSILON
Epsilon for floating point {@value}, as once computed via getMachineEpsilon() on an AMD-64 CPU.
static float cos(final float a)
static float tan(final float a)
static StringBuilder matrixToString(StringBuilder sb, final String rowPrefix, final String f, final FloatBuffer a, final int aOffset, final int rows, final int columns, final boolean rowMajorOrder)
static boolean isZero(final float a, final float epsilon)
Returns true if value is zero, i.e.
static boolean isEqual(final float a, final float b, final float epsilon)
Returns true if both values are equal, i.e.
Horizontal and vertical field of view (FOV) halves, allowing a non-centered projection.
final FovHVHalves toTangents()
Returns this instance in tangent values.
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
boolean isEqual(final Matrix4f o, final float epsilon)
Equals check using a given FloatUtil#EPSILON value and FloatUtil#isEqual(float, float,...
final Matrix4f mul(final Matrix4f b)
Multiply matrix: [this] = [this] x [b].
boolean invert(final Matrix4f src)
Invert the src matrix values into this matrix.
static boolean mapObjToWin(final Vec3f obj, final Matrix4f mMv, final Matrix4f mP, final Recti viewport, final Vec3f winPos)
Map object coordinates to window coordinates.
final Matrix4f rotate(final Quaternion quat, final Matrix4f tmp)
Rotate this matrix with the given Quaternion, i.e.
Matrix4f load(final Matrix4f src)
Load the values of the given matrix src to this matrix.
final Matrix4f loadIdentity()
Set this matrix to identity.
Vec3f getRow(final int row, final Vec3f v_out)
Get the named row of the given column-major matrix to v_out.
Matrix4f setToPerspective(final FovHVHalves fovhv, final float zNear, final float zFar)
Set this matrix to perspective frustum projection.
Matrix4f()
Creates a new identity matrix.
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 mul(final Matrix4f a, final Matrix4f b)
Multiply matrix: [this] = [a] x [b].
final Matrix4f rotate(final float ang_rad, final float x, final float y, final float z, final Matrix4f tmp)
Rotate this matrix about give axis and angle in radians, i.e.
static boolean mapWinToObj(final float winx, final float winy, final float winz, final Matrix4f invPMv, final Recti viewport, final Vec3f objPos)
Map window coordinates to object coordinates.
final Matrix4f scale(final float s, final Matrix4f tmp)
Scale this matrix, i.e.
Frustum getFrustum(final Frustum frustum)
Calculate the frustum planes in world coordinates using this column major order matrix,...
final Matrix4f scale(final float x, final float y, final float z, final Matrix4f tmp)
Scale this matrix, i.e.
static boolean mapWinToRay(final float winx, final float winy, final float winz0, final float winz1, final Matrix4f invPMv, final Recti viewport, final Ray ray)
Map two window coordinates w/ shared X/Y and distinctive Z to a Ray.
Vec4f getRow(final int row, final Vec4f v_out)
Get the named row of the given column-major matrix to v_out.
Matrix4f(final FloatBuffer m)
Creates a new matrix based on given FloatBuffer 4x4 column major order.
final Matrix4f translate(final float x, final float y, final float z, final Matrix4f tmp)
Translate this matrix, i.e.
static boolean mapWinToObj(final float winx, final float winy, final float winz1, final float winz2, final Matrix4f invPMv, final Recti viewport, final Vec3f objPos1, final Vec3f objPos2)
Map two window coordinates to two object coordinates, distinguished by their z component.
static boolean mapWinToObj4(final float winx, final float winy, final float winz, final float clipw, final Matrix4f invPMv, final Recti viewport, final float near, final float far, final Vec4f objPos)
Map window coordinates to object coordinates.
final Matrix4f transpose()
Transpose this matrix.
Matrix4f setToRotationEuler(final float bankX, final float headingY, final float attitudeZ)
Set this matrix to rotation from the given Euler rotation angles in radians.
Matrix4f setToPerspective(final float fovy_rad, final float aspect, final float zNear, final float zFar)
Set this matrix to perspective frustum projection.
Matrix4f load(final FloatBuffer src)
Load the values of the given matrix src to this matrix.
boolean invert()
Invert this matrix.
Matrix4f load(final float[] src)
Load the values of the given matrix src to this matrix.
final Matrix4f setToRotationAxis(final float ang_rad, final Vec3f axis)
Set this matrix to rotation from the given axis and angle in radians.
final Matrix4f setToRotation(final Quaternion q)
Set this matrix to rotation using the given Quaternion.
final Matrix4f setToTranslation(final Vec3f t)
Set this matrix to translation.
float determinant()
Returns the determinant of this matrix.
final Matrix4f translate(final Vec3f t, final Matrix4f tmp)
Translate this matrix, i.e.
Vec3f getColumn(final int column, final Vec3f v_out)
Get the named column of the given column-major matrix to v_out.
final Matrix4f transpose(final Matrix4f src)
Transpose the given src matrix into this matrix.
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.
final Quaternion getRotation(final Quaternion res)
Returns the rotation [m00 .
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 load(final float[] src, final int src_off)
Load the values of the given matrix src to this matrix.
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.
Matrix4f(final float[] m, final int m_off)
Creates a new matrix based on given float[4*4] column major order.
final Vec4f mulVec4f(final Vec4f v_in, final Vec4f v_out)
Matrix4f setToPick(final float x, final float y, final float deltaX, final float deltaY, final Recti viewport, final Matrix4f mat4Tmp)
Set this matrix to the pick matrix based on given parameters.
Matrix4f setToRotationEuler(final Vec3f angradXYZ)
Set this matrix to rotation from the given Euler rotation angles in radians.
final Matrix4f rotate(final float ang_rad, final Vec3f axis, final Matrix4f tmp)
Rotate this matrix about give axis and angle in radians, i.e.
final Matrix4f setToScale(final float x, final float y, final float z)
Set this matrix to scale.
static boolean mapObjToWin(final Vec3f obj, final Matrix4f mPMv, final Recti viewport, final Vec3f winPos)
Map object coordinates to window coordinates.
Matrix4f(final Matrix4f src)
Creates a new matrix copying the values of the given src matrix.
final Vec4f mulVec4f(final Vec4f v_inout)
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.
Matrix4f(final float[] m)
Creates a new matrix based on given float[4*4] column major order.
boolean isEqual(final Matrix4f o)
Equals check using FloatUtil#EPSILON value and FloatUtil#isEqual(float, float, float).
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 setToScale(final Vec3f s)
Set this matrix to scale.
Vec4f getColumn(final int column, final Vec4f v_out)
Get the named column of the given column-major matrix to v_out.
boolean equals(final Object o)
final Vec3f mulVec3f(final Vec3f v_inout)
Affine 3f-vector transformation by 4x4 matrix.
final Matrix4f setToTranslation(final float x, final float y, final float z)
Set this matrix to translation.
Quaternion implementation supporting Gimbal-Lock free rotations.
final float magnitudeSquared()
See magnitude() for special handling of epsilon, which is not applied here.
Quaternion setFromMat(final float m00, final float m01, final float m02, final float m10, final float m11, final float m12, final float m20, final float m21, final float m22)
Compute the quaternion from a 3x3 column rotation matrix.
Simple compound denoting a ray.
final Vec3f dir
Normalized direction vector of ray.
final Vec3f orig
Origin of Ray.
Rectangle with x, y, width and height integer components.
3D Vector based upon three float components.
Vec3f scale(final float s)
this = this * s, returns this.
Vec3f sub(final Vec3f b)
this = this - b, returns this.
Vec3f normalize()
Normalize this vector in place.
float length()
Return the length of this vector, a.k.a the norm or magnitude
Vec3f cross(final Vec3f arg)
Returns this cross arg; creates new vector.
Vec3f set(final Vec3f o)
this = o, returns this.
4D Vector based upon four float components.
Vec4f set(final Vec4f o)
this = o, returns this.
Vec4f scale(final float s)
this = this * s, returns this.
Vec4f mul(final float val)
Returns this * val; creates new vector.
Vec4f add(final float dx, final float dy, final float dz, final float dw)
this = this + { dx, dy, dz, dw }, returns this.
Plane equation := dot(n, x - p) = 0 -> Ax + By + Cz + d == 0.
final Vec3f n
Normal of the plane.
Providing frustum planes derived by different inputs (P*MV, ..) used to classify objects.
static final int NEAR
Index for near plane: {@value}.
static final int BOTTOM
Index for bottom plane: {@value}.
final Plane[] getPlanes()
Planes are ordered in the returned array as follows:
static final int RIGHT
Index for right plane: {@value}.
static final int TOP
Index for top plane: {@value}.
static final int LEFT
Index for left plane: {@value}.
static final int FAR
Index for far plane: {@value}.