LWJGL 3中的渲染透视投影矩阵 [英] Render perspective projection matrix in LWJGL 3

查看:147
本文介绍了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);
}

当我设置 projectionMatrix 来确定其渲染效果时.另外,当我将其设置为正交投影时,它也会渲染.

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

创建透视投影矩阵:

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;
}

结果矩阵:

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.

正交投影矩阵:

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

结果矩阵:

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.

在正交情况下,将近平面设置为z_eye=1,将远平面设置为z_eye=-1,因此z_eye范围[-1,1]之外的所有内容将永远不可见.在透视图设置中,将近平面设置为z_eye=-1,将远平面设置为z_eye=-1000. (请注意,视线方向在眼空间中为-z,并且您为近平面和远平面指定的值是到该视线方向的距离,因此实际的z值将被取反.这也意味着在正交情况下,实际上是将近平面设置在相机"的后面). 这意味着仅当基元落入z中的[-1000,-1]范围内时,它们才可见.

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.

因此,如果将本原语放置在摄像头(z_eye=-1)的正前方一个单位上且没有任何数值不稳定性,则仅在两者矩阵中均可见.

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.

这篇关于LWJGL 3中的渲染透视投影矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆