Search code examples
xnadrawingvertex-shader

DrawUserPrimitives<VertexPositionTexture> complains about Color0 missing for vertex shader


First of all, I am new to XNA and the way GPU works and how it cooperates with the XNA (or DirectX) API.

I have a polygon to draw using the SpriteBatch. I'm triangulating the polygon, and creating a VertexPositionTexture array to hold the vertices. I set the vertices (and just for simplicity, set the texture offset vector to zero), and try to draw the primitives, but I get this error:

The current vertex declaration does not include all the elements required by the current vertex shader. Color0 is missing.

Here is my code, I've double checked my vectors from triangulation, they are fine:

        VertexPositionTexture[] vertices = new VertexPositionTexture[triangulationResult.Count * 3];
        int ctr = 0;
        foreach (var item in triangulationResult)
        {
            foreach (var point in item.Vertices)
            {
                vertices[ctr++] = new VertexPositionTexture(new Vector3(point.X, point.Y, 0), Vector2.Zero);
            }
        }

        sb.GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, vertices, 0, triangulationResult.Count);

What am I possibly doing wrong here?


Solution

  • Use BasicEffect if you are drawing polygons (MSDN tutorial). You should only use SpriteBatch for sprite drawing (ie: using its Draw methods).

    The vertex element type that BasicEffect requires will depend on what settings you apply to it.

    To use a vertex element type without a colour component (like VertexPositionTexture), set BasicEffect.VertexColorEnabled to false.

    Or alternately, use a vertex element type that supplies a colour, such as VertexPositionColorTexture.

    If you want to create a BasicEffect that has the same coordinate system as SpriteBatch, see this answer or this blog post.