View Matrix in LWJGL 3 not showing right

Clash Royale CLAN TAG#URR8PPP
View Matrix in LWJGL 3 not showing right
My view matrix in my camera class is setup up correctly, with the rotation and negative translation, but it moves the camera somewhere else, and I can't seem to find my model. Rotation and translation matrices are working because translations are working and my model and projection matrices are also working, but when I add the view matrix, it only shows parts of the model. By the way, made my own matrix math classes.
Matrix Math Class
package engine.maths;
public class Matrix4f
private float matrix;
public Matrix4f()
matrix = new float[4][4];
public Matrix4f identity()
matrix[0][0] = 1; matrix[0][1] = 0; matrix[0][2] = 0; matrix[0][3] = 0;
matrix[1][0] = 0; matrix[1][1] = 1; matrix[1][2] = 0; matrix[1][3] = 0;
matrix[2][0] = 0; matrix[2][1] = 0; matrix[2][2] = 1; matrix[2][3] = 0;
matrix[3][0] = 0; matrix[3][1] = 0; matrix[3][2] = 0; matrix[3][3] = 1;
return this;
public Matrix4f translate(Vector3f vector)
matrix[0][0] = 1; matrix[0][1] = 0; matrix[0][2] = 0; matrix[0][3] = vector.getX();
matrix[1][0] = 0; matrix[1][1] = 1; matrix[1][2] = 0; matrix[1][3] = vector.getY();
matrix[2][0] = 0; matrix[2][1] = 0; matrix[2][2] = 1; matrix[2][3] = vector.getZ();
matrix[3][0] = 0; matrix[3][1] = 0; matrix[3][2] = 0; matrix[3][3] = 1;
return this;
public Matrix4f rotate(Vector3f vector)
Matrix4f rotateX = new Matrix4f();
Matrix4f rotateY = new Matrix4f();
Matrix4f rotateZ = new Matrix4f();
Vector3f rotatedVector = new Vector3f((float) Math.toRadians(vector.getX()), (float) Math.toRadians(vector.getY()), (float) Math.toRadians(vector.getZ()));
rotateZ.matrix[0][0] = (float)Math.cos(rotatedVector.getZ()); rotateZ.matrix[0][1] = -(float)Math.sin(rotatedVector.getZ()); rotateZ.matrix[0][2] = 0; rotateZ.matrix[0][3] = 0;
rotateZ.matrix[1][0] = (float)Math.sin(rotatedVector.getZ()); rotateZ.matrix[1][1] = (float)Math.cos(rotatedVector.getZ()); rotateZ.matrix[1][2] = 0; rotateZ.matrix[1][3] = 0;
rotateZ.matrix[2][0] = 0; rotateZ.matrix[2][1] = 0; rotateZ.matrix[2][2] = 1; rotateZ.matrix[2][3] = 0;
rotateZ.matrix[3][0] = 0; rotateZ.matrix[3][1] = 0; rotateZ.matrix[3][2] = 0; rotateZ.matrix[3][3] = 1;
rotateX.matrix[0][0] = 1; rotateX.matrix[0][1] = 0; rotateX.matrix[0][2] = 0; rotateX.matrix[0][3] = 0;
rotateX.matrix[1][0] = 0; rotateX.matrix[1][1] = (float)Math.cos(rotatedVector.getX()); rotateX.matrix[1][2] = -(float)Math.sin(rotatedVector.getX()); rotateX.matrix[1][3] = 0;
rotateX.matrix[2][0] = 0; rotateX.matrix[2][1] = (float)Math.sin(rotatedVector.getX()); rotateX.matrix[2][2] = (float)Math.cos(rotatedVector.getX()); rotateX.matrix[2][3] = 0;
rotateX.matrix[3][0] = 0; rotateX.matrix[3][1] = 0; rotateX.matrix[3][2] = 0; rotateX.matrix[3][3] = 1;
rotateY.matrix[0][0] = (float)Math.cos(rotatedVector.getY()); rotateY.matrix[0][1] = 0; rotateY.matrix[0][2] = -(float)Math.sin(rotatedVector.getY()); rotateY.matrix[0][3] = 0;
rotateY.matrix[1][0] = 0; rotateY.matrix[1][1] = 1; rotateY.matrix[1][2] = 0; rotateY.matrix[1][3] = 0;
rotateY.matrix[2][0] = (float)Math.sin(rotatedVector.getY()); rotateY.matrix[2][1] = 0; rotateY.matrix[2][2] = (float)Math.cos(rotatedVector.getY()); rotateY.matrix[2][3] = 0;
rotateY.matrix[3][0] = 0; rotateY.matrix[3][1] = 0; rotateY.matrix[3][2] = 0; rotateY.matrix[3][3] = 1;
matrix = rotateZ.mul(rotateY.mul(rotateX)).getMatrix();
return this;
public Matrix4f scale(Vector3f vector)
matrix[0][0] = vector.getX(); matrix[0][1] = 0; matrix[0][2] = 0; matrix[0][3] = 0;
matrix[1][0] = 0; matrix[1][1] = vector.getY(); matrix[1][2] = 0; matrix[1][3] = 0;
matrix[2][0] = 0; matrix[2][1] = 0; matrix[2][2] = vector.getZ(); matrix[2][3] = 0;
matrix[3][0] = 0; matrix[3][1] = 0; matrix[3][2] = 0; matrix[3][3] = 1;
return this;
public Matrix4f projection(float fov, float aspectRatio, float zNear, float zFar)
float tanHalfFOV = (float) Math.tan(Math.toRadians(fov / 2));
float zRange = zNear - zFar;
matrix[0][0] = 1.0f / (tanHalfFOV * aspectRatio); matrix[0][1] = 0; matrix[0][2] = 0; matrix[0][3] = 0;
matrix[1][0] = 0; matrix[1][1] = 1.0f / tanHalfFOV; matrix[1][2] = 0; matrix[1][3] = 0;
matrix[2][0] = 0; matrix[2][1] = 0; matrix[2][2] = (-zNear - zFar) / zRange; matrix[2][3] = 2 * zFar * zNear / zRange;
matrix[3][0] = 0; matrix[3][1] = 0; matrix[3][2] = 1; matrix[3][3] = 0;
return this;
public Matrix4f mul(Matrix4f m)
Matrix4f result = new Matrix4f();
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
result.set(i, j, matrix[i][0] * m.get(0, j) +
matrix[i][1] * m.get(1, j) +
matrix[i][2] * m.get(2, j) +
matrix[i][3] * m.get(3, j));
return result;
public float getMatrix()
return matrix;
public void setMatrix(float matrix)
this.matrix = matrix;
public float get(int x, int y)
return matrix[x][y];
public void set(int x, int y, float value)
matrix[x][y] = value;
public String toString()
String matrix = "";
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
matrix += (Float.toString(get(i, j)) + ", ");
matrix += "n";
return matrix + "";
Camera Class
package engine.rendering;
import engine.maths.Matrix4f;
import engine.maths.Vector3f;
public class Camera
private Vector3f position, rotation;
public Camera(Vector3f position, Vector3f rotation)
this.position = position;
this.rotation = rotation;
public Matrix4f getViewMatrix()
Matrix4f rotationMatrix = new Matrix4f().identity().rotate(rotation);
Vector3f negativePosition = new Vector3f(-position.getX(), -position.getY(), -position.getZ());
Matrix4f translationMatrix = new Matrix4f().identity().translate(negativePosition);
return rotationMatrix.mul(translationMatrix);
public void addPosition(float x, float y, float z)
position.add(new Vector3f(x, y, z));
public void addPosition(Vector3f value)
position.add(value);
public void setPosition(float x, float y, float z)
position = new Vector3f(x, y, z);
public void setPosition(Vector3f pos)
position = pos;
public Vector3f getPosition()
return position;
public void addRotation(float x, float y, float z)
rotation.add(new Vector3f(x, y, z));
public void addRotation(Vector3f value)
rotation.add(value);
public void setRotation(float x, float y, float z)
rotation = new Vector3f(x, y, z);
public void setRotation(Vector3f rot)
rotation = rot;
public Vector3f getRotation()
return rotation;
Loading Matrix Class
package engine.shaders;
import engine.maths.Matrix4f;
public class BasicShader extends Shader
private static final String VERTEX_FILE = "src/engine/shaders/basicVertexShader.glsl";
private static final String FRAGMENT_FILE = "src/engine/shaders/basicFragmentShader.glsl";
private int tvpMatrixLocation;
private Matrix4f transformationMatrix = new Matrix4f().identity(), projectionMatrix = new Matrix4f().identity(), viewMatrix = new Matrix4f().identity();
public BasicShader()
super(VERTEX_FILE, FRAGMENT_FILE);
@Override
protected void bindAttributes()
super.bindAttribute(0, "position");
super.bindAttribute(1, "textCoords");
@Override
protected void getAllUniforms()
tvpMatrixLocation = super.getUniform("tvpMatrix");
public void useMatrices()
super.loadMatrixUniform(tvpMatrixLocation, transformationMatrix.mul(projectionMatrix.mul(viewMatrix)));
public void loadTransformationMatrix(Matrix4f matrix)
transformationMatrix = matrix;
public void loadProjectionMatrix(Matrix4f matrix)
projectionMatrix = matrix;
public void loadViewMatrix(Matrix4f matrix)
viewMatrix = matrix;
@vandench I use the rotation to rotate my game objects and it works fine
– A P
Aug 1 at 3:28
2 Answers
2
Your projection matrix has a fault: matrix[3][2] = 1 must be -1.
matrix[3][2] = 1
-1
This is due to OpenGL NDC space is special, cross product XxY gives -Z instead of Z.
XxY
-Z
Z
The surprising fact is that all visible objects must have a negative Z-coordinate in view space.
Is this about the left-handed and right-handed projection matrices? Also, it doesn't work...
– A P
Aug 1 at 21:43
My projection matrix works fine as well!
– A P
Aug 1 at 22:59
This is not making any sense:
super.loadMatrixUniform(tvpMatrixLocation, transformationMatrix.mul(projectionMatrix.mul(viewMatrix)));
No matter what conventions you use, and which order the mul() method actually multiplies. you either end up with T*P*V, or V*P*T, which both woudln't be correct (for the typical use case of not applying some additional transformation after the projection).
mul()
T*P*V
V*P*T
So what would be the order
– A P
Sep 3 at 19:30
Depending on your conventions, either
P*V*T or T*V*P– derhass
Sep 4 at 6:54
P*V*T
T*V*P
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Have you validated that you added matrix rotation correctly? You’ll have to verify that you using the correct metric (radians/degrees). Otherwise you could just use a library like JOML, it is probably far more performant (doesn’t use 2D arrays) and more feature complete.
– van dench
Aug 1 at 3:22