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: Anonymous
  When: 07/05/2014 at 09:48:39

 forced subtitle
  Posted by: Applefly
  When: 07/05/2014 at 06:00:48

 convert DVD into PMS
  Posted by: Applefly
  When: 07/05/2014 at 05:55:25

 DVD to Digital Copy easily
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:52:29

 DVD on Xbox 360/Xbox One Console
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:51:47

 Extract .Srt Subtitles
  Posted by: Applefly
  When: 04/05/2014 at 03:54:38

 Encode Movie collection
  Posted by: Applefly
  When: 04/05/2014 at 03:52:41

 Convert DVD to WMV
  Posted by: Applefly
  When: 29/04/2014 at 05:53:50

 rip DVDs into digital files
  Posted by: Applefly
  When: 29/04/2014 at 05:51:20

 iTunes movies/music to Kindle Fire
  Posted by: ciciyu80
  When: 29/04/2014 at 05:10:20


Ads

Moving to world space -- translating and rotating the triangle

-- These tutorials were kindly provided by TzeJian Chear --


Camera

The positions of the vertices above are already in the form of screen positions. We will use world coordinate to describe them and let Direct3D convert them to screen positions. Replace the vertex initialization codes with these:

 Dim vertices As CustomVertex.PositionColored() = New CustomVertex.PositionColored(2) {}
         vertices(0).Position = New Vector3(0, 0, 0)
         vertices(0).Color = Color.Red.ToArgb
         vertices(1).Position = New Vector3(10, 0, 0)
         vertices(1).Color = Color.Green.ToArgb
         vertices(2).Position = New Vector3(5, 10, 0)
         vertices(2).Color = Color.Yellow.ToArgb

Tell Direct3D we?re using a different vertex format. Change this in Paint event:

 device.VertexFormat = CustomVertex.PositionColored.Format

Set our camera?s position and direction to point at the triangle?s direction. Otherwise the triangle can?t be seen. Add these lines to the Initialize sub:

 device.Transform.Projection = Matrix.PerspectiveFovLH(CSng(Math.PI/4),Me.Width / Me.Height, 1, 50) 'sets field of view, aspect ratio, etc
 device.Transform.View = Matrix.LookAtLH(New Vector3(0, 0, 30), New Vector3(0, 0, 0), New Vector3(0, 1, 0)) 'position and direction

The triangle is invisible because there?s no light to illuminate it. We tell Direct3D to disregard lighting conditions. Add this line before device.Clear:

 device.RenderState.Lighting = False

Note: Direct3D uses left-hand coordinates, so the green vertex, which is defined on a positive x-axis, appears on left instead. If we instead place the camera behind the triangle (negative z-position for camera), that vertex should appear on right. But Direct3D draws only triangles whose vertices are defined clockwise relative to the camera, so the triangle won?t be drawn if the camera?s behind (the vertices become counter-clockwise relative to the hind camera). Either we reinitialize the vertices to be relatively clockwise, or we tell Direct3D to draw ALL triangles indiscriminately (slows down the machine, therefore not recommended). Should you want to draw all triangles, add this line before device.Clear:

 device.RenderState.CullMode = Cull.None 'no triangle is culled

Vertices Initialization

If you notice, we re-initializate the same vertices every time before we draw. We should do this only once. Add this line right after Public Class Form1.

 Private vertices As CustomVertex.PositionColored()'an array of vertices

Remove the vertices initialization code in Paint event and add this procedure:

 Private Sub InitVertices()
         vertices = New CustomVertex.PositionColored(2) {}
         vertices(0).Position = New Vector3(0, 0, 0)
         vertices(0).Color = Color.Red.ToArgb
         vertices(1).Position = New Vector3(10, 0, 0)
         vertices(1).Color = Color.Green.ToArgb
         vertices(2).Position = New Vector3(5, 10, 0)
         vertices(2).Color = Color.Yellow.ToArgb
 End Sub

Call InitVertices() only ONCE right after calling Initialize() procedure. Try to run this code.

Rotation and Translation

To keep track of rotation angle, add this code after Public Class Form1:

 Private angle As Single = 0.0

To actually rotate, add these lines right before calling DrawUserPrimitives:

 angle = angle + 0.1
 device.Transform.World = Matrix.RotationZ(angle)

Run the program to see what this does.

To translate (move around) the vertices, say, to (-3, 5, 2), add this line:

 device.Transform.World = Matrix.Translation(-3, 5, 2)

To rotate around a point, FIRST TRANSLATE such that the triangle?s center is at that point, and THEN ROTATE it. They must be multiplied together. Replace with this line:

 device.Transform.World = Matrix.Translation(-5, -10 * 1 / 3, 0) * Matrix.RotationZ(angle)

To rotate around a custom-axis, replace RotationZ with this:

 Matrix.RotationAxis(New Vector3(angle*0.2, angle*1.5, angle*3), angle)

When you run this code, you should see a triangle spinning around.




DirectX Tutorial 2 - Rotation - translation

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:
  • This doesn't work
          device.Transform.World = Matrix.Translation(-5, -1...


    And the complete code:

     Imports Microsoft.DirectX
     Imports Microsoft.DirectX.Direct3D
     
     
     
     Public Class Form1
         Private angle As Single = 0.0
         Private vertices As CustomVertex.PositionColored() 'an array of vertices
         Private device As Direct3D.Device
         Private Sub InitVertices()
             vertices = New CustomVertex.PositionColored(2) {}
             vertices(0).Position = New Vector3(0, 0, 0)
             vertices(0).Color = Color.Red.ToArgb
             vertices(1).Position = New Vector3(10, 0, 0)
             vertices(1).Color = Color.Green.ToArgb
             vertices(2).Position = New Vector3(5, 10, 0)
             vertices(2).Color = Color.Yellow.ToArgb
         End Sub
     
         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)
             device.Transform.Projection = Matrix.PerspectiveFovLH(CSng(Math.PI / 4), Me.Width / Me.Height, 1, 50) 'sets field of view, aspect ratio, etc
             device.Transform.View = Matrix.LookAtLH(New Vector3(0, 0, 30), New Vector3(0, 0, 0), New Vector3(0, 1, 0)) 'position and direction
         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
             InitVertices()
     
             device.Transform.Projection = Matrix.PerspectiveFovLH(CSng(Math.PI / 4), Me.Width / Me.Height, 1, 50) 'sets field of view, aspect ratio, etc
             device.Transform.View = Matrix.LookAtLH(New Vector3(0, 0, 30), New Vector3(0, 0, 0), New Vector3(0, 1, 0)) 'position and direction
     
             device.RenderState.Lighting = False
             device.RenderState.CullMode = Cull.None 'no triangle is culled
     
             device.Clear(ClearFlags.Target, Color.Black, 1.0, 0)
     
             device.BeginScene() 'all drawings after this line
             device.VertexFormat = CustomVertex.PositionColored.Format
             angle = angle + 0.1
             device.Transform.World = Matrix.Translation(-5, -10 * 1 / 3, 0) * Matrix.RotationAxis(New Vector3(angle * 0.2, angle * 1.5, angle * 3), angle)
     
             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!