I've been starting to write some XNA / Monogame code lately, and wanted to draw a quad with a Texture2D attached to it.
After searching the internet for a while, I've come up with this render code:
public void Draw(GraphicsDevice gd)
{
BasicEffect effect = new(gd);
effect.Texture = this.texture;
effect.TextureEnabled = true;
VertexPositionTexture[] vertices = new VertexPositionTexture[4];
vertices[0].Position = this.pointA;
vertices[1].Position = this.pointB;
vertices[2].Position = this.pointC;
vertices[3].Position = this.pointD;
foreach (EffectPass pass in effect.CurrentTechnique.Passes) {
pass.Apply();
gd.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices,
0, 3, new[] { 0, 1, 2, 1, 2, 3 }, 0, 2);
}
}
in a Quad
class:
internal sealed class Quad
{
Vector3 pointA;
Vector3 pointB;
Vector3 pointC;
Vector3 pointD;
Texture2D texture;
}
with this initialization code:
Quad quad = new Quad(new Vector3[]
{ new Vector3(-0.5f, -0.5f, 1f), new Vector3(-0.5f, +0.5f, 1f),
new Vector3(+0.5f, -0.5f, 1f), new Vector3(+0.5f, +0.5f, 1f) } //points
debugTexture);
Currently, nothing appears on the screen.
I've tried drawing 1 triangle only, switching out the debugTexture
,
using DrawUserPrimitives
instead, and none of them seem to work.
After doing some testing, I found that it did actually work. I was just clearing the screen with white, which, coincidentally, the quad I was drawing was also white.
This means that the only problem now was that the quad didn't have the texture attached.