Skip to content

Commit 970cb80

Browse files
committed
Tmp
1 parent 3bdd0a1 commit 970cb80

File tree

1 file changed

+112
-22
lines changed

1 file changed

+112
-22
lines changed

src/main/java/math/Matrix4f.java

Lines changed: 112 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -241,30 +241,66 @@ public static Matrix4f lookAt(Vector3f eye, Vector3f target, Vector3f up) {
241241
* @return This matrix for chaining calls.
242242
* @throws IllegalArgumentException if the input parameters are invalid.
243243
*/
244-
public Matrix4f setPerspective(float fov, float aspect, float nearPlane, float farPlane) {
245-
if (nearPlane > farPlane) {
246-
throw new IllegalArgumentException(
247-
String.format(
248-
"Near plane (%.2f) cannot be greater than far plane (%.2f).", nearPlane, farPlane));
249-
}
250-
if (aspect <= 0) {
251-
throw new IllegalArgumentException("Aspect ratio must be a positive number.");
252-
}
253-
if (fov <= 0.0 || fov >= Math.PI) {
254-
throw new IllegalArgumentException("Field of view must be between 0 and π radians.");
244+
// public Matrix4f setPerspective(float fov, float aspect, float nearPlane, float farPlane) {
245+
// if (nearPlane > farPlane) {
246+
// throw new IllegalArgumentException(
247+
// String.format(
248+
// "Near plane (%.2f) cannot be greater than far plane (%.2f).", nearPlane, farPlane));
249+
// }
250+
// if (aspect <= 0) {
251+
// throw new IllegalArgumentException("Aspect ratio must be a positive number.");
252+
// }
253+
// if (fov <= 0.0 || fov >= Math.PI) {
254+
// throw new IllegalArgumentException("Field of view must be between 0 and π radians.");
255+
// }
256+
//
257+
// float f = (float) (1.0 / Math.tan(fov / 2.0));
258+
// Arrays.fill(values, 0);
259+
// values[0] = f / aspect;
260+
// values[5] = f;
261+
// values[10] = (farPlane + nearPlane) / (nearPlane - farPlane);
262+
// values[11] = -1;
263+
// values[14] = (2 * farPlane * nearPlane) / (nearPlane - farPlane);
264+
//
265+
// return this;
266+
// }
267+
268+
public Matrix4f setPerspective(float fov, float aspect, float nearPlane, float farPlane) {
269+
// Check if the near and far planes are valid
270+
if (nearPlane <= 0 || farPlane <= 0) {
271+
throw new IllegalArgumentException("Near and far planes must be positive values.");
272+
}
273+
if (nearPlane > farPlane) {
274+
throw new IllegalArgumentException(
275+
String.format(
276+
"Near plane (%.2f) cannot be greater than far plane (%.2f).", nearPlane,
277+
farPlane));
278+
}
279+
if (aspect <= 0) {
280+
throw new IllegalArgumentException("Aspect ratio must be a positive number.");
281+
}
282+
if (fov <= 0.0 || fov >= Math.PI) {
283+
throw new IllegalArgumentException("Field of view must be between 0 and π radians.");
284+
}
285+
286+
// Calculate the tangent of the vertical field of view angle
287+
float f = (float) (1.0 / Math.tan(fov / 2.0));
288+
289+
// Reset the matrix values to zero
290+
Arrays.fill(values, 0);
291+
292+
// Set the perspective matrix values
293+
values[0] = f / aspect; // Horizontal scaling factor
294+
values[5] = f; // Vertical scaling factor
295+
values[10] = -(farPlane + nearPlane) / (farPlane - nearPlane); // Depth scaling factor
296+
values[11] = -1; // Far/near plane relationship
297+
values[14] =
298+
-(2 * farPlane * nearPlane) / (farPlane - nearPlane); // Near and far planes interaction
299+
values[15] = 0; // Perspective divide (used for homogeneous coordinates)
300+
301+
return this;
255302
}
256303

257-
float f = (float) (1.0 / Math.tan(fov / 2.0));
258-
Arrays.fill(values, 0);
259-
values[0] = f / aspect;
260-
values[5] = f;
261-
values[10] = (farPlane + nearPlane) / (nearPlane - farPlane);
262-
values[11] = -1;
263-
values[14] = (2 * farPlane * nearPlane) / (nearPlane - farPlane);
264-
265-
return this;
266-
}
267-
268304
// /**
269305
// * Constructs a right-handed view matrix for an FPS (First-Person Shooter)
270306
// * style camera with -Y as up.
@@ -365,6 +401,60 @@ public static Matrix4f fpsViewRH(Vector3f eye, float pitch, float yaw) {
365401

366402
return viewMatrix;
367403
}
404+
405+
public Matrix4f rotateX(float angle) {
406+
float cos = (float) Math.cos(angle);
407+
float sin = (float) Math.sin(angle);
408+
Matrix4f rotation = new Matrix4f(
409+
1, 0, 0, 0,
410+
0, cos, -sin, 0,
411+
0, sin, cos, 0,
412+
0, 0, 0, 1
413+
);
414+
return this.multiply(rotation);
415+
}
416+
417+
public Matrix4f rotateY(float angle) {
418+
float cos = (float) Math.cos(angle);
419+
float sin = (float) Math.sin(angle);
420+
Matrix4f rotation = new Matrix4f(
421+
cos, 0, sin, 0,
422+
0, 1, 0, 0,
423+
-sin, 0, cos, 0,
424+
0, 0, 0, 1
425+
);
426+
return this.multiply(rotation);
427+
}
428+
429+
public Matrix4f rotateZ(float angle) {
430+
float cos = (float) Math.cos(angle);
431+
float sin = (float) Math.sin(angle);
432+
Matrix4f rotation = new Matrix4f(
433+
cos, -sin, 0, 0,
434+
sin, cos, 0, 0,
435+
0, 0, 1, 0,
436+
0, 0, 0, 1
437+
);
438+
return this.multiply(rotation);
439+
}
440+
441+
public Matrix4f translate(float x, float y, float z) {
442+
Matrix4f translation = new Matrix4f(
443+
1, 0, 0, x,
444+
0, 1, 0, y,
445+
0, 0, 1, z,
446+
0, 0, 0, 1
447+
);
448+
return this.multiply(translation);
449+
}
450+
451+
public Matrix4f setViewMatrix(Vector3f rotation, Vector3f position) {
452+
return this.identity()
453+
.rotateX(-rotation.x)
454+
.rotateY(-rotation.y)
455+
.rotateZ(-rotation.z)
456+
.translate(-position.x, -position.y, -position.z);
457+
}
368458

369459
@Override
370460
public String toString() {

0 commit comments

Comments
 (0)