Export (0) Print
Expand All

How to: Transform Direct3D Objects

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

You can move Direct3D objects in a scene by using world transformations.

Note Note

Managed Direct3D mobile applications require Windows Mobile version 5.0 software for Pocket PCs and Smartphones. See External Resources for the .NET Compact Framework for information about Windows Mobile software and SDKs.

World space is similar to 3-dimensional Cartesian space that extends infinitely in all directions. World transformations define where Direct3D objects are positioned in world space, before the view transformation translates the objects from world space to screen space. By using world transformations, you can translate (move), rotate, and scale Direct3D objects.

To combine multiple world transformations on an object, you must multiply their transformation matrices. In the following example, a rotation matrix and a translation matrix are multiplied by using the Multiply method. You must transform matrices in the correct order to obtain the result that you want. For example, to multiply a rotation matrix and a translation matrix, you must transform rotations before translations, as demonstrated in the following example.

The following example animates a primitive box mesh that represents a ship. The example provides a complete form that includes the following objects:

  • A primitive Mesh object that represents a ship.

  • A set of primitive Mesh objects that represent buildings.

  • Several Light objects to provide light. One of these objects represents light from the ship engines.

  • A Device object.


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



Class MatrixTransformsHowTo
    Inherits Form
    ' Mesh representing the ship.
    Private shipMesh As Mesh = Nothing
    ' Meshes representing buildings.
    Private wallMeshes(2) As Mesh

    Private meshColor As Color = Color.Goldenrod

    ' Description of the Direct3D light.
    Private lightData As Light

    Private device As Device


    Private Enum ShipStatus
        SS_LIFTOFF = 1
        SS_TURNING = 2
        SS_ENGINEON = 3
    End Enum
    Private myShipStatus As ShipStatus = ShipStatus.SS_LIFTOFF

    Private firstTick As Integer = 0
    Private newTick As Integer = 0

    Private yVal As Single = - 2F
    Private zVal As Single = 2F
    Private Const midAltitude As Integer = 85

    Private yCameraPosition As Single = - 1
    Private xCameraPosition As Single = - 2

    Private startAngle As Single = 0F
    Private angle As Single = 0.1F
    Private lastIncrement As Single = 0F

    Private isEngineFired As Boolean = False
    Private isShipDeparted As Boolean = False
    Private isNewRotationOperation As Boolean = True


    Public Sub New() 

        Dim present As PresentParameters

        Me.Text = "Flying Ship"

        ' Enable the form to be closed.
        ' 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

        Dim i As Integer
        For i = 0 To wallMeshes.Length
            wallMeshes(i) = Nothing
        Next i

        OnDeviceReset(Nothing, EventArgs.Empty)

    End Sub


    Private Sub OnDeviceReset(ByVal sender As Object, ByVal e As EventArgs) 
        ' Meshes must be recreated whenever the device
        ' is reset, no matter which pool they are created in.
        ' Instead of loading a mesh from a file,
        ' this sample uses primitive box meshes
        ' to represent the ship and the buildings.
        shipMesh = Mesh.Box(device, 0.8F, 0.18F, 2.2F)

        wallMeshes(0) = Mesh.Box(device, 0.5F, 3.6F, 1F)
        wallMeshes(1) = Mesh.Box(device, 0.5F, 1.8F, 2F)
        wallMeshes(2) = Mesh.Box(device, 0.2F, 1F, 0.75F)

        device.RenderState.Ambient = Color.White

        ' Provides main directional lighting.
        device.Lights(0).Type = LightType.Directional
        device.Lights(0).Direction = New Vector3(0.3F, - 0.5F, 0.2F)
        device.Lights(0).Diffuse = Color.LightBlue
        device.Lights(0).Update()

        ' Provides frontal lighting.
        device.Lights(1).Type = LightType.Directional
        device.Lights(1).Direction = New Vector3(0F, - 1F, - 3F)
        device.Lights(1).Diffuse = Color.DarkSlateGray
        device.Lights(1).Update()

        ' Turn on the lights.
        device.Lights(0).Enabled = True
        device.Lights(1).Enabled = True
        ' Turn off the light representing the engine.
        device.Lights(2).Enabled = False

        ' For the projection matrix, set up a perspective transform (which
        ' transforms geometry from 3-D view space to 2-D viewport space, with
        ' a perspective divide that makes objects smaller in the distance). To build
        ' a perspective transform, you need the field of view (1/4 PI is common),
        ' the aspect ratio, and the near and far clipping planes (which define at
        ' the distances at which geometry should be no longer be rendered).
        device.Transform.Projection = Matrix.PerspectiveFovLH(System.Convert.ToSingle(Math.PI) / 4F, System.Convert.ToSingle(Me.ClientSize.Width) / System.Convert.ToSingle(Me.ClientSize.Height), 1F, 80F)

    End Sub


    Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs) 
        ' Do nothing.
    End Sub


    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) 
        Dim material As New Material()
        Dim engineMaterial As New Material()

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

        device.BeginScene()

        material.Diffuse = Color.WhiteSmoke
        ' Specifies the ambient color for the engines.
        engineMaterial.Ambient = Color.White

        SetupMatrices()

        device.Material = material

        ' Draw ship on the screen.
        shipMesh.DrawSubset(0)
        SetupMovingLight()

        If isEngineFired Then
            device.Material = engineMaterial
            device.Lights(2).Enabled = True
            ' Bind the vertex buffers of the primitive
            ' mesh to the Device object.
            device.SetStreamSource(0, shipMesh.VertexBuffer, 0)
            ' Redraw the face of the ship representing the engine.
            ' A Box mesh has 4 vertices per face. The 20th vertex
            ' is the first vertex representing the engine. To use
            ' adjacent triangles, set the type to Primitive.TriangleFan.
            device.DrawPrimitives(PrimitiveType.TriangleFan, 20, 2)
        End If

        material.Diffuse = Color.GhostWhite
        device.Material = material

        ' Draw buildings, providing coordinates to locate each
        ' building on the x, y, and z planes. Because the camera is placed
        ' "behind" the scene initially (at a positive z-axis value in
        ' the call to Matrix.LookAtLH), positive z-axis values draw
        ' objects closer to the camera. In addition, positive x-axis
        ' values draw objects farther to the left instead of to the right.
        ' Draw the tall building.
        device.Transform.World = Matrix.Translation(0.75F, - 0.2F, - 2F)
        wallMeshes(0).DrawSubset(0)

        ' Draw the medium-sized buildings.
        device.Transform.World = Matrix.Translation(- 1F, - 0.9F, 0F)
        wallMeshes(1).DrawSubset(0)
        device.Transform.World = Matrix.Translation(0F, - 0.9F, 0F)
        wallMeshes(1).DrawSubset(0)
        device.Transform.World = Matrix.Translation(1F, - 0.9F, 0F)
        wallMeshes(1).DrawSubset(0)
        device.Transform.World = Matrix.Translation(2F, - 0.9F, 0F)
        wallMeshes(1).DrawSubset(0)

        ' Draw the small buildings.
        device.Transform.World = Matrix.Translation(- 2F, - 1.5F, 5F)
        wallMeshes(2).DrawSubset(0)
        device.Transform.World = Matrix.Translation(- 1.25F, - 1.5F, 5F)
        wallMeshes(2).DrawSubset(0)
        device.Transform.World = Matrix.Translation(- 0.5F, - 1.5F, 5F)
        wallMeshes(2).DrawSubset(0)
        device.Transform.World = Matrix.Translation(0.75F, - 1.5F, 5F)
        wallMeshes(2).DrawSubset(0)

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

        ' Repaint the scene.
        Me.Invalidate()

    End Sub

    Private Sub SetupMatrices() 
        ' Set the transformation matrices.
        Dim fAngle As Single = angle

        ' To render the ship, combine a rotation on the y-axis with a
        ' translation (move) using the Matrix.Multiply method.
        device.Transform.World = Matrix.Multiply(Matrix.RotationY(fAngle + startAngle), Matrix.Translation(- 0.5F, yVal, zVal))

        ' Set up the view matrix. You can define a view matrix with a camera position,
        ' a point to look at (camera target), and an "up" direction.
        ' First vector passed to LookAtLH is the camera position.
        ' Second vector passed to LookAtLH is the camera target.
        ' Third vector passed to LookAtLH defines the "up" direction.
        ' In this example, you set the camera seven units up along the z-axis ("behind"
        ' the scene), down one unit, and left two units. You then point the camera
        ' just above the origin and define "up" to be in the y-direction.
        If Not isShipDeparted Then
            device.Transform.View = Matrix.LookAtLH(New Vector3(- 2, - 1, 7), New Vector3(0, 1, 0), New Vector3(0, 1, 0))
        Else
            ' Handles movement of camera after 
            ' the ship "fires" the main engines.
            device.Transform.View = Matrix.LookAtLH(New Vector3(xCameraPosition, yCameraPosition, 7), New Vector3(0, 1, 0), New Vector3(0, 1, 0))
            xCameraPosition += 0.01F
            yCameraPosition += 0.01F
        End If
        ' Use the system time to control the animation.
        ' The high-resolution timer, if present for
        ' the hardware, could be used instead.
        Dim tick As Integer = System.Environment.TickCount
        If newTick = 0 Then
            firstTick = tick / 100
        End If
        newTick = tick / 100 - firstTick + 1

        ' Use the tick count to change the current
        ' ship status. Animation is then
        ' dependent on the current status.
        If newTick <= 10 Then
            myShipStatus = ShipStatus.SS_LIFTOFF
        ElseIf newTick <= midAltitude Then
            myShipStatus = ShipStatus.SS_TURNING
        Else
            myShipStatus = ShipStatus.SS_ENGINEON
        End If 
        Select Case myShipStatus
            Case ShipStatus.SS_LIFTOFF
                yVal += 0.015F
            Case ShipStatus.SS_TURNING
                yVal += 0.015F
                angle = SetRotation(angle, 180F)
            Case ShipStatus.SS_ENGINEON
                isEngineFired = True
                zVal = zVal - 0.04F
                yVal = yVal + 0.005F
                angle = SetRotation(angle, 180F)
                If newTick > midAltitude + 30 Then
                    isShipDeparted = True
                End If
        End Select

    End Sub

    Private Function SetRotation(ByVal tempAngle As Single, ByVal rotationThreshold As Single) As Single 
        ' SetRotation manipulates rotation values to simulate a vessel that
        ' gradually increases in turning speed, and then slows to
        ' a stop. rotationThreshold should be <= 180 degrees.
        If isNewRotationOperation Then
            ' Reset values if this is a new rotation operation.
            ' Starting angle of ship must be added back in
            ' before the call to Matrix.RotationY.
            tempAngle = 0.1F
            isNewRotationOperation = False
        End If
        rotationThreshold = DegreesToRadians(rotationThreshold)

        If tempAngle < rotationThreshold Then
            Dim increment As Single = tempAngle
            ' Provide a gradual but increasing turning speed.
            tempAngle *= 1.015F
            lastIncrement = tempAngle - increment
            Return tempAngle
        Else
            ' Provide a gradual slowing to a stop.
            tempAngle += lastIncrement * 0.75F
            lastIncrement = lastIncrement * 0.75F
            Return tempAngle
        End If

    End Function

    Private Function DegreesToRadians(ByVal degrees As Single) As Single 
        Dim radians As Single = degrees *(3.141593F / 180F)
        Return radians

    End Function

    Private Sub SetupMovingLight() 
        device.Lights(2).Type = LightType.Point
        lightData = device.Lights(2)

        device.Lights(2).Diffuse = Color.White
        device.Lights(2).Range = 200F

        If Not device.DeviceCaps.VertexProcessingCaps.SupportsPositionalLights Then
            If device.LightsFixed(2).Type = LightType.Point Then
                device.LightsFixed(2).Type = LightType.Directional
            End If
        End If 
        ' Handle positioning for the light that emanates
        ' from the ship, representing the light from
        ' the main engines.
        Select Case device.Lights(2).Type
            Case LightType.Point
                device.Lights(2).Position = New Vector3(0, yVal, zVal)
                device.Lights(2).Attenuation1 = 0.2F
            Case LightType.Directional
        End Select ' Not implemented.
        device.Lights(2).Update()

    End Sub


    Shared Sub Main() 
        Try
            Dim d3dApp As New MatrixTransformsHowTo()
            System.Windows.Forms.Application.Run(d3dApp)
        Catch 
            MessageBox.Show("Your device does not have the needed 3-D " + "support to run this sample")
        Catch 
            MessageBox.Show("Your device does not have the needed 3-D " + "support to run this sample")
        Catch e As Exception
            MessageBox.Show("The sample has run into an error and needs" + "to close: " + e.Message)
        End Try

    End Sub
End Class


Show:
© 2015 Microsoft