⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blockform.vb

📁 Windows mobile 6 Direct3D 编程例子
💻 VB
字号:
Imports System.Drawing
Imports Microsoft.WindowsMobile.DirectX
Imports Microsoft.WindowsMobile.DirectX.Direct3D

Module ProgramMain
    Sub Main()
        Dim frm As BlockForm = New BlockForm

        ' Initialize Direct3D
        If Not frm.Init() Then
            MessageBox.Show("Could not initialize Direct3D")
            Return
        End If

        System.Windows.Forms.Application.Run(frm)
    End Sub
End Module

Public Class BlockForm

    Public Class ImageBackground
        ''' <summary>
        ''' Texture to be drawn with. Passed into the constructor.
        ''' </summary>
        Public BackgroundTexture As Texture

        ''' <summary>
        ''' This defines the shape of our background image and the texture of the vertices.
        ''' </summary>
        Public Vertices As CustomVertex.PositionNormalTextured()

        ''' <summary>
        ''' Vertex buffer which is used by the display hardware 
        ''' recieve details of what we want to draw.
        ''' </summary>
        Public VertBuffer As VertexBuffer = Nothing

        Public Sub New(ByVal device As Device, ByVal textureStream As System.IO.Stream, ByVal sz As Single, ByVal z As Single)
            BackgroundTexture = TextureLoader.FromStream(device, textureStream)
            Vertices = New CustomVertex.PositionNormalTextured(5) {}
            Vertices(0) = New CustomVertex.PositionNormalTextured(-sz, -sz, z, 0.0F, 0.0F, -1.0F, 0.0F, 1.0F)
            Vertices(1) = New CustomVertex.PositionNormalTextured(-sz, sz, z, 0.0F, 0.0F, -1.0F, 0.0F, 0.0F)
            Vertices(2) = New CustomVertex.PositionNormalTextured(sz, -sz, z, 0.0F, 0.0F, -1.0F, 1.0F, 1.0F)
            Vertices(3) = New CustomVertex.PositionNormalTextured(sz, -sz, z, 0.0F, 0.0F, -1.0F, 1.0F, 1.0F)
            Vertices(4) = New CustomVertex.PositionNormalTextured(-sz, sz, z, 0.0F, 0.0F, -1.0F, 0.0F, 0.0F)
            Vertices(5) = New CustomVertex.PositionNormalTextured(sz, sz, z, 0.0F, 0.0F, -1.0F, 1.0F, 0.0F)

            VertBuffer = New VertexBuffer(GetType(CustomVertex.PositionNormalTextured), Vertices.Length, device, Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.SystemMemory)
        End Sub

        ''' <summary>
        ''' Draw the textured image.
        ''' </summary>
        ''' <param name="device">target device to draw on</param>
        Public Sub Draw(ByVal device As Device)

            ' Set the data in the vertex buffer to our triangles
            VertBuffer.SetData(Vertices, 0, LockFlags.None)

            ' clear off any existing transformations
            device.Transform.World = Matrix.Identity

            ' Point the device at our vertex buffer
            device.SetStreamSource(0, VertBuffer, 0)

            ' Assign the texture to the device.
            device.SetTexture(0, BackgroundTexture)

            ' Ask the device to draw the contents of the buffer
            device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2)

            ' Stop using this texture
            device.SetTexture(0, Nothing)
        End Sub
    End Class

    Private iImageBackground As ImageBackground

    ' <summary>
    ' Currently executing assembly. Cached because it is used
    ' to load a lot of resources. 
    ' </summary>
    ReadOnly assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly

    ' <summary>
    ' Direct3D device we are going to draw on.
    ' </summary>
    Private device As Device

    Public Vert As CustomVertex.PositionNormalColored()

    Public VertBuffer As VertexBuffer = Nothing

    Public Sub MakeBlock()
        Vert = New CustomVertex.PositionNormalColored(35) {}

        ' Front face
        Dim iColor As Integer = Color.Red.ToArgb
        Vert(0) = New CustomVertex.PositionNormalColored(-0.5F, -0.5F, -0.5F, 0.0F, 0.0F, -1.0F, iColor)
        Vert(1) = New CustomVertex.PositionNormalColored(-0.5F, 0.5F, -0.5F, 0.0F, 0.0F, -1.0F, iColor)
        Vert(2) = New CustomVertex.PositionNormalColored(0.5F, -0.5F, -0.5F, 0.0F, 0.0F, -1.0F, iColor)
        iColor = Color.White.ToArgb()
        Vert(3) = New CustomVertex.PositionNormalColored(0.5F, -0.5F, -0.5F, 0.0F, 0.0F, -1.0F, iColor)
        Vert(4) = New CustomVertex.PositionNormalColored(-0.5F, 0.5F, -0.5F, 0.0F, 0.0F, -1.0F, iColor)
        Vert(5) = New CustomVertex.PositionNormalColored(0.5F, 0.5F, -0.5F, 0.0F, 0.0F, -1.0F, iColor)
        iColor = Color.Green.ToArgb

        ' Right hand face
        Vert(6) = New CustomVertex.PositionNormalColored(0.5F, -0.5F, -0.5F, 1.0F, 0.0F, 0.0F, iColor)
        Vert(7) = New CustomVertex.PositionNormalColored(0.5F, 0.5F, -0.5F, 1.0F, 0.0F, 0.0F, iColor)
        Vert(8) = New CustomVertex.PositionNormalColored(0.5F, -0.5F, 0.5F, 1.0F, 0.0F, 0.0F, iColor)
        iColor = Color.White.ToArgb()
        Vert(9) = New CustomVertex.PositionNormalColored(0.5F, 0.5F, -0.5F, 1.0F, 0.0F, 0.0F, iColor)
        Vert(10) = New CustomVertex.PositionNormalColored(0.5F, 0.5F, 0.5F, 1.0F, 0.0F, 0.0F, iColor)
        Vert(11) = New CustomVertex.PositionNormalColored(0.5F, -0.5F, 0.5F, 1.0F, 0.0F, 0.0F, iColor)
        iColor = Color.Blue.ToArgb

        ' Bottom face
        Vert(12) = New CustomVertex.PositionNormalColored(-0.5F, -0.5F, -0.5F, 0.0F, -1.0F, 0.0F, iColor)
        Vert(13) = New CustomVertex.PositionNormalColored(0.5F, -0.5F, -0.5F, 0.0F, -1.0F, 0.0F, iColor)
        Vert(14) = New CustomVertex.PositionNormalColored(0.5F, -0.5F, 0.5F, 0.0F, -1.0F, 0.0F, iColor)
        iColor = Color.White.ToArgb()
        Vert(15) = New CustomVertex.PositionNormalColored(-0.5F, -0.5F, -0.5F, 0.0F, -1.0F, 0.0F, iColor)
        Vert(16) = New CustomVertex.PositionNormalColored(0.5F, -0.5F, 0.5F, 0.0F, -1.0F, 0.0F, iColor)
        Vert(17) = New CustomVertex.PositionNormalColored(-0.5F, -0.5F, 0.5F, 0.0F, -1.0F, 0.0F, iColor)
        iColor = Color.Yellow.ToArgb

        ' Left hand face
        Vert(18) = New CustomVertex.PositionNormalColored(-0.5F, -0.5F, -0.5F, -1.0F, 0.0F, 0.0F, iColor)
        Vert(19) = New CustomVertex.PositionNormalColored(-0.5F, -0.5F, 0.5F, -1.0F, 0.0F, 0.0F, iColor)
        Vert(20) = New CustomVertex.PositionNormalColored(-0.5F, 0.5F, -0.5F, -1.0F, 0.0F, 0.0F, iColor)
        iColor = Color.White.ToArgb()
        Vert(21) = New CustomVertex.PositionNormalColored(-0.5F, 0.5F, -0.5F, -1.0F, 0.0F, 0.0F, iColor)
        Vert(22) = New CustomVertex.PositionNormalColored(-0.5F, -0.5F, 0.5F, -1.0F, 0.0F, 0.0F, iColor)
        Vert(23) = New CustomVertex.PositionNormalColored(-0.5F, 0.5F, 0.5F, -1.0F, 0.0F, 0.0F, iColor)
        iColor = Color.Orange.ToArgb

        ' Back face
        Vert(24) = New CustomVertex.PositionNormalColored(-0.5F, -0.5F, 0.5F, 0.0F, 0.0F, 1.0F, iColor)
        Vert(25) = New CustomVertex.PositionNormalColored(0.5F, -0.5F, 0.5F, 0.0F, 0.0F, 1.0F, iColor)
        Vert(26) = New CustomVertex.PositionNormalColored(-0.5F, 0.5F, 0.5F, 0.0F, 0.0F, 1.0F, iColor)
        iColor = Color.White.ToArgb()
        Vert(27) = New CustomVertex.PositionNormalColored(0.5F, -0.5F, 0.5F, 0.0F, 0.0F, 1.0F, iColor)
        Vert(28) = New CustomVertex.PositionNormalColored(0.5F, 0.5F, 0.5F, 0.0F, 0.0F, 1.0F, iColor)
        Vert(29) = New CustomVertex.PositionNormalColored(-0.5F, 0.5F, 0.5F, 0.0F, 0.0F, 1.0F, iColor)
        iColor = Color.Cyan.ToArgb

        ' Top face
        Vert(30) = New CustomVertex.PositionNormalColored(-0.5F, 0.5F, -0.5F, 0.0F, 1.0F, 0.0F, iColor)
        Vert(31) = New CustomVertex.PositionNormalColored(-0.5F, 0.5F, 0.5F, 0.0F, 1.0F, 0.0F, iColor)
        Vert(32) = New CustomVertex.PositionNormalColored(0.5F, 0.5F, 0.5F, 0.0F, 1.0F, 0.0F, iColor)
        iColor = Color.White.ToArgb()
        Vert(33) = New CustomVertex.PositionNormalColored(-0.5F, 0.5F, -0.5F, 0.0F, 1.0F, 0.0F, iColor)
        Vert(34) = New CustomVertex.PositionNormalColored(0.5F, 0.5F, 0.5F, 0.0F, 1.0F, 0.0F, iColor)
        Vert(35) = New CustomVertex.PositionNormalColored(0.5F, 0.5F, -0.5F, 0.0F, 1.0F, 0.0F, iColor)

        VertBuffer = New VertexBuffer( _
            GetType(CustomVertex.PositionNormalColored), _
            Vert.Length, _
            device, _
            Usage.WriteOnly, _
            CustomVertex.PositionNormalColored.Format, _
            Pool.SystemMemory)
    End Sub

    Private scale1 As Matrix
    Private scale2 As Matrix

    Private rot1 As Matrix
    Private rot2 As Matrix

    Private pos1 As Matrix
    Private pos2 As Matrix

    Public Function Init() As Boolean
        Try

            Dim presentParams As PresentParameters = New PresentParameters
            presentParams.Windowed = True
            presentParams.SwapEffect = SwapEffect.Discard
            presentParams.EnableAutoDepthStencil = True
            presentParams.AutoDepthStencilFormat = DepthFormat.D16

            device = New Device(0, DeviceType.Default, Me, CreateFlags.None, presentParams)

            device.RenderState.Lighting = True

            device.Lights(0).Diffuse = Color.Blue
            device.Lights(0).Type = LightType.Directional
            device.Lights(0).Direction = New Vector3(-0.5F, -0.5F, 0.5F)
            device.Lights(0).Update()
            device.Lights(0).Enabled = True

            device.Lights(1).Diffuse = Color.White
            device.Lights(1).Type = LightType.Directional
            device.Lights(1).Direction = New Vector3(0.5F, 0.5F, 0.5F)
            device.Lights(1).Update()
            device.Lights(1).Enabled = True

            device.RenderState.Ambient = Color.White

            device.Transform.View = Matrix.LookAtLH(New Vector3(0.0F, 0.0F, -3.0F), New Vector3(0.0F, 0.0F, 0.0F), New Vector3(0.0F, 1.0F, 0.0F))

            device.Transform.Projection = Matrix.PerspectiveFovLH(CType(Math.PI, Single) / 4, 1.0F, 1.0F, 100.0F)

            MakeBlock()

            iImageBackground = New ImageBackground(device, assembly.GetManifestResourceStream("Block.Background.png"), 3.0F, 0.2F)

            scale1 = Matrix.Scaling(0.6F, 0.6F, 0.6F)
            scale2 = Matrix.Scaling(0.6F, 0.6F, 0.6F)

            rot1 = Matrix.RotationYawPitchRoll(0.5F, 0.5F, 0.5F)
            rot2 = Matrix.RotationYawPitchRoll(-0.5F, -0.5F, -0.5F)

            pos1 = Matrix.Translation(-0.6F, 0.6F, 0)
            pos2 = Matrix.Translation(0.6F, -0.6F, 0)
        Catch ex As Exception
            Return False
        End Try

        Return True
    End Function

    ''' <summary>
    ''' The yaw value for our segment to animate the tumble
    ''' </summary>
    Public Yaw As Single

    ''' <summary>
    ''' Change value for the yaw. Set at random when the
    ''' segment is constructed.
    ''' </summary>
    Public YawSpeed As Single = 0.07F

    ''' <summary>
    ''' The pitch value for our segment to animate the tumble
    ''' </summary>
    Public Pitch As Single

    ''' <summary>
    ''' Change value for the pitch. Set at random when the
    ''' segment is constructed.
    ''' </summary>
    ''' <remarks></remarks>
    Public PitchSpeed As Single = 0.05F

    ''' <summary>
    ''' The roll value for our segment to animate the tumble
    ''' </summary>
    Public Roll As Single

    ''' <summary>
    ''' Change value for the roll. Set at random when the
    ''' segment is constructed.
    ''' </summary>
    ''' <remarks></remarks>
    Public RollSpeed As Single = 0.1F

    Private Sub Render()
        device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.LightGray, 1.0F, 0)

        device.BeginScene()

        'iImageBackground.Draw(device)

        Yaw += YawSpeed
        Pitch += PitchSpeed
        Roll += RollSpeed

        Dim rrot As Matrix = Matrix.RotationYawPitchRoll(Yaw, Pitch, Roll)

        ' Set the data in the vertex buffer to our triangles
        VertBuffer.SetData(Vert, 0, LockFlags.None)

        ' block 1
        device.Transform.World = scale1 * rrot * rot1 * pos1

        ' Point the device at our vertex buffer
        device.SetStreamSource(0, VertBuffer, 0)

        ' Ask the device to draw the contents of the buffer
        device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12)

        ' block 2
        device.Transform.World = scale2 * rrot * rot2 * pos2

        ' Ask the device to draw the contents of the buffer
        device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12)

        device.EndScene()
        device.Present()
    End Sub

    Private Sub updateTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updateTimer.Tick
        Me.Invalidate()
    End Sub

#Region "Event handlers"
    Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Render()
    End Sub
#End Region


    Private Sub exitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitMenuItem.Click
        Application.Exit()
    End Sub

End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -