Question

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

Was it helpful?

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.

OTHER TIPS

Testing for containment of bounding structures for your cubes instead of just the cube's position would work but that adds to your game's complexity by needing to manage a bunch of bounding structures plus the math of testing a bounding structure as opposed to a point. If you need the bounding structures for other stuff, then go that route. But if not, then I would simply take the cube's position, determine a point to the cube's width left or right of it and test those points. If either are 'in', then draw the cube.

Vector3 leftRightVector = Matrix.Transpose(view).Right;//calc once for each frame(or only when camera rotates)

Vestor3 testPoint1 = cubePosition + (leftRightVector * maxCubeWidth);//calc for each cube
Vestor3 testPoint2 = cubePosition + (leftRightVector * -maxCubeWidth);
if(frustum.Contains(testPoint1 || testPoint2)
{
  //draw
}

As far as I can see, you are just checking the position of each cube. The most effective fix would be to make a BoundingSphere that would fully encompass one of your cubes, translate it to the cubes location and done the Frustum.Contains with that sphere instead of position :)

Also; Make the sphere just a tad bit larger than needed to account for the edges of the frustum. And remember; If you want to make a minecraft-clone, you will need to use some sort of batch-rendering technique. I reccomend using an instance buffer, as there will be less data to send to the GPU, as well as easier to implement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top