Question Creating 3D XNA using DrawIndexedPrimitives

njsokalski

Well-known member
Joined
Mar 16, 2011
Messages
102
Programming Experience
5-10
I am relatively new to 3D (and XNA in general), and am working on my first project involving 3D. Right now, I am simply trying to make sure I understand how to draw a triangle using the DrawIndexedPrimitives method. I have been able to draw one using the following:

In my override of LoadContent I have:

Dim vertices As New List(Of VertexPositionNormalTexture)
Dim indices As New List(Of UShort)
vertices.Add(New VertexPositionNormalTexture(New Vector3(0, 0, 0), New Vector3(0, 0, 1), Nothing))
vertices.Add(New VertexPositionNormalTexture(New Vector3(0, 50, 0), New Vector3(0, 0, 1), Nothing))
vertices.Add(New VertexPositionNormalTexture(New Vector3(50, 0, 0), New Vector3(0, 0, 1), Nothing))
indices.Add(0)
indices.Add(1)
indices.Add(2)
Me.vertexbfr = New VertexBuffer(GraphicsDevice, GetType(VertexPositionNormalTexture), vertices.Count, BufferUsage.None)
Me.vertexbfr.SetData(vertices.ToArray())
Me.indexbfr = New IndexBuffer(GraphicsDevice, GetType(UShort), indices.Count, BufferUsage.None)
Me.indexbfr.SetData(indices.ToArray())
Me.basiceff = New BasicEffect(GraphicsDevice)
Me.basiceff.EnableDefaultLighting()
Me.basiceff.PreferPerPixelLighting = True

And in my override of Draw I have:
GraphicsDevice.Clear(Color.CornflowerBlue)
Me.basiceff.World = Matrix.CreateFromYawPitchRoll(0, 0, 0) * Matrix.CreateTranslation(New Vector3(0, 0, 0))
Me.basiceff.View = Matrix.CreateLookAt(New Vector3(100, 100, 100), New Vector3(0, 0, 0), Vector3.Up)
Me.basiceff.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 1, 10000)
Me.basiceff.DiffuseColor = Color.Red.ToVector3()
GraphicsDevice.SetVertexBuffer(Me.vertexbfr)
GraphicsDevice.Indices = Me.indexbfr
For Each effpass As EffectPass In Me.basiceff.CurrentTechnique.Passes
    effpass.Apply()
    GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, Me.vertexbfr.VertexCount, 0, CInt(Me.indexbfr.IndexCount / 3))
Next
MyBase.Draw(gameTime)

This works fine, but if I change the code that adds the indices to:
indices.Add(0)
indices.Add(2)
indices.Add(1)

it does not work. This is a triangle, shouldn't it be irrelevant what order the indices are in (as long as they are in groups of 3)? This is my first time using 3D or XNA, so it is quite possible that I just don't know about something, but if somebody could help me here it would be greatly appreciated. Thanks.
 
Last edited by a moderator:
Back
Top