XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   
My XNA Book
      
       Go to section on this site

Additional info


Latest Forum posts

 Account settings
  Posted by: igthorn
  When: 21/04/2014 at 10:20:00

 Pavtube 2014 Easter Deals with 20% off
  Posted by: igthorn
  When: 21/04/2014 at 10:16:23

 20% off on iMedia Converter for Mac
  Posted by: VIKIVannessa
  When: 20/04/2014 at 01:35:54

 Pavtube Video Converter Ultimate Deal
  Posted by: VIKIVannessa
  When: 20/04/2014 at 01:35:50

 Easter Day Deals
  Posted by: Applefly
  When: 19/04/2014 at 22:18:14

 40% OFF Pavtube DVD Ripper for Mac
  Posted by: VIKIVannessa
  When: 19/04/2014 at 02:02:54

 DVD Ripper with 40% off
  Posted by: VIKIVannessa
  When: 19/04/2014 at 02:02:22

 Get DVD Ripper Wind/Mac + $14 Coupon
  Posted by: VIKIVannessa
  When: 19/04/2014 at 02:02:03

 Backup DVD Escape Plan
  Posted by: Applefly
  When: 17/04/2014 at 06:00:12

 Disney DVDs to MP4
  Posted by: Applefly
  When: 17/04/2014 at 05:56:07


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:
  • 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


    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 - 2011 Riemer Grootjans
  • Translations

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

    Microsoft MVP Award



    2007 - 2011 MVP Award
    DirectX - XNA

    Contents

    News
    Home
    Forum
    XNA 2.0 Recipes Book (8)
    XNA 3.0 Recipes Book (8)
    Downloads
    Extra Reading (3)
    Matrices: geometrical
    Matrix Mathematics
    Homogenous matrices
    Community Projects (1)
    Tutorials (160)
    XNA 4.0 using C# (89)
    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!