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
|
public class Polygon
{
uint textureId;
Plane polyPlane;
uint nbVertices;
Vertex [] vertices;
// main device
GraphicsDevice device;
VertexBuffer vertexBuffer;
IndexBuffer indexBuffer;
int nbIndices;
public Polygon(GraphicsDevice device, uint textureId, Plane polyPlane, uint nbVertices, Vertex [] vertices)
{
this.device = device;
this.textureId = textureId;
this.polyPlane = polyPlane;
this.nbVertices = nbVertices;
this.vertices = vertices;
}
public Polygon(GraphicsDevice device)
{
this.device = device;
}
public void Initialize()
{
InitializeVertices();
InitializeIndices();
}
private void InitializeVertices()
{
VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[nbVertices];
for (int i = 0; i < nbVertices; i++)
{
float x = (float)this.Vertices[i].Coord.X;
float y = (float)this.Vertices[i].Coord.Y;
float z = (float)this.Vertices[i].Coord.Z;
Vector3 normal = new Vector3((float)this.PolyPlane.Normal.X,
(float)this.PolyPlane.Normal.Y,
(float)this.PolyPlane.Normal.Z);
Vector2 texture = new Vector2((float)this.Vertices[i].U,
(float)this.Vertices[i].V);
Vector3 position = new Vector3(x, y, z);
vertices[i].Normal = normal;
vertices[i].Position = position;
vertices[i].TextureCoordinate = texture;
}
vertexBuffer = new VertexBuffer(device,
typeof(VertexPositionNormalTexture),
(int)nbVertices,
ResourceUsage.WriteOnly,
ResourceManagementMode.Automatic);
vertexBuffer.SetData(vertices);
}
private void InitializeIndices()
{
nbIndices = ((int)nbVertices - 2) * 3;
indexBuffer = new IndexBuffer(
device,
typeof(short),
nbIndices,
ResourceUsage.WriteOnly,
ResourceManagementMode.Automatic);
short[] indices = new short[nbIndices];
for (short i=0, j=0; i < nbIndices; i += 3, j++)
{
indices[i] = 0;
indices[i + 1] = j; indices[i + 1]++;
indices[i + 2] = indices[i + 1]; indices[i + 2]++;
}
indexBuffer.SetData(indices);
}
public void Render()
{
int nbPrimitives = nbIndices / 3;
device.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes);
device.Indices = indexBuffer;
device.VertexDeclaration = new VertexDeclaration(this.device, VertexPositionNormalTexture.VertexElements);
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, (int)nbVertices, 0, nbPrimitives);
}
public uint TextureId
{
get { return textureId; }
set { textureId = value; }
}
public Plane PolyPlane
{
get { return polyPlane; }
set { polyPlane = value; }
}
public uint NbVertices
{
get { return nbVertices; }
set { nbVertices = value; }
}
public Vertex[] Vertices
{
get { return vertices; }
set { vertices = value; }
}
} |
Partager