Чтобы прямо как в некоторых примерах к СДК (чайник, человечек там какой-то..). Собрал вот из
кусочков примеров все что мне было нужно, а про вращение мышкой ничего не нашел. Делаю на VB.Net:
- Код: Выделить всё
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Imports Microsoft.DirectX.DirectInput
Public Class frm3dView
Private D3D_Device As Direct3D.Device
Private D3D_PresentParameters As New Direct3D.PresentParameters
Private DI_Mouse As DirectInput.Device
Private DI_Keyboard As DirectInput.Device
Private DI_MouseState As DirectInput.MouseState
Private DI_KeyboardState As DirectInput.KeyboardState
Private DI_MouseLeft, DI_MouseRight, DI_MouseMidle As Boolean
Private DI_KeysLeft, DI_KeysRight, DI_KeysUp, DI_KeysDown, DI_KeysEsc As Boolean
Private DI_MouseUpdated As System.Threading.AutoResetEvent
Private DI_KeyboardUpdated As System.Threading.AutoResetEvent
Private DI_Shutdown As System.Threading.ManualResetEvent
Private axes(5) As CustomVertex.PositionColored
Private rotX, rotY, rotZ As Single
Private rotM As Microsoft.DirectX.Matrix
#Region "Init"
Private Sub frm3dView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Me.SetStyle(ControlStyles.Opaque, True)
InitGraphics()
InitInput()
InitCamera()
CreateAxes()
rotM.Translate(0, 0, 0)
End Sub
Private Sub InitGraphics()
'init Direct3D
D3D_PresentParameters.Windowed = True
D3D_PresentParameters.SwapEffect = SwapEffect.Discard
D3D_PresentParameters.EnableAutoDepthStencil = True
D3D_PresentParameters.AutoDepthStencilFormat = DepthFormat.D16
D3D_Device = New Direct3D.Device(0, Direct3D.DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, D3D_PresentParameters)
D3D_Device.Transform.Projection = Matrix.PerspectiveFovLH(CSng(Math.PI) / 4, 4 / 3, 1, 100)
'lights
D3D_Device.RenderState.Lighting = False
End Sub
Private Sub InitInput()
DI_Mouse = New DirectInput.Device(SystemGuid.Mouse)
DI_Mouse.SetCooperativeLevel(Me, CooperativeLevelFlags.NonExclusive Or CooperativeLevelFlags.Background)
DI_MouseUpdated = New System.Threading.AutoResetEvent(False)
DI_Shutdown = New System.Threading.ManualResetEvent(False)
DI_Mouse.SetEventNotification(DI_MouseUpdated)
Dim threadLoop1 As System.Threading.Thread = New System.Threading.Thread(AddressOf MouseThreadFunction)
threadLoop1.Start()
DI_Keyboard = New DirectInput.Device(SystemGuid.Keyboard)
DI_Keyboard.SetCooperativeLevel(Me, CooperativeLevelFlags.NonExclusive Or CooperativeLevelFlags.Background)
DI_KeyboardUpdated = New System.Threading.AutoResetEvent(False)
DI_Shutdown = New System.Threading.ManualResetEvent(False)
DI_Keyboard.SetEventNotification(DI_KeyboardUpdated)
Dim threadLoop2 As System.Threading.Thread = New System.Threading.Thread(AddressOf KeyboardThreadFunction)
threadLoop2.Start()
DI_Mouse.Acquire()
DI_Keyboard.Acquire()
End Sub
Private Sub MouseThreadFunction()
Dim handls() As System.Threading.WaitHandle = {DI_MouseUpdated, DI_Shutdown}
Do While True
Dim index As Integer = System.Threading.WaitHandle.WaitAny(handls)
If index = 0 Then
UpdateMouseState()
Else
If index = 1 Then
Exit Do
End If
End If
Loop
End Sub
Private Sub KeyboardThreadFunction()
Dim handls() As System.Threading.WaitHandle = {DI_KeyboardUpdated, DI_Shutdown}
Do While True
Dim index As Integer = System.Threading.WaitHandle.WaitAny(handls)
If index = 0 Then
UpdateKeyboardState()
Else
If index = 1 Then
Exit Do
End If
End If
Loop
End Sub
Private Sub UpdateMouseState()
DI_MouseState = DI_Mouse.CurrentMouseState
DI_MouseLeft = (DI_Mouse.CurrentMouseState.GetMouseButtons(0) = 128)
DI_MouseRight = (DI_Mouse.CurrentMouseState.GetMouseButtons(1) = 128)
DI_MouseMidle = (DI_Mouse.CurrentMouseState.GetMouseButtons(2) = 128)
End Sub
Private Sub UpdateKeyboardState()
DI_KeysLeft = DI_Keyboard.GetCurrentKeyboardState.Item(Microsoft.DirectX.DirectInput.Key.LeftArrow)
DI_KeysRight = DI_Keyboard.GetCurrentKeyboardState.Item(Microsoft.DirectX.DirectInput.Key.RightArrow)
DI_KeysUp = DI_Keyboard.GetCurrentKeyboardState.Item(Microsoft.DirectX.DirectInput.Key.UpArrow)
DI_KeysDown = DI_Keyboard.GetCurrentKeyboardState.Item(Microsoft.DirectX.DirectInput.Key.DownArrow)
DI_KeysEsc = DI_Keyboard.GetCurrentKeyboardState.Item(Microsoft.DirectX.DirectInput.Key.Escape)
If DI_KeysUp Then
rotX = 0.1
rotM = rotM * Matrix.RotationX(rotX)
End If
If DI_KeysDown Then
rotX = -0.1
rotM = rotM * Matrix.RotationX(rotX)
End If
If DI_KeysLeft Then
rotY = -0.1
rotM = rotM * Matrix.RotationY(rotY)
End If
If DI_KeysRight Then
rotY = 0.1
rotM = rotM * Matrix.RotationY(rotY)
End If
If DI_KeysEsc Then
rotM.Translate(0, 0, 0)
End If
End Sub
Private Sub InitCamera()
D3D_Device.Transform.Projection = Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(45), CSng(Me.Width / Me.Height), -0.0F, 100.0F)
D3D_Device.Transform.View = Matrix.LookAtLH(New Vector3(0.5F, 0.5F, -15.0F), New Vector3(0.5F, 0.5F, 0.0F), New Vector3(0.0F, 1.0F, 0.0F))
D3D_Device.RenderState.CullMode = Cull.None
End Sub
Private Sub frm3dView_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
DI_Shutdown.Set()
End Sub
Private Sub CreateAxes()
Dim len As Single = 10
'X
axes(0).Position = New Vector3(0.0F, 0.0F, 0.0F)
axes(1).Position = New Vector3(len, 0.0F, 0.0F)
axes(0).Color = Color.Blue.ToArgb
axes(1).Color = Color.Blue.ToArgb
'Y
axes(2).Position = New Vector3(0.0F, 0.0F, 0.0F)
axes(3).Position = New Vector3(0.0F, len, 0.0F)
axes(2).Color = Color.Red.ToArgb
axes(3).Color = Color.Red.ToArgb
'Z
axes(4).Position = New Vector3(0.0F, 0.0F, 0.0F)
axes(5).Position = New Vector3(0.0F, 0.0F, len)
axes(4).Color = Color.Green.ToArgb
axes(5).Color = Color.Green.ToArgb
End Sub
#End Region
Private Sub frm3dView_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
draw()
Me.Invalidate()
End Sub
Private Sub draw()
D3D_Device.BeginScene()
D3D_Device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, System.Drawing.Color.LightBlue, 1, 0)
D3D_Device.VertexFormat = CustomVertex.PositionColored.Format
D3D_Device.DrawUserPrimitives(PrimitiveType.LineList, 3, axes)
D3D_Device.Transform.World = rotM
D3D_Device.EndScene()
D3D_Device.Present()
End Sub
End Class