I managed to draw a 3d cube using triangles like this:
protected override void LoadContent()
{
basicEffect = new BasicEffect(GraphicsDevice);
VertexPositionColor[] vertices = new VertexPositionColor[8];
short[] indices = new short[36];
vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), vertices.Length, BufferUsage.WriteOnly);
indexBuffer = new IndexBuffer(graphics.GraphicsDevice, typeof(short), indices.Length, BufferUsage.WriteOnly);
Color Color1 = Color.DarkRed;
Color Color2 = Color.Pink;
float width = 2;
float height = 1;
float depth = 0.5f;
vertices[0] = new VertexPositionColor(new Vector3(0, 0, 0), Color1);
vertices[1] = new VertexPositionColor(new Vector3(width, 0, 0), Color1);
vertices[2] = new VertexPositionColor(new Vector3(width, -height, 0), Color1);
vertices[3] = new VertexPositionColor(new Vector3(0, -height, 0), Color1);
vertices[4] = new VertexPositionColor(new Vector3(0, 0, depth), Color1);
vertices[5] = new VertexPositionColor(new Vector3(width, 0, depth), Color1);
vertices[6] = new VertexPositionColor(new Vector3(width, -height, depth), Color1);
vertices[7] = new VertexPositionColor(new Vector3(0, -height, depth), Color1);
indices[0] = 0; indices[1] = 1; indices[2] = 2;
indices[3] = 0; indices[4] = 3; indices[5] = 2;
indices[6] = 4; indices[7] = 0; indices[8] = 3;
indices[9] = 4; indices[10] = 7; indices[11] = 3;
indices[12] = 3; indices[13] = 7; indices[14] = 6;
indices[15] = 3; indices[16] = 6; indices[17] = 2;
indices[18] = 1; indices[19] = 5; indices[20] = 6;
indices[21] = 1; indices[22] = 5; indices[23] = 2;
indices[24] = 4; indices[25] = 5; indices[26] = 6;
indices[27] = 4; indices[28] = 7; indices[29] = 6;
indices[30] = 0; indices[31] = 1; indices[32] = 5;
indices[33] = 0; indices[34] = 4; indices[35] = 5;
vertexBuffer.SetData<VertexPositionColor>(vertices);
indexBuffer.SetData(indices);
}
I have two questions:
- As you can see i am using
float width = 2; float height = 1; float depth = 0.5f;
but why "width = 2" looks like 80 pixels instead of 2? I want the cubes to be 72x24x12, i can't figure out how to calculate the pixels from the field of view / aspect ratio. To draw the cube on different positions i am usingworld = Matrix.CreateTranslation(10.0f, 10.0f, 0.0f);
, is that the correct way to do this ? - How can i draw each face of the cube a different color? (Right now i know how to give color to each vertice, see Color1 variable). Right now i am using 2 different
BasicEffect
s, is there any better way perhaps ?