Hi,
I'm having some issues running this DirectX app inside VB 2008 Express (maybe not the best to use for this, but I don't have an option). What I'm trying to do is get a MDI child window into which I can load all the usual 3D shapes (probably from an STL file, I know how to do that). I've attached the code I have below. The issue I have is that one of the sides of the Tetrahedral does not display and also when I resize the child window things go really weird... translations, rotations and zoom are working fine (so far).
references to Microsoft.DirectX and Microsoft.Direct3D are added.
Any suggestions? I think most of the coding is done, but I must be missing something somewhere...
(p.s. sorry for the formatting, this was copied across and then some unnecessary items deleted)
Imports
Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Public
Class Form1
Public Vertices As CustomVertex.PositionNormalColored() 'an array of vertices defining faces
Public Device As Direct3D.Device
Public WithEvents mdiChildForm As New Form 'create Child Form
Public angle As Single = 0
Public NVert As Integer 'number of vertices
Public NFac As Integer 'number of faces
Public DownLoc As Point 'location where mouse button was pressed
Public TransLoc As Microsoft.DirectX.Vector3 'holds translation vector to current pos
Public RotLoc As Microsoft.DirectX.Vector3 'holds rotation vector to current pos
Public Sub Initialize()
'initialize the DirectX window
Dim Present As PresentParameters = New PresentParameters
With Present
.Windowed = True
.SwapEffect = SwapEffect.Discard
End With
'use the child form as the device
Device = New Direct3D.Device(0, DeviceType.Hardware, mdiChildForm, CreateFlags.SoftwareVertexProcessing, Present)
'set initial view
Device.Transform.Projection = Matrix.PerspectiveFovRH(1, mdiChildForm.Width / mdiChildForm.Height, -5, 5) 'sets field of view, aspect ratio, etc
Device.Transform.View = Matrix.LookAtRH(New Vector3(0, 0, 10), New Vector3(0, 0, 0), New Vector3(0, 1, 0)) 'position and direction
End Sub
Public Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'set the display styles
Me.SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint, True)
Me.UpdateStyles()
'set child's form parent to current
With mdiChildForm
.MdiParent = Me
.Show()
.Location = New Point(10, 10)
End With
Me.Refresh()
End Sub
Public Sub mdiChildForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles mdiChildForm.Load
'initialize the direcX device
Initialize()
End Sub
Public Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
'paints the data to the device/screen
If Device.Disposed = True Then Exit Sub
Dim DoLights As Boolean = True
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
'Lights for the scene (only makes sense w/ faces)
If DoLights = True Then
Device.RenderState.Lighting = True
Device.Lights(0).Type = LightType.Directional
Device.Lights(0).Diffuse = Color.White
Device.Lights(0).Direction = New Vector3(0, 0, -2)
Device.Lights(0).Enabled = True
Device.RenderState.Lighting = True
Else
Device.RenderState.Lighting =
False
End If
'define drawn faces
Device.VertexFormat = CustomVertex.PositionNormalColored.Format
If NVert > 0 And Not IsNothing(Vertices) Then Device.DrawUserPrimitives(PrimitiveType.TriangleList, NVert, Vertices)
End If
Device.EndScene()
'all drawings before this line
If mdiChildForm.Visible = True Then Device.Present()
Me.Invalidate() 'redraw
End Sub
Public Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mdiChildForm.MouseDown
'set the location
DownLoc = MousePosition
End Sub
Public Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mdiChildForm.MouseMove
'modify the translation by the distance moved relative to the MouseDown location
Dim DeSens As Single = 20
If MouseButtons.ToString = "Left" Then
'translation
TransLoc.X = TransLoc.X - (DownLoc.X - MousePosition.X) / DeSens
'invert
TransLoc.Y = TransLoc.Y + (DownLoc.Y - MousePosition.Y) / DeSens
TransLoc.Z = TransLoc.Z + 0
'execute translation
Device.Transform.World = Matrix.Translation(TransLoc) * Matrix.RotationYawPitchRoll(RotLoc.X, RotLoc.Y, RotLoc.Z)
'set mouse down to current value
DownLoc.X = MousePosition.X
DownLoc.Y = MousePosition.Y
'return focus to main window (and thereby refreshes the view)
Me.Focus()
ElseIf MouseButtons.ToString = "Right" Then
'rotation
RotLoc.X = RotLoc.X + (DownLoc.X - MousePosition.X) / DeSens
RotLoc.Y = RotLoc.Y + (DownLoc.Y - MousePosition.Y) / DeSens
RotLoc.Z = RotLoc.Z + 0
'execute rotation
Device.Transform.World = Matrix.Translation(TransLoc) * Matrix.RotationYawPitchRoll(RotLoc.X, RotLoc.Y, RotLoc.Z)
'set mouse down to current value
DownLoc.X = MousePosition.X
DownLoc.Y = MousePosition.Y
'return focus to main window (and thereby refreshes the view)
Me.Focus()
ElseIf MouseButtons.ToString = "Middle" Or (InStr(MouseButtons.ToString, "Right") <> 0 And InStr(MouseButtons.ToString, "Left") <> 0) Then
'zoom (allow either middle or left+right buttons as zoom options)
TransLoc.X = TransLoc.X + 0
TransLoc.Y = TransLoc.Y + 0
TransLoc.Z = TransLoc.Z + (DownLoc.Y - MousePosition.Y) / DeSens 'execute zoom
Device.Transform.World = Matrix.Translation(TransLoc) * Matrix.RotationYawPitchRoll(RotLoc.X, RotLoc.Y, RotLoc.Z)
'set mouse down to current value
DownLoc.X = MousePosition.X
DownLoc.Y = MousePosition.Y
'return focus to main window (and thereby refreshes the view)
Me.Focus()
End If
End Sub
Public Sub Form1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
'use the mouse wheel (where available) as a zoom option
Dim zoom As Single
Dim DeSens As Single = 120
zoom = e.Delta
If zoom <> 0 Then
'zoom in/out
'zoom
TransLoc.X = TransLoc.X + 0
TransLoc.Y = TransLoc.Y + 0
TransLoc.Z = TransLoc.Z + (zoom) / DeSens
'execute zoom
Device.Transform.World = Matrix.Translation(TransLoc) * Matrix.RotationYawPitchRoll(RotLoc.X, RotLoc.Y, RotLoc.Z)
'return focus to main window (and thereby refreshes the view)
Me.Focus()
End If
End Sub
Public Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles mdiChildForm.DoubleClick
'reset view
TransLoc.X = 0
TransLoc.Y = 0
TransLoc.Z = 0
RotLoc.X = 0
RotLoc.Y = 0
RotLoc.Z = 0
Device.Transform.World = Matrix.Translation(0, 0, 0) * Matrix.RotationYawPitchRoll(0, 0, 0) * Matrix.RotationZ(0)
'return focus to main window (and thereby refreshes the view)
Me.Focus()
End Sub
Private
Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'load a standard tet4
NVert = 11 : NFac = 4
Vertices =
New CustomVertex.PositionNormalColored(NVert) {}
'A
Vertices(0).Position =
New Vector3(0, 0, 1)
Vertices(1).Position =
New Vector3(1, -1, -1)
Vertices(2).Position =
New Vector3(1, 1, -1)
'B
Vertices(3).Position =
New Vector3(0, 0, 1)
Vertices(4).Position =
New Vector3(1, 1, -1)
Vertices(5).Position =
New Vector3(-1, 0, -1)
'C
Vertices(6).Position =
New Vector3(0, 0, 1)
Vertices(7).Position =
New Vector3(-1, 0, -1)
Vertices(8).Position =
New Vector3(1, -1, -1)
'D
Vertices(9).Position =
New Vector3(1, -1, -1)
Vertices(10).Position =
New Vector3(1, 1, -1)
Vertices(11).Position =
New Vector3(-1, 0, -1)
Vertices(0).Color = Color.Red.ToArgb
Vertices(1).Color = Color.Red.ToArgb
Vertices(2).Color = Color.Red.ToArgb
Vertices(3).Color = Color.Green.ToArgb
Vertices(4).Color = Color.Green.ToArgb
Vertices(5).Color = Color.Green.ToArgb
Vertices(6).Color = Color.Blue.ToArgb
Vertices(7).Color = Color.Blue.ToArgb
Vertices(8).Color = Color.Blue.ToArgb
Vertices(9).Color = Color.White.ToArgb
Vertices(10).Color = Color.White.ToArgb
Vertices(11).Color = Color.White.ToArgb
Vertices(0).Normal =
New Vector3(1, 0, 0)
Vertices(1).Normal =
New Vector3(1, 0, 0)
Vertices(2).Normal =N
ew Vector3(1, 0, 0)
Vertices(3).Normal =
New Vector3(0, 1, 0)
Vertices(4).Normal =
New Vector3(0, 1, 0)
Vertices(5).Normal =
New Vector3(0, 1, 0)
Vertices(6).Normal =
New Vector3(-1, 0, 0)
Vertices(7).Normal =
New Vector3(-1, 0, 0)
Vertices(8).Normal =
New Vector3(-1, 0, 0)
Vertices(9).Normal =N
ew Vector3(0, 0, -1)
Vertices(10).Normal =
New Vector3(0, 0, -1)
Vertices(11).Normal =
New Vector3(0, 0, -1)
'reset
Initialize()
'reset view
Form1_DoubleClick(sender, e)
Me.Invalidate() 'redraw
End Sub
End
Class