In the code below, I want to draw a box on my screen. But when I run it, there is an error message:
The current vertex declaration does not include all the elements required by the current vertex shader. Color0 is missing.
Here is the code in question:
private SpriteBatch _sb;
private Vector3 _position;
public Vector3 Position
{
get { return _position; }
}
private Matrix _world;
public Matrix World
{
get { return _world; }
}
private Texture2D _image;
private VertexPositionColor[] _vertices;
private short[] _indices;
private Game _game;
private IndexBuffer _ib;
private VertexBuffer _vb;
private BasicEffect _basicEffect;
private BoundingBox _boundingBox;
public BoundingBox BoundingBox
{
get { return _boundingBox; }
}
public Blackhole(Game game, Vector3 position, Matrix view, Matrix projection)
{
_game = game;
_vertices = new VertexPositionColor[4];
_vertices[0] = new VertexPositionColor(new Vector3(-100, 100, 0), Color.Red);
_vertices[1] = new VertexPositionColor(new Vector3(100, 100, 0), Color.Green);
_vertices[2] = new VertexPositionColor(new Vector3(100, -100, 0), Color.Blue);
_vertices[3] = new VertexPositionColor(new Vector3(-100, -100, 0), Color.Yellow);
_indices = new short[6];
_indices[0] = 0; _indices[1] = 1; _indices[2] = 2;
_indices[3] = 0; _indices[4] = 2; _indices[5] = 3;
_vb = new VertexBuffer(game.GraphicsDevice, VertexPositionColor.VertexDeclaration, 4, BufferUsage.WriteOnly);
_vb.SetData<VertexPositionColor>(_vertices);
_ib = new IndexBuffer(game.GraphicsDevice, typeof(short), 6, BufferUsage.WriteOnly);
_ib.SetData<short>(_indices);
game.GraphicsDevice.SetVertexBuffer(_vb);
game.GraphicsDevice.Indices = _ib;
_basicEffect = new BasicEffect(game.GraphicsDevice);
_basicEffect.View = view;
_basicEffect.Projection = projection;
_basicEffect.World = _world;
_basicEffect.VertexColorEnabled = true;
_basicEffect.TextureEnabled = false;
RasterizerState newState = new RasterizerState();
newState.CullMode = CullMode.None;
game.GraphicsDevice.RasterizerState = newState;
_boundingBox = new BoundingBox(new Vector3(_position.X, _position.Y, -1), new Vector3(-_position.X, -_position.Y, 1));
}
public void Update(GameTime gt)
{
_world = Matrix.Identity * Matrix.CreateTranslation(_position);
}
public void Draw(Matrix view, Matrix projection)
{
_basicEffect.View = view;
_basicEffect.Projection = projection;
foreach (EffectPass f in _basicEffect.CurrentTechnique.Passes)
{
f.Apply();
_game.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
}
}
What is the cause of my error?