0

What's wrong with my instancing below? I'm trying to draw multiple instances of a single box with vertex colors. The examples I adapted this code from used textures and a shader rather than vertex colors. So I suspect that I'm going wrong with VertexPositionColor or my use of BasicEffect. I don't need a texture so I tried to remove the shader. I should also mention this is part of a winforms application.

What I get out of this code is a big red X indicating something went terribly wrong.

What am I missing here? Obviously there's something I'm not understanding.

namespace Die
{
class DieRender
{
    VertexDeclaration instanceVertexDeclaration;

    VertexBuffer dieGeometryBuffer;
    IndexBuffer dieIndexBuffer;

    VertexBuffer instanceBuffer;

    VertexBufferBinding[] bindings;
    InstanceInfo[] instances;

    int instanceCount = 3;

    struct InstanceInfo
    {
        public Vector4 World;
    };

    public void Initialize(GraphicsDevice device) {
        GenerateInstanceVertexDeclaration();
        GenerateDieGeometry(device, Color.Blue);
        GenerateInstanceInformation(device, instanceCount);

        bindings = new VertexBufferBinding[2];
        bindings[0] = new VertexBufferBinding(dieGeometryBuffer);
        bindings[1] = new VertexBufferBinding(instanceBuffer, 0, 1);
    }

    private void GenerateInstanceVertexDeclaration() {
        VertexElement[] instanceStreamElements = new VertexElement[1];

        instanceStreamElements[0] =
                new VertexElement(0, VertexElementFormat.Vector4,
                    VertexElementUsage.Position, 1);

        instanceVertexDeclaration = new VertexDeclaration(instanceStreamElements);
    }

    public void GenerateDieGeometry(GraphicsDevice device, Color color) {
        VertexPositionColor[] vertices = new VertexPositionColor[4];
        int[] indices = new int[6];

        vertices[0].Position = new Vector3(-1, 1, 0);
        vertices[1].Position = new Vector3(1, 1, 0);
        vertices[2].Position = new Vector3(-1, -1, 0);
        vertices[3].Position = new Vector3(1, -1, 0);
        for (int i = 0; i < vertices.Count(); i++)
            vertices[i].Color = color;

        dieGeometryBuffer = new VertexBuffer(device,VertexPositionColor.VertexDeclaration,
            4, BufferUsage.WriteOnly);
        dieGeometryBuffer.SetData(vertices);

        indices[0] = 0; indices[1] = 1; indices[2] = 2;
        indices[3] = 1; indices[4] = 3; indices[5] = 2;

        dieIndexBuffer = new IndexBuffer(device, typeof(int), 6, BufferUsage.WriteOnly);
        dieIndexBuffer.SetData(indices);
    }

    private void GenerateInstanceInformation(GraphicsDevice device, Int32 count) {
        instances = new InstanceInfo[count];
        Random rnd = new Random();

        for (int i = 0; i < count; i++) {
            instances[i].World = new Vector4(-rnd.Next(20),
                                             -rnd.Next(20),
                                             -rnd.Next(20), 0);

        }

        instanceBuffer = new VertexBuffer(device, instanceVertexDeclaration,
                                          count, BufferUsage.WriteOnly);
        instanceBuffer.SetData(instances);
    }

    public void Draw(Matrix world, Matrix view, Matrix projection, GraphicsDevice device) {
        device.Clear(Color.White);

        BasicEffect effect = new BasicEffect(device);
        effect.EnableDefaultLighting();
        effect.TextureEnabled = false;
        effect.World = world;
        effect.View = view;
        effect.Projection = projection;
        effect.VertexColorEnabled = true;

        device.Indices = dieIndexBuffer;
        device.SetVertexBuffers(bindings);

        foreach (EffectPass pass in effect.CurrentTechnique.Passes) {
            pass.Apply();
            device.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2, instanceCount);
        }

    }
}
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
John Yost
  • 693
  • 5
  • 20
  • Update: I enabled exception catching in VS for the CLR and it caught the following exception on 'device.DrawInstancedPrimitives': The current vertex declaration does not include all the elements required by the current vertex shader. Normal0 is missing. – John Yost Feb 26 '14 at 22:35

1 Answers1

2

Ah! I found it... struggled with this for days and found the problem just as soon as I posted the question.

After enabling the exceptions from the CLR I caught the exception and found this similar question: XNA: The current vertex declaration does not include all the elements required by the current vertex shader. Normal0 is missing

Sorry if I wasted anybody's time.

Now... on to the next bug.

Community
  • 1
  • 1
John Yost
  • 693
  • 5
  • 20
  • 1
    "Sorry if I wasted anybody's time." Don't worry, the next person who has this problem and finds this question will benefit from it. – Cyral Feb 27 '14 at 00:18