DirectX 2D网格

时间:2013-08-20 11:38:28

标签: vb.net map grid 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

1 个答案:

答案 0 :(得分:0)

构建顶点的循环似乎过早结束,因为每行/每列都有n + 1个顶点。它仅将数组填充到forelast列,这会导致顶点移位。

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