Culling frustum becoming narrow when looking up or down

  3d, c++, culling, glm-math, opengl

I have a problem implementing frustum culling in OpenGL.
I implemented plane extraction in world space, using this Lighthouse3d tutorial (the rest of the engine comes from learnopengl.com).
It works well, but only when the camera points towards the horizon. If the camera points up or down, the frustum becomes progressively narrow.

Indeed, far frustum plane size is 1023 * 828 when pointing to horizon, but only 17 * 14 when pointing up or down. The same problem occurs for the near frustum plane.

Here is the code that updates the frustum each frame (C++):

  tang = (float) tan(glm::radians(45.0f) * 0.5f);
  ratio = SCR_WIDTH * 1.0 / SCR_HEIGHT;

  // Direction vectors
  cameraZ = cameraFront * -1.0f;
  cameraRight = glm::cross(glm::vec3(0.0f, 1.0f,  0.0f), cameraZ);
  cameraUp = glm::cross(cameraZ, cameraRight);

  // Dimensions of near and far frustum planes
  Hnear = nearDist * tang; // Hnear = near plane height
  Wnear = Hnear * ratio; // Wnear = near plane width
  Hfar = farDist  * tang; // Hfar = far plane height
  Wfar = Hfar * ratio; // Wfar = far plane width

  // Distance of near and far planes
  fc = cameraPos - cameraZ * farDist; // fc = far center
  nc = cameraPos - cameraZ * nearDist; // nc = near center

  // Extraction of frustum points
  ntl = nc + (cameraUp * Hnear) - (cameraRight * Wnear);
  ntr = nc + (cameraUp * Hnear) + (cameraRight * Wnear);
  nbl = nc - (cameraUp * Hnear) - (cameraRight * Wnear);
  nbr = nc - (cameraUp * Hnear) + (cameraRight * Wnear);

  ftl = fc + (cameraUp * Hfar) - (cameraRight * Wfar);
  ftr = fc + (cameraUp * Hfar) + (cameraRight * Wfar);
  fbl = fc - (cameraUp * Hfar) - (cameraRight * Wfar);
  fbr = fc - (cameraUp * Hfar) + (cameraRight * Wfar);

  // Computation of the 6 frustum planes
  topPlane = {ntr,ntl,ftl};
  bottomPlane = {nbl,nbr,fbr};
  leftPlane = {ntl,nbl,fbl};
  rightPlane = {nbr,ntr,fbr};
  nearPlane = {ntl,ntr,nbr};
  farPlane = {ftr,ftl,fbl};

Here is the result:

Frustum culling bug

Source: Windows Questions C++

LEAVE A COMMENT