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
| public class QuadRenderer
{
//buffers for rendering the quad
private readonly VertexPositionNormalTexture[] _vertexBuffer;
private readonly short[] _indexBuffer;
//Declaration des Vector3 position
public Vector3 Origin;
public Vector3 UpperLeft;
public Vector3 LowerLeft;
public Vector3 UpperRight;
public Vector3 LowerRight;
public Vector3 Normal;
public Vector3 Up;
public Vector3 Left;
public Vector3 Position;
//Déclaration du type de vertex à utiliser
List<Vector2> textureCorner;
//private VertexBuffer _vBuffer;
//private IndexBuffer _iBuffer;
public QuadRenderer(GraphicsDevice graphicsDevice)
{
_vertexBuffer = new VertexPositionNormalTexture[4];
_vertexBuffer[0] = new VertexPositionNormalTexture(new Vector3(-1, -0, 0), Vector3.Forward, new Vector2(0, 0));
_vertexBuffer[1] = new VertexPositionNormalTexture(new Vector3(1, 0, 0), Vector3.Forward, new Vector2(1, 0));
_vertexBuffer[2] = new VertexPositionNormalTexture(new Vector3(0, 1, 0), Vector3.Forward, new Vector2(0, 1));
_vertexBuffer[3] = new VertexPositionNormalTexture(new Vector3(-0, 0, 0), Vector3.Forward, new Vector2(1, 1));
_indexBuffer = new short[] { 0, 3, 2, 0, 1, 3 };
}
public QuadRenderer(Vector3 origin, Vector3 normal, Vector3 up, float width, float height)
{
textureCorner = new List<Vector2>();
Position = Vector3.Zero;
Origin = origin;
Normal = normal;
Up = up;
// Calculate the quad corners
Left = Vector3.Cross(normal, Up);
Vector3 uppercenter = (Up * height / 2) + origin;
UpperLeft = uppercenter + (Left * width / 2);
UpperRight = uppercenter - (Left * width / 2);
LowerLeft = UpperLeft - (Up * height);
LowerRight = UpperRight - (Up * height);
_vertexBuffer = new VertexPositionNormalTexture[4];
Vector2 textureUpperLeft = new Vector2(0.0f, 0.0f);
Vector2 textureUpperRight = new Vector2(1.0f, 0.0f);
Vector2 textureLowerLeft = new Vector2(0.0f, 1.0f);
Vector2 textureLowerRight = new Vector2(1.0f, 1.0f);
_vertexBuffer[0].Position = LowerLeft;
_vertexBuffer[0].TextureCoordinate = textureLowerLeft;
_vertexBuffer[1].Position = UpperLeft;
_vertexBuffer[1].TextureCoordinate = textureUpperLeft;
_vertexBuffer[2].Position = LowerRight;
_vertexBuffer[2].TextureCoordinate = textureLowerRight;
_vertexBuffer[3].Position = UpperRight;
_vertexBuffer[3].TextureCoordinate = textureUpperRight;
_indexBuffer = new short[6];
_indexBuffer[0] = 0;
_indexBuffer[1] = 3;
_indexBuffer[2] = 2;
_indexBuffer[3] = 0;
_indexBuffer[4] = 1;
_indexBuffer[5] = 3;
}
public void RenderQuad(GraphicsDevice graphicsDevice )
{
graphicsDevice.DrawUserIndexedPrimitives
(PrimitiveType.TriangleList, _vertexBuffer, 0, _vertexBuffer.Length, _indexBuffer, 0, _indexBuffer.Length / 3);
}
public void RenderQuad(GraphicsDevice graphicsDevice, Vector2 v1, Vector2 v2, bool invert)
{
if (invert)
{
_vertexBuffer[0].Position.X = v1.X;
_vertexBuffer[0].Position.Y = -v2.Y;
_vertexBuffer[2].Position.X = v2.X;
_vertexBuffer[2].Position.Y = -v2.Y;
_vertexBuffer[1].Position.X = v1.X;
_vertexBuffer[1].Position.Y = -v1.Y;
_vertexBuffer[3].Position.X = v2.X;
_vertexBuffer[3].Position.Y = -v1.Y;
}
else
{
_vertexBuffer[0].Position.X = v1.X;
_vertexBuffer[0].Position.Y = v2.Y;
_vertexBuffer[1].Position.X = v2.X;
_vertexBuffer[1].Position.Y = v2.Y;
_vertexBuffer[2].Position.X = v1.X;
_vertexBuffer[2].Position.Y = v1.Y;
_vertexBuffer[3].Position.X = v2.X;
_vertexBuffer[3].Position.Y = v1.Y;
}
for (int i = 0; i < _vertexBuffer.Length; i++)
{
_vertexBuffer[i].Position.Z = 0;
}
graphicsDevice.DrawUserIndexedPrimitives
(PrimitiveType.TriangleList, _vertexBuffer, 0, _vertexBuffer.Length, _indexBuffer, 0, _indexBuffer.Length / 3);
}
} |
Partager