XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   July 19: Series4 - 2 Pass Renderstates
My Book: Out Now!
      
       Go to section on this site

Additional info


Latest Forum posts

 WinForm not found
  Posted by: riemer
  When: 20/07/2008 at 14:30:03

 Bounding Boxes for Maya Data
  Posted by: riemer
  When: 20/07/2008 at 14:27:31

 QuadTree
  Posted by: riemer
  When: 20/07/2008 at 14:23:48

 Bounding Boxes for Maya Data
  Posted by: vijay
  When: 20/07/2008 at 12:25:40

 Error on compiling
  Posted by: libia
  When: 20/07/2008 at 02:36:16

 Terrain From RAW
  Posted by: reiko
  When: 19/07/2008 at 20:36:39

 WinForm not found
  Posted by: kapil.rajak
  When: 19/07/2008 at 16:24:52

 human skin colors
  Posted by: aguacate
  When: 19/07/2008 at 13:34:55

 Bounding Boxes for Maya Data
  Posted by: Archenon
  When: 19/07/2008 at 09:19:16

 Volunteer to write VB.net tutorial
  Posted by: riemer
  When: 19/07/2008 at 07:00:19


Ads

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.




DirectX Tutorial 1 - The first triangle

If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !



Click here to go to the forum on this chapter!

Or click on one of the topics on this chapter to go there:
  • 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


    Google
     
    Webwww.riemers.net


    If you appreciate the amount of time I spend creating and updating
    these pages, feel free to donate -- any amount is welcome !



    - Website design & XNA + DirectX code : Riemer Grootjans -
    ©2003 - 2008 Riemer Grootjans
  • Translations

    This site in English
    This site in Korean
    This site in Czech

    Microsoft MVP Award



    2007 - 2008 MVP Award
    DirectX - XNA

    Contents

    News
    Home
    Forum
    XNA 2.0 Recipes Book (8)
    Downloads
    Extra Reading (3)
    Matrices: geometrical
    Matrix Mathematics
    Homogenous matrices
    Tutorials (136)
    XNA 2.0 using C# (65)
    DirectX using C# (54)
    DirectX using C++ (15)
    DirectX using VB (2)
    Series 1: Intro (2)
    The first triangle
    Rotation - translation
    -- Expand all --


    Thank you!

    Support this site --
    any amount is welcome !

    Stay up-to-date

    I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!