LWJGL Forum
Programming => Lightweight Java Gaming Library => Topic started by: jemhop on May 02, 2021, 18:25:38
-
I have some code to calculate the view matrix, following code from LearnOpenGL. I'm only really using LWJGL and JOML.
This is my method to get the view matrix:
public Matrix4f getViewMatrix() {
Matrix4f viewMatrix = new Matrix4f();
viewMatrix = viewMatrix.identity();
/*direction = new Vector3f(Math.cos(Math.toRadians(yaw)) * Math.cos(Math.toRadians(pitch)), Math.sin(Math.toRadians(pitch)), Math.sin(Math.toRadians(yaw)) * Math.cos(Math.toRadians(pitch)));
direction.normalize();
First do the rotation so camera rotates over its position
viewMatrix.rotate((float)pitch, new Vector3f(1, 0, 0))
.rotate((float)yaw, new Vector3f(0, 1, 0));
Then do the translation*/
viewMatrix.translate(-position.x, -position.y, -position.z);
updateCameraVectors();
viewMatrix.lookAt(position, position.add(cameraFront) , cameraUp, viewMatrix);
return viewMatrix;
}
And my function to update the vectors
public void updateCameraVectors() {
cameraFront.x = Math.cos(Math.toRadians(yaw)) * Math.cos(Math.toRadians(pitch));
cameraFront.y = Math.sin(Math.toRadians(pitch));
cameraFront.z = Math.sin(Math.toRadians(yaw)) * Math.cos(Math.toRadians(pitch));
cameraFront.normalize();
//cross product of camerafront and 0,1,0 stored in cameraRight
cameraFront.cross(new Vector3f(0, 1, 0), cameraRight);
cameraRight.normalize();
//gets the cross product of camera right with camera front and stores it in cameraUp
cameraRight.cross(cameraFront, cameraUp);
cameraUp.normalize();
}
The problem I'm having is that when I do viewMatrix.lookAt(position, position.add(cameraFront) , cameraUp, viewMatrix), it's equal to NaN. I can't really tell what I'm doing wrong, any help would be appreciated.
-
position.add(cameraFront) does not do what you think it does.
Please read: https://github.com/JOML-CI/JOML/wiki/Common-Pitfalls#methods-without-a-dest-parameter-modify-this-and-return-this
So, position itself is being modified and so the first and the second argument to the lookAt method will be the same then.
-
Then I'm assuming that
viewMatrix.translate(-position.x, -position.y, -position.z);
updateCameraVectors();
Vector3f temp = new Vector3f();
position.add(cameraFront, temp);
viewMatrix.lookAt(position, temp, cameraUp, viewMatrix);
System.out.println(viewMatrix);
return viewMatrix;
would work? If so, I'm having some other problem that makes me unable to see my square, but whatever it is, I'm glad to have eliminated one thing.
edit: turns out my mouselook is just broken