Question

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?

Was it helpful?

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.

OTHER TIPS

Your shader is expecting a Color in the vertex stream.... so you have to use VertexPositionColorTexture or change your shader.

It seems that you are not using any shader. If the active shader is the one used with spritebatch you won't be able to draw it right.

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

    sb.GraphicsDevice.DrawUserPrimitives<VertexPositionColorTexture>(PrimitiveType.TriangleList, vertices, 0, triangulationResult.Count);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top