Search code examples
xnacameratransforminstancefrustum

How draw only the models " front of the camera " full or partially displayed - XNA


I am developing a small game in XNA style "MinecraftGame."

As there are a lot of cubes to draw. I created a function that allows you to draw only the cubes in front of the camera! But the problem is that if a cube is not completely full in my field of vision, it will not be drawn. As you can see on the "screenshot" below. Cubes located on the edges are not drawn. How to draw cubes fully and partially displayed in front of the camera? and not only entirely.

Thanks a lot

Here my code to check if the Frustum contain the model:

//Initialize frustum
private void GenerateFrustum()
{
    Matrix viewProjection = View * Projection;
    Frustum = new BoundingFrustum(viewProjection);
}

//private void UpdateFrustum
{
    Matrix viewProjection = View * Projection;
    Frustum.Matrix = viewProjection;
}

//Function that will add models instantiated in the transformation matrix only if the    model is in the field of view !
private udpateTransformModelInstancied()
{
     for (int i = 0; i < ListInstance.Count; i++)
     {
         if(camera.Frustum.Contains(ListInstance[i].Transform.Translation) != ContainmentType.Disjoint)
         {
               instanceTransforms.Add(ListInstance[i].Transform);
         }

      }
 .......
}

SreenShot : enter image description here


Solution

  • You're checking the position of the cubes. This means that you're not taking the cubes' physical size into account; you're treating them as a point, and if that point is out of view, then you won't render it. What you need to do is check whether any part of the cube is visible. The two simplest ways to do this are to work out a bounding shape and use that to check, or to check whether your view contains any of the cube's corner points, rather than just its position point.