頂點數爲要繪製的三角形的頂點數總和,包括重複的
索引一定要是short(int16)類型
根據dx左手座標系,從左下角爲0,開始計算索引,一定要是順時針方向繪製
CODE
Dim countX As Integer = mdata.PointCountX
Dim countY As Integer = mdata.PointCountY
Dim Indices((countX - 1) * (countY - 1) * 6 - 1) As Short
Dim Vertices(countX * countY - 1) As CustomVertex.PositionColored
' Create mesh
For j As Integer = 0 To countY - 1
For i As Integer = 0 To countX - 1
Vertices(j * countX + i) = New CustomVertex.PositionColored(CSng(mdata.ValueX(i)), CSng(mdata.GetPointValue(i, j)), CSng(mdata.ValueY(j)), mdata.GetPointColor(i, j))
Next
Next
'' Calculate the index buffer.
Dim idx As Integer = 0
For y As Integer = 0 To (countY - 2)
For x As Integer = 0 To (countX - 2)
idx = (y * (countX - 1) + x) * 6
Dim vertexIndex As Integer = y * countX + x
Indices(idx) = CShort(vertexIndex)
Indices((idx + 1)) = CShort(vertexIndex + countX)
Indices((idx + 2)) = CShort(vertexIndex + countX + 1)
Indices((idx + 3)) = CShort(vertexIndex)
Indices((idx + 4)) = CShort(vertexIndex + countX + 1)
Indices((idx + 5)) = CShort(vertexIndex + +1)
Next x
Next y
Dim mesh As New Mesh(countX * countY * 2, countX * countY * 3, MeshFlags.Managed, CustomVertex.PositionColored.Format, device)
mesh.VertexBuffer.SetData(Vertices, 0, LockFlags.None)
mesh.IndexBuffer.SetData(Indices, 0, LockFlags.None)
Return mesh