我看着從一個例子,我從谷歌發現並實現它變成我的遊戲的一些代碼,但問題是,我得到一個錯誤:當前頂點聲明並沒有包括所有由所需的元素當前頂點着色器。位置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);
另外請注意,我從另一個類調用這個方法抽籤,然後調用的類在我的主遊戲類繪製方法 –
當前的頂點聲明並沒有包括所有當前頂點着色器所需的元素。這是着色器錯誤。編寫自己的着色器是最簡單的。但是,如果您使用BasicEffect並使用顏色繪製,則需要設置基本效果以禁用Texture並啓用VertexColor。 effect.TextureEnabled = false; effect.VertexColorEnabled = true;此外,您可以在傳遞效果後嘗試使用DrawIndexPrimitives,如果這樣做。 – Deukalion
的BasicEffect方法調用另一個類繪製方法這是問題... –