简体   繁体   中英

Render perspective projection matrix in LWJGL 3

When adding a perspective projection matrix to my vertex shader the textured quad is not visible.

shader.vert

#version 400

in vec3 position;
in vec2 textureCoordinate;

out vec3 colour;
out vec2 passTextureCoordinate;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;

void main() {
  gl_Position = projectionMatrix * transformationMatrix * vec4(position, 1.0);
  passTextureCoordinate = textureCoordinate;
  colour = vec3(position.x+.5f, 0.0, position.y+.5f);
}

When I set the projectionMatrix to identity its rendering fine. Also, when I set it to orthographic projection it renders too.

Creating the perspective projection matrix:

private static final float FOV = 70f;
private static final float NEAR_PLANE = 1.0f;
private static final float FAR_PLANE = 1000.0f;

    private void createProjectionMatrix(){
      IntBuffer w = BufferUtils.createIntBuffer(4);
      IntBuffer h = BufferUtils.createIntBuffer(4);
      GLFW.glfwGetWindowSize(WindowManager.getWindow(), w, h);
      float width = w.get(0);
      float height = h.get(0);
      float aspectRatio = width / height;
      float yScale = (float) ((1f / Math.tan(Math.toRadians(FOV / 2f))) * aspectRatio);
      float xScale = y_scale / aspectRatio;
      float frustumLength = FAR_PLANE - NEAR_PLANE;

      projectionMatrix = new Matrix4f();
      projectionMatrix.m00 = xScale;
      projectionMatrix.m11 = yScale;
      projectionMatrix.m22 = -((FAR_PLANE + NEAR_PLANE) / frustumLength);
      projectionMatrix.m23 = -1;
      projectionMatrix.m32 = -((2 * NEAR_PLANE * FAR_PLANE) / frustumLength);
      projectionMatrix.m33 = 0;
}

The result matrix:

0.8925925 0.0       0.0       0.0
0.0       1.428148  0.0       0.0
0.0       0.0      -1.002002 -2.002002
0.0       0.0      -1.0       0.0

And this is the code for the orthographic projection matrix.

Orthographic projection matrix:

private void createOrthographicMatrix() {
    projectionMatrix = Matrix4f.orthographic(-10f, 10f, -10f * 9f / 16f, 10f * 9f / 16f, -1f, 1f);
}

The result matrix:

0.1  0.0      0.0  0.0
0.0  0.177778 0.0  0.0
0.0  0.0     -1.0  0.0
0.0  0.0      0.0  1.0

I am suspecting that I am missing something in the setup. But have not been able to figure it out.

You have not specified where exactly the triangle is that you try to draw. However, it is extremely likely that the triangle won't appear with your perspective matrix if it does appear with the ortho matrix you are using.

In the ortho case, you set the near plane at z_eye=1 and the far plane at z_eye=-1 , so everything outside of the z_eye range [-1,1] will never be visible. In your perspective setup, you set the near plane at z_eye=-1 and the far plane at z_eye=-1000 . (Note that the viewing direction is -z in eye space, and the values you specify for the near and far plane are distances into that viewing direction, so the actual z values are negated. This also means that in the ortho case, you actually set the near plane behind the "camera"). This means that primitives will only be visible if they fall into the [-1000,-1] range in z .

So a primtive can only be visible with both matrices if it is placed exactly one unit in front of the camera ( z_eye=-1 ), and that only without any numerical instabilities.

So, my problem was not in the projection matrix, but in the method generating the transformation matrix. I have accidently sat the Z to Y. As my Y value was always 0 so to was the Z value. As a consequence, the object was always outside the frustum.

public static Matrix4f createTransformationMatrix(Vector3f translation, float rx, float ry, float rz, float scale) {
    Matrix4f translations = new Matrix4f();
    translations.setIdentity();

    translations = Matrix4f.translate(translation.x, translation.y, translation.y); // Bug
    ...
}

I wish the solution would have been more interesting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM