|
|
|
|
DirectX 9 Managed Code with VB.net: A crash course |
-- These tutorials were kindly provided by TzeJian Chear --
Initialization
Create a new Visual Basic windows application project; add reference to Microsoft.DirectX and Microsoft.DirectX.Direct3D, by selecting the Project menu, and then Add reference. Make sure they have the same version.
Select the form and press F7, so you can see the code. To use DirectX, add these lines before the class structure:
Imports Microsoft.DirectX Imports Microsoft.DirectX.Direct3D
3D drawings are managed by the Direct3D object, but you?ve to create it first. You must also give certain parameters (like setting up a canvas). Write these lines:
Public Class Form1 Private device As Direct3D.Device Public Sub Initialize() Dim present As PresentParameters = New PresentParameters present.Windowed = True 'we?ll draw on a window present.SwapEffect = SwapEffect.Discard 'discuss later device = New Direct3D.Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, present) End Sub End Class
Argument explanation in order: 0 for default adapter, use hardware acceleration, Me is the form D3D will create its canvas upon, and we use software to process vertices. Note: Call Initialize() only ONCE before anything else, preferably in Form_Load event.
Clearing screen
We Clear the form with black color whenever paint event is called (determined by OS) and always Present it so it?ll get drawn. Add these lines to Form_Paint event:
device.Clear(ClearFlags.Target, Color.Black, 1.0, 0) device.Present()
Perpetual painting
Paint event gets called only when the form needs to be redrawn, which we don?t want. We want it to be redrawn perpetually. Add these lines before calling Initialize():
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True) 'Do not draw form?s background Me.Height = 500 ?Set window size and title Me.Width = 500 Me.Text = "DirectX Tutorial using Visual Basic"
Add this as the last line in the Form_Paint event:
Me.Invalidate()'redraw
Drawing triangle
All objects are drawn using triangles made of 3 vertices (points) detailing position, color, etc. We define the color and 3D position of each vertex. The Position property needs a Vector4 (to let DirectX do its job), so as 4th coordinate we always pass 1. Add these codes before device.Clear to create the vertices.
Dim vertices As CustomVertex.TransformedColored() = New CustomVertex.TransformedColored(0 To 2) {} ?create an array of vertices vertices(0).Position = New Vector4(150, 100, 0, 1) vertices(0).Color = Color.Red.ToArgb 'encode color in Argb vertices(1).Position = New Vector4(Me.Width/2 + 100,100,0,1) vertices(1).Color = Color.Green.ToArgb vertices(2).Position = New Vector4(250, 300, 0, 1) vertices(2).Color = Color.Yellow.ToArgb
Add these codes after device.Clear and before device.Present.
device.BeginScene() 'all drawings after this line device.VertexFormat = CustomVertex.TransformedColored.Format device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices) device.EndScene() 'all drawings before this line
Vertex format tells what kind of vertices Direct3D is drawing. DrawUserPrimitives actually draws the vertices based on the vertex format.
Now hit F5. If everything works well, you should see the image below.

Click here to go to the forum on this chapter!
Or click on one of the topics on this chapter to go there: import Imports Microsoft.DirectX
Imports Microsoft.Direc...Help would be appreciated. first, let me say that I am a beginner, so I may j...Need Help with First Triangle! in my attempts at vb, i have succeeded in doing so...
Below you can find the code you should have at this moment:
Imports Microsoft.DirectX Imports Microsoft.DirectX.Direct3D Public Class Form1 Private device As Direct3D.Device Public Sub Initialize() Dim present As PresentParameters = New PresentParameters present.Windowed = True 'we?ll draw on a window present.SwapEffect = SwapEffect.Discard 'discuss later device = New Direct3D.Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, present) End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True) 'Do not draw form?s background\ Me.Height = 500 Me.Width = 500 Me.Text = "DirectX Tutorial using Visual Basic" Initialize() End Sub Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint Dim vertices As CustomVertex.TransformedColored() = New CustomVertex.TransformedColored(0 To 2) {} 'create an array of vertices vertices(0).Position = New Vector4(150, 100, 0, 1) vertices(0).Color = Color.Red.ToArgb 'encode color in Argb vertices(1).Position = New Vector4(Me.Width / 2 + 100, 100, 0, 1) vertices(1).Color = Color.Green.ToArgb vertices(2).Position = New Vector4(250, 300, 0, 1) vertices(2).Color = Color.Yellow.ToArgb device.Clear(ClearFlags.Target, Color.Black, 1.0, 0) device.BeginScene() 'all drawings after this line device.VertexFormat = CustomVertex.TransformedColored.Format device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices) device.EndScene() 'all drawings before this line device.Present() Me.Invalidate() 'redraw End Sub End Class
- Website design & XNA + DirectX code : Riemer Grootjans - ©2003 - 2011 Riemer Grootjans
|
|
|
|
|