HOW TO:建立網狀結構

更新:2007 年 11 月

您有四種基本方式可以建立網狀結構:

  • 從檔案載入網狀結構資料。

  • 複製或最佳化現有的網狀結構。

  • 使用形狀建立函式,並指定要用於建立形狀之三角形的大小和數目。

  • 使用 Mesh 建構函式。

注意事項:

Managed Direct3D Mobile 應用程式需要適用於 Pocket PC 和 Smartphone 的 Windows Mobile 5.0 版軟體。如需 Windows Mobile 軟體和 SDK 的詳細資訊,請參閱 .NET Compact Framework 的外部資源

從檔案建立網狀結構

  • 從檔案載入網狀結構資料,然後以資料填入網狀結構。.NET Compact Framework 不直接支援從檔案載入網狀結構,但是 Direct3D Mobile 範例 會定義載入網狀結構的類別 (Class)。

若要從現有網狀結構建立網狀結構

  • 請使用 Optimize 方法來建立具有最佳化資料的新網狀結構。

    -或-

    請使用 OptimizeInPlace 方法來最佳化目前的網狀結構。

    複製的主要用途是要將網狀結構從浮點格式轉換成定點格式。最佳化的主要用途是要建立可以更快速描繪的網狀結構。網狀結構最佳化會重新排列網狀結構中的三角形,以便讓在網狀結構上進行的描繪呼叫能夠更快地執行。網狀結構最佳化也會產生屬性資料表,這個表格可用來識別必須以不同紋理、呈現狀態和材料描繪的網狀結構區域。

若要使用形狀建立函式來建立網狀結構

  • 請使用 Mesh 類別的下列其中一個靜態方法,建立以浮點數學指定位置和法線的網狀結構:

若要使用 Mesh 建構函式建立網狀結構

  1. 請以需要的引數呼叫 Mesh 建構函式。

  2. 設定索引緩衝區、頂點緩衝區及屬性 (Attribute) 資料表資料。在此狀況下的資料通常都是在執行階段產生。下列範例顯示以此方式建立網狀結構的步驟。

範例

下列程式碼範例會建立位於 X-Y 平面,並以 Z 座標表示垂直維度 (Dimension) 的高度場網狀結構。建立的特定網狀結構從 (0, 0) 延伸至 (1, 1),並具有 GetHeight 方法所指定的高度。這個網狀結構會以單一的紋理覆蓋整個網狀結構。範例所定義的 tessellation 參數,用於控制沿著網狀結構邊緣使用的點數。

Class Form1

    Private Sub New()
        MyBase.New()
        ' In this example, initialize the mesh with
        ' 4 tessellations
        Me.InitializeMesh(4)
    End Sub

    Private Sub InitializeMesh(ByVal tessellation As Integer)
        Dim mesh1 As Mesh = CreateHeightfieldMesh(tessellation)
    End Sub


    Private Function GetHeight(ByVal x As Single, ByVal y As Single) As Single
        Return 0

    End Function

    Private Function CreateHeightfieldMesh(ByVal tessellation As Integer) As Mesh
        Dim mesh As Mesh

        Dim device As Device = Nothing
        Dim arrayIndices((tessellation - 1) * (tessellation - 1) * 6) As Short
        Dim arrayVertices(tessellation * tessellation) As CustomVertex.PositionTextured
        Dim attributeRange As New AttributeRange()

        ' Create mesh with desired vertex format and desired size.
        mesh = New Mesh(arrayIndices.Length / 3, arrayVertices.Length, MeshFlags.SystemMemory, CustomVertex.PositionTextured.Format, device)

        ' For each point in the height field calculate the x, y, z and
        ' texture coordinates.
        Dim y As Integer
        For y = 0 To tessellation
            Dim x As Integer
            For x = 0 To tessellation
                Dim arrayIndex As Integer = y * tessellation + x
                Dim xCoordinate As Single = System.Convert.ToSingle(x) / System.Convert.ToSingle(tessellation - 1)
                Dim yCoordinate As Single = System.Convert.ToSingle(y) / System.Convert.ToSingle(tessellation - 1)
                Dim vertex As New CustomVertex.PositionTextured(xCoordinate, yCoordinate, GetHeight(xCoordinate, yCoordinate), xCoordinate, yCoordinate)
                arrayVertices(arrayIndex) = vertex
            Next x
        Next y

        ' Calculate the index buffer.
        Dim z As Integer
        For z = 0 To (tessellation - 1)
            Dim x As Integer
            For x = 0 To (tessellation - 1)
                Dim arrayIndex As Integer = (z * (tessellation - 1) + x) * 6
                Dim vertexIndex As Integer = z * tessellation + x

                arrayIndices(arrayIndex) = Fix(vertexIndex)
                arrayIndices((arrayIndex + 1)) = Fix(vertexIndex + 1)
                arrayIndices((arrayIndex + 2)) = Fix(vertexIndex + tessellation)
                arrayIndices((arrayIndex + 3)) = Fix(vertexIndex + tessellation)
                arrayIndices((arrayIndex + 4)) = Fix(vertexIndex + 1)
                arrayIndices((arrayIndex + 5)) = Fix(vertexIndex + tessellation + 1)
            Next x
        Next z

        ' There is only one attribute value for this mesh.
        ' By specifying an attribute range the DrawSubset function
        ' does not have to scan the entire mesh for all faces that are
        ' are marked with a particular attribute ID.
        attributeRange.AttributeId = 0
        attributeRange.FaceStart = 0
        attributeRange.FaceCount = arrayIndices.Length / 3
        attributeRange.VertexStart = 0
        attributeRange.VertexCount = arrayVertices.Length

        mesh.VertexBuffer.SetData(arrayVertices, 0, LockFlags.None)
        mesh.IndexBuffer.SetData(arrayIndices, 0, LockFlags.None)
        mesh.SetAttributeTable(New AttributeRange() {attributeRange})

        Return mesh

    End Function

    Public Shared Sub Main()
        Try
            Dim Form1 As New Form()
            Application.Run(Form1)
        Catch e As NotSupportedException

            MsgBox("Your device does not have the " + _
                "needed 3d support to run this sample")

        Catch e As DriverUnsupportedException

            MsgBox("Your device does not have the " + _
                "needed 3d driver support to run this sample")

        Catch e As Exception

            MsgBox("The sample has run into an error and " + _
                "needs to close: " + e.Message)
        End Try

    End Sub
End Class
class Form1
{
    Form1()
    {
        // In this example, initialize the Mesh object
        // with 4 tessellations
        this.InitializeMesh(4);
    }

    private void InitializeMesh(int tessellation)
    {
        Mesh mesh1 = CreateHeightfieldMesh(tessellation);

    }
    private float GetHeight(float x, float y)
    {
        return 0;
        //TODO: fill in this function
    }

    private Mesh CreateHeightfieldMesh(int tessellation)
    {
        Mesh mesh;

        Device device = null; // TODO: initialize this

        short[] arrayIndices = new short[(tessellation - 1) * (tessellation - 1) * 6];
        CustomVertex.PositionTextured[] arrayVertices =
                new CustomVertex.PositionTextured[tessellation * tessellation];
        AttributeRange attributeRange = new AttributeRange();

        // Create mesh with desired vertex format and desired size
        mesh = new Mesh(arrayIndices.Length / 3, arrayVertices.Length, MeshFlags.SystemMemory,
        CustomVertex.PositionTextured.Format, device);

        // For each point in the height field calculate the x, y, z and
        // texture coordinates.
        for (int y = 0; y < tessellation; y++)
        {
            for (int x = 0; x < tessellation; x++)
            {
                int arrayIndex = y * tessellation + x;
                float xCoordinate = (float)x / (float)(tessellation - 1);
                float yCoordinate = (float)y / (float)(tessellation - 1);
                CustomVertex.PositionTextured vertex = new CustomVertex.PositionTextured
                        (xCoordinate, yCoordinate, GetHeight(xCoordinate, yCoordinate), xCoordinate, yCoordinate);
                arrayVertices[arrayIndex] = vertex;
            }
        }

        // Calculate the index buffer.
        for (int y = 0; y < (tessellation - 1); y++)
        {
            for (int x = 0; x < (tessellation - 1); x++)
            {
                int arrayIndex = (y * (tessellation - 1) + x) * 6;
                int vertexIndex = y * tessellation + x;

                arrayIndices[arrayIndex] = (short)vertexIndex;
                arrayIndices[arrayIndex + 1] = (short)(vertexIndex + 1);
                arrayIndices[arrayIndex + 2] = (short)(vertexIndex + tessellation);
                arrayIndices[arrayIndex + 3] = (short)(vertexIndex + tessellation);
                arrayIndices[arrayIndex + 4] = (short)(vertexIndex + 1);
                arrayIndices[arrayIndex + 5] = (short)(vertexIndex + tessellation + 1);
            }
        }

        // There is only one attribute value for this mesh.
        // By specifying an attribute range the DrawSubset function
        // does not have to scan the entire mesh for all faces that are
        // are marked with a particular attribute id.
        attributeRange.AttributeId = 0;
        attributeRange.FaceStart = 0;
        attributeRange.FaceCount = arrayIndices.Length / 3;
        attributeRange.VertexStart = 0;
        attributeRange.VertexCount = arrayVertices.Length;

        mesh.VertexBuffer.SetData(arrayVertices, 0, LockFlags.None);
        mesh.IndexBuffer.SetData(arrayIndices, 0, LockFlags.None);
        mesh.SetAttributeTable(new AttributeRange[] { attributeRange });

        return (mesh);

    }
    public static void Main()
    {
        try
        {
            Form Form1 = new Form();
            Application.Run(Form1);
        }
        catch (NotSupportedException)
        {
            MessageBox.Show("Your device does not have the needed 3d " +
                "support to run this sample");
        }
        catch (DriverUnsupportedException)
        {
            MessageBox.Show("Your device does not have the needed 3d " +
                "driver support to run this sample");
        }
        catch (Exception e)
        {
            MessageBox.Show("The sample has run into an error and " +
                "needs to close: " + e.Message);
        }
    }
}

編譯程式碼

這個範例需要下列命名空間的參考:

請參閱

概念

.NET Compact Framework HOW TO 主題

其他資源

.NET Compact Framework 中的 Mobile Direct3D 程式設計