I'm having some trouble calculating the direction and position of the ray from my matrices. I have tried some things such as:
private Vec3 getPick(Mat4 projection, Mat4 view) {
Mat4 inverseProject = projection.copy().inverse();
Mat4 pick = inverseProject.mul(view.copy());
Vec3 pos = new Vec3(pick.m10, pick.m11, pick.m12);
return pos;
}
I am using custom math classes and I'm pretty sure they work. Also, would it be better to perform this math in my shader? For better performance etc..?
I contacted Notch (creator of Minecraft) on this issue and he told me to inverse the view Matrix and multiply by the projection, then multiply by the mouse coordinates. I tried that, it didn't do much.
EDIT: Tried normalising the screen space. It seems to do the job except in negative directions. Plus its a little off.
Vec3 getPick(double w, double h, double ar) {
double sx = ((Mouse.getX() / w / 2.0) - 1.0) * ar;
double sw = 1.0 - (2 * ((h - Mouse.getY()) / h));
Mat4 viewProjInv = view.copy().mul(projection).inverse();
Vec3 mouseNormal = new Vec3(sx, sw, 1.0);
Vec3 dir = viewProjInv.mul(mouseNormal);
return dir.mul(1);
}