JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestMatrix4fProject01NOUI.java
Go to the documentation of this file.
1/**
2 * Copyright 2014-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 */
28package com.jogamp.opengl.test.junit.math;
29
30import java.util.Arrays;
31
32import com.jogamp.common.nio.Buffers;
33import com.jogamp.junit.util.JunitTracer;
34import com.jogamp.math.FloatUtil;
35import com.jogamp.math.Matrix4f;
36import com.jogamp.math.Recti;
37import com.jogamp.math.Vec3f;
38import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
39import com.jogamp.opengl.glu.GLU;
40
41import jogamp.opengl.gl2.ProjectDouble;
42
43import com.jogamp.opengl.util.PMVMatrix;
44
45import org.junit.Assert;
46import org.junit.Test;
47import org.junit.FixMethodOrder;
48import org.junit.runners.MethodSorters;
49
50@FixMethodOrder(MethodSorters.NAME_ASCENDING)
51public class TestMatrix4fProject01NOUI extends JunitTracer {
52
53 static final float epsilon = 0.00001f;
54
55 // Simple 10 x 10 view port
56 static final Recti viewport = new Recti(0,0,10,10);
57 static final int[] viewport_i4 = new int[] { 0, 0, 10, 10 };
58
59 /**
60 * PMVMatrix w/ separate P + Mv vs Matrix4f.mapObjToWin() w/ single PMv
61 *
62 * Both using same Matrix4f.mapObjToWin(..).
63 */
64 @Test
66 final Vec3f winA00 = new Vec3f();
67 final Vec3f winA01 = new Vec3f();
68 final Vec3f winA10 = new Vec3f();
69 final Vec3f winA11 = new Vec3f();
70 final Vec3f winB00 = new Vec3f();
71 final Vec3f winB01 = new Vec3f();
72 final Vec3f winB10 = new Vec3f();
73 final Vec3f winB11 = new Vec3f();
74
75 final PMVMatrix m = new PMVMatrix();
76 final Matrix4f mat4PMv = new Matrix4f();
77 m.getMulPMv(mat4PMv);
78 System.err.println(mat4PMv.toString(null, "mat4PMv", "%10.5f"));
79
80 m.mapObjToWin(new Vec3f(1f, 0f, 0f), viewport, winA00); // separate P + Mv
81 System.err.println("A.0.0 - Project 1,0 -->" + winA00);
82 Matrix4f.mapObjToWin(new Vec3f(1f, 0f, 0f), mat4PMv, viewport, winB00); // single PMv
83 System.err.println("B.0.0 - Project 1,0 -->" + winB00);
84
85 m.mapObjToWin(new Vec3f(0f, 0f, 0f), viewport, winA01);
86 System.err.println("A.0.1 - Project 0,0 -->" + winA01);
87 Matrix4f.mapObjToWin(new Vec3f(0f, 0f, 0f), mat4PMv, viewport, winB01);
88 System.err.println("B.0.1 - Project 0,0 -->" + winB01);
89
91 m.glOrthof(0, 10, 0, 10, 1, -1);
92 System.err.println("MATRIX - Ortho 0,0,10,10 - Locate the origin in the bottom left and scale");
93 System.err.println(m);
94 m.getMulPMv(mat4PMv);
95 System.err.println(mat4PMv.toString(null, "mat4PMv", "%10.5f"));
96
97 m.mapObjToWin(new Vec3f(1f, 0f, 0f), viewport, winA10);
98 System.err.println("A.1.0 - Project 1,0 -->" +winA10);
99 Matrix4f.mapObjToWin(new Vec3f(1f, 0f, 0f), mat4PMv, viewport, winB10);
100 System.err.println("B.1.0 - Project 1,0 -->" +winB10);
101
102 m.mapObjToWin(new Vec3f(0f, 0f, 0f), viewport, winA11);
103 System.err.println("A.1.1 - Project 0,0 -->" +winA11);
104 Matrix4f.mapObjToWin(new Vec3f(0f, 0f, 0f), mat4PMv, viewport, winB11);
105 System.err.println("B.1.1 - Project 0,0 -->" +winB11);
106
107 Assert.assertEquals("A/B 0.0 Project 1,0 failure", winB00, winA00);
108 Assert.assertEquals("A/B 0.1 Project 0,0 failure", winB01, winA01);
109 Assert.assertEquals("A/B 1.0 Project 1,0 failure", winB10, winA10);
110 Assert.assertEquals("A/B 1.1 Project 0,0 failure", winB11, winA11);
111 }
112
113 /**
114 * PMVMatrix vs Matrix4f.mapObjToWin(), both w/ separate P + Mv
115 *
116 * Both using same Matrix4f.mapObjToWin().
117 */
118 @Test
120 final Vec3f winA00 = new Vec3f();
121 final Vec3f winA01 = new Vec3f();
122 final Vec3f winA10 = new Vec3f();
123 final Vec3f winA11 = new Vec3f();
124 final Vec3f winB00 = new Vec3f();
125 final Vec3f winB01 = new Vec3f();
126 final Vec3f winB10 = new Vec3f();
127 final Vec3f winB11 = new Vec3f();
128
129 final PMVMatrix m = new PMVMatrix();
130 final Matrix4f mat4Mv = new Matrix4f();
131 final Matrix4f mat4P = new Matrix4f();
132 final float[] mat4Mv_f16 = new float[16];
133 final float[] mat4P_f16 = new float[16];
134
137 System.err.println(FloatUtil.matrixToString(null, "mat4Mv", "%10.5f", mat4Mv_f16, 0, 4, 4, false /* rowMajorOrder */));
138 System.err.println(FloatUtil.matrixToString(null, "mat4P ", "%10.5f", mat4P_f16, 0, 4, 4, false /* rowMajorOrder */));
141 Assert.assertEquals(new Matrix4f(mat4Mv_f16), mat4Mv);
142 Assert.assertEquals(new Matrix4f(mat4P_f16), mat4P);
143 Assert.assertEquals(mat4Mv, m.getMv());
144 Assert.assertEquals(mat4P, m.getP());
145
146 m.mapObjToWin(new Vec3f(1f, 0f, 0f), viewport, winA00);
147 System.err.println("A.0.0 - Project 1,0 -->" + winA00);
148 Matrix4f.mapObjToWin(new Vec3f(1f, 0f, 0f), mat4Mv, mat4P, viewport, winB00);
149 System.err.println("B.0.0 - Project 1,0 -->" + winB00);
150
151 m.mapObjToWin(new Vec3f(0f, 0f, 0f), viewport, winA01);
152 System.err.println("A.0.1 - Project 0,0 -->" + winA01);
153 Matrix4f.mapObjToWin(new Vec3f(0f, 0f, 0f), mat4Mv, mat4P, viewport, winB01);
154 System.err.println("B.0.1 - Project 0,0 -->" + winB01);
155
157 m.glOrthof(0, 10, 0, 10, 1, -1);
158 System.err.println("MATRIX - Ortho 0,0,10,10 - Locate the origin in the bottom left and scale");
159 System.err.println(m);
162 System.err.println(FloatUtil.matrixToString(null, "mat4Mv", "%10.5f", mat4Mv_f16, 0, 4, 4, false /* rowMajorOrder */));
163 System.err.println(FloatUtil.matrixToString(null, "mat4P ", "%10.5f", mat4P_f16, 0, 4, 4, false /* rowMajorOrder */));
166 Assert.assertEquals(new Matrix4f(mat4Mv_f16), mat4Mv);
167 Assert.assertEquals(new Matrix4f(mat4P_f16), mat4P);
168 Assert.assertEquals(mat4Mv, m.getMv());
169 Assert.assertEquals(mat4P, m.getP());
170
171 m.mapObjToWin(new Vec3f(1f, 0f, 0f), viewport, winA10);
172 System.err.println("A.1.0 - Project 1,0 -->" +winA10);
173 Matrix4f.mapObjToWin(new Vec3f(1f, 0f, 0f), mat4Mv, mat4P, viewport, winB10);
174 System.err.println("B.1.0 - Project 1,0 -->" +winB10);
175
176 m.mapObjToWin(new Vec3f(0f, 0f, 0f), viewport, winA11);
177 System.err.println("A.1.1 - Project 0,0 -->" +winA11);
178 Matrix4f.mapObjToWin(new Vec3f(0f, 0f, 0f), mat4Mv, mat4P, viewport, winB11);
179 System.err.println("B.1.1 - Project 0,0 -->" +winB11);
180
181 Assert.assertEquals("A/B 0.0 Project 1,0 failure", winB00, winA00);
182 Assert.assertEquals("A/B 0.1 Project 0,0 failure", winB01, winA01);
183 Assert.assertEquals("A/B 1.0 Project 1,0 failure", winB10, winA10);
184 Assert.assertEquals("A/B 1.1 Project 0,0 failure", winB11, winA11);
185 }
186
187 /**
188 * GLU ProjectFloat vs Matrix4f.mapObjToWin(), both w/ separate P + Mv
189 */
190 @Test
191 public void test03GLUToMatrix4f2() {
192 final float[] winA00 = new float[4];
193 final float[] winA01 = new float[4];
194 final float[] winA10 = new float[4];
195 final float[] winA11 = new float[4];
196 final Vec3f winB00 = new Vec3f();
197 final Vec3f winB01 = new Vec3f();
198 final Vec3f winB10 = new Vec3f();
199 final Vec3f winB11 = new Vec3f();
200
201 final PMVMatrix m = new PMVMatrix();
202 final Matrix4f mat4Mv = new Matrix4f();
203 final Matrix4f mat4P = new Matrix4f();
204 final float[] mat4Mv_f16 = new float[16];
205 final float[] mat4P_f16 = new float[16];
206 final GLU glu = new GLU();
207
210 System.err.println(FloatUtil.matrixToString(null, "mat4Mv", "%10.5f", mat4Mv_f16, 0, 4, 4, false /* rowMajorOrder */));
211 System.err.println(FloatUtil.matrixToString(null, "mat4P ", "%10.5f", mat4P_f16, 0, 4, 4, false /* rowMajorOrder */));
212 mat4Mv.load( m.getMv() );
213 mat4P.load( m.getP() );
214
215 glu.gluProject(1f, 0f, 0f, mat4Mv_f16, 0, mat4P_f16, 0, viewport_i4, 0, winA00, 0);
216 System.err.println("A.0.0 - Project 1,0 -->" + winA00);
217 Matrix4f.mapObjToWin(new Vec3f(1f, 0f, 0f), mat4Mv, mat4P, viewport, winB00);
218 System.err.println("B.0.0 - Project 1,0 -->" + winB00);
219
220 glu.gluProject(0f, 0f, 0f, mat4Mv_f16, 0, mat4P_f16, 0, viewport_i4, 0, winA01, 0);
221 System.err.println("A.0.1 - Project 0,0 -->" + winA01);
222 Matrix4f.mapObjToWin(new Vec3f(0f, 0f, 0f), mat4Mv, mat4P, viewport, winB01);
223 System.err.println("B.0.1 - Project 0,0 -->" + winB01);
224
226 m.glOrthof(0, 10, 0, 10, 1, -1);
227 System.err.println("MATRIX - Ortho 0,0,10,10 - Locate the origin in the bottom left and scale");
228 System.err.println(m);
231 System.err.println(FloatUtil.matrixToString(null, "mat4Mv", "%10.5f", mat4Mv_f16, 0, 4, 4, false /* rowMajorOrder */));
232 System.err.println(FloatUtil.matrixToString(null, "mat4P ", "%10.5f", mat4P_f16, 0, 4, 4, false /* rowMajorOrder */));
233 mat4Mv.load( m.getMv() );
234 mat4P.load( m.getP() );
235
236 glu.gluProject(1f, 0f, 0f, mat4Mv_f16, 0, mat4P_f16, 0, viewport_i4, 0, winA10, 0);
237 System.err.println("A.1.0 - Project 1,0 -->" +winA10);
238 Matrix4f.mapObjToWin(new Vec3f(1f, 0f, 0f), mat4Mv, mat4P, viewport, winB10);
239 System.err.println("B.1.0 - Project 1,0 -->" +winB10);
240
241 glu.gluProject(0f, 0f, 0f, mat4Mv_f16, 0, mat4P_f16, 0, viewport_i4, 0, winA11, 0);
242 System.err.println("A.1.1 - Project 0,0 -->" +winA11);
243 Matrix4f.mapObjToWin(new Vec3f(0f, 0f, 0f), mat4Mv, mat4P, viewport, winB11);
244 System.err.println("B.1.1 - Project 0,0 -->" +winB11);
245
246 Assert.assertEquals("A/B 0.0 Project 1,0 failure", winB00, new Vec3f(winA00));
247 Assert.assertEquals("A/B 0.1 Project 0,0 failure", winB01, new Vec3f(winA01));
248 Assert.assertEquals("A/B 1.0 Project 1,0 failure", winB10, new Vec3f(winA10));
249 Assert.assertEquals("A/B 1.1 Project 0,0 failure", winB11, new Vec3f(winA11));
250 }
251
252 /**
253 * GLU ProjectDouble vs Matrix4f.mapObjToWin(), both w/ separate P + Mv
254 */
255 @Test
257 final double[] winA00 = new double[3];
258 final double[] winA01 = new double[3];
259 final double[] winA10 = new double[3];
260 final double[] winA11 = new double[3];
261 final Vec3f winB00 = new Vec3f();
262 final Vec3f winB01 = new Vec3f();
263 final Vec3f winB10 = new Vec3f();
264 final Vec3f winB11 = new Vec3f();
265
266 final PMVMatrix m = new PMVMatrix();
267 final Matrix4f mat4Mv = new Matrix4f();
268 final Matrix4f mat4P = new Matrix4f();
269 final float[] mat4Mv_f16 = new float[16];
270 final float[] mat4P_f16 = new float[16];
271 final ProjectDouble glu = new ProjectDouble();
272
275 System.err.println(FloatUtil.matrixToString(null, "mat4Mv", "%10.5f", mat4Mv_f16, 0, 4, 4, false /* rowMajorOrder */));
276 System.err.println(FloatUtil.matrixToString(null, "mat4P ", "%10.5f", mat4P_f16, 0, 4, 4, false /* rowMajorOrder */));
277 mat4Mv.load( m.getMv() );
278 mat4P.load( m.getP() );
279 double[] mat4Mv_d16 = Buffers.getDoubleArray(mat4Mv_f16, 0, null, 0, -1);
280 double[] mat4P_d16 = Buffers.getDoubleArray(mat4P_f16, 0, null, 0, -1);
281
282 glu.gluProject(1f, 0f, 0f, mat4Mv_d16, 0, mat4P_d16, 0, viewport_i4, 0, winA00, 0);
283 System.err.println("A.0.0 - Project 1,0 -->" + Arrays.toString(winA00));
284 Matrix4f.mapObjToWin(new Vec3f(1f, 0f, 0f), mat4Mv, mat4P, viewport, winB00);
285 System.err.println("B.0.0 - Project 1,0 -->" + winB00);
286
287 glu.gluProject(0f, 0f, 0f, mat4Mv_d16, 0, mat4P_d16, 0, viewport_i4, 0, winA01, 0);
288 System.err.println("A.0.1 - Project 0,0 -->" + Arrays.toString(winA01));
289 Matrix4f.mapObjToWin(new Vec3f(0f, 0f, 0f), mat4Mv, mat4P, viewport, winB01);
290 System.err.println("B.0.1 - Project 0,0 -->" + winB01);
291
293 m.glOrthof(0, 10, 0, 10, 1, -1);
294 System.err.println("MATRIX - Ortho 0,0,10,10 - Locate the origin in the bottom left and scale");
295 System.err.println(m);
298 System.err.println(FloatUtil.matrixToString(null, "mat4Mv", "%10.5f", mat4Mv_f16, 0, 4, 4, false /* rowMajorOrder */));
299 System.err.println(FloatUtil.matrixToString(null, "mat4P ", "%10.5f", mat4P_f16, 0, 4, 4, false /* rowMajorOrder */));
300 mat4Mv.load( m.getMv() );
301 mat4P.load( m.getP() );
302 mat4Mv_d16 = Buffers.getDoubleArray(mat4Mv_f16, 0, null, 0, -1);
303 mat4P_d16 = Buffers.getDoubleArray(mat4P_f16, 0, null, 0, -1);
304
305 glu.gluProject(1f, 0f, 0f, mat4Mv_d16, 0, mat4P_d16, 0, viewport_i4, 0, winA10, 0);
306 System.err.println("A.1.0 - Project 1,0 -->" +Arrays.toString(winA10));
307 Matrix4f.mapObjToWin(new Vec3f(1f, 0f, 0f), mat4Mv, mat4P, viewport, winB10);
308 System.err.println("B.1.0 - Project 1,0 -->" +winB10);
309
310 glu.gluProject(0f, 0f, 0f, mat4Mv_d16, 0, mat4P_d16, 0, viewport_i4, 0, winA11, 0);
311 System.err.println("A.1.1 - Project 0,0 -->" +Arrays.toString(winA11));
312 Matrix4f.mapObjToWin(new Vec3f(0f, 0f, 0f), mat4Mv, mat4P, viewport, winB11);
313 System.err.println("B.1.1 - Project 0,0 -->" +winB11);
314
315 final float[] tmp = new float[3];
316 double[] d_winBxx = Buffers.getDoubleArray(winB00.get(tmp), 0, null, 0, -1);
317 Assert.assertArrayEquals("A/B 0.0 Project 1,0 failure", d_winBxx, winA00, epsilon);
318 d_winBxx = Buffers.getDoubleArray(winB01.get(tmp), 0, null, 0, -1);
319 Assert.assertArrayEquals("A/B 0.1 Project 0,0 failure", d_winBxx, winA01, epsilon);
320 d_winBxx = Buffers.getDoubleArray(winB10.get(tmp), 0, null, 0, -1);
321 Assert.assertArrayEquals("A/B 1.0 Project 1,0 failure", d_winBxx, winA10, epsilon);
322 d_winBxx = Buffers.getDoubleArray(winB11.get(tmp), 0, null, 0, -1);
323 Assert.assertArrayEquals("A/B 1.1 Project 0,0 failure", d_winBxx, winA11, epsilon);
324 }
325
326 public static void main(final String args[]) {
327 org.junit.runner.JUnitCore.main(TestMatrix4fProject01NOUI.class.getName());
328 }
329}
Basic Float math utility functions.
Definition: FloatUtil.java:83
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)
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
Definition: Matrix4f.java:89
static boolean mapObjToWin(final Vec3f obj, final Matrix4f mMv, final Matrix4f mP, final Recti viewport, final Vec3f winPos)
Map object coordinates to window coordinates.
Definition: Matrix4f.java:1696
Matrix4f load(final Matrix4f src)
Load the values of the given matrix src to this matrix.
Definition: Matrix4f.java:186
StringBuilder toString(final StringBuilder sb, final String rowPrefix, final String f)
Definition: Matrix4f.java:2085
Rectangle with x, y, width and height integer components.
Definition: Recti.java:34
3D Vector based upon three float components.
Definition: Vec3f.java:37
float[] get(final float[] xyz)
xyz = this, returns xyz.
Definition: Vec3f.java:137
final Matrix4f getMulPMv(final Matrix4f result)
Returns multiplication result of P and Mv matrix, i.e.
final Matrix4f getMv()
Returns the modelview matrix (Mv).
final Matrix4f getP()
Returns the projection matrix (P).
final boolean mapObjToWin(final Vec3f objPos, final Recti viewport, final Vec3f winPos)
Map object coordinates to window coordinates.
Provides access to the OpenGL Utility Library (GLU).
Definition: GLU.java:43
boolean gluProject(float objX, float objY, float objZ, float[] model, int model_offset, float[] proj, int proj_offset, int[] view, int view_offset, float[] winPos, int winPos_offset)
Interface to C language function: GLint gluProject(GLdouble objX, GLdouble objY,...
Definition: GLU.java:1373
void test01PMVMatrixToMatrix4f2()
PMVMatrix vs Matrix4f.mapObjToWin(), both w/ separate P + Mv.
void test01PMVMatrixToMatrix4f()
PMVMatrix w/ separate P + Mv vs Matrix4f.mapObjToWin() w/ single PMv.
void test04GLUDoubleToMatrix4f2()
GLU ProjectDouble vs Matrix4f.mapObjToWin(), both w/ separate P + Mv.
void test03GLUToMatrix4f2()
GLU ProjectFloat vs Matrix4f.mapObjToWin(), both w/ separate P + Mv.
PMVMatrix implements a subset of the fixed function pipeline GLMatrixFunc using PMVMatrix4f.
Definition: PMVMatrix.java:62
final void glMatrixMode(final int matrixName)
Sets the current matrix mode.
Definition: PMVMatrix.java:218
final Matrix4f getMat(final int matrixName)
Definition: PMVMatrix.java:201
final void glGetFloatv(final int matrixGetName, final FloatBuffer params)
Definition: PMVMatrix.java:231
final void glOrthof(final float left, final float right, final float bottom, final float top, final float zNear, final float zFar)
Multiply the current matrix with the orthogonal matrix.
Definition: PMVMatrix.java:469
Subset of OpenGL fixed function pipeline's matrix operations.
static final int GL_PROJECTION
Matrix mode projection.
static final int GL_MODELVIEW_MATRIX
Matrix access name for modelview.
static final int GL_PROJECTION_MATRIX
Matrix access name for projection.