# Strange rendering in XNA/Monogame

I am trying to render G-Code generated for a 3d-printer as the printed product by reading the file as line segments and the drawing cylinders with the diameter of the filament around the segment. I think I have managed to do this part right because the vertex I am sending to the graphics device appear to have been processed correctly. My problem I think lies somewhere in the rendering. What basically happens is that when I start rotating my model in the X or Y axis then it renders perfectly for half of the rotation but then for the other half it has this weird effect where you start seeing through the outer filament into some of the shapes inside. This effect is the strongest with X rotations though.

Here is a picture of the part of the rotation that looks correct:

And here is one that looks horrible:

I am still quite new to XNA and/Monogame and 3d programming as a whole. I have no idea what could possibly be causing this and even less of an idea of what this type of behavior is called. I am guessing this has something to do with rendering so have added the code for that part:

    protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);

basicEffect.World = world;
basicEffect.View = view;
basicEffect.Projection = projection;
basicEffect.VertexColorEnabled = true;
basicEffect.EnableDefaultLighting();

GraphicsDevice.SetVertexBuffer(vertexBuffer);

RasterizerState rasterizerState = new RasterizerState();
rasterizerState.CullMode = CullMode.CullClockwiseFace;
rasterizerState.ScissorTestEnable = true;
GraphicsDevice.RasterizerState = rasterizerState;

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, vertexBuffer.VertexCount);
}

base.Draw(gameTime);
}


I don't know if it could be because I am shading something that does not really have a texture. I am using this custom vertex declaration I found on some tutorial that allows me to store a vertex with a position, color and normal:

    public struct VertexPositionColorNormal
{
public Vector3 Position;
public Color Color;
public Vector3 Normal;

public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
(
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
new VertexElement(sizeof(float) * 3, VertexElementFormat.Color, VertexElementUsage.Color, 0),
new VertexElement(sizeof(float) * 3 + 4, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0)
);
}


If any of you have ever seen this type of thing please help. Also, if you think that the problem might lay somewhere else in my code then please just request what part you would like to see in the comments section.

• try rasterizerState.CullMode = CullMode.CullCounterClockwise; (which is default) and also GraphicsDevice.DepthStencilState = DepthStencilState.Default; – user9790 Jun 2 '13 at 8:19
• Thanks, this helps a lot but the effect is still very much there. It does appear though that the amount it can rotate before becoming completely messed up has increased. – Gerharddc Jun 2 '13 at 8:26
• Do you use a SpriteBatch at all by any chance? It changes a lot of properties that can cause some damage. – Vaughan Hilts Jun 3 '13 at 1:50

I don't know XNA so I can't advise you as to specific programming, but make sure that when you create/request your context (GraphicsDevice?) you request that it has a depth buffer.