This is how am creating transformationMatrix and the respective results, followed by projection matrix and the results
public static Matrix4f createTransformationMatrix(Vector3f translation, float rx, float ry,
float rz, float scale) {
Matrix4f matrix = new Matrix4f();
matrix.setIdentity();
Matrix4f.translate(translation, matrix, matrix);
Matrix4f.rotate((float) Math.toRadians(rx), new Vector3f(1,0,0), matrix, matrix);
Matrix4f.rotate((float) Math.toRadians(ry), new Vector3f(0,1,0), matrix, matrix);
Matrix4f.rotate((float) Math.toRadians(rz), new Vector3f(0,0,1), matrix, matrix);
Matrix4f.scale(new Vector3f(scale,scale,scale), matrix, matrix);
return matrix;
}
1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0 -5.0
0.0 0.0 0.0 1.0
//ProjectionMatrix
public void createProjectionMatrix() {
float aspectRatio = (float) Display.getWidth() / (float) Display.getHeight();
float y_scale = (float) ((1f / Math.tan(Math.toRadians(FOV / 2f))) * aspectRatio);
float x_scale = y_scale / aspectRatio;
float frustum_length = FAR_PLANE - NEAR_PLANE;
projectionMatrix = new Matrix4f();
projectionMatrix.m00 = x_scale;
projectionMatrix.m11 = y_scale;
projectionMatrix.m22 = -((FAR_PLANE + NEAR_PLANE) / frustum_length);
projectionMatrix.m23 = -1;
projectionMatrix.m32 = -((2 * NEAR_PLANE * FAR_PLANE) / frustum_length);
projectionMatrix.m33 = 0;
}
1.428148 0.0 0.0 0.0
0.0 1.7236269 0.0 0.0
0.0 0.0 -1.0001999 -0.20002
0.0 0.0 -1.0 0.0
am multiplying ProjectionMatrix * transformationMatrix
Matrix4f mvp = ProjectionMatrix * transformationMatrix; // am supplying this as column major to joml mvp matrix.
Later am using this method to calculate the hit.
// Resultant MVP matrix that am supplying
1.4281 ,0, 0, 0
0, 1.723, 0, 0
0, 0, -1.00, -1
0, 0, 4.8005, 5
// int[] viewport = {0, 0, 700, 580};
// float[] vertices = {
// -0.5f, 0.5f, 0,
// -0.5f, -0.5f, 0,
// 0.5f, -0.5f, 0
// };
public static void selectTriangle(Matrix4f mvp, int[] viewport, float[] vertices)
{
float mouseX = Mouse.getX();
float mouseY = Mouse.getY();
org.joml.Vector3f origin = new org.joml.Vector3f();
org.joml.Vector3f dir = new org.joml.Vector3f();
mvp.unprojectRay(mouseX, mouseY,viewport,origin,dir);
float minDist = Float.POSITIVE_INFINITY;
float dist = Intersectionf.intersectRayTriangle
(origin.x, origin.y,origin.z,
dir.x,dir.y,dir.z, -0.5f, 0.5f, 0f,-0.5f, -0.5f, 0f,0.5f, -0.5f, 0f, 1E-6f);
//System.out.println(origin+" "+dir+" "+ dist);
if (dist > 0.0f && dist < minDist) {
System.out.println("Hit");
}
}
if am missing anything else please let me know happy to provide, Thanks