Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to get my code from calling each individual draw call down to using at least a VertexBuffer, and preferably an indexBuffer, but now that I'm attempting to test my code, I'm getting the error:

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

Which makes absolutely no sense to me, as my VertexDeclaration is:

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)
        );

Which clearly contains the information. I am attempting to draw with the following lines:

VertexBuffer vb = new VertexBuffer(GraphicsDevice, VertexPositionColorNormal.VertexDeclaration, c.VertexList.Count, BufferUsage.WriteOnly);
IndexBuffer ib = new IndexBuffer(GraphicsDevice, typeof(int), c.IndexList.Count, BufferUsage.WriteOnly);

vb.SetData<VertexPositionColorNormal>(c.VertexList.ToArray());
ib.SetData<int>(c.IndexList.ToArray());

GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vb.VertexCount, 0, c.IndexList.Count/3);

Where c is a Chunk class containing an 8x8x8 array of boxes.

Full code is available at https://github.com/mrbaggins/Box/tree/ProperMeshing/box/box.

Relevant locations are Chunk.cs (Contains the VertexDeclaration) and Game1.cs (Draw() is in Lines 230-250). Not much else of relevance to this problem anywhere else. Note that large commented sections are from old version of drawing.

EDIT: I condensed the problem down even further, the following draw code won't even run, with the same error:

VertexBuffer vb = new VertexBuffer(GraphicsDevice, VertexPositionColor.VertexDeclaration, 3, BufferUsage.WriteOnly);
VertexPositionColor[] temp = {
            new VertexPositionColor(new Vector3(-3.0f,-3.0f,3.0f), Color.Blue),
            new VertexPositionColor(new Vector3(0.0f,0.0f,0.0f), Color.Blue),
            new VertexPositionColor(new Vector3(1.0f,1.0f,1.0f), Color.Blue) };

vb.SetData<VertexPositionColor>(temp);
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
share|improve this question
    
I downloaded your project and it builds/runs for me. –  Luis Estrada Nov 25 '12 at 7:39
    
I downloaded too and its ok –  Blau Nov 25 '12 at 9:16
    
Really? Are you getting a stripey box, or an XYZ marker? Because the XYZ marker is the current master branch, whereas the stripey box is the current branch (And the one I linked to)... –  Phil Nov 25 '12 at 10:13
    
It was an XYZ marker with a tiny red square –  Luis Estrada Nov 25 '12 at 17:04
    
Ah, pretty sure that's the master branch, and I'm 99% sure a big brown/grey/green stripey box is the branch with the problems. If you have git, checkout propermeshing. –  Phil Nov 25 '12 at 21:38

1 Answer 1

You need to tell the GraphicsDevice which IndexBuffer and VertexBuffer to use.

GraphicsDevice.Indices = ib;
GraphicsDevice.SetVertexBuffer(vb);

Place those 2 lines before your draw call.

share|improve this answer
    
Well, that gets the program running again, which is nice. However: DrawIndexedPrimitives doesn't draw anything (I have other stuff drawing, which runs fine, but the boxes don't) DrawPrimitives draws some stuff, but because it's designed to be indexed it's all out of order and the boxes are a little out of whack / faces are wrong. –  Phil Nov 26 '12 at 4:48
    
Update^ Nevermind, I had to uncomment the ib.setData<>. Now I have it drawing again, but every second triangle has it's normal backwards, which is odd because I explicitly set them. Will have to play with it. –  Phil Nov 26 '12 at 5:03

Your Answer

 
discard

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.