0

我一直在尝试制作 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 x 25 块的完美方形网格。但线框看起来像:

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

4

1 回答 1

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
于 2013-08-21T12:01:33.733 回答