完全封装对象的透视矩阵

时间:2014-07-04 10:10:32

标签: java opengl 3d lwjgl fieldofview

我想制作一个投影矩阵,用于渲染来自任意相机的物体。我设法设置了一个viewMatrix,它将从任意眼睛位置查看对象,但是我在设置投影矩阵时遇到了困难。

假设一个以(x,y,z)为中心的物体是距离中心最远点的r,对于任意方向都可以完全包围在半径为r的球体中,其原点为(x,y,z),I计算我的透视矩阵如下:

float dist2Object = (float) Math.sqrt(vec.lengthSquared());

float objectRadius = (float) Math.sqrt(3 * Math.pow(0.5, 2));
float far = dist2Object + objectRadius;
float near = dist2Object - objectRadius;

// fov_2 = FOV / 2
float fov_2 = (float) (Math.asin(objectRadius / dist2Object));

float r = (float) (Math.tan(fov_2));
float frustum_length = far - near;

depthProjectionMatrix.m00 = 1 / r;
depthProjectionMatrix.m11 = depthProjectionMatrix.m00;
depthProjectionMatrix.m22 = -((far + near) / frustum_length);
depthProjectionMatrix.m23 = -1;
depthProjectionMatrix.m32 = -((2 * near * far) / frustum_length);
depthProjectionMatrix.m33 = 0;

在我的例子中:

  • vec是从相机到对象的矢量
  • 对象是一个最远的顶点为(0.5,0.5,0.5)的立方体,给出一个r的sqrt(0.75)

据我所知,几何和三角学应该是正确的,但使用以下片段着色器渲染坐标:

#version 150 core

in vec3 pCoord;

out vec4 out_Color;

void main(void) {
    out_Color = vec4(0,1,0,1);
    if(pCoord.x <= -1){
        out_Color = vec4(1,0,0,1);
    }
    if(pCoord.x >= 1){
        out_Color = vec4(0,0,1,1);
    }
    if(pCoord.z <= -1){
        out_Color = vec4(1,0,1,1);
    }
    if(pCoord.z >= 1){
        out_Color = vec4(1,0,1,1);
    }
}
image显示的

显示FOV太窄,近和远的平面也太窄。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

Haven暂时没有这样做,但似乎你在计算中缺少视口大小。这是我用于项目的内容。请注意,此处的矩阵以_11开头,您使用m00:

double aspectRatio = (double)this.viewPort.Width /(double)this.viewPort.Height;
double fov = Math.toRadians(fieldOfView/2.0);
double size = nearClip * Math.tan(fov);

double left = -size* aspectRatio, right = size* aspectRatio, bottom = -size , top = size ;

projectionMatrix.resetToZero();

// the values in comments are for non symetrical frustrum 

// First Column
projectionMatrix._11 = (float) (nearClip/right);//(float) ((2 * nearClip )/ (double)(right - left));

// Second Column
projectionMatrix._22 = (float)(nearClip/top);//(float) (2 * nearClip / (double)(top - bottom));

// Third Column
projectionMatrix._31 = 0;//(float) ((right + left) / (right - left));
projectionMatrix._32 = 0;//(float) ((top + bottom) / (top - bottom));
projectionMatrix._33 = -1*(farClip + nearClip) / (float)(farClip - nearClip);
projectionMatrix._34 = -1;

// Fourth Column
projectionMatrix._43 = -(2 * farClip * nearClip) / (float)(farClip - nearClip);