Share via


SO WIRD'S GEMACHT: Erstellen eines Gitters

Dieser Dokumentation für die Vorschau nur ist und in späteren Versionen geändert. Leere Themen wurden als Platzhalter eingefügt.]

Sie können ein Netz vier grundlegende Arten erstellen:

  • Indem Sie die Mesh-Daten aus einer Datei laden.

  • Klonen oder einem vorhandenen Netz zu optimieren.

  • Mithilfe einer Funktion Form erstellen und Festlegen der Größe und die Anzahl der Dreiecke, die zum Erstellen der Form verwendet wird.

  • Mithilfe des Mesh-Konstruktors.

Hinweis

Verwaltete Direct3D mobile-Anwendungen erfordern Windows Mobile 5.0 Software für Pocket PCs und Smartphones.Finden Sie unter Externe Ressourcen für .NET Compact Framework für Informationen über Windows Mobile-Software und SDKs.

So erstellen Sie ein Netz aus einer Datei

  • Geladen Sie Mesh-Daten aus einer Datei, und füllen Sie ein Netz mit den Daten. .NET Compact Framework nicht direkt unterstützt ein Netz aus einer Datei laden, aber die Direct3D Mobile Meshes Sample definiert eine Klasse, ein Netz zu laden.

So erstellen Sie ein Netz aus einem vorhandenen Netz

  • Verwenden Sie die Optimize-Methode, um ein neues Netz mit optimierten Daten zu erstellen.

    -oder-

    Verwenden Sie die OptimizeInPlace-Methode, um das aktuelle Netz zu optimieren.

    Der Haupteinsatzzweck für Klonen ist das Netz von Gleitkommazahlen in Festkomma Format zu konvertieren. Der Haupteinsatzzweck für die Optimierung ist die Erstellung ein Gitters, die schneller zu zeichnen. Mesh Optimierung ordnet Dreiecke im Mesh, sodass Zeichenbefehle auf das Gitter schneller ausführen. Mesh Optimierung erzeugt ebenfalls eine Attributtabelle, die zum Identifizieren der Bereiche des Gitters, die mit unterschiedlichen Texturen gezeichnet werden, Status und Materialien müssen verwendet wird.

Um ein Netz mithilfe einer Shape-Erstellung-Funktion zu erstellen

  • Verwenden Sie eine der folgenden statischen Methoden der Klasse Mesh, um ein Gitter mit Position und in Gleitkommaoperationen angegebenen Normals zu erstellen:

Um ein Netz mithilfe des Konstruktors Mesh zu erstellen

  1. Rufen Sie den Mesh-Konstruktor mit den gewünschten Argumenten.

  2. Legen Sie die Indexpuffer, den Vertexpuffer und die Tabellendaten Attribut. Die Daten werden in diesem Fall häufig zur Laufzeit generiert. Das folgende Beispiel zeigt die Schritte um ein Gitter auf diese Weise zu erstellen.

Beispiel

Das folgenden Codebeispiel wird ein Netz Heightfield auf der X-y-Ebene erstellt, mit der Z-Koordinate die vertikale Dimension darstellt. Bestimmte Mesh erstellt führt aus (0, 0), (1, 1) und eine Höhe von GetHeight-Methode angegeben ist. Dieses Mesh ist mit einer einzelnen Struktur auch über das gesamte Netz strukturierte. Der im Beispiel definierte Parameter tessellation wird verwendet, um steuern, wie viele Punkte entlang des Randes des Gitters verwendet werden.

                        Class Form1

    PrivateSubNew()
        MyBase.New()
        ' In this example, initialize the mesh with        ' 4 tessellationsMe.InitializeMesh(4)
    EndSubPrivateSub InitializeMesh(ByVal tessellation AsInteger)
        Dim mesh1 As Mesh = CreateHeightfieldMesh(tessellation)
    EndSubPrivateFunction GetHeight(ByVal x AsSingle, ByVal y AsSingle) AsSingleReturn 0

    EndFunctionPrivateFunction CreateHeightfieldMesh(ByVal tessellation AsInteger) As Mesh
        Dim mesh As Mesh

        Dim device As Device = NothingDim arrayIndices((tessellation - 1) * (tessellation - 1) * 6) AsShortDim arrayVertices(tessellation * tessellation) As CustomVertex.PositionTextured
        Dim attributeRange AsNew 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 AsIntegerFor y = 0 To tessellation
            Dim x AsIntegerFor x = 0 To tessellation
                Dim arrayIndex AsInteger = y * tessellation + x
                Dim xCoordinate AsSingle = System.Convert.ToSingle(x) / System.Convert.ToSingle(tessellation - 1)
                Dim yCoordinate AsSingle = System.Convert.ToSingle(y) / System.Convert.ToSingle(tessellation - 1)
                Dim vertex AsNew CustomVertex.PositionTextured(xCoordinate, yCoordinate, GetHeight(xCoordinate, yCoordinate), xCoordinate, yCoordinate)
                arrayVertices(arrayIndex) = vertex
            Next x
        Next y

        ' Calculate the index buffer.Dim z AsIntegerFor z = 0 To (tessellation - 1)
            Dim x AsIntegerFor x = 0 To (tessellation - 1)
                Dim arrayIndex AsInteger = (z * (tessellation - 1) + x) * 6
                Dim vertexIndex AsInteger = 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

    EndFunctionPublicSharedSub Main()
        TryDim Form1 AsNew 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)
        EndTryEndSubEndClass
                        class Form1
{
    Form1()
    {
        // In this example, initialize the Mesh object// with 4 tessellationsthis.InitializeMesh(4);
    }

    privatevoid InitializeMesh(int tessellation)
    {
        Mesh mesh1 = CreateHeightfieldMesh(tessellation);

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

    private Mesh CreateHeightfieldMesh(int tessellation)
    {
        Mesh mesh;

        Device device = null; // TODO: initialize thisshort[] arrayIndices = newshort[(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);

    }
    publicstaticvoid 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);
        }
    }
}

Kompilieren des Codes

In diesem Beispiel sind Verweise auf die folgenden Namespaces erforderlich:

Siehe auch

Konzepte

.NET compact Framework Gewusst-wie-Themen

Weitere Ressourcen

Mobile Direct3D-Programmierung in .NET Compact Framework