'spinning cube directx 9c example Option Strict On Imports Microsoft.DirectX 'C:\Windows\assembly\GAC\Microsoft.DirectX\1.0.2902.0__31bf3856ad364e35\Microsoft.DirectX.dll Imports Microsoft.DirectX.Direct3D 'C:\Windows\assembly\GAC\Microsoft.DirectX.Direct3D\1.0.2902.0__31bf3856ad364e35\Microsoft.DirectX.Direct3D.dll Public Class Form1 Private device As Device = Nothing Private vb As VertexBuffer = Nothing Private tex1 As Texture = Nothing Private angle As Single = 0.0F Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True) Me.ClientSize = New Size(400, 400) InitializeGraphics() End Sub Public Sub InitializeGraphics() 'create the directx graphics device Dim presentParams As New PresentParameters() presentParams.Windowed = True presentParams.SwapEffect = SwapEffect.Discard device = New Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, presentParams) 'create the object data vb = New VertexBuffer(GetType(CustomVertex.PositionTextured), 36, device, Usage.Dynamic Or Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default) AddHandler vb.Created, AddressOf Me.OnVertexBufferCreate OnVertexBufferCreate(vb, Nothing) 'read the texture image - add the path to your own image tex1 = New Texture(device, New Bitmap("c:\bitmaps\rusty.bmp"), Usage.Dynamic, Pool.Default) End Sub Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs) device.Clear(ClearFlags.Target, Color.CornflowerBlue, 1.0F, 0) 'Setup Camera device.Transform.Projection = Matrix.PerspectiveFovLH(Math.PI / 4, CSng(Me.Width / Me.Height), 1.0F, 100.0F) device.Transform.View = Matrix.LookAtLH(New Vector3(0, 0, 8.0F), New Vector3(), New Vector3(0, 1, 0)) device.RenderState.Lighting = False 'begin drawing device.BeginScene() device.VertexFormat = CustomVertex.PositionTextured.Format device.SetStreamSource(0, vb, 0) 'draw the rotating cube angle += 0.01F Dim yaw As Single = CSng(angle / Math.PI) Dim pitch As Single = CSng(2 * angle / Math.PI) Dim roll As Single = CSng(4 * angle / Math.PI) Dim x, y, z As Single 'set the local rotation and location device.Transform.World = Matrix.Multiply(Matrix.RotationYawPitchRoll(yaw, pitch, roll), Matrix.Translation(x, y, 3)) 'assign the image to the triangles device.SetTexture(0, tex1) 'add the triangles that make the cube device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12) device.EndScene() device.Present() 'create infinate loop Me.Invalidate() End Sub Private Sub OnVertexBufferCreate(sender As Object, e As EventArgs) Dim buffer As VertexBuffer = CType(sender, VertexBuffer) Dim verts(35) As CustomVertex.PositionTextured ' Front face verts(0) = New CustomVertex.PositionTextured(-1.0F, 1.0F, 1.0F, 0.0F, 0.0F) verts(1) = New CustomVertex.PositionTextured(-1.0F, -1.0F, 1.0F, 0.0F, 1.0F) verts(2) = New CustomVertex.PositionTextured(1.0F, 1.0F, 1.0F, 1.0F, 0.0F) verts(3) = New CustomVertex.PositionTextured(-1.0F, -1.0F, 1.0F, 0.0F, 1.0F) verts(4) = New CustomVertex.PositionTextured(1.0F, -1.0F, 1.0F, 1.0F, 1.0F) verts(5) = New CustomVertex.PositionTextured(1.0F, 1.0F, 1.0F, 1.0F, 0.0F) ' Back face (remember this is facing *away* from the camera, so vertices should be clockwise order) verts(6) = New CustomVertex.PositionTextured(-1.0F, 1.0F, -1.0F, 0.0F, 0.0F) verts(7) = New CustomVertex.PositionTextured(1.0F, 1.0F, -1.0F, 1.0F, 0.0F) verts(8) = New CustomVertex.PositionTextured(-1.0F, -1.0F, -1.0F, 0.0F, 1.0F) verts(9) = New CustomVertex.PositionTextured(-1.0F, -1.0F, -1.0F, 0.0F, 1.0F) verts(10) = New CustomVertex.PositionTextured(1.0F, 1.0F, -1.0F, 1.0F, 0.0F) verts(11) = New CustomVertex.PositionTextured(1.0F, -1.0F, -1.0F, 1.0F, 1.0F) ' Top face verts(12) = New CustomVertex.PositionTextured(-1.0F, 1.0F, 1.0F, 0.0F, 0.0F) verts(13) = New CustomVertex.PositionTextured(1.0F, 1.0F, -1.0F, 1.0F, 1.0F) verts(14) = New CustomVertex.PositionTextured(-1.0F, 1.0F, -1.0F, 0.0F, 1.0F) verts(15) = New CustomVertex.PositionTextured(-1.0F, 1.0F, 1.0F, 0.0F, 0.0F) verts(16) = New CustomVertex.PositionTextured(1.0F, 1.0F, 1.0F, 1.0F, 0.0F) verts(17) = New CustomVertex.PositionTextured(1.0F, 1.0F, -1.0F, 1.0F, 1.0F) ' Bottom face (remember this is facing *away* from the camera, so vertices should be clockwise order) verts(18) = New CustomVertex.PositionTextured(-1.0F, -1.0F, 1.0F, 0.0F, 0.0F) verts(19) = New CustomVertex.PositionTextured(-1.0F, -1.0F, -1.0F, 0.0F, 1.0F) verts(20) = New CustomVertex.PositionTextured(1.0F, -1.0F, -1.0F, 1.0F, 1.0F) verts(21) = New CustomVertex.PositionTextured(-1.0F, -1.0F, 1.0F, 0.0F, 0.0F) verts(22) = New CustomVertex.PositionTextured(1.0F, -1.0F, -1.0F, 1.0F, 1.0F) verts(23) = New CustomVertex.PositionTextured(1.0F, -1.0F, 1.0F, 1.0F, 0.0F) ' Left face verts(24) = New CustomVertex.PositionTextured(-1.0F, 1.0F, 1.0F, 0.0F, 0.0F) verts(25) = New CustomVertex.PositionTextured(-1.0F, -1.0F, -1.0F, 1.0F, 1.0F) verts(26) = New CustomVertex.PositionTextured(-1.0F, -1.0F, 1.0F, 1.0F, 0.0F) verts(27) = New CustomVertex.PositionTextured(-1.0F, 1.0F, -1.0F, 0.0F, 1.0F) verts(28) = New CustomVertex.PositionTextured(-1.0F, -1.0F, -1.0F, 1.0F, 1.0F) verts(29) = New CustomVertex.PositionTextured(-1.0F, 1.0F, 1.0F, 0.0F, 0.0F) ' Right face (remember this is facing *away* from the camera, so vertices should be clockwise order) verts(30) = New CustomVertex.PositionTextured(1.0F, 1.0F, 1.0F, 0.0F, 0.0F) verts(31) = New CustomVertex.PositionTextured(1.0F, -1.0F, 1.0F, 1.0F, 0.0F) verts(32) = New CustomVertex.PositionTextured(1.0F, -1.0F, -1.0F, 1.0F, 1.0F) verts(33) = New CustomVertex.PositionTextured(1.0F, 1.0F, -1.0F, 0.0F, 1.0F) verts(34) = New CustomVertex.PositionTextured(1.0F, 1.0F, 1.0F, 0.0F, 0.0F) verts(35) = New CustomVertex.PositionTextured(1.0F, -1.0F, -1.0F, 1.0F, 1.0F) buffer.SetData(verts, 0, LockFlags.None) End Sub End Class