Hello Guest

Flipped x-axis

  • 1 Replies
  • 4417 Views
Flipped x-axis
« on: August 28, 2016, 23:56:02 »
For some reason my x axis is flipped instead of -x<->x mine goes x<->-x. I have set up a projection matrix.
Code: [Select]
public class ProjectionMatrix {
private static final float FOV = 70;
private static final float NEAR_PLANE = 0.1f;
private static final float FAR_PLANE = 1000f;

public static Matrix4f create(){
Matrix4f projectionMatrix = new Matrix4f();
float aspectRatio = (float) Window.getWidth() / (float) Window.getHeight();
float frustum_length = FAR_PLANE - NEAR_PLANE;
//x scale
projectionMatrix.m00 = (float) (1f / Math.tan(Math.toRadians(FOV / 2f)));
//y scale
projectionMatrix.m11 = projectionMatrix.m00 * aspectRatio;
projectionMatrix.m22 = -((FAR_PLANE + NEAR_PLANE) / frustum_length);
projectionMatrix.m23 = -1;
projectionMatrix.m32 = -((2 * NEAR_PLANE * FAR_PLANE) / frustum_length);
projectionMatrix.m33 = 0;
return projectionMatrix;
}
}

I think this is right.

I have a view matrix.
Code: [Select]
public class ViewMatrix {

public static Matrix4f create() {
Matrix4f matrix = new Matrix4f();
matrix.rotate((float) Math.toRadians(Camera.getPitch()), new Vector3f(1, 0, 0));
matrix.rotate((float) Math.toRadians(Camera.getYaw()), new Vector3f(0, 1, 0));
matrix.rotate((float) Math.toRadians(Camera.getRoll()), new Vector3f(0, 0, 1));
Vector3f cameraPos = Game.CAMERA.position().get();
Vector3f negativeCameraPos = new Vector3f(-cameraPos.x, -cameraPos.y, -cameraPos.z);
matrix.translate(negativeCameraPos);
return matrix;
}
}

I think this is right.

and this is how I implement them into the shader
Code: [Select]
vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * viewMatrix * worldPosition;

I have no clue why the x-axis flipped. I thought I had a grasp on this but I am having trouble with this.

*

Offline asyx

  • *
  • 23
Re: Flipped x-axis
« Reply #1 on: August 31, 2016, 23:48:33 »
Any reason why you don't use the JOML methods (I assume you use JOML because that seems to be the default for LWJGL 3+) for the view and projection matrix?

Anyway, the most common reason for a flipped x axis is actually a misunderstanding of the OpenGL coordinate system.

It's a right handed coordinate system. Take your right hand and hold it in front of you (looking at your palm) and point to yourself with your middle finger. The thumb is your x axis, the index finger your y axis and the middle finger your z axis.

You're looking down the NEGATIVE z axis.

Now, if you position your camera in the part of the z axis and look at (0, 0, 0) (which is something people generally do for a first project), you're looking at the whole thing from the back of your hand (so turn your hand around pointing the index finger away from you). In that case, the x runs from your right to your left! And is therefore flipped.

Maybe it's that? It's really common mistake so, without checking if your self made matrix stuff is correct, I thing there's a good chance that this is your problem.