2013-08-20 140 views
0

我一直在試圖讓2D網格去。這是一個遊戲地圖。 不幸的是,網格並不是它應該的。我無法弄清楚爲什麼。 有沒有人有想法?DirectX 2D網格

Imports Microsoft.DirectX 
Imports Microsoft.DirectX.Direct3D 

Public Class clsIsometric 

'================================== 
' SETTINGS 
'================================== 

Private tile_size As New Point(64, 64) 'Size of one tile in pixels 
Private map_size As New Point(25, 25) 'Amount of tiles in total 

Private gDevice As Device 
Private bufVertex As VertexBuffer 
Private bufIndex As IndexBuffer 
Private gVertices() As CustomVertex.TransformedColored 
Private gIndices() As Integer 

'================================== 
' CONSTRUCTOR 
'================================== 

Public Sub New(vDevice As Device) 

    gDevice = vDevice 

End Sub 

Public Sub dispose() 

    bufVertex.Dispose() 
    bufIndex.Dispose() 
    bufVertex = Nothing 
    bufIndex = Nothing 

End Sub 

'================================== 
' RENDERING 
'================================== 

Public Sub buildMap() 

    ' Recreate buffers to fit the map size 
    ReDim gVertices((map_size.X + 1) * (map_size.Y + 1)) ' x+1 * y+1 
    ReDim gIndices(map_size.X * map_size.Y * 6)    ' x * y * 6 
    Dim k As Integer 

    For cX = 0 To map_size.X - 1 'Rows 
     For cY = 0 To map_size.Y - 1 'Columns 

      'VERTEX 
      k = cX * map_size.X + cY 
      gVertices(k) = New CustomVertex.TransformedColored(cX * tile_size.X, cY * tile_size.Y, 0, 1, Color.Blue.ToArgb) 

     Next cY 
    Next cX 

    Dim vertexPerCol As Integer = map_size.Y + 1 

    k = 0 
    For ccX = 0 To map_size.X - 1 
     For ccY = 0 To map_size.Y - 1 

      gIndices(k) = ccX * vertexPerCol + ccY     ' 0 
      gIndices(k + 1) = (ccX + 1) * vertexPerCol + (ccY + 1) ' 1 
      gIndices(k + 2) = (ccX + 1) * vertexPerCol + ccY  ' 2 

      gIndices(k + 3) = ccX * vertexPerCol + ccY    ' 3 
      gIndices(k + 4) = ccX * vertexPerCol + (ccY + 1)  ' 4 
      gIndices(k + 5) = (ccX + 1) * vertexPerCol + (ccY + 1) ' 5 

      k += 6 'Each tile has 6 indices. Increase for next tile 

     Next 
    Next 

    bufVertex = New VertexBuffer(GetType(CustomVertex.TransformedColored), gVertices.Length, gDevice, Usage.Dynamic Or Usage.WriteOnly, CustomVertex.TransformedColored.Format, Pool.Default) 
    bufIndex = New IndexBuffer(GetType(Integer), gIndices.Length, gDevice, Usage.WriteOnly, Pool.Default) 

End Sub 

Public Sub render() 

    'RENDER THE MAP 

    bufVertex.SetData(gVertices, 0, LockFlags.ReadOnly) 
    bufIndex.SetData(gIndices, 0, LockFlags.None) 

    gDevice.VertexFormat = CustomVertex.TransformedColored.Format 
    gDevice.SetStreamSource(0, bufVertex, 0) 
    gDevice.Indices = bufIndex 
    gDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, gVertices.Length, 0, CInt(gIndices.Length/3)) 

End Sub 

End Class 

這應該輸出25乘25瓦的完美正方形網格。但線框的樣子:

http://i43.tinypic.com/2whf51c.jpg

+0

我想我必須在創建頂點的同時實現世界的變換。還不確定,測試。 – JaredNinja

+0

你有一個View Projection Matrix嗎? D3DXMatrixOrthoLH matOrtho,640,480,-1000,1000 D3DDevice.SetTransform D3DTS_PROJECTION,matOrtho –

+0

Hey Codie。事實證明,它與任何觀點,投影或世界無關。這是上面代碼中循環內的算法。但謝謝你的想法。 – JaredNinja

回答

0

你環路建設頂點似乎太早結束,因爲你有N + 1個頂點每行/列。它只將數組填充到前列,這會導致頂點移位。

For cX = 0 To map_size.X 'Rows 
    For cY = 0 To map_size.Y 'Columns 

     'VERTEX 
     k = cX * (map_size.X + 1) + cY 
     gVertices(k) = New CustomVertex.TransformedColored(cX * tile_size.X, cY * tile_size.Y, 0, 1, Color.Blue.ToArgb) 

    Next cY 
Next cX 
+0

你是對的Gnietschow!感謝您注意到這一點。現在它是一個很好的方形地圖。然而!這不是一個網格,它看起來像一個日本國旗哈哈。以下是截圖:http://i41.tinypic.com/dlkpzk.png – JaredNinja