29package com.jogamp.opengl.test.junit.math;
31import java.nio.FloatBuffer;
33import com.jogamp.math.FloatUtil;
34import com.jogamp.math.FovHVHalves;
35import com.jogamp.math.Quaternion;
36import com.jogamp.math.Ray;
37import com.jogamp.math.Vec3f;
38import com.jogamp.math.Vec4f;
39import com.jogamp.math.VectorUtil;
40import com.jogamp.math.geom.AABBox;
41import com.jogamp.math.geom.Frustum;
42import com.jogamp.math.geom.Frustum.Plane;
146 m[0+0*4] = m[1+1*4] = m[2+2*4] = m[3+3*4] = 1.0f;
147 m[0+1*4] = m[0+2*4] = m[0+3*4] =
148 m[1+0*4] = m[1+2*4] = m[1+3*4] =
149 m[2+0*4] = m[2+1*4] = m[2+3*4] =
150 m[3+0*4] = m[3+1*4] = m[3+2*4] = 0.0f;
160 System.arraycopy(src.m, 0, m, 0, 16);
170 System.arraycopy(src, 0, m, 0, 16);
181 System.arraycopy(src, src_off, m, 0, 16);
204 public float get(
final int i) {
215 v_out.
set(
get(0+column*4),
229 v_out.
set(
get(0+column*4),
242 v_out.
set(
get(row+0*4),
256 v_out.
set(
get(row+0*4),
269 public float[]
get(
final float[] dst,
final int dst_off) {
270 System.arraycopy(m, 0, dst, dst_off, 16);
280 public float[]
get(
final float[] dst) {
281 System.arraycopy(m, 0, dst, 0, 16);
295 public FloatBuffer
get(
final FloatBuffer dst) {
310 ret += m[0+0*4] * ( + m[1+1*4]*(m[2+2*4]*m[3+3*4] - m[2+3*4]*m[3+2*4]) - m[1+2*4]*(m[2+1*4]*m[3+3*4] - m[2+3*4]*m[3+1*4]) + m[1+3*4]*(m[2+1*4]*m[3+2*4] - m[2+2*4]*m[3+1*4]));
311 ret -= m[0+1*4] * ( + m[1+0*4]*(m[2+2*4]*m[3+3*4] - m[2+3*4]*m[3+2*4]) - m[1+2*4]*(m[2+0*4]*m[3+3*4] - m[2+3*4]*m[3+0*4]) + m[1+3*4]*(m[2+0*4]*m[3+2*4] - m[2+2*4]*m[3+0*4]));
312 ret += m[0+2*4] * ( + m[1+0*4]*(m[2+1*4]*m[3+3*4] - m[2+3*4]*m[3+1*4]) - m[1+1*4]*(m[2+0*4]*m[3+3*4] - m[2+3*4]*m[3+0*4]) + m[1+3*4]*(m[2+0*4]*m[3+1*4] - m[2+1*4]*m[3+0*4]));
313 ret -= m[0+3*4] * ( + m[1+0*4]*(m[2+1*4]*m[3+2*4] - m[2+2*4]*m[3+1*4]) - m[1+1*4]*(m[2+0*4]*m[3+2*4] - m[2+2*4]*m[3+0*4]) + m[1+2*4]*(m[2+0*4]*m[3+1*4] - m[2+1*4]*m[3+0*4]));
362 m[0+0*4] = src.m[0+0*4];
363 m[1+0*4] = src.m[0+1*4];
364 m[2+0*4] = src.m[0+2*4];
365 m[3+0*4] = src.m[0+3*4];
367 m[0+1*4] = src.m[1+0*4];
368 m[1+1*4] = src.m[1+1*4];
369 m[2+1*4] = src.m[1+2*4];
370 m[3+1*4] = src.m[1+3*4];
372 m[0+2*4] = src.m[2+0*4];
373 m[1+2*4] = src.m[2+1*4];
374 m[2+2*4] = src.m[2+2*4];
375 m[3+2*4] = src.m[2+3*4];
377 m[0+3*4] = src.m[3+0*4];
378 m[1+3*4] = src.m[3+1*4];
379 m[2+3*4] = src.m[3+2*4];
380 m[3+3*4] = src.m[3+3*4];
391 float max = Math.abs(m[0]);
393 for(
int i = 1; i < 16; i++ ) {
394 final float a = Math.abs(m[i]);
395 if( a > max ) max = a;
403 final float a00 = m[0+0*4]*
scale;
404 final float a10 = m[1+0*4]*
scale;
405 final float a20 = m[2+0*4]*
scale;
406 final float a30 = m[3+0*4]*
scale;
408 final float a01 = m[0+1*4]*
scale;
409 final float a11 = m[1+1*4]*
scale;
410 final float a21 = m[2+1*4]*
scale;
411 final float a31 = m[3+1*4]*
scale;
413 final float a02 = m[0+2*4]*
scale;
414 final float a12 = m[1+2*4]*
scale;
415 final float a22 = m[2+2*4]*
scale;
416 final float a32 = m[3+2*4]*
scale;
418 final float a03 = m[0+3*4]*
scale;
419 final float a13 = m[1+3*4]*
scale;
420 final float a23 = m[2+3*4]*
scale;
421 final float a33 = m[3+3*4]*
scale;
423 final float b00 = + a11*(a22*a33 - a23*a32) - a12*(a21*a33 - a23*a31) + a13*(a21*a32 - a22*a31);
424 final float b01 = -( + a10*(a22*a33 - a23*a32) - a12*(a20*a33 - a23*a30) + a13*(a20*a32 - a22*a30));
425 final float b02 = + a10*(a21*a33 - a23*a31) - a11*(a20*a33 - a23*a30) + a13*(a20*a31 - a21*a30);
426 final float b03 = -( + a10*(a21*a32 - a22*a31) - a11*(a20*a32 - a22*a30) + a12*(a20*a31 - a21*a30));
428 final float b10 = -( + a01*(a22*a33 - a23*a32) - a02*(a21*a33 - a23*a31) + a03*(a21*a32 - a22*a31));
429 final float b11 = + a00*(a22*a33 - a23*a32) - a02*(a20*a33 - a23*a30) + a03*(a20*a32 - a22*a30);
430 final float b12 = -( + a00*(a21*a33 - a23*a31) - a01*(a20*a33 - a23*a30) + a03*(a20*a31 - a21*a30));
431 final float b13 = + a00*(a21*a32 - a22*a31) - a01*(a20*a32 - a22*a30) + a02*(a20*a31 - a21*a30);
433 final float b20 = + a01*(a12*a33 - a13*a32) - a02*(a11*a33 - a13*a31) + a03*(a11*a32 - a12*a31);
434 final float b21 = -( + a00*(a12*a33 - a13*a32) - a02*(a10*a33 - a13*a30) + a03*(a10*a32 - a12*a30));
435 final float b22 = + a00*(a11*a33 - a13*a31) - a01*(a10*a33 - a13*a30) + a03*(a10*a31 - a11*a30);
436 final float b23 = -( + a00*(a11*a32 - a12*a31) - a01*(a10*a32 - a12*a30) + a02*(a10*a31 - a11*a30));
438 final float b30 = -( + a01*(a12*a23 - a13*a22) - a02*(a11*a23 - a13*a21) + a03*(a11*a22 - a12*a21));
439 final float b31 = + a00*(a12*a23 - a13*a22) - a02*(a10*a23 - a13*a20) + a03*(a10*a22 - a12*a20);
440 final float b32 = -( + a00*(a11*a23 - a13*a21) - a01*(a10*a23 - a13*a20) + a03*(a10*a21 - a11*a20));
441 final float b33 = + a00*(a11*a22 - a12*a21) - a01*(a10*a22 - a12*a20) + a02*(a10*a21 - a11*a20);
443 final float det = (a00*b00 + a01*b01 + a02*b02 + a03*b03) /
scale;
447 final float invdet = 1.0f / det;
449 m[0+0*4] = b00 * invdet;
450 m[1+0*4] = b01 * invdet;
451 m[2+0*4] = b02 * invdet;
452 m[3+0*4] = b03 * invdet;
454 m[0+1*4] = b10 * invdet;
455 m[1+1*4] = b11 * invdet;
456 m[2+1*4] = b12 * invdet;
457 m[3+1*4] = b13 * invdet;
459 m[0+2*4] = b20 * invdet;
460 m[1+2*4] = b21 * invdet;
461 m[2+2*4] = b22 * invdet;
462 m[3+2*4] = b23 * invdet;
464 m[0+3*4] = b30 * invdet;
465 m[1+3*4] = b31 * invdet;
466 m[2+3*4] = b32 * invdet;
467 m[3+3*4] = b33 * invdet;
479 float max = Math.abs(src.m[0]);
481 for(
int i = 1; i < 16; i++ ) {
482 final float a = Math.abs(src.m[i]);
483 if( a > max ) max = a;
491 final float a00 = src.m[0+0*4]*
scale;
492 final float a10 = src.m[1+0*4]*
scale;
493 final float a20 = src.m[2+0*4]*
scale;
494 final float a30 = src.m[3+0*4]*
scale;
496 final float a01 = src.m[0+1*4]*
scale;
497 final float a11 = src.m[1+1*4]*
scale;
498 final float a21 = src.m[2+1*4]*
scale;
499 final float a31 = src.m[3+1*4]*
scale;
501 final float a02 = src.m[0+2*4]*
scale;
502 final float a12 = src.m[1+2*4]*
scale;
503 final float a22 = src.m[2+2*4]*
scale;
504 final float a32 = src.m[3+2*4]*
scale;
506 final float a03 = src.m[0+3*4]*
scale;
507 final float a13 = src.m[1+3*4]*
scale;
508 final float a23 = src.m[2+3*4]*
scale;
509 final float a33 = src.m[3+3*4]*
scale;
511 final float b00 = + a11*(a22*a33 - a23*a32) - a12*(a21*a33 - a23*a31) + a13*(a21*a32 - a22*a31);
512 final float b01 = -( + a10*(a22*a33 - a23*a32) - a12*(a20*a33 - a23*a30) + a13*(a20*a32 - a22*a30));
513 final float b02 = + a10*(a21*a33 - a23*a31) - a11*(a20*a33 - a23*a30) + a13*(a20*a31 - a21*a30);
514 final float b03 = -( + a10*(a21*a32 - a22*a31) - a11*(a20*a32 - a22*a30) + a12*(a20*a31 - a21*a30));
516 final float b10 = -( + a01*(a22*a33 - a23*a32) - a02*(a21*a33 - a23*a31) + a03*(a21*a32 - a22*a31));
517 final float b11 = + a00*(a22*a33 - a23*a32) - a02*(a20*a33 - a23*a30) + a03*(a20*a32 - a22*a30);
518 final float b12 = -( + a00*(a21*a33 - a23*a31) - a01*(a20*a33 - a23*a30) + a03*(a20*a31 - a21*a30));
519 final float b13 = + a00*(a21*a32 - a22*a31) - a01*(a20*a32 - a22*a30) + a02*(a20*a31 - a21*a30);
521 final float b20 = + a01*(a12*a33 - a13*a32) - a02*(a11*a33 - a13*a31) + a03*(a11*a32 - a12*a31);
522 final float b21 = -( + a00*(a12*a33 - a13*a32) - a02*(a10*a33 - a13*a30) + a03*(a10*a32 - a12*a30));
523 final float b22 = + a00*(a11*a33 - a13*a31) - a01*(a10*a33 - a13*a30) + a03*(a10*a31 - a11*a30);
524 final float b23 = -( + a00*(a11*a32 - a12*a31) - a01*(a10*a32 - a12*a30) + a02*(a10*a31 - a11*a30));
526 final float b30 = -( + a01*(a12*a23 - a13*a22) - a02*(a11*a23 - a13*a21) + a03*(a11*a22 - a12*a21));
527 final float b31 = + a00*(a12*a23 - a13*a22) - a02*(a10*a23 - a13*a20) + a03*(a10*a22 - a12*a20);
528 final float b32 = -( + a00*(a11*a23 - a13*a21) - a01*(a10*a23 - a13*a20) + a03*(a10*a21 - a11*a20));
529 final float b33 = + a00*(a11*a22 - a12*a21) - a01*(a10*a22 - a12*a20) + a02*(a10*a21 - a11*a20);
531 final float det = (a00*b00 + a01*b01 + a02*b02 + a03*b03) /
scale;
535 final float invdet = 1.0f / det;
537 m[0+0*4] = b00 * invdet;
538 m[1+0*4] = b01 * invdet;
539 m[2+0*4] = b02 * invdet;
540 m[3+0*4] = b03 * invdet;
542 m[0+1*4] = b10 * invdet;
543 m[1+1*4] = b11 * invdet;
544 m[2+1*4] = b12 * invdet;
545 m[3+1*4] = b13 * invdet;
547 m[0+2*4] = b20 * invdet;
548 m[1+2*4] = b21 * invdet;
549 m[2+2*4] = b22 * invdet;
550 m[3+2*4] = b23 * invdet;
552 m[0+3*4] = b30 * invdet;
553 m[1+3*4] = b31 * invdet;
554 m[2+3*4] = b32 * invdet;
555 m[3+3*4] = b33 * invdet;
570 final float b00 = b.m[0+0*4];
571 final float b10 = b.m[1+0*4];
572 final float b20 = b.m[2+0*4];
573 final float b30 = b.m[3+0*4];
574 final float b01 = b.m[0+1*4];
575 final float b11 = b.m[1+1*4];
576 final float b21 = b.m[2+1*4];
577 final float b31 = b.m[3+1*4];
578 final float b02 = b.m[0+2*4];
579 final float b12 = b.m[1+2*4];
580 final float b22 = b.m[2+2*4];
581 final float b32 = b.m[3+2*4];
582 final float b03 = b.m[0+3*4];
583 final float b13 = b.m[1+3*4];
584 final float b23 = b.m[2+3*4];
585 final float b33 = b.m[3+3*4];
591 m[0+0*4] = ai0 * b00 + ai1 * b10 + ai2 * b20 + ai3 * b30 ;
592 m[0+1*4] = ai0 * b01 + ai1 * b11 + ai2 * b21 + ai3 * b31 ;
593 m[0+2*4] = ai0 * b02 + ai1 * b12 + ai2 * b22 + ai3 * b32 ;
594 m[0+3*4] = ai0 * b03 + ai1 * b13 + ai2 * b23 + ai3 * b33 ;
600 m[1+0*4] = ai0 * b00 + ai1 * b10 + ai2 * b20 + ai3 * b30 ;
601 m[1+1*4] = ai0 * b01 + ai1 * b11 + ai2 * b21 + ai3 * b31 ;
602 m[1+2*4] = ai0 * b02 + ai1 * b12 + ai2 * b22 + ai3 * b32 ;
603 m[1+3*4] = ai0 * b03 + ai1 * b13 + ai2 * b23 + ai3 * b33 ;
609 m[2+0*4] = ai0 * b00 + ai1 * b10 + ai2 * b20 + ai3 * b30 ;
610 m[2+1*4] = ai0 * b01 + ai1 * b11 + ai2 * b21 + ai3 * b31 ;
611 m[2+2*4] = ai0 * b02 + ai1 * b12 + ai2 * b22 + ai3 * b32 ;
612 m[2+3*4] = ai0 * b03 + ai1 * b13 + ai2 * b23 + ai3 * b33 ;
618 m[3+0*4] = ai0 * b00 + ai1 * b10 + ai2 * b20 + ai3 * b30 ;
619 m[3+1*4] = ai0 * b01 + ai1 * b11 + ai2 * b21 + ai3 * b31 ;
620 m[3+2*4] = ai0 * b02 + ai1 * b12 + ai2 * b22 + ai3 * b32 ;
621 m[3+3*4] = ai0 * b03 + ai1 * b13 + ai2 * b23 + ai3 * b33 ;
637 final float b00 = b.m[0+0*4];
638 final float b10 = b.m[1+0*4];
639 final float b20 = b.m[2+0*4];
640 final float b30 = b.m[3+0*4];
641 final float b01 = b.m[0+1*4];
642 final float b11 = b.m[1+1*4];
643 final float b21 = b.m[2+1*4];
644 final float b31 = b.m[3+1*4];
645 final float b02 = b.m[0+2*4];
646 final float b12 = b.m[1+2*4];
647 final float b22 = b.m[2+2*4];
648 final float b32 = b.m[3+2*4];
649 final float b03 = b.m[0+3*4];
650 final float b13 = b.m[1+3*4];
651 final float b23 = b.m[2+3*4];
652 final float b33 = b.m[3+3*4];
654 float ai0=a.m[0+0*4];
655 float ai1=a.m[0+1*4];
656 float ai2=a.m[0+2*4];
657 float ai3=a.m[0+3*4];
658 m[0+0*4] = ai0 * b00 + ai1 * b10 + ai2 * b20 + ai3 * b30 ;
659 m[0+1*4] = ai0 * b01 + ai1 * b11 + ai2 * b21 + ai3 * b31 ;
660 m[0+2*4] = ai0 * b02 + ai1 * b12 + ai2 * b22 + ai3 * b32 ;
661 m[0+3*4] = ai0 * b03 + ai1 * b13 + ai2 * b23 + ai3 * b33 ;
667 m[1+0*4] = ai0 * b00 + ai1 * b10 + ai2 * b20 + ai3 * b30 ;
668 m[1+1*4] = ai0 * b01 + ai1 * b11 + ai2 * b21 + ai3 * b31 ;
669 m[1+2*4] = ai0 * b02 + ai1 * b12 + ai2 * b22 + ai3 * b32 ;
670 m[1+3*4] = ai0 * b03 + ai1 * b13 + ai2 * b23 + ai3 * b33 ;
676 m[2+0*4] = ai0 * b00 + ai1 * b10 + ai2 * b20 + ai3 * b30 ;
677 m[2+1*4] = ai0 * b01 + ai1 * b11 + ai2 * b21 + ai3 * b31 ;
678 m[2+2*4] = ai0 * b02 + ai1 * b12 + ai2 * b22 + ai3 * b32 ;
679 m[2+3*4] = ai0 * b03 + ai1 * b13 + ai2 * b23 + ai3 * b33 ;
685 m[3+0*4] = ai0 * b00 + ai1 * b10 + ai2 * b20 + ai3 * b30 ;
686 m[3+1*4] = ai0 * b01 + ai1 * b11 + ai2 * b21 + ai3 * b31 ;
687 m[3+2*4] = ai0 * b02 + ai1 * b12 + ai2 * b22 + ai3 * b32 ;
688 m[3+3*4] = ai0 * b03 + ai1 * b13 + ai2 * b23 + ai3 * b33 ;
697 public final float[]
mulVec4f(
final float[] v_in,
final float[] v_out) {
699 final float x = v_in[0], y = v_in[1], z = v_in[2], w = v_in[3];
700 v_out[0] = x * m[0+0*4] + y * m[0+1*4] + z * m[0+2*4] + w * m[0+3*4];
701 v_out[1] = x * m[1+0*4] + y * m[1+1*4] + z * m[1+2*4] + w * m[1+3*4];
702 v_out[2] = x * m[2+0*4] + y * m[2+1*4] + z * m[2+2*4] + w * m[2+3*4];
703 v_out[3] = x * m[3+0*4] + y * m[3+1*4] + z * m[3+2*4] + w * m[3+3*4];
714 final float x = v_in.x(), y = v_in.y(), z = v_in.z(), w = v_in.w();
715 v_out.
set( x * m[0+0*4] + y * m[0+1*4] + z * m[0+2*4] + w * m[0+3*4],
716 x * m[1+0*4] + y * m[1+1*4] + z * m[1+2*4] + w * m[1+3*4],
717 x * m[2+0*4] + y * m[2+1*4] + z * m[2+2*4] + w * m[2+3*4],
718 x * m[3+0*4] + y * m[3+1*4] + z * m[3+2*4] + w * m[3+3*4] );
733 public final float[]
mulVec3f(
final float[] v_in,
final float[] v_out) {
735 final float x = v_in[0], y = v_in[1], z = v_in[2];
736 v_out[0] = x * m[0+0*4] + y * m[0+1*4] + z * m[0+2*4] + 1f * m[0+3*4];
737 v_out[1] = x * m[1+0*4] + y * m[1+1*4] + z * m[1+2*4] + 1f * m[1+3*4];
738 v_out[2] = x * m[2+0*4] + y * m[2+1*4] + z * m[2+2*4] + 1f * m[2+3*4];
755 final float x = v_in.x(), y = v_in.y(), z = v_in.z();
756 v_out.
set( x * m[0+0*4] + y * m[0+1*4] + z * m[0+2*4] + 1f * m[0+3*4],
757 x * m[1+0*4] + y * m[1+1*4] + z * m[1+2*4] + 1f * m[1+3*4],
758 x * m[2+0*4] + y * m[2+1*4] + z * m[2+2*4] + 1f * m[2+3*4] );
781 m[0+0*4] = m[1+1*4] = m[2+2*4] = m[3+3*4] = 1.0f;
785 m[0+1*4] = m[0+2*4] =
786 m[1+0*4] = m[1+2*4] =
787 m[2+0*4] = m[2+1*4] =
788 m[3+0*4] = m[3+1*4] = m[3+2*4] = 0.0f;
827 m[0+1*4] = m[0+2*4] = m[0+3*4] =
828 m[1+0*4] = m[1+2*4] = m[1+3*4] =
829 m[2+0*4] = m[2+1*4] = m[2+3*4] =
830 m[3+0*4] = m[3+1*4] = m[3+2*4] = 0.0f;
852 final float ic= 1.0f - c;
855 final float[] tmpVec3f = { x, y, z };
857 x = tmpVec3f[0]; y = tmpVec3f[1]; z = tmpVec3f[2];
859 final float xy = x*y;
860 final float xz = x*z;
861 final float xs = x*s;
862 final float ys = y*s;
863 final float yz = y*z;
864 final float zs = z*s;
943 m[0+1*4] = sh*sb - ch*sa*cb;
945 m[2+1*4] = sh*sa*cb + ch*sb;
948 m[0+2*4] = ch*sa*sb + sh*cb;
950 m[2+2*4] = -sh*sa*sb + ch*cb;
989 srecip = 2.0f / norm;
992 final float x = q.x();
993 final float y = q.y();
994 final float z = q.z();
995 final float w = q.w();
997 final float xs = srecip * x;
998 final float ys = srecip * y;
999 final float zs = srecip * z;
1001 final float xx = x * xs;
1002 final float xy = x * ys;
1003 final float xz = x * zs;
1004 final float xw = xs * w;
1005 final float yy = y * ys;
1006 final float yz = y * zs;
1007 final float yw = ys * w;
1008 final float zz = z * zs;
1009 final float zw = zs * w;
1011 m[0+0*4] = 1f - ( yy + zz );
1012 m[0+1*4] = ( xy - zw );
1013 m[0+2*4] = ( xz + yw );
1016 m[1+0*4] = ( xy + zw );
1017 m[1+1*4] = 1f - ( xx + zz );
1018 m[1+2*4] = ( yz - xw );
1021 m[2+0*4] = ( xz - yw );
1022 m[2+1*4] = ( yz + xw );
1023 m[2+2*4] = 1f - ( xx + yy );
1026 m[3+0*4] = m[3+1*4] = m[3+2*4] = 0f;
1039 res.
setFromMat(m[0+0*4], m[0+1*4], m[0+2*4], m[1+0*4], m[1+1*4], m[1+2*4], m[2+0*4], m[2+1*4], m[2+2*4]);
1061 final float bottom,
final float top,
1062 final float zNear,
final float zFar) {
1065 m[1+0*4] = m[2+0*4] = m[3+0*4] = 0f;
1066 m[0+1*4] = m[2+1*4] = m[3+1*4] = 0f;
1067 m[0+2*4] = m[1+2*4] = m[3+2*4] = 0f;
1070 final float dx=right-left;
1071 final float dy=top-bottom;
1072 final float dz=zFar-zNear;
1073 final float tx=-1.0f*(right+left)/dx;
1074 final float ty=-1.0f*(top+bottom)/dy;
1075 final float tz=-1.0f*(zFar+zNear)/dz;
1079 m[2+2*4] = -2.0f/dz;
1109 final float bottom,
final float top,
1110 final float zNear,
final float zFar)
throws IllegalArgumentException {
1111 if( zNear <= 0.0f || zFar <= zNear ) {
1112 throw new IllegalArgumentException(
"Requirements zNear > 0 and zFar > zNear, but zNear "+zNear+
", zFar "+zFar);
1114 if( left == right || top == bottom) {
1115 throw new IllegalArgumentException(
"GL_INVALID_VALUE: top,bottom and left,right must not be equal");
1119 m[1+0*4] = m[2+0*4] = m[3+0*4] = 0f;
1120 m[0+1*4] = m[2+1*4] = m[3+1*4] = 0f;
1121 m[0+3*4] = m[1+3*4] = 0f;
1123 final float zNear2 = 2.0f*zNear;
1124 final float dx=right-left;
1125 final float dy=top-bottom;
1126 final float dz=zFar-zNear;
1127 final float A=(right+left)/dx;
1128 final float B=(top+bottom)/dy;
1129 final float C=-1.0f*(zFar+zNear)/dz;
1130 final float D=-2.0f*(zFar*zNear)/dz;
1132 m[0+0*4] = zNear2/dx;
1133 m[1+1*4] = zNear2/dy;
1157 public Matrix4fb setToPerspective(
final float fovy_rad,
final float aspect,
final float zNear,
final float zFar)
throws IllegalArgumentException {
1159 final float bottom = -1.0f * top;
1160 final float left = aspect * bottom;
1161 final float right = aspect * top;
1162 return setToFrustum(left, right, bottom, top, zNear, zFar);
1178 final float top = fovhvTan.top * zNear;
1179 final float bottom = -1.0f * fovhvTan.bottom * zNear;
1180 final float left = -1.0f * fovhvTan.left * zNear;
1181 final float right = fovhvTan.right * zNear;
1182 return setToFrustum(left, right, bottom, top, zNear, zFar);
1198 final Vec3f p_n = p.n;
1199 p_n.
set( m[3+0*4] + m[0+0*4],
1200 m[3+1*4] + m[0+1*4],
1201 m[3+2*4] + m[0+2*4] );
1202 p.d = m[3+3*4] + m[0+3*4];
1209 final Vec3f p_n = p.n;
1210 p_n.
set( m[3+0*4] - m[0+0*4],
1211 m[3+1*4] - m[0+1*4],
1212 m[3+2*4] - m[0+2*4] );
1213 p.d = m[3+3*4] - m[0+3*4];
1220 final Vec3f p_n = p.n;
1221 p_n.
set( m[3+0*4] + m[1+0*4],
1222 m[3+1*4] + m[1+1*4],
1223 m[3+2*4] + m[1+2*4] );
1224 p.d = m[3+3*4] + m[1+3*4];
1231 final Vec3f p_n = p.n;
1232 p_n.
set( m[3+0*4] - m[1+0*4],
1233 m[3+1*4] - m[1+1*4],
1234 m[3+2*4] - m[1+2*4] );
1235 p.d = m[3+3*4] - m[1+3*4];
1242 final Vec3f p_n = p.n;
1243 p_n.
set( m[3+0*4] + m[2+0*4],
1244 m[3+1*4] + m[2+1*4],
1245 m[3+2*4] + m[2+2*4] );
1246 p.d = m[3+3*4] + m[2+3*4];
1253 final Vec3f p_n = p.n;
1254 p_n.
set( m[3+0*4] - m[2+0*4],
1255 m[3+1*4] - m[2+1*4],
1256 m[3+2*4] - m[2+2*4] );
1257 p.d = m[3+3*4] - m[2+3*4];
1261 for (
int i = 0; i < 6; ++i) {
1264 final float invLen = 1f / p_n.
length();
1292 final Vec3f fwd =
new Vec3f( center.x() - eye.x(),
1293 center.y() - eye.y(),
1302 m[0+0*4] = side.x();
1304 m[2+0*4] = -fwd.x();
1307 m[0+1*4] = side.y();
1309 m[2+1*4] = -fwd.y();
1312 m[0+2*4] = side.z();
1314 m[2+2*4] = -fwd.z();
1449 for(
int i=0; i<16; ++i) {
1501 final int[] viewport,
final float[] winPos)
1512 if (rawWinPos.w() == 0.0f) {
1516 final float s = ( 1.0f / rawWinPos.w() ) * 0.5f;
1519 rawWinPos.
scale(s).
add(0.5f, 0.5f, 0.5f, 0f);
1522 winPos[0] = rawWinPos.x() * viewport[2] + viewport[0];
1523 winPos[1] = rawWinPos.y() * viewport[3] + viewport[1];
1524 winPos[2] = rawWinPos.z();
1542 final int[] viewport,
final float[] winPos)
1549 if (rawWinPos.w() == 0.0f) {
1553 final float s = ( 1.0f / rawWinPos.w() ) * 0.5f;
1556 rawWinPos.
scale(s).
add(0.5f, 0.5f, 0.5f, 0f);
1559 winPos[0] = rawWinPos.x() * viewport[2] + viewport[0];
1560 winPos[1] = rawWinPos.y() * viewport[3] + viewport[1];
1561 winPos[2] = rawWinPos.z();
1582 public static boolean mapWinToObj(
final float winx,
final float winy,
final float winz,
1584 final int[] viewport,
1594 final Vec4f winPos =
new Vec4f(winx, winy, winz, 1f);
1597 winPos.
add(-viewport[0], -viewport[1], 0f, 0f).
mul(1f/viewport[2], 1f/viewport[3], 1f, 1f);
1600 winPos.
mul(2f, 2f, 2f, 1f).
add(-1f, -1f, -1f, 0f);
1605 if ( rawObjPos.w() == 0.0f ) {
1608 objPos.
set( rawObjPos.
scale( 1f / rawObjPos.w() ) );
1628 public static boolean mapWinToObj(
final float winx,
final float winy,
final float winz,
1630 final int[] viewport,
1634 final Vec4f winPos =
new Vec4f(winx, winy, winz, 1f);
1637 winPos.
add(-viewport[0], -viewport[1], 0f, 0f).
mul(1f/viewport[2], 1f/viewport[3], 1f, 1f);
1640 winPos.
mul(2f, 2f, 2f, 1f).
add(-1f, -1f, -1f, 0f);
1645 if ( rawObjPos.w() == 0.0f ) {
1648 objPos.
set( rawObjPos.
scale( 1f / rawObjPos.w() ) );
1670 public static boolean mapWinToObj(
final float winx,
final float winy,
final float winz1,
final float winz2,
1672 final int[] viewport,
1676 final Vec4f winPos =
new Vec4f(winx, winy, winz1, 1f);
1679 winPos.
add(-viewport[0], -viewport[1], 0f, 0f).
mul(1f/viewport[2], 1f/viewport[3], 1f, 1f);
1682 winPos.
mul(2f, 2f, 2f, 1f).
add(-1f, -1f, -1f, 0f);
1687 if ( rawObjPos.w() == 0.0f ) {
1690 objPos1.
set( rawObjPos.
scale( 1f / rawObjPos.w() ) );
1696 winPos.
setZ( winz2 * 2f - 1f );
1699 invPMv.
mulVec4f(winPos, rawObjPos);
1701 if ( rawObjPos.w() == 0.0f ) {
1704 objPos2.
set( rawObjPos.
scale( 1f / rawObjPos.w() ) );
1728 public static boolean mapWinToObj4(
final float winx,
final float winy,
final float winz,
final float clipw,
1730 final int[] viewport,
1731 final float near,
final float far,
1741 final Vec4f winPos =
new Vec4f(winx, winy, winz, clipw);
1744 winPos.
add(-viewport[0], -viewport[1], -near, 0f).
mul(1f/viewport[2], 1f/viewport[3], 1f/(far-near), 1f);
1747 winPos.
mul(2f, 2f, 2f, 1f).
add(-1f, -1f, -1f, 0f);
1752 if ( objPos.w() == 0.0f ) {
1782 public static boolean mapWinToRay(
final float winx,
final float winy,
final float winz0,
final float winz1,
1785 final int[] viewport,
1794 if(
mapWinToObj(winx, winy, winz0, winz1, invPMv, viewport,
1795 ray.
orig, ray.
dir, mat4Tmp2) ) {
1813 public StringBuilder
toString(
final StringBuilder sb,
final String rowPrefix,
final String f) {
1814 final float[] tmp =
new float[16];
1821 return toString(
null,
null,
"%10.5f").toString();
1824 private final float[] m =
new float[16];
1826 final Stack stack =
new Stack(0, 16*16);
1828 private static class Stack {
1829 private int position;
1830 private float[] buffer;
1831 private final int growSize;
1838 public Stack(
final int initialSize,
final int growSize) {
1840 this.growSize = growSize;
1841 this.buffer =
new float[initialSize];
1844 private final void growIfNecessary(
final int length)
throws IndexOutOfBoundsException {
1845 if( position + length > buffer.length ) {
1846 if( 0 >= growSize ) {
1847 throw new IndexOutOfBoundsException(
"Out of fixed stack size: "+
this);
1849 final float[] newBuffer =
1850 new float[buffer.length + growSize];
1851 System.arraycopy(buffer, 0, newBuffer, 0, position);
1857 growIfNecessary(16);
1858 src.get(buffer, position);
1865 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.
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.
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.
static float[] normalizeVec3(final float[] vector)
Normalize a vector in place.
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}.
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
final Matrix4fb setToTranslation(final Vec3f t)
Set this matrix to translation.
Matrix4fb load(final float[] src, final int src_off)
Load the values of the given matrix src to this matrix.
final Matrix4fb translate(final Vec3f t, final Matrix4fb tmp)
Translate this matrix, i.e.
final Matrix4fb transpose(final Matrix4fb src)
Transpose the given src matrix into this matrix.
Matrix4fb()
Creates a new identity matrix.
boolean invert()
Invert this matrix.
Matrix4fb setToRotationEuler(final float bankX, final float headingY, final float attitudeZ)
Set this matrix to rotation from the given Euler rotation angles in radians.
float determinant()
Returns the determinant of this matrix.
static boolean mapObjToWin(final Vec3f obj, final Matrix4fb mMv, final Matrix4fb mP, final int[] viewport, final float[] winPos)
Map object coordinates to window coordinates.
final float[] mulVec3f(final float[] v_in, final float[] v_out)
Affine 3f-vector transformation by 4x4 matrix.
final Quaternion getRotation(final Quaternion res)
Returns the rotation [m[0+0*4] .
float get(final int i)
Gets the ith component, 0 <= i < 16.
boolean isEqual(final Matrix4fb o, final float epsilon)
Equals check using a given FloatUtil#EPSILON value and FloatUtil#isEqual(float, float,...
final Vec4f mulVec4f(final Vec4f v_in, final Vec4f v_out)
boolean invert(final Matrix4fb src)
Invert the src matrix values into this matrix.
final void push()
Push the matrix to it's stack, while preserving this matrix values.
void updateFrustumPlanes(final Frustum frustum)
Calculate the frustum planes in world coordinates using the passed float[16] as premultiplied P*MV (c...
Matrix4fb load(final Matrix4fb src)
Load the values of the given matrix b to this matrix.
final Matrix4fb setToRotationAxis(final float ang_rad, float x, float y, float z)
Set this matrix to rotation from the given axis and angle in radians.
static boolean mapWinToObj(final float winx, final float winy, final float winz, final Matrix4fb invPMv, final int[] viewport, final Vec3f objPos, final Matrix4fb mat4Tmp)
Map window coordinates to object coordinates.
Matrix4fb 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.
StringBuilder toString(final StringBuilder sb, final String rowPrefix, final String f)
final Matrix4fb rotate(final float ang_rad, final float x, final float y, final float z, final Matrix4fb tmp)
Rotate this matrix about give axis and angle in radians, i.e.
Matrix4fb(final float[] m, final int m_off)
Creates a new matrix based on given float[4*4] column major order.
static boolean mapObjToWin(final Vec3f obj, final Matrix4fb mPMv, final int[] viewport, final float[] winPos)
Map object coordinates to window coordinates.
final float[] mulVec4f(final float[] v_in, final float[] v_out)
Vec4f getRow(final int row, final Vec4f v_out)
Get the named row of the given column-major matrix to v_out.
Vec3f getColumn(final int column, final Vec3f v_out)
Get the named column of the given column-major matrix to v_out.
Vec4f getColumn(final int column, final Vec4f v_out)
Get the named column of the given column-major matrix to v_out.
final Matrix4fb mul(final Matrix4fb b)
Multiply matrix: [this] = [this] x [b].
final Matrix4fb rotate(final float ang_rad, final Vec3f axis, final Matrix4fb tmp)
Rotate this matrix about give axis and angle in radians, i.e.
final Matrix4fb transpose()
Transpose this matrix.
final Matrix4fb setToScale(final float x, final float y, final float z)
Set this matrix to scale.
Matrix4fb setToPerspective(final FovHVHalves fovhv, final float zNear, final float zFar)
Set this matrix to perspective frustum projection.
final Matrix4fb rotate(final Quaternion quat, final Matrix4fb tmp)
Rotate this matrix with the given Quaternion, i.e.
final Matrix4fb setToTranslation(final float x, final float y, final float z)
Set this matrix to translation.
static boolean mapWinToRay(final float winx, final float winy, final float winz0, final float winz1, final Matrix4fb mMv, final Matrix4fb mP, final int[] viewport, final Ray ray, final Matrix4fb mat4Tmp1, final Matrix4fb mat4Tmp2)
Map two window coordinates w/ shared X/Y and distinctive Z to a Ray.
final void pop()
Pop the current matrix from it's stack, replacing this matrix values.
static boolean mapWinToObj4(final float winx, final float winy, final float winz, final float clipw, final Matrix4fb mMv, final Matrix4fb mP, final int[] viewport, final float near, final float far, final Vec4f objPos, final Matrix4fb mat4Tmp)
Map window coordinates to object coordinates.
final Matrix4fb setToRotation(final Quaternion q)
Set this matrix to rotation using the given Quaternion.
final Matrix4fb setToRotationAxis(final float ang_rad, final Vec3f axis)
Set this matrix to rotation from the given axis and angle in radians.
Matrix4fb(final float[] m)
Creates a new matrix based on given float[4*4] column major order.
final Matrix4fb translate(final float x, final float y, final float z, final Matrix4fb tmp)
Translate this matrix, i.e.
final Matrix4fb scale(final float s, final Matrix4fb tmp)
Scale this matrix, i.e.
Matrix4fb setToLookAt(final Vec3f eye, final Vec3f center, final Vec3f up, final Matrix4fb tmp)
Make given matrix the look-at matrix based on given parameters.
boolean equals(final Object o)
static boolean mapWinToObj(final float winx, final float winy, final float winz, final Matrix4fb mMv, final Matrix4fb mP, final int[] viewport, final Vec3f objPos, final Matrix4fb mat4Tmp)
Map window coordinates to object coordinates.
Matrix4fb load(final FloatBuffer src)
Load the values of the given matrix src to this matrix.
Matrix4fb setToFrustum(final float left, final float right, final float bottom, final float top, final float zNear, final float zFar)
Set this matrix to frustum.
final Matrix4fb mul(final Matrix4fb a, final Matrix4fb b)
Multiply matrix: [this] = [a] x [b].
boolean isEqual(final Matrix4fb o)
Equals check using FloatUtil#EPSILON value and FloatUtil#isEqual(float, float, float).
Matrix4fb(final Matrix4fb src)
Creates a new matrix copying the values of the given src matrix.
Matrix4fb setToPerspective(final float fovy_rad, final float aspect, final float zNear, final float zFar)
Set this matrix to perspective frustum projection.
static boolean mapWinToObj(final float winx, final float winy, final float winz1, final float winz2, final Matrix4fb invPMv, final int[] viewport, final Vec3f objPos1, final Vec3f objPos2, final Matrix4fb mat4Tmp)
Map two window coordinates to two object coordinates, distinguished by their z component.
final Matrix4fb 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.
Matrix4fb load(final float[] src)
Load the values of the given matrix src to this matrix.
final Matrix4fb scale(final float x, final float y, final float z, final Matrix4fb tmp)
Scale this matrix, i.e.
final Vec3f mulVec3f(final Vec3f v_in, final Vec3f v_out)
Affine 3f-vector transformation by 4x4 matrix.