Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm drawing a simple Voxel world. When I naively render all the vertices, things look fine. But when I change my code to include a ViewFrustum check, I start getting strange visual glitches like this:


The large square block is meant to be there; the strange triangles on the bottom left are not. These glitches appear all over the screen, and intermittently appear/vanish as I move my camera around. The draw code looks something like this:

basicEffect.World = Matrix.Identity;
basicEffect.View = Matrix.CreateLookAt(
    camera.Position + camera.GetForward(), 
basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(

BoundingFrustum viewFrustum = new BoundingFrustum(basicEffect.View * basicEffect.Projection);

List<VertexPositionTexture> vertices = new List<VertexPositionTexture>();
foreach (Voxel voxel in voxelWorld.GetAllVoxels())
    if (viewFrustum.Intersects(voxel.Cube.BoundingBox))

if (vertices.Count != 0)
    foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
        using (VertexBuffer buffer = new VertexBuffer(
        graphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, vertices.Count);

So if I remove the viewFrustum.Intersects() check everything is fine. It seems unlikely to me that the viewFrustum check itself is causing the error, but I can't figure out where the problem is coming from. Any suggestions?

share|improve this question
Not related to your Q, but: BoundingFrustum, List, & VertexBuffer are classes, not a structs. Creating a new one each frame is going to create garbage which can affect CPU performance. As for the Q, Have you tried either a DynamicVertexBuffer that you update each frame (instead of a new regular VertexBuffer - or - DrawUserPrimitives() and just send a list of Vector3 triangle positions to the device each frame? – Steve H Feb 6 '12 at 21:30
Shouldn't DrawPrimitives take the triangle count, not the vertex count? – Blecki Feb 7 '12 at 0:56
@Blecki has your answer (why not turn it into a real answer?). 'primitiveCount' refers to the primitive type you are using (e.g. TriangleList: divide vertex count by 3, TriangleStrip: subtract 2 from vertex count and so forth). Those artifacts are arising due to uninitialized memory and will be different across application executions (or machine reboots). – Jonathan Dickinson Feb 7 '12 at 22:33
@Blecki Thanks! I think this is it. If you post that as the answer I can give you some kudos. – phillipwei Feb 11 '12 at 14:50
@SteveH Good points. I'm aware of the general performance concerns (I rearranged some code to try and make the question more concise) though I hadn't heard of DynamicVertexBuffers -- will incorporate that. – phillipwei Feb 11 '12 at 14:52
up vote 2 down vote accepted

DrawPrimitives takes the triangle count, not the vertex count. You should pass verticies.Count / 3.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.