@S_B wrote:
Hi,
I'm having some trouble renedering simples shapes with Monogame.
Im on Windows 10 and im using the monogame directX template.I have a basic code that is supposed to draw just a rectangle on screen and rotate it around.
I'm trying to use a vertex buffer and a index buffer and render the whole thing as a TriangleStrip.I've came up with this :
And then i coded it. I understand the fact that monogame is culling my vertex in a counter clock wise direction.
So i indexed them using clock ward direction (points 0-1-2 for the first triangle and 2-3-0 for the second one).The thing is, when i try to render it on my screen, i get one of them displaying properly and another that is being drawn in the opposite way.
Maybe there is an issue with monogame or (more probably) i didnt understood the concept of indexed primitives correctly.
Here is the result : (for some reasons the gif isnt showing except if you click on it, on my computer at least)Its supposed to be a simple rectangle rotating on the screen and disapearing when you see his backward face but instead i have 2 triangles facing opposite direction, even tho im respecting the counter clock wise culling thing.
And here is the basic code i used to do it :
public class Game1 : Game { GraphicsDeviceManager graphics; VertexBuffer VertexBuffer { get; set; } IndexBuffer IndexBuffer { get; set; } BasicEffect effect; public Game1() { graphics = new GraphicsDeviceManager(this); } protected override void Initialize() { // Create the shape into the buffers VertexPositionColor[] vertices = new VertexPositionColor[4]; vertices[0] = new VertexPositionColor(new Vector3(-1, 1, 0), Color.Red); vertices[1] = new VertexPositionColor(new Vector3(1, 1, 0), Color.Green); vertices[2] = new VertexPositionColor(new Vector3(1, -1, 0), Color.Blue); vertices[3] = new VertexPositionColor(new Vector3(-1, -1, 0), Color.Yellow); VertexBuffer = new VertexBuffer(graphics.GraphicsDevice, typeof(VertexPositionColor), 4, BufferUsage.WriteOnly); VertexBuffer.SetData(vertices); short[] indices = new short[6]; indices[0] = 0; indices[1] = 1; indices[2] = 2; indices[3] = 2; indices[4] = 3; indices[5] = 0; IndexBuffer = new IndexBuffer(graphics.GraphicsDevice, typeof(short), 6, BufferUsage.WriteOnly); IndexBuffer.SetData(indices); base.Initialize(); } protected override void LoadContent() { System.Console.WriteLine(GraphicsDevice.RasterizerState); effect = new BasicEffect(GraphicsDevice); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // Draw the shape GraphicsDevice.SetVertexBuffer(VertexBuffer); GraphicsDevice.Indices = IndexBuffer; effect.World = Matrix.CreateTranslation(Vector3.Zero) * Matrix.CreateRotationY((float)gameTime.TotalGameTime.TotalSeconds); effect.View = Matrix.CreateLookAt(new Vector3(0, 0, -10), Vector3.Zero, Vector3.UnitY); effect.Projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.PiOver4, Window.ClientBounds.Width / (float)Window.ClientBounds.Height, 1, 100); effect.VertexColorEnabled = true; foreach (var pass in effect.CurrentTechnique.Passes) { pass.Apply(); graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, 0, 0, 4); } base.Draw(gameTime); } }
Can someone tell me where i fucked up ?
Posts: 6
Participants: 3