|
|

楼主 |
发表于 2009-4-6 00:53:00
|
显示全部楼层
Re: 初探 DirectX (vb2005&vb2008)_贴上材质的立方体
Option Explicit On
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Public Class Form1
Private device As Device
Private vb As VertexBuffer
Private angle As Single = 0
Private bmp As Bitmap
Private texture As Texture
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ppms As New PresentParameters
With ppms
.SwapEffect = SwapEffect.Copy
.Windowed = True
End With
device = New Device(Manager.Adapters.Default.Adapter, DeviceType.Hardware, _
Me, CreateFlags.HardwareVertexProcessing, ppms)
vb = New VertexBuffer(GetType(CustomVertex.PositionTextured), 36, device, _
Usage.Dynamic Or Usage.WriteOnly, VertexFormats.None, Pool.Default)
AddHandler vb.Created, AddressOf VertBufferCreated
bmp = DirectCast(Image.FromFile("1.jpg").Clone, Bitmap)
With Me
.SetStyle(ControlStyles.Opaque, True)
.MinimumSize = SystemInformation.MinimumWindowSize + New Size(0, 1)
.Text = "DirectXTest"
.VertBufferCreated(Nothing, EventArgs.Empty)
End With
End Sub
Private Sub VertBufferCreated(ByVal sender As Object, ByVal e As EventArgs)
Dim vts(35) As CustomVertex.PositionTextured
'前
With vts(0)
.Position = New Vector3(1, 1, 1)
.Tu = 1
.Tv = 0
End With
With vts(1)
.Position = New Vector3(-1, -1, 1)
.Tu = 0
.Tv = 1
End With
With vts(2)
.Position = New Vector3(1, -1, 1)
.Tu = 1
.Tv = 1
End With
With vts(3)
.Position = New Vector3(-1, -1, 1)
.Tu = 0
.Tv = 1
End With
With vts(4)
.Position = New Vector3(1, 1, 1)
.Tu = 1
.Tv = 0
End With
With vts(5)
.Position = New Vector3(-1, 1, 1)
.Tu = 0
.Tv = 0
End With
'後
With vts(6)
.Position = New Vector3(1, 1, -1)
.Tu = 1
.Tv = 0
End With
With vts(7)
.Position = New Vector3(-1, -1, -1)
.Tu = 0
.Tv = 1
End With
With vts(8)
.Position = New Vector3(-1, 1, -1)
.Tu = 0
.Tv = 0
End With
With vts(9)
.Position = New Vector3(-1, -1, -1)
.Tu = 0
.Tv = 1
End With
With vts(10)
.Position = New Vector3(1, 1, -1)
.Tu = 1
.Tv = 0
End With
With vts(11)
.Position = New Vector3(1, -1, -1)
.Tu = 1
.Tv = 1
End With
'右
With vts(12)
.Position = New Vector3(1, -1, -1)
.Tu = 0
.Tv = 1
End With
With vts(13)
.Position = New Vector3(1, 1, 1)
.Tu = 1
.Tv = 0
End With
With vts(14)
.Position = New Vector3(1, -1, 1)
.Tu = 1
.Tv = 1
End With
With vts(15)
.Position = New Vector3(1, 1, 1)
.Tu = 1
.Tv = 0
End With
With vts(16)
.Position = New Vector3(1, -1, -1)
.Tu = 0
.Tv = 1
End With
With vts(17)
.Position = New Vector3(1, 1, -1)
.Tu = 0
.Tv = 0
End With
'左
With vts(18)
.Position = New Vector3(-1, 1, 1)
.Tu = 1
.Tv = 0
End With
With vts(19)
.Position = New Vector3(-1, -1, -1)
.Tu = 0
.Tv = 1
End With
With vts(20)
.Position = New Vector3(-1, -1, 1)
.Tu = 1
.Tv = 1
End With
With vts(21)
.Position = New Vector3(-1, -1, -1)
.Tu = 0
.Tv = 1
End With
With vts(22)
.Position = New Vector3(-1, 1, 1)
.Tu = 1
.Tv = 0
End With
With vts(23)
.Position = New Vector3(-1, 1, -1)
.Tu = 0
.Tv = 0
End With
'上
With vts(24)
.Position = New Vector3(1, 1, 1)
.Tu = 1
.Tv = 0
End With
With vts(25)
.Position = New Vector3(1, 1, -1)
.Tu = 1
.Tv = 1
End With
With vts(26)
.Position = New Vector3(-1, 1, -1)
.Tu = 0
.Tv = 1
End With
With vts(27)
.Position = New Vector3(1, 1, 1)
.Tu = 1
.Tv = 0
End With
With vts(28)
.Position = New Vector3(-1, 1, -1)
.Tu = 0
.Tv = 1
End With
With vts(29)
.Position = New Vector3(-1, 1, 1)
.Tu = 0
.Tv = 0
End With
'下
With vts(30)
.Position = New Vector3(1, -1, 1)
.Tu = 1
.Tv = 0
End With
With vts(31)
.Position = New Vector3(-1, -1, 1)
.Tu = 0
.Tv = 0
End With
With vts(32)
.Position = New Vector3(-1, -1, -1)
.Tu = 0
.Tv = 1
End With
With vts(33)
.Position = New Vector3(1, -1, 1)
.Tu = 1
.Tv = 0
End With
With vts(34)
.Position = New Vector3(-1, -1, -1)
.Tu = 0
.Tv = 1
End With
With vts(35)
.Position = New Vector3(1, -1, -1)
.Tu = 1
.Tv = 1
End With
vb.SetData(vts, 0, LockFlags.Discard)
texture = New Texture(device, bmp, Usage.Dynamic, Pool.Default)
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Me.Camera()
With device
.Clear(ClearFlags.Target, Color.Aqua, 1, 0)
.VertexFormat = VertexFormats.Position Or VertexFormats.Texture1
.BeginScene()
.SetStreamSource(0, vb, 0)
.SetTexture(0, texture)
.DrawPrimitives(PrimitiveType.TriangleList, 0, 12)
.EndScene()
.Present()
End With
Me.Invalidate()
End Sub
Private Sub Camera()
angle += 3
If angle >= 360 Then angle = 0
With device
.Transform.Projection = Matrix.PerspectiveFovLH(Math.PI / 4, _
CSng(Me.ClientSize.Width / Me.ClientSize.Height), 1, 100)
.RenderState.Lighting = False
.Transform.World = Matrix.RotationAxis(New Vector3(1, 1, 0), CSng(angle * Math.PI / 180))
.Transform.View = Matrix.LookAtLH(New Vector3(0, 0, 10), New Vector3(0, 0, 0), _
New Vector3(0, 1, 0))
End With
End Sub
End Class |
|