-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathMatrix4f.java
More file actions
447 lines (386 loc) · 14.3 KB
/
Matrix4f.java
File metadata and controls
447 lines (386 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* The MIT License (MIT)
*
* Copyright © 2015-2017, Heiko Brumme
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package silvertiger.tutorial.lwjgl.math;
import java.nio.FloatBuffer;
/**
* This class represents a 4x4-Matrix. GLSL equivalent to mat4.
*
* @author Heiko Brumme
*/
public class Matrix4f {
private float m00, m01, m02, m03;
private float m10, m11, m12, m13;
private float m20, m21, m22, m23;
private float m30, m31, m32, m33;
/**
* Creates a 4x4 identity matrix.
*/
public Matrix4f() {
setIdentity();
}
/**
* Creates a 4x4 matrix with specified columns.
*
* @param col1 Vector with values of the first column
* @param col2 Vector with values of the second column
* @param col3 Vector with values of the third column
* @param col4 Vector with values of the fourth column
*/
public Matrix4f(Vector4f col1, Vector4f col2, Vector4f col3, Vector4f col4) {
m00 = col1.x;
m10 = col1.y;
m20 = col1.z;
m30 = col1.w;
m01 = col2.x;
m11 = col2.y;
m21 = col2.z;
m31 = col2.w;
m02 = col3.x;
m12 = col3.y;
m22 = col3.z;
m32 = col3.w;
m03 = col4.x;
m13 = col4.y;
m23 = col4.z;
m33 = col4.w;
}
/**
* Sets this matrix to the identity matrix.
*/
public final void setIdentity() {
m00 = 1f;
m11 = 1f;
m22 = 1f;
m33 = 1f;
m01 = 0f;
m02 = 0f;
m03 = 0f;
m10 = 0f;
m12 = 0f;
m13 = 0f;
m20 = 0f;
m21 = 0f;
m23 = 0f;
m30 = 0f;
m31 = 0f;
m32 = 0f;
}
/**
* Adds this matrix to another matrix.
*
* @param other The other matrix
*
* @return Sum of this + other
*/
public Matrix4f add(Matrix4f other) {
Matrix4f result = new Matrix4f();
result.m00 = this.m00 + other.m00;
result.m10 = this.m10 + other.m10;
result.m20 = this.m20 + other.m20;
result.m30 = this.m30 + other.m30;
result.m01 = this.m01 + other.m01;
result.m11 = this.m11 + other.m11;
result.m21 = this.m21 + other.m21;
result.m31 = this.m31 + other.m31;
result.m02 = this.m02 + other.m02;
result.m12 = this.m12 + other.m12;
result.m22 = this.m22 + other.m22;
result.m32 = this.m32 + other.m32;
result.m03 = this.m03 + other.m03;
result.m13 = this.m13 + other.m13;
result.m23 = this.m23 + other.m23;
result.m33 = this.m33 + other.m33;
return result;
}
/**
* Negates this matrix.
*
* @return Negated matrix
*/
public Matrix4f negate() {
return multiply(-1f);
}
/**
* Subtracts this matrix from another matrix.
*
* @param other The other matrix
*
* @return Difference of this - other
*/
public Matrix4f subtract(Matrix4f other) {
return this.add(other.negate());
}
/**
* Multiplies this matrix with a scalar.
*
* @param scalar The scalar
*
* @return Scalar product of this * scalar
*/
public Matrix4f multiply(float scalar) {
Matrix4f result = new Matrix4f();
result.m00 = this.m00 * scalar;
result.m10 = this.m10 * scalar;
result.m20 = this.m20 * scalar;
result.m30 = this.m30 * scalar;
result.m01 = this.m01 * scalar;
result.m11 = this.m11 * scalar;
result.m21 = this.m21 * scalar;
result.m31 = this.m31 * scalar;
result.m02 = this.m02 * scalar;
result.m12 = this.m12 * scalar;
result.m22 = this.m22 * scalar;
result.m32 = this.m32 * scalar;
result.m03 = this.m03 * scalar;
result.m13 = this.m13 * scalar;
result.m23 = this.m23 * scalar;
result.m33 = this.m33 * scalar;
return result;
}
/**
* Multiplies this matrix to a vector.
*
* @param vector The vector
*
* @return Vector product of this * other
*/
public Vector4f multiply(Vector4f vector) {
float x = this.m00 * vector.x + this.m01 * vector.y + this.m02 * vector.z + this.m03 * vector.w;
float y = this.m10 * vector.x + this.m11 * vector.y + this.m12 * vector.z + this.m13 * vector.w;
float z = this.m20 * vector.x + this.m21 * vector.y + this.m22 * vector.z + this.m23 * vector.w;
float w = this.m30 * vector.x + this.m31 * vector.y + this.m32 * vector.z + this.m33 * vector.w;
return new Vector4f(x, y, z, w);
}
/**
* Multiplies this matrix to another matrix.
*
* @param other The other matrix
*
* @return Matrix product of this * other
*/
public Matrix4f multiply(Matrix4f other) {
Matrix4f result = new Matrix4f();
result.m00 = this.m00 * other.m00 + this.m01 * other.m10 + this.m02 * other.m20 + this.m03 * other.m30;
result.m10 = this.m10 * other.m00 + this.m11 * other.m10 + this.m12 * other.m20 + this.m13 * other.m30;
result.m20 = this.m20 * other.m00 + this.m21 * other.m10 + this.m22 * other.m20 + this.m23 * other.m30;
result.m30 = this.m30 * other.m00 + this.m31 * other.m10 + this.m32 * other.m20 + this.m33 * other.m30;
result.m01 = this.m00 * other.m01 + this.m01 * other.m11 + this.m02 * other.m21 + this.m03 * other.m31;
result.m11 = this.m10 * other.m01 + this.m11 * other.m11 + this.m12 * other.m21 + this.m13 * other.m31;
result.m21 = this.m20 * other.m01 + this.m21 * other.m11 + this.m22 * other.m21 + this.m23 * other.m31;
result.m31 = this.m30 * other.m01 + this.m31 * other.m11 + this.m32 * other.m21 + this.m33 * other.m31;
result.m02 = this.m00 * other.m02 + this.m01 * other.m12 + this.m02 * other.m22 + this.m03 * other.m32;
result.m12 = this.m10 * other.m02 + this.m11 * other.m12 + this.m12 * other.m22 + this.m13 * other.m32;
result.m22 = this.m20 * other.m02 + this.m21 * other.m12 + this.m22 * other.m22 + this.m23 * other.m32;
result.m32 = this.m30 * other.m02 + this.m31 * other.m12 + this.m32 * other.m22 + this.m33 * other.m32;
result.m03 = this.m00 * other.m03 + this.m01 * other.m13 + this.m02 * other.m23 + this.m03 * other.m33;
result.m13 = this.m10 * other.m03 + this.m11 * other.m13 + this.m12 * other.m23 + this.m13 * other.m33;
result.m23 = this.m20 * other.m03 + this.m21 * other.m13 + this.m22 * other.m23 + this.m23 * other.m33;
result.m33 = this.m30 * other.m03 + this.m31 * other.m13 + this.m32 * other.m23 + this.m33 * other.m33;
return result;
}
/**
* Transposes this matrix.
*
* @return Transposed matrix
*/
public Matrix4f transpose() {
Matrix4f result = new Matrix4f();
result.m00 = this.m00;
result.m10 = this.m01;
result.m20 = this.m02;
result.m30 = this.m03;
result.m01 = this.m10;
result.m11 = this.m11;
result.m21 = this.m12;
result.m31 = this.m13;
result.m02 = this.m20;
result.m12 = this.m21;
result.m22 = this.m22;
result.m32 = this.m23;
result.m03 = this.m30;
result.m13 = this.m31;
result.m23 = this.m32;
result.m33 = this.m33;
return result;
}
/**
* Stores the matrix in a given Buffer.
*
* @param buffer The buffer to store the matrix data
*/
public void toBuffer(FloatBuffer buffer) {
buffer.put(m00).put(m10).put(m20).put(m30);
buffer.put(m01).put(m11).put(m21).put(m31);
buffer.put(m02).put(m12).put(m22).put(m32);
buffer.put(m03).put(m13).put(m23).put(m33);
buffer.flip();
}
/**
* Creates a orthographic projection matrix. Similar to
* <code>glOrtho(left, right, bottom, top, near, far)</code>.
*
* @param left Coordinate for the left vertical clipping pane
* @param right Coordinate for the right vertical clipping pane
* @param bottom Coordinate for the bottom horizontal clipping pane
* @param top Coordinate for the bottom horizontal clipping pane
* @param near Coordinate for the near depth clipping pane
* @param far Coordinate for the far depth clipping pane
*
* @return Orthographic matrix
*/
public static Matrix4f orthographic(float left, float right, float bottom, float top, float near, float far) {
Matrix4f ortho = new Matrix4f();
float tx = -(right + left) / (right - left);
float ty = -(top + bottom) / (top - bottom);
float tz = -(far + near) / (far - near);
ortho.m00 = 2f / (right - left);
ortho.m11 = 2f / (top - bottom);
ortho.m22 = -2f / (far - near);
ortho.m03 = tx;
ortho.m13 = ty;
ortho.m23 = tz;
return ortho;
}
/**
* Creates a perspective projection matrix. Similar to
* <code>glFrustum(left, right, bottom, top, near, far)</code>.
*
* @param left Coordinate for the left vertical clipping pane
* @param right Coordinate for the right vertical clipping pane
* @param bottom Coordinate for the bottom horizontal clipping pane
* @param top Coordinate for the bottom horizontal clipping pane
* @param near Coordinate for the near depth clipping pane, must be
* positive
* @param far Coordinate for the far depth clipping pane, must be
* positive
*
* @return Perspective matrix
*/
public static Matrix4f frustum(float left, float right, float bottom, float top, float near, float far) {
Matrix4f frustum = new Matrix4f();
float a = (right + left) / (right - left);
float b = (top + bottom) / (top - bottom);
float c = -(far + near) / (far - near);
float d = -(2f * far * near) / (far - near);
frustum.m00 = (2f * near) / (right - left);
frustum.m11 = (2f * near) / (top - bottom);
frustum.m02 = a;
frustum.m12 = b;
frustum.m22 = c;
frustum.m32 = -1f;
frustum.m23 = d;
frustum.m33 = 0f;
return frustum;
}
/**
* Creates a perspective projection matrix. Similar to
* <code>gluPerspective(fovy, aspec, zNear, zFar)</code>.
*
* @param fovy Field of view angle in degrees
* @param aspect The aspect ratio is the ratio of width to height
* @param near Distance from the viewer to the near clipping plane, must
* be positive
* @param far Distance from the viewer to the far clipping plane, must be
* positive
*
* @return Perspective matrix
*/
public static Matrix4f perspective(float fovy, float aspect, float near, float far) {
Matrix4f perspective = new Matrix4f();
float f = (float) (1f / Math.tan(Math.toRadians(fovy) / 2f));
perspective.m00 = f / aspect;
perspective.m11 = f;
perspective.m22 = (far + near) / (near - far);
perspective.m32 = -1f;
perspective.m23 = (2f * far * near) / (near - far);
perspective.m33 = 0f;
return perspective;
}
/**
* Creates a translation matrix. Similar to
* <code>glTranslate(x, y, z)</code>.
*
* @param x x coordinate of translation vector
* @param y y coordinate of translation vector
* @param z z coordinate of translation vector
*
* @return Translation matrix
*/
public static Matrix4f translate(float x, float y, float z) {
Matrix4f translation = new Matrix4f();
translation.m03 = x;
translation.m13 = y;
translation.m23 = z;
return translation;
}
/**
* Creates a rotation matrix. Similar to
* <code>glRotate(angle, x, y, z)</code>.
*
* @param angle Angle of rotation in degrees
* @param x x coordinate of the rotation vector
* @param y y coordinate of the rotation vector
* @param z z coordinate of the rotation vector
*
* @return Rotation matrix
*/
public static Matrix4f rotate(float angle, float x, float y, float z) {
Matrix4f rotation = new Matrix4f();
float c = (float) Math.cos(Math.toRadians(angle));
float s = (float) Math.sin(Math.toRadians(angle));
Vector3f vec = new Vector3f(x, y, z);
if (vec.length() != 1f) {
vec = vec.normalize();
x = vec.x;
y = vec.y;
z = vec.z;
}
rotation.m00 = x * x * (1f - c) + c;
rotation.m10 = y * x * (1f - c) + z * s;
rotation.m20 = x * z * (1f - c) - y * s;
rotation.m01 = x * y * (1f - c) - z * s;
rotation.m11 = y * y * (1f - c) + c;
rotation.m21 = y * z * (1f - c) + x * s;
rotation.m02 = x * z * (1f - c) + y * s;
rotation.m12 = y * z * (1f - c) - x * s;
rotation.m22 = z * z * (1f - c) + c;
return rotation;
}
/**
* Creates a scaling matrix. Similar to <code>glScale(x, y, z)</code>.
*
* @param x Scale factor along the x coordinate
* @param y Scale factor along the y coordinate
* @param z Scale factor along the z coordinate
*
* @return Scaling matrix
*/
public static Matrix4f scale(float x, float y, float z) {
Matrix4f scaling = new Matrix4f();
scaling.m00 = x;
scaling.m11 = y;
scaling.m22 = z;
return scaling;
}
}