Skip to content

Instantly share code, notes, and snippets.

@johdah
Last active October 24, 2019 04:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johdah/4556289 to your computer and use it in GitHub Desktop.
Save johdah/4556289 to your computer and use it in GitHub Desktop.
A simple Quad object for XNA
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace XNAGists
{
public class SimpleQuad
{
private GraphicsDevice device;
private VertexPositionColor[] vertices;
private short[] indices;
private VertexBuffer vertexBuffer;
private IndexBuffer indexBuffer;
private Vector3 position = Vector3.Zero;
public SimpleQuad(GraphicsDevice device)
{
this.device = device;
InitVertices();
InitIndices();
}
private void InitIndices()
{
indices = new short[4];
// First triangle
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
// Second triangle
indices[3] = 3;
indexBuffer = new IndexBuffer(device, IndexElementSize.SixteenBits, 4, BufferUsage.None);
indexBuffer.SetData<short>(indices);
}
private void InitVertices()
{
vertices = new VertexPositionColor[4];
vertices[0] = new VertexPositionColor();
vertices[0].Position = new Vector3(-1, 1, 0);
vertices[0].Color = Color.Red;
vertices[1] = new VertexPositionColor();
vertices[1].Position = new Vector3(1, 1, 0);
vertices[1].Color = Color.Green;
vertices[2] = new VertexPositionColor();
vertices[2].Position = new Vector3(-1, -1, 0);
vertices[2].Color = Color.Blue;
vertices[3] = new VertexPositionColor();
vertices[3].Position = new Vector3(1, -1, 0);
vertices[3].Color = Color.Purple;
// Create our VertexBuffer
vertexBuffer = new VertexBuffer(device, typeof(VertexPositionColor), 4, BufferUsage.None);
vertexBuffer.SetData<VertexPositionColor>(vertices);
}
public void Update(Microsoft.Xna.Framework.GameTime gameTime)
{
//throw new NotImplementedException();
}
public void Draw(Microsoft.Xna.Framework.GameTime gameTime, BasicEffect effect)
{
effect.VertexColorEnabled = true;
device.SetVertexBuffer(vertexBuffer);
device.Indices = indexBuffer;
effect.CurrentTechnique.Passes[0].Apply();
device.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, 0, 0, vertices.Length, 0, 2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment