Share via


SO WIRD'S GEMACHT: Anzeigen und Auswählen von Gitter

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 erstellen und anzeigen ein Array von Netzen bereit, sodass beim ein Netz ausgewählt wird (tippen), auf dem Gerät, Farbe geändert.

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.

Das Formular für dieses Codebeispiel enthält die folgenden Objekte:

  • Ein Mesh-Objekt für das aktive Netz.

  • Ein Array von neun Mesh Objekte der verschiedenen Farben, die kommissioniert werden können.

  • Eine Vector3-Struktur, die Speicherorte Mesh definiert.

  • Zwei Vector3-Strukturen, die das umgebende Feld definieren.

  • Device-Objekt.

Konstruktor des Formulars gibt Einstellungen für das Gerät PresentationParameters -Eigenschaft, erstellt das Objekt Device , Geräteereignis OnDeviceResetDeviceReset Ereignishandler hinzugefügt, und ruft anschließend die OnDeviceReset-Methode auf, um das Gitter zu erstellen. Die folgende Tabelle beschreibt die Methoden, die die Netze dargestellt und Benutzerinteraktionen ermöglichen.

Methode

Aktionen

OnDeviceReset

Erstellt die Netze, fügt Sie an Positionen im umgebenden Feld und definiert die Transformationsmatrizen.

OnPaint

Beginnt die Szene, zeichnet die Netze und beendet die Szene.

OnMouseDown

Veranschaulicht ein Netz mithilfe einer Technik, die ein Strahl über logischen 3D-Raum erstellt und führt eine Feld-Ray Schnittmenge Kommissionierung.

Der Strahl stellt dar, den Stift über den 3D-Raum drücken. Das Feld stellt ein Rahmenfeld um das 3D-Objekt dar. Wenn die beiden schneiden, hat der Benutzer an einem Speicherort geklickt, die das 3D-Objekt enthält.

Beispiel

Das folgende Codebeispiel stellt ein vollständiges Formular bereit. Es zeigt Mesh Objekte verschiedenen Farben, die ausgewählt werden können. Wenn ein Netz ausgewählt wird, ändert sich seine Farbe.

                        Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.WindowsMobile.DirectX
Imports Microsoft.WindowsMobile.DirectX.Direct3D
Imports Microsoft.VisualBasic




Class MeshPickingHowto
    Inherits Form
    PrivateConst numberOfMeshes AsInteger = 9
    Private meshes() As Mesh
    Private meshColors() As Color = {Color.Green, Color.Orange, Color.Purple, Color.Pink, Color.Violet, Color.Blue, Color.Yellow, Color.Brown, Color.Aquamarine}

    Private meshLocations() As Vector3
    Private meshBoundingBoxMinValues() As Vector3
    Private meshBoundingBoxMaxValues() As Vector3
    Private activeMesh As Mesh

    Private device As Device


    PublicSubNew() 
        Dim present As PresentParameters

        Me.Text = "Mesh Picking"
        ' Enable the form to be closed.        ' This is required so that Hwnd of the form changes.Me.MinimizeBox = False

        present = New PresentParameters()
        present.Windowed = True
        present.AutoDepthStencilFormat = DepthFormat.D16
        present.EnableAutoDepthStencil = True
        present.SwapEffect = SwapEffect.Discard

        device = New Device(0, DeviceType.Default, Me, CreateFlags.None, present)
        AddHandler device.DeviceReset, AddressOf OnDeviceReset
        OnDeviceReset(Nothing, EventArgs.Empty)

    EndSubPrivateSub OnDeviceReset(ByVal sender AsObject, ByVal e As EventArgs) 
        ' Meshes must be recreated whenever the device        ' is reset, no matter which pool they are created in.
        meshes = New Mesh(numberOfMeshes) {}
        meshLocations = New Vector3(numberOfMeshes) {}
        meshBoundingBoxMinValues = New Vector3(numberOfMeshes) {}
        meshBoundingBoxMaxValues = New Vector3(numberOfMeshes) {}
        activeMesh = Nothing
        ' Create several meshes and associated data.Dim i AsIntegerFor i = 0 To numberOfMeshes
            Dim vertexData As GraphicsStream

            meshes(i) = Mesh.Box(device, 1F, 1F, 1F)

            ' Arrange the boxes in a grid, with each            ' successive box farther in the distance.
            meshLocations(i) = New Vector3((i Mod 3) * 2 - 2, i / 3 * 2 - 2, i)

            ' Compute the bounding box for a mesh.Dim description As VertexBufferDescription = meshes(i).VertexBuffer.Description
            vertexData = meshes(i).VertexBuffer.Lock(0, 0, LockFlags.ReadOnly)
            Geometry.ComputeBoundingBox(vertexData, meshes(i).NumberVertices, description.VertexFormat, meshBoundingBoxMinValues(i), meshBoundingBoxMaxValues(i))
            meshes(i).VertexBuffer.Unlock()
        Next i
        ' Set the transformation matrices.
        device.Transform.Projection = Matrix.PerspectiveFovRH(System.Convert.ToSingle(Math.PI) / 4F, System.Convert.ToSingle(Me.ClientSize.Width) / System.Convert.ToSingle(Me.ClientSize.Height), 0.001F, 40)

        device.Transform.View = Matrix.LookAtRH(New Vector3(0, 2, - 7), New Vector3(0, 0, 0), New Vector3(0, 1, 0))

        device.RenderState.Ambient = Color.White

    EndSubProtectedOverridesSub OnPaintBackground(ByVal e As PaintEventArgs) 
        ' Do nothing.EndSubProtectedOverridesSub OnPaint(ByVal e As PaintEventArgs) 
        Dim material AsNew Material()

        ' Begin the scene and clear the back buffer to black.
        device.BeginScene()
        device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.Black, 1F, 0)

        ' Draw each mesh to the screen.        ' The active mesh is drawn in red.Dim i AsIntegerFor i = 0 To numberOfMeshes
            If activeMesh Is meshes(i) Then
                material.Ambient = Color.Red
            Else
                material.Ambient = meshColors(i)
            EndIf
            device.Transform.World = Matrix.Translation(meshLocations(i))
            device.Material = material
            meshes(i).DrawSubset(0)
        Next i

        ' Finish the scene and present it on the screen.
        device.EndScene()
        device.Present()

    EndSub

    ' This method demonstrates picking.ProtectedOverridesSub OnMouseDown(ByVal e As MouseEventArgs) 
        ' The technique used here is to create a ray through the entire        ' logical 3-D space, and then perform an intersection test        ' for the bounding box and ray.Dim i AsIntegerFor i = 0 To numberOfMeshes
            Dim nearVector AsNew Vector3(e.X, e.Y, 0)
            Dim farVector AsNew Vector3(e.X, e.Y, 1)

            ' Create ray.
            nearVector.Unproject(device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Translation(meshLocations(i)))

            farVector.Unproject(device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Translation(meshLocations(i)))

            farVector.Subtract(nearVector)

            ' Perform intersection test for the bounding box and ray.If Geometry.BoxBoundProbe(meshBoundingBoxMinValues(i), meshBoundingBoxMaxValues(i), nearVector, farVector) Then                ' Perform operation on detection of click on mesh object.                ' In this case, you designate the mesh as the active                ' mesh and invalidate the window so that it is redrawn.
                activeMesh = meshes(i)
                Me.Invalidate()
                ExitForEndIfNext i

    EndSubSharedSub Main() 
        Application.Run(New MeshPickingHowto())

    EndSubEndClass
                        using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.WindowsMobile.DirectX;
using Microsoft.WindowsMobile.DirectX.Direct3D;

namespace MeshPick
{
    class MeshPickingHowto : Form
    {
        constint numberOfMeshes = 9;
        Mesh [] meshes;
        Color [] meshColors = new Color [] { Color.Green, Color.Orange,
            Color.Purple, Color.Pink, Color.Violet, Color.Blue, Color.Yellow,
            Color.Brown, Color.Aquamarine };

        Vector3 [] meshLocations;
        Vector3 [] meshBoundingBoxMinValues;
        Vector3 [] meshBoundingBoxMaxValues;
        Mesh activeMesh;

        Device device;

        public MeshPickingHowto()
        {
            PresentParameters present;

            this.Text = "Mesh Picking";

            // Enable the form to be closed.// This is required so that Hwnd of the form changes.this.MinimizeBox = false;

            present = new PresentParameters();
            present.Windowed = true;
            present.AutoDepthStencilFormat = DepthFormat.D16;
            present.EnableAutoDepthStencil = true;
            present.SwapEffect = SwapEffect.Discard;

            device = new Device(0, DeviceType.Default, this,
                                CreateFlags.None, present);
            device.DeviceReset += new EventHandler(OnDeviceReset);
            OnDeviceReset(null, EventArgs.Empty);
        }

        privatevoid OnDeviceReset(object sender, EventArgs e)
        {
            // Meshes must be recreated whenever the device// is reset, no matter which pool they are created in.

            meshes = new Mesh[numberOfMeshes];
            meshLocations = new Vector3[numberOfMeshes];
            meshBoundingBoxMinValues = new Vector3[numberOfMeshes];
            meshBoundingBoxMaxValues = new Vector3[numberOfMeshes];
            activeMesh = null;
            // Create several meshes and associated data.for (int i = 0; i < numberOfMeshes; i++)
            {
                GraphicsStream vertexData;

                meshes[i] = Mesh.Box(device, 1.0f, 1.0f, 1.0f);

                // Arrange the boxes in a grid, with each// successive box farther in the distance.
                meshLocations[i] = new Vector3(((i % 3) * 2) - 2,
                                            ((i / 3) * 2) - 2, i);

                // Compute the bounding box for a mesh.
                VertexBufferDescription description =
                    meshes[i].VertexBuffer.Description;
                vertexData = meshes[i].VertexBuffer.Lock
                                         (0, 0, LockFlags.ReadOnly);
                Geometry.ComputeBoundingBox(vertexData,
                    meshes[i].NumberVertices,description.VertexFormat,
                    out meshBoundingBoxMinValues[i],
                    out meshBoundingBoxMaxValues[i]);
                meshes[i].VertexBuffer.Unlock();
            }
            // Set the transformation matrices.
            device.Transform.Projection = Matrix.PerspectiveFovRH(
               (float)Math.PI/4.0F,
               (float)this.ClientSize.Width / (float)this.ClientSize.Height,
               0.001f, 40);

            device.Transform.View = Matrix.LookAtRH(new Vector3(0, 2, -7),
                new Vector3(0, 0, 0), new Vector3(0, 1, 0));

            device.RenderState.Ambient = Color.White;
        }

        protectedoverridevoid OnPaintBackground(PaintEventArgs e)
        {
            // Do nothing.
        }

        protectedoverridevoid OnPaint(PaintEventArgs e)
        {
            Material material = new Material();

            // Begin the scene and clear the back buffer to black.
            device.BeginScene();
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer,
                        Color.Black, 1.0f, 0);

            // Draw each mesh to the screen.// The active mesh is drawn in red.for (int i = 0; i < numberOfMeshes; i++)
            {
                if (activeMesh == meshes[i])
                    material.Ambient = Color.Red;
                else
                    material.Ambient = meshColors[i];

                device.Transform.World = Matrix.Translation(meshLocations[i]);
                device.Material = material;
                meshes[i].DrawSubset(0);
            }

            // Finish the scene and present it on the screen.
            device.EndScene();
            device.Present();
        }

        // This method demonstrates picking.protectedoverridevoid OnMouseDown(MouseEventArgs e)
        {
            // The technique used here is to create a ray through the entire// logical 3-D space, and then perform an intersection test// for the bounding box and ray.for (int i = 0; i < numberOfMeshes; i++)
            {
                Vector3 nearVector = new Vector3(e.X, e.Y, 0);
                Vector3 farVector = new Vector3(e.X, e.Y, 1);

                // Create ray.

                nearVector.Unproject(device.Viewport,
                            device.Transform.Projection,
                            device.Transform.View,
                            Matrix.Translation(meshLocations[i]));

                farVector.Unproject(device.Viewport,
                            device.Transform.Projection,
                            device.Transform.View,
                            Matrix.Translation(meshLocations[i]));

                farVector.Subtract(nearVector);

                // Perform intersection test for the bounding box and ray.if (Geometry.BoxBoundProbe(meshBoundingBoxMinValues[i],
                    meshBoundingBoxMaxValues[i], nearVector, farVector))
                {
                    // Perform operation on detection of click on mesh object.// In this case, you designate the mesh as the active// mesh and invalidate the window so that it is redrawn.

                    activeMesh = meshes[i];
                    this.Invalidate();
                    break;
                }
            }
        }

        staticvoid Main()
        {
            Application.Run(new MeshPickingHowto());
        }
    }
}

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