pythag0ras_
pythag0ras_

Reputation: 162

MonoGame VertexPositionColor draws in incorrect place

I'm trying to make a rendering library for monogame and I'm currently working on drawing 2D polygons. However, The positions don't make any sense. Somehow, drawing them at (0, 0, 0), (100. 0, 0), (0, 100, 0), and (100, 100, 0) doesn't reach the top-left coordinate (0, 0). How do I fix this?

My Code:

 BasicEffect basicEffect = new BasicEffect(GraphicsDevice);  
 VertexPositionColor[] vert = new VertexPositionColor[4];  
 vert[0].Position = new Vector3(0, 0, 0);  
 vert[1].Position = new Vector3(100, 0, 0);  
 vert[2].Position = new Vector3(0, 100, 0);  
 vert[3].Position = new Vector3(100, 100, 0); 
 short[] ind = new short[6];  
 ind[0] = 0;  
 ind[1] = 2;  
 ind[2] = 1;  
 ind[3] = 1;  
 ind[4] = 2;  
 ind[5] = 3;  
 foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes)   
 {  
     effectPass.Apply();  
     GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(  
     PrimitiveType.TriangleList, vert, 0, vert.Length, ind, 0, ind.Length / 3);

}

RESULT: https://i.sstatic.net/qUhUG.jpg

Upvotes: 0

Views: 280

Answers (1)

CoolBots
CoolBots

Reputation: 4869

MonoGame uses a different origin for 2D and 3D coordinate systems. In 2D, (0, 0) is top-left corner, and Y increases toward the bottom of the screen. In 3D, (0,0,0) is the center of the screen, and the coordinate grid works very much like it does in mathematics - think 4 quadrants in math, if you "flatten" the z-axis.

You're drawing in Quadrant I. If you want the drawing to be based on top-left corner, you need to translate your vertices by -1/2 your viewport width and +1/2 your viewport height.

Upvotes: 1

Related Questions