1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace VoxelLib
{
public class Cube
{
const short TAILLE = 2;
/*private Vector3 moveTrans;*/
private Matrix world;
private Boolean drawned = false;
Color color;
private VertexPositionColor[] vertices;
private int[] indices;
private VertexBuffer vb;
private IndexBuffer ib;
private GraphicsDevice gd;
private BasicEffect effect;
public Cube(Color color)
{
this.color = color;
}
private void CreateIndexedCubeColored(Vector3 position)
{
this.vertices = new VertexPositionColor[]
{
new VertexPositionColor( new Vector3( (position.X * TAILLE) , (position.Y * TAILLE) , - (position.Z * TAILLE) ) ,color),
new VertexPositionColor( new Vector3( (position.X * TAILLE) , - (position.Y * TAILLE) , - (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3(- (position.X * TAILLE) , - (position.Y * TAILLE) , - (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3(- (position.X * TAILLE) , (position.Y * TAILLE) , - (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3( (position.X * TAILLE) , (position.Y * TAILLE) , (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3(- (position.X * TAILLE) , (position.Y * TAILLE) , (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3(- (position.X * TAILLE) , - (position.Y * TAILLE) , (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3( (position.X * TAILLE) , - (position.Y * TAILLE) , (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3( (position.X * TAILLE) , (position.Y * TAILLE) , - (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3( (position.X * TAILLE) , (position.Y * TAILLE) , (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3( (position.X * TAILLE) , - (position.Y * TAILLE) , (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3( (position.X * TAILLE) , - (position.Y * TAILLE) , - (position.Z * TAILLE)) ,color),
/* new VertexPositionColor( new Vector3( (position.X * TAILLE)*10 , - (position.Y * TAILLE) *10, - (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3( (position.X * TAILLE)*10 , - (position.Y * TAILLE)*10 , (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , - (position.Y * TAILLE)*10 , (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , - (position.Y * TAILLE) *10, - (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , - (position.Y * TAILLE)*10 , - (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , - (position.Y * TAILLE)*10 , (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , (position.Y * TAILLE)*10 , (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , (position.Y * TAILLE)*10 , - (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3( (position.X * TAILLE)*10 , (position.Y * TAILLE)*10 , (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3( (position.X * TAILLE)*10 , (position.Y * TAILLE)*10 , - (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , (position.Y * TAILLE)*10 , - (position.Z * TAILLE)) ,color),
new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , (position.Y * TAILLE)*10 , (position.Z * TAILLE)) ,color)*/
};
this.indices = new int[]
{
0, 3, 2, 0, 2, 1,
4, 7, 6, 4,6, 5,
0, 1, 7,0,7, 9,
1, 2, 6, 1, 6, 3,
2, 3, 5,2,5, 6,
4, 5, 3,4,3, 0
};
//0 = 8 ; 21 ;
//1 = 11; 12 ;
//2 = 15 ; 16 ;
//3 = 19 ; 22
//4 = 9 ; 4 ; 20 ;
//5 = 18; 23
//6 = 14 ; 17 ;
//7 = 10 ; 13 ;
this.vb = new VertexBuffer(this.gd, typeof(VertexPositionColor), this.vertices.Length, BufferUsage.WriteOnly);
vb.SetData(this.vertices);
this.gd.SetVertexBuffer(this.vb);
this.ib = new IndexBuffer(this.gd, IndexElementSize.ThirtyTwoBits, this.indices.Length, BufferUsage.WriteOnly);
this.ib.SetData(this.indices);
this.gd.Indices = this.ib;
}
public void Initialize(GraphicsDevice gd, Vector3 position)
{
this.gd = gd;
this.CreateIndexedCubeColored(position);
this.world = Matrix.Identity;
}
public void LoadContent()
{
this.effect = new BasicEffect(this.gd);
}
public void Update() {
/* if (Keyboard.GetState().IsKeyDown(Keys.Z))
{
this.moveTrans += new Vector3(0, (float)0.1, 0);
} else if (Keyboard.GetState().IsKeyDown(Keys.S)){
this.moveTrans += new Vector3(0, (float)-0.1, 0);
} else if (Keyboard.GetState().IsKeyDown(Keys.Q)){
this.moveTrans += new Vector3((float)-0.1, 0, 0);
} else if (Keyboard.GetState().IsKeyDown(Keys.D)){
this.moveTrans += new Vector3((float)0.1, 0, 0);
}*/
}
public void Draw(Camera camera)
{
effect.View = camera.ViewMat;
effect.Projection = camera.Projection;
effect.World = world /* * Matrix.CreateTranslation(moveTrans)*/;
effect.VertexColorEnabled = true;
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
this.gd.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, this.vertices.Length, 0, this.indices.Length / 3);
}
drawned = true;
}
}
} |