我查看了从谷歌找到的示例中的一些代码并将其实现到我的游戏中,但问题是我收到错误:当前顶点声明不包括当前顶点着色器所需的所有元素。位置 0 丢失。我查看了 MSDN 文档,但看不出我做错了什么
class Cube
{
public GraphicsDevice device;
const int number_of_vertices = 8;
const int number_of_indices = 36;
private VertexBuffer vertices;
private IndexBuffer indices;
public Cube(GraphicsDevice graphicsDevice)
{
device = graphicsDevice;
}
void CreateCubeVertexBuffer()
{
VertexPositionColor[]cubeVertices = new VertexPositionColor[number_of_vertices];
cubeVertices[0].Position = new Vector3(-1, -1, -1);
cubeVertices[1].Position = new Vector3(-1, -1, 1);
cubeVertices[2].Position = new Vector3(1, -1, 1);
cubeVertices[3].Position = new Vector3(1, -1, -1);
cubeVertices[4].Position = new Vector3(-1, 1, -1);
cubeVertices[5].Position = new Vector3(-1, 1, 1);
cubeVertices[6].Position = new Vector3(1, 1, 1);
cubeVertices[7].Position = new Vector3(1, 1, -1);
cubeVertices[0].Color = Color.Black;
cubeVertices[1].Color = Color.Red;
cubeVertices[2].Color = Color.Yellow;
cubeVertices[3].Color = Color.Green;
cubeVertices[4].Color = Color.Blue;
cubeVertices[5].Color = Color.Magenta;
cubeVertices[6].Color = Color.White;
cubeVertices[7].Color = Color.Cyan;
vertices = new VertexBuffer(device, VertexPositionColor.VertexDeclaration, number_of_vertices, BufferUsage.WriteOnly);
vertices.SetData < VertexPositionColor > (cubeVertices);
}
void CreateCubeIndexBuffer() {
UInt16[]cubeIndices = new UInt16[number_of_indices];
//bottom face
cubeIndices[0] = 0;
cubeIndices[1] = 2;
cubeIndices[2] = 3;
cubeIndices[3] = 0;
cubeIndices[4] = 1;
cubeIndices[5] = 2;
//top face
cubeIndices[6] = 4;
cubeIndices[7] = 6;
cubeIndices[8] = 5;
cubeIndices[9] = 4;
cubeIndices[10] = 7;
cubeIndices[11] = 6;
//front face
cubeIndices[12] = 5;
cubeIndices[13] = 2;
cubeIndices[14] = 1;
cubeIndices[15] = 5;
cubeIndices[16] = 6;
cubeIndices[17] = 2;
//back face
cubeIndices[18] = 0;
cubeIndices[19] = 7;
cubeIndices[20] = 4;
cubeIndices[21] = 0;
cubeIndices[22] = 3;
cubeIndices[23] = 7;
//left face
cubeIndices[24] = 0;
cubeIndices[25] = 4;
cubeIndices[26] = 1;
cubeIndices[27] = 1;
cubeIndices[28] = 4;
cubeIndices[29] = 5;
//right face
cubeIndices[30] = 2;
cubeIndices[31] = 6;
cubeIndices[32] = 3;
cubeIndices[33] = 3;
cubeIndices[34] = 6;
cubeIndices[35] = 7;
indices = new IndexBuffer(device, IndexElementSize.SixteenBits, number_of_indices, BufferUsage.WriteOnly);
indices.SetData < UInt16 > (cubeIndices);
}
public void Draw(BasicEffect effect)
{
device.SetVertexBuffer(vertices);
device.Indices = indices;
foreach(EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, number_of_vertices, 0, number_of_indices / 3);
}
}
}
编辑:
public override void Draw(GameTime gameTime)
{
cube.CreateCubeIndexBuffer();
cube.CreateCubeVertexBuffer();
foreach (var item in entities)
{
effect.VertexColorEnabled = false;
effect.TextureEnabled = true;
effect.Texture = item.Texture;
Matrix center = Matrix.CreateTranslation(new Vector3(-0.5f, -0.5f, -0.5f));
Matrix scale = Matrix.CreateScale(1f);
Matrix translate = Matrix.CreateTranslation(item.Position);
effect.World = center * scale * translate;
effect.View = camera.View;
effect.Projection = camera.Projection;
cube.Draw(effect);
}
base.Draw(gameTime);
}
这可行,但 FPS 较慢。
构造函数:
public Cube(GraphicsDevice graphicsDevice)
{
device = graphicsDevice;
CreateCubeIndexBuffer();
CreateCubeVertexBuffer();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
font = Content.Load<SpriteFont>("font");
grass = Content.Load<Texture2D>("grass");
// TODO: use this.Content to load your game content here
Components.Add(cubes);
cube.CreateCubeIndexBuffer();
cube.CreateCubeVertexBuffer();
for (int x = 0; x < 50; x++)
{
for (int z = 0; z < 50; z++)
{
cubes.Add(new Vector3(mapX[x], 0f, mapZ[z]), Matrix.Identity, grass);
}
}
}
这是我的主要游戏课。这不起作用,并为我提供了未设置为对象错误实例的对象引用,该对象错误指向我的多维数据集类中的行:
indices = new IndexBuffer(device, IndexElementSize.SixteenBits, number_of_indices, BufferUsage.WriteOnly);