|
|
|
|

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.

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
- Website design & XNA + DirectX code : Riemer Grootjans - ©2003 - 2008 Riemer Grootjans
|
|
|
|
|