简体   繁体   English

OpenGL:除以零的w

[英]OpenGL: Divide by w of zero

I'm currently trying to implement a Camera class, complete with frustum, that I can use for culling objects from the scene. 我目前正在尝试实现一个带有平截头体的Camera类,可用于从场景中剔除对象。 To find the bounds of the frustum, I'm transforming the system coordinate space (cube with sides ranging [-1..1] each) by the inverse of the PVM matrix. 为了找到平截头体的边界,我通过PVM矩阵的来变换系统坐标空间(边的范围为[-1..1]的多维数据集)。 This seems to be working flawlessly, except... 除...外,这似乎工作正常。

When transforming the origin (0, 0, 0, 1) by the inverse PVM matrix for testing purposes, I get (0, 0, 0, 0 ). 当变换原点(0,0,0,1)通过逆矩阵PVM出于测试目的,我得到(0,0,0,0)。 I know exactly why it's happening given the particular values of the PVM matrix whilst debugging, but I'm wondering if I'm logically doing something wrong, since as far as I know, the Euclidian point should be (0, 0, 0), not a bunch of NaNs resulting from aw of 0. What am I forgetting here? 我确切地知道为什么在调试时给定PVM矩阵的特定值会发生这种情况,但是我想知道我是否在逻辑上做错了,因为据我所知,欧几里得点应该是(0,0,0) ,而不是因aw为0而产生的一堆NaN。我在这里忘记了什么? Am I not supposed to divide by w? 我不应该除以w吗? Are my matrices are wrong? 我的矩阵错了吗? Blargh... 布拉赫...

More details: 更多细节:

Projection matrix is defined by the following method (using LWJGL in Java): 投影矩阵通过以下方法定义(在Java中使用LWJGL):

/** fieldOfView is in radians, aspectRatio is simply width / height. */
public static Matrix4f makeProjectionMatrix(Matrix4f dest, float fieldOfView, float aspectRatio, float nearPlane, float farPlane)
{
    float y_scale = GLUtils.cotan(fieldOfView * .5f);
    float x_scale = y_scale / aspectRatio;
    float frustumLength = farPlane - nearPlane;

    dest.setIdentity();
    dest.m00 = x_scale;
    dest.m11 = y_scale;
    dest.m22 = -(farPlane + nearPlane) / frustumLength;
    dest.m23 = -1f;
    dest.m32 = -2f * nearPlane * farPlane / frustumLength;
    dest.m33 = 0f;

    return dest;
}

*Based on http://unspecified.wordpress.com/2012/06/21/calculating-the-gluperspective-matrix-and-other-opengl-matrix-maths/ *基于http://unspecified.wordpress.com/2012/06/21/calculating-the-gluperspective-matrix-and-other-opengl-matrix-maths/

Notice that m33 is always 0, which happens to be true for my inverse as well (and I believe to be true of all inverses given the above formula). 请注意,m33始终为0,这对我的逆也同样适用(并且我相信在上述公式中所有逆都适用)。

Since the Model and View are both the identity matrix, and the Projection is unmodified from the above, a coordinate (0, 0, 0, x) transformed by the inverse PVM matrix would always yield w of 0... right? 由于Model和View都是单位矩阵,并且从上面未修改Projection,因此由 PVM矩阵转换的坐标(0,0,0,x)始终将w设为0 ...是吗?

Example matrices causing the dilemma: 造成困境的示例矩阵:

Original Projection: 原始投影:

1.732 0     0  0
0     1.732 0  0
0     0     0 -2
0     0    -1  0

Inverted Projection: 倒投:

.577 0    0   0
0    .577 0   0
0    0    0  -1
0    0   -.5  0

I'd be happy to post additional code if a miscalculation is evident. 如果计算错误很明显,我很乐意发布其他代码。

Further to the comments: 除了评论:

Well az of 0 produces aw of 0 because thats what your projection matrix says to do ;) That said in projection space az of 0 is right at the origin of the projection. 的az为0会产生aw为0,因为那是您的投影矩阵所说的;)在投影空间az为0时所说的正好位于投影的原点。 it needs to be in front of the projection to be visible. 它必须在投影的前面才能可见。 Otherwise both the center of projection AND the vertex position occupy the exact same location. 否则,投影中心和顶点位置都将占据完全相同的位置。 This is why we have near planes to prevent any geometry with aw of 0 (or close to) getting drawn. 这就是为什么我们有近平面来防止绘制任何aw为0(或接近)的几何图形的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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