I am trying to learn to draw a cube.

I am getting an InvalidCallException on the line
VB Code:
  1. device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 12)

I cannot find anything in VB.NET on Indexing . There is some older versions floating out there,
especially if I need to translate from some archaic C code. Did you know that the lock and unlock thingy
has been reworked!

So what is the problem with this????
VB Code:
  1. Imports Microsoft.DirectX
  2. Imports Microsoft.DirectX.Direct3D
  3.  
  4. Public Class Cube
  5.   Inherits Form
  6.   Dim vertices As VertexBuffer = Nothing
  7.   Dim indices As IndexBuffer = Nothing
  8.   Dim PP As PresentParameters = New PresentParameters()
  9.   Dim pause As Boolean = False
  10.   Dim device As Device = Nothing
  11.  
  12.   Public Function InitializeGraphics() As Boolean
  13.     Try
  14.       PP.Windowed = True
  15.       PP.SwapEffect = SwapEffect.Copy
  16.       device = New Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, PP)
  17.       OnCreateVertexBuffer()
  18.       OnCreateIndexBuffer()
  19.       device.RenderState.CullMode = Cull.None
  20.       device.RenderState.Lighting = False
  21.       pause = False
  22.       Return True
  23.     Catch ex As DirectXException
  24.       Return False
  25.     End Try
  26.   End Function
  27.  
  28.   Public Sub OnCreateVertexBuffer()
  29.     Dim vertex(7) As CustomVertex.PositionColored
  30.     Dim vcolor(7) As System.Drawing.Color
  31.     vertices = New VertexBuffer(GetType(CustomVertex.PositionColored), 8, device, Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default)
  32.  
  33.     vcolor(0) = Color.AntiqueWhite
  34.     vcolor(1) = Color.Brown
  35.     vcolor(2) = Color.Blue
  36.     vcolor(3) = Color.Cyan
  37.     vcolor(4) = Color.DarkGreen
  38.     vcolor(5) = Color.MediumPurple
  39.     vcolor(6) = Color.Red
  40.     vcolor(7) = Color.Violet
  41.     vertex(0).Position = New Vector3(1.0F, 1.0F, 1.0F)
  42.     vertex(0).Color = vcolor(0).ToArgb()
  43.     vertex(1).Position = New Vector3(1.0F, 1.0F, -1.0F)
  44.     vertex(1).Color = vcolor(1).ToArgb()
  45.     vertex(2).Position = New Vector3(-1.0F, 1.0F, 1.0F)
  46.     vertex(2).Color = vcolor(2).ToArgb()
  47.     vertex(3).Position = New Vector3(-1.0F, 1.0F, -1.0F)
  48.     vertex(3).Color = vcolor(3).ToArgb()
  49.     vertex(4).Position = New Vector3(-1.0F, -1.0F, 1.0F)
  50.     vertex(4).Color = vcolor(4).ToArgb()
  51.     vertex(5).Position = New Vector3(-1.0F, -1.0F, -1.0F)
  52.     vertex(5).Color = vcolor(5).ToArgb()
  53.     vertex(6).Position = New Vector3(1.0F, -1.0F, 1.0F)
  54.     vertex(6).Color = vcolor(6).ToArgb()
  55.     vertex(7).Position = New Vector3(1.0F, -1.0F, -1.0F)
  56.     vertex(7).Color = vcolor(7).ToArgb()
  57.  
  58.     vertices.SetData(vertex, 0, LockFlags.None)
  59.   End Sub
  60.  
  61.   Public Sub OnCreateIndexBuffer()
  62.     Dim pointlist(35) As Integer
  63.     indices = New IndexBuffer(pointlist(0).GetType, pointlist.Length, device, Usage.WriteOnly, Pool.Default)
  64.  
  65.     pointlist(0) = 0 : pointlist(1) = 1 : pointlist(2) = 2
  66.     pointlist(3) = 2 : pointlist(4) = 1 : pointlist(5) = 3
  67.     pointlist(6) = 3 : pointlist(7) = 1 : pointlist(8) = 7
  68.     pointlist(9) = 5 : pointlist(10) = 3 : pointlist(11) = 7
  69.     pointlist(12) = 2 : pointlist(13) = 3 : pointlist(14) = 5
  70.     pointlist(15) = 2 : pointlist(16) = 5 : pointlist(17) = 4
  71.     pointlist(18) = 0 : pointlist(19) = 2 : pointlist(20) = 4
  72.     pointlist(21) = 0 : pointlist(22) = 4 : pointlist(23) = 6
  73.     pointlist(24) = 0 : pointlist(25) = 6 : pointlist(26) = 1
  74.     pointlist(27) = 1 : pointlist(28) = 6 : pointlist(29) = 7
  75.     pointlist(30) = 5 : pointlist(31) = 7 : pointlist(32) = 6
  76.     pointlist(33) = 5 : pointlist(34) = 6 : pointlist(35) = 4
  77.  
  78.     indices.SetData(pointlist, 0, LockFlags.None)
  79.   End Sub
  80.  
  81.   Public Sub Render()
  82.     If device Is Nothing Then Return
  83.     If pause Then Return
  84.  
  85.      device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0F, 0)
  86.      device.BeginScene()
  87.      SetupMatrices()
  88.     device.SetStreamSource(0, vertices, 0)
  89.     device.Indices = indices
  90.     ' device.VertexFormat = CustomVertex.PositionColored.Format
  91.     device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 12)
  92.     device.EndScene()
  93.     device.Present()
  94.   End Sub
  95.   Private Sub SetupMatrices()
  96.     Dim time As Integer = 5000
  97.  
  98.     Dim iTime As Integer = Environment.TickCount Mod time
  99.     Dim yAngle As Single = iTime * (2.0F * CSng(Math.PI)) / time
  100.     device.Transform.World = Matrix.RotationY(yAngle)
  101.     Dim etime As Integer = Environment.TickCount Mod time
  102.     Dim zangle As Single = etime * (2.0F * CSng(Math.PI)) / time
  103.  
  104.     device.Transform.World = Matrix.RotationZ(zangle)
  105.    device.Transform.View = Matrix.LookAtLH(New Vector3(0.0F, 3.0F, -5.0F), New Vector3(0.0F, 0.0F, 0.0F), New Vector3(0.0F, 1.0F, 0.0F))
  106.    device.Transform.Projection = Matrix.PerspectiveFovLH(CSng(Math.PI) / 6, 1.0F, 1.0F, 100.0F)
  107.   End Sub
  108.  
  109.   Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
  110.     Me.Render()
  111.   End Sub
  112.  
  113.   Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
  114.     If CInt(CByte(AscW(e.KeyChar))) = CInt(System.Windows.Forms.Keys.Escape) Then
  115.       Me.Close()
  116.     End If
  117.  
  118.   End Sub
  119.  
  120.   Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
  121.     pause = ((Me.WindowState = FormWindowState.Minimized) Or Not Me.Visible)
  122.   End Sub
  123. End Class
  124.  
  125. Module main
  126.    Sub Main()
  127.  
  128.     Using paper As New Cube
  129.       If Not paper.InitializeGraphics() Then
  130.         MessageBox.Show("exitting with error")
  131.         Return
  132.       End If
  133.       paper.Show()
  134.       While (paper.Created)
  135.         paper.Render()
  136.         Application.DoEvents()
  137.       End While
  138.     End Using
  139.   End Sub
  140. End Module