JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestMatrix4f01NOUI.java
Go to the documentation of this file.
1/**
2 * Copyright 2012-2023 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.opengl.test.junit.math;
30
31import org.junit.Assert;
32import org.junit.Test;
33import org.junit.FixMethodOrder;
34import org.junit.runners.MethodSorters;
35
36import com.jogamp.junit.util.JunitTracer;
37import com.jogamp.math.FloatUtil;
38import com.jogamp.math.Matrix4f;
39import com.jogamp.math.Vec3f;
40
41@FixMethodOrder(MethodSorters.NAME_ASCENDING)
42public class TestMatrix4f01NOUI extends JunitTracer {
43
44 final float[] mI_0 = new float[]{ 1, 0, 0, 0,
45 0, 1, 0, 0,
46 0, 0, 1, 0,
47 0, 0, 0, 1 };
48 final Matrix4f mI = new Matrix4f(mI_0);
49
50 final float[] m1_0 = new float[]{ 1, 3, 4, 0,
51 6, 7, 8, 5,
52 98, 7, 6, 9,
53 54, 3, 2, 5 };
54 final Matrix4f m1 = new Matrix4f(m1_0);
55
56 final float[] m1T_0 = new float[]{ 1, 6, 98, 54,
57 3, 7, 7, 3,
58 4, 8, 6, 2,
59 0, 5, 9, 5 };
60 final Matrix4f m1T = new Matrix4f(m1T_0);
61
62 final float[] m2_0 = new float[]{ 1, 6, 98, 54,
63 3, 7, 7, 3,
64 4, 8, 6, 2,
65 0, 5, 9, 5 };
66 final Matrix4f m2 = new Matrix4f(m2_0);
67
68 final float[] m2xm1_0 =
69 new float[]{ 26, 59, 143, 71,
70 59, 174, 730, 386,
71 143, 730, 9770, 5370,
72 71, 386, 5370, 2954 };
73 final Matrix4f m2xm1 = new Matrix4f(m2xm1_0);
74
75 final float[] m1xm2_0 =
76 new float[]{12557, 893, 748, 1182,
77 893, 116, 116, 113,
78 748, 116, 120, 104,
79 1182, 113, 104, 131 };
80 final Matrix4f m1xm2 = new Matrix4f(m1xm2_0);
81
82 @Test
83 public void test00_load_get() {
84 {
85 final Matrix4f m = new Matrix4f();
86 Assert.assertEquals(mI, m);
87 }
88 {
89 final float[] f16 = new float[16];
90 m1.get(f16);
91 Assert.assertArrayEquals(m1_0, f16, FloatUtil.EPSILON);
92 final Matrix4f m = new Matrix4f();
93 m.load(f16);
94 Assert.assertEquals(m1, m);
95 }
96 }
97
98 @Test
99 public void test01_mul(){
100 {
101 final float[] r_0 = new float[16];
102 FloatUtil.multMatrix(m1_0, 0, m2_0, 0, r_0, 0);
103 Assert.assertArrayEquals(m1xm2_0, r_0, 0f);
104
105 Assert.assertEquals(m1xm2, new Matrix4f(m1).mul(m2));
106 Assert.assertEquals(m1xm2, new Matrix4f().mul(m1, m2));
107 }
108 {
109 final float[] r_0 = new float[16];
110 FloatUtil.multMatrix(m2_0, 0, m1_0, 0, r_0, 0);
111 Assert.assertArrayEquals(m2xm1_0, r_0, 0f);
112
113 Assert.assertEquals(m2xm1, new Matrix4f(m2).mul(m1));
114 Assert.assertEquals(m2xm1, new Matrix4f().mul(m2, m1));
115 }
116 }
117
118 @Test
119 public void test02_transpose() {
120 Assert.assertEquals(m1T, new Matrix4f(m1).transpose());
121 Assert.assertEquals(m1T, new Matrix4f().transpose(m1));
122 }
123
124 @Test
125 public void test80LookAtNegZIsNoOp() throws Exception {
126 final Matrix4f tmp = new Matrix4f();
127 final Matrix4f m = new Matrix4f();
128 // Look towards -z
129 m.setToLookAt(
130 new Vec3f(0, 0, 0), // eye
131 new Vec3f(0, 0, -1), // center
132 new Vec3f(0, 1, 0), // up
133 tmp);
134
135 /**
136 * The 3 rows of the matrix (= the 3 columns of the array/buffer) should be: side, up, -forward.
137 */
138 final Matrix4f exp = new Matrix4f(
139 new float[] {
140 1, 0, 0, 0,
141 0, 1, 0, 0,
142 0, 0, 1, 0,
143 0, 0, 0, 1
144 } );
145
146 Assert.assertEquals(exp, m);
147 }
148
149 @Test
150 public void test81LookAtPosY() throws Exception {
151 final Matrix4f tmp = new Matrix4f();
152 final Matrix4f m = new Matrix4f();
153 // Look towards -z
154 m.setToLookAt(
155 new Vec3f(0, 0, 0), // eye
156 new Vec3f(0, 1, 0), // center
157 new Vec3f(0, 0, 1), // up
158 tmp);
159
160 /**
161 * The 3 rows of the matrix (= the 3 columns of the array/buffer) should be: side, up, -forward.
162 */
163 final Matrix4f exp = new Matrix4f(
164 new float[] {
165 1, 0, 0, 0,
166 0, 0, -1, 0,
167 0, 1, 0, 0,
168 0, 0, 0, 1
169 } );
170
171 Assert.assertEquals(exp, m);
172 }
173
174 public static void main(final String args[]) {
175 org.junit.runner.JUnitCore.main(TestMatrix4f01NOUI.class.getName());
176 }
177}
Basic Float math utility functions.
Definition: FloatUtil.java:83
static final float EPSILON
Epsilon for floating point {@value}, as once computed via getMachineEpsilon() on an AMD-64 CPU.
static void multMatrix(final float[] a, final int a_off, final float[] b, final int b_off, final float[] d, final int d_off)
Multiply matrix: [d] = [a] x [b].
Definition: FloatUtil.java:785
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
Definition: Matrix4f.java:89
Matrix4f load(final Matrix4f src)
Load the values of the given matrix src to this matrix.
Definition: Matrix4f.java:186
float get(final int i)
Gets the ith component, 0 <= i < 16.
Definition: Matrix4f.java:279
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.
Definition: Matrix4f.java:1432
3D Vector based upon three float components.
Definition: Vec3f.java:37