c – OpenGL – Frustum没有剔除远处飞机上的多边形

我已经实现了视锥体剔除,并检查了它与平截头体平面交叉的边界框.我添加了暂停视锥体更新的功能,这让我可以看到视锥体剔除是否正常工作.当我暂停后转身时,没有任何东西在我后面和左右两侧呈现,它们也会像你期望的那样逐渐变细.超出剪辑距离(远平面),它们仍然呈现,我不确定它是我的视锥体更新或边界框检查代码的问题,或我使用错误的矩阵或什么.当我将投影矩阵中的距离设置为3000.0f时,它仍然表示边界框仍然处于平截头体内,但事实并非如此.

这是我创建模型视图矩阵的地方:

projectionMatrix = glm::perspective(newFOV, 4.0f / 3.0f, 0.1f, 3000.0f);

viewMatrix = glm::mat4(1.0);
viewMatrix = glm::scale(viewMatrix, glm::vec3(1.0, 1.0, -1.0));
viewMatrix = glm::rotate(viewMatrix, anglePitch, glm::vec3(1.0, 0.0, 0.0));
viewMatrix = glm::rotate(viewMatrix, angleYaw, glm::vec3(0.0, 1.0, 0.0));
viewMatrix = glm::translate(viewMatrix, glm::vec3(-x, -y, -z));

modelViewProjectiomMatrix = projectionMatrix * viewMatrix;

我在Z方向上将其缩放-1的原因是因为水平设计为使用DirectX渲染,所以我反转Z方向.

这是我更新我的视锥体的地方:

void CFrustum::calculateFrustum()
{
    glm::mat4 mat = camera.getModelViewProjectionMatrix();

    // Calculate the LEFT side
    m_Frustum[LEFT][A] = (mat[0][3]) + (mat[0][0]);
    m_Frustum[LEFT][B] = (mat[1][3]) + (mat[1][0]);
    m_Frustum[LEFT][C] = (mat[2][3]) + (mat[2][0]);
    m_Frustum[LEFT][D] = (mat[3][3]) + (mat[3][0]);

    // Calculate the RIGHT side
    m_Frustum[RIGHT][A] = (mat[0][3]) - (mat[0][0]);
    m_Frustum[RIGHT][B] = (mat[1][3]) - (mat[1][0]);
    m_Frustum[RIGHT][C] = (mat[2][3]) - (mat[2][0]);
    m_Frustum[RIGHT][D] = (mat[3][3]) - (mat[3][0]);

    // Calculate the TOP side
    m_Frustum[TOP][A] = (mat[0][3]) - (mat[0][1]);
    m_Frustum[TOP][B] = (mat[1][3]) - (mat[1][1]);
    m_Frustum[TOP][C] = (mat[2][3]) - (mat[2][1]);
    m_Frustum[TOP][D] = (mat[3][3]) - (mat[3][1]);

    // Calculate the BOTTOM side
    m_Frustum[BOTTOM][A] = (mat[0][3]) + (mat[0][1]);
    m_Frustum[BOTTOM][B] = (mat[1][3]) + (mat[1][1]);
    m_Frustum[BOTTOM][C] = (mat[2][3]) + (mat[2][1]);
    m_Frustum[BOTTOM][D] = (mat[3][3]) + (mat[3][1]);

    // Calculate the FRONT side
    m_Frustum[FRONT][A] = (mat[0][3]) + (mat[0][2]);
    m_Frustum[FRONT][B] = (mat[1][3]) + (mat[1][2]);
    m_Frustum[FRONT][C] = (mat[2][3]) + (mat[2][2]);
    m_Frustum[FRONT][D] = (mat[3][3]) + (mat[3][2]);

    // Calculate the BACK side
    m_Frustum[BACK][A] = (mat[0][3]) - (mat[0][2]);
    m_Frustum[BACK][B] = (mat[1][3]) - (mat[1][2]);
    m_Frustum[BACK][C] = (mat[2][3]) - (mat[2][2]);
    m_Frustum[BACK][D] = (mat[3][3]) - (mat[3][2]);

    // Normalize all the sides
    NormalizePlane(m_Frustum, LEFT);
    NormalizePlane(m_Frustum, RIGHT);
    NormalizePlane(m_Frustum, TOP);
    NormalizePlane(m_Frustum, BOTTOM);
    NormalizePlane(m_Frustum, FRONT);
    NormalizePlane(m_Frustum, BACK);
}

最后,我检查边界框:

bool CFrustum::BoxInFrustum( float x, float y, float z, float x2, float y2, float z2)
{
    // Go through all of the corners of the box and check then again each plane
    // in the frustum.  If all of them are behind one of the planes, then it most
    // like is not in the frustum.
    for(int i = 0; i < 6; i++ )
    {
        if(m_Frustum[i][A] * x  + m_Frustum[i][B] * y  + m_Frustum[i][C] * z  + m_Frustum[i][D] > 0)  continue;
        if(m_Frustum[i][A] * x2 + m_Frustum[i][B] * y  + m_Frustum[i][C] * z  + m_Frustum[i][D] > 0)  continue;
        if(m_Frustum[i][A] * x  + m_Frustum[i][B] * y2 + m_Frustum[i][C] * z  + m_Frustum[i][D] > 0)  continue;
        if(m_Frustum[i][A] * x2 + m_Frustum[i][B] * y2 + m_Frustum[i][C] * z  + m_Frustum[i][D] > 0)  continue;
        if(m_Frustum[i][A] * x  + m_Frustum[i][B] * y  + m_Frustum[i][C] * z2 + m_Frustum[i][D] > 0)  continue;
        if(m_Frustum[i][A] * x2 + m_Frustum[i][B] * y  + m_Frustum[i][C] * z2 + m_Frustum[i][D] > 0)  continue;
        if(m_Frustum[i][A] * x  + m_Frustum[i][B] * y2 + m_Frustum[i][C] * z2 + m_Frustum[i][D] > 0)  continue;
        if(m_Frustum[i][A] * x2 + m_Frustum[i][B] * y2 + m_Frustum[i][C] * z2 + m_Frustum[i][D] > 0)  continue;

        // If we get here, it isn't in the frustum
        return false;
    }

    // Return a true for the box being inside of the frustum
    return true;
}

最佳答案 我注意到了一些事情,特别是你如何设置投影矩阵.对于初学者来说,除非你使用某种包装或奇怪的api,否则gluProject不会返回一个值. gluLookAt更常用.

接下来,假设缩放,旋转和平移功能旨在更改模型视图矩阵,您需要反转它们的顺序. OpenGL实际上并没有移动对象;相反,它有效地移动原点,并使用< 0,0,0>的新定义渲染每个对象.因此,您“移动”到您想要渲染的位置,然后根据需要旋转轴,然后拉伸网格.

至于裁剪问题,你可能想给glClipPlane()一个好看.如果其他一切大部分都有效,但似乎有一些舍入错误,请尝试将您的透视(,,,)函数中的近剪裁平面从0.1更改为1.0(较小的值往往会弄乱z缓冲区).

我看到很多不熟悉的语法,所以我认为你正在使用某种包装器;但是我使用的是我自己的GL项目中的一些(Qt)代码片段.可能有帮助,不知道:

//This gets called during resize, as well as once during initialization
void GLWidget::resizeGL(int width, int height) {
  int side = qMin(width, height);
  padX = (width-side)/2.0;
  padY = (height-side)/2.0;
  glViewport(padX, padY, side, side);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(60.0, 1.0, 1.0, 2400.0);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

//This fragment gets called at the top of every paint event:
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glPushMatrix();

  glLightfv(GL_LIGHT0, GL_POSITION, FV0001);

  camMain.stepVars();

  gluLookAt(camMain.Pos[0],camMain.Pos[1],camMain.Pos[2],
            camMain.Aim[0],camMain.Aim[1],camMain.Aim[2],   
            0.0,1.0,0.0);

  glPolygonMode(GL_FRONT_AND_BACK, drawMode);

//And this fragment represents a typical draw event
void GLWidget::drawFleet(tFleet* tIn) {
  if (tIn->firstShip != 0){
    glPushMatrix();

    glTranslatef(tIn->Pos[0], tIn->Pos[1], tIn->Pos[2]);
    glRotatef(tIn->Yaw, 0.0, 1.0, 0.0);
    glRotatef(tIn->Pitch,0,0,1);

    drawShip(tIn->firstShip);

    glPopMatrix();
  }
}

我正在假设你对GL很新,所以如果我有点迂腐,我会道歉.

点赞