replace OpenTK.GLControl with OpenTK.GLWpfControl and update code to use OpenTK 4.0 (not tested yet)

This commit is contained in:
2020-11-25 09:08:43 +01:00
parent 5303adf095
commit 4051a92b4e
4 changed files with 55 additions and 49 deletions

View File

@@ -6,16 +6,17 @@ Imports OpenTK
Imports OpenTK.Graphics.OpenGL
Imports Pilz.S3DFileParser
Imports Point = System.Drawing.Point
Imports KeyboardState = OpenTK.Input.KeyboardState
Imports Keyboard = OpenTK.Input.Keyboard
Imports Key = OpenTK.Input.Key
Imports Color = System.Drawing.Color
Imports System.Windows.Forms.Integration
Imports OpenTK.Mathematics
Imports System.Windows.Input
Namespace PreviewN
Public Class ModelPreview
Private WithEvents glControl1 As GLControl
Private WithEvents glControl1 As Wpf.GLWpfControl
Private WithEvents glControlHost As ElementHost
Private WithEvents MyCamera As New Camera
Private ProjMatrix As Matrix4 = Nothing
Private FOV As Single = 1.048F
@@ -75,21 +76,19 @@ Namespace PreviewN
Public ReadOnly Property GLControl As Control
Get
Return glControl1
Return glControlHost
End Get
End Property
Private ReadOnly Property IsStrgPressed As Boolean
Get
Dim state As KeyboardState = Keyboard.GetState()
Return state(Key.ControlLeft) OrElse state(Key.ControlRight)
Return Keyboard.IsKeyDown(Key.LeftCtrl) OrElse Keyboard.IsKeyDown(Key.RightCtrl)
End Get
End Property
Private ReadOnly Property IsShiftPressed As Boolean
Get
Dim state As KeyboardState = Keyboard.GetState()
Return state(Key.ShiftLeft) OrElse state(Key.ShiftRight)
Return Keyboard.IsKeyDown(Key.LeftShift) OrElse Keyboard.IsKeyDown(Key.RightShift)
End Get
End Property
@@ -99,7 +98,7 @@ Namespace PreviewN
End Get
Set(value As Boolean)
_isMouseDown = value
glControl1.Refresh()
glControlHost.Refresh()
End Set
End Property
@@ -122,18 +121,18 @@ Namespace PreviewN
DoubleBuffered = True
'glControl1
Me.glControl1 = New GLControl
Me.glControl1.BackColor = Color.Black
Me.glControl1.Location = New Point(0, 0)
Me.glControl1.MinimumSize = New Size(600, 120)
Me.glControl1.Name = "glControl1"
Me.glControl1.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Right Or AnchorStyles.Bottom
Me.glControl1.Location = New Point(0, 0)
Me.glControl1.Size = Me.ClientSize
Me.glControl1.TabIndex = 0
Me.glControl1.TabStop = False
Me.glControl1.VSync = False
Me.Controls.Add(Me.glControl1)
Me.glControl1 = New Wpf.GLWpfControl
Me.glControlHost.Child = glControl1
Me.glControlHost = New ElementHost
Me.glControlHost.BackColor = Color.Black
Me.glControlHost.Name = "glControl1"
Me.glControlHost.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Right Or AnchorStyles.Bottom
Me.glControlHost.Location = New Point(0, 0)
Me.glControlHost.MinimumSize = New Size(600, 120)
Me.glControlHost.Size = Me.ClientSize
Me.glControlHost.TabIndex = 0
Me.glControlHost.TabStop = False
Me.Controls.Add(Me.glControlHost)
Me.ResumeLayout(False)
'RenderTimer.SynchronizingObject = Nothing
@@ -141,10 +140,11 @@ Namespace PreviewN
'Toolkit.Init()
glControl1.CreateControl()
Dim controlSettings As New Wpf.GLWpfControlSettings
glControl1.Start(controlSettings)
AddHandler glControl1.MouseWheel, AddressOf glControl1_Wheel
ProjMatrix = Matrix4.CreatePerspectiveFieldOfView(FOV, glControl1.Width / glControl1.Height, 100.0F, 100000.0F)
glControl1.Enabled = False
glControlHost.Enabled = True
MyCamera.SetCameraMode(CameraMode.FLY, camMtx)
MyCamera.UpdateMatrix(camMtx)
@@ -163,8 +163,8 @@ Namespace PreviewN
End Sub
Public Sub UpdateView()
If glControl1.Enabled Then
glControl1.Invoke(Sub() glControl1.Invalidate())
If glControl1.IsEnabled Then
glControlHost.Invoke(Sub() glControlHost.Invalidate())
End If
End Sub
@@ -179,9 +179,9 @@ Namespace PreviewN
End Sub
Public Sub HandlesOnShown(sender As Object, e As EventArgs) Handles MyBase.Shown
glControl1.Enabled = True
glControlHost.Enabled = True
RenderModels()
glControl1.Invalidate()
glControlHost.Invalidate()
End Sub
Public Sub RenderModels()
@@ -197,7 +197,7 @@ Namespace PreviewN
End If
End Sub
Private Sub glControl1_Load(sender As Object, e As EventArgs) Handles glControl1.Load
Private Sub glControl1_Load(sender As Object, e As EventArgs) Handles glControl1.Loaded
GL.Enable(EnableCap.Blend)
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha)
@@ -227,7 +227,7 @@ Namespace PreviewN
End If
End Sub
Public Sub HandlesOnPaint(sender As Object, e As PaintEventArgs) Handles glControl1.Paint
Public Sub HandlesOnPaint(renderTime As TimeSpan) Handles glControl1.Render
GL.ClearColor(ClearColor)
GL.Clear(ClearBufferMask.ColorBufferBit Or ClearBufferMask.DepthBufferBit)
@@ -243,24 +243,24 @@ Namespace PreviewN
End If
Next
glControl1.SwapBuffers()
'glControl1.SwapBuffers()
End Sub
Private Sub glControl1_Resize(sender As Object, e As EventArgs) Handles glControl1.Resize
glControl1.Context.Update(glControl1.WindowInfo)
Private Sub glControl1_Resize(sender As Object, e As EventArgs) Handles glControlHost.Resize
'glControl1.Context.Update(glControl1.WindowInfo)
GL.Viewport(0, 0, glControl1.Width, glControl1.Height)
ProjMatrix = Matrix4.CreatePerspectiveFieldOfView(FOV, glControl1.Width / glControl1.Height, 100.0F, 100000.0F)
glControl1.Invalidate()
glControlHost.Invalidate()
End Sub
Private Sub glControl1_Wheel(sender As Object, e As MouseEventArgs)
Private Sub glControl1_Wheel(sender As Object, e As Windows.Input.MouseWheelEventArgs)
MyCamera.ResetMouseStuff()
MyCamera.UpdateCameraMatrixWithScrollWheel(CInt(Math.Truncate(e.Delta * (If(IsShiftPressed, 3.5F, 1.5F)))), camMtx)
savedCamPos = MyCamera.Position
glControl1.Invalidate()
glControlHost.Invalidate()
End Sub
Private Sub glControl1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles glControl1.MouseDown
Private Sub glControl1_MouseDown(ByVal sender As Object, ByVal e As Windows.Input.MouseButtonEventArgs) Handles glControl1.MouseDown
IsMouseDown = True
savedCamPos = MyCamera.Position
End Sub
@@ -270,14 +270,14 @@ Namespace PreviewN
IsMouseDown = False
End Sub
Private Sub glControl1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles glControl1.MouseMove
Private Sub glControl1_MouseMove(ByVal sender As Object, ByVal e As Windows.Forms.MouseEventArgs) Handles glControlHost.MouseMove
If IsMouseDown AndAlso e.Button = MouseButtons.Left Then
If IsShiftPressed Then
MyCamera.UpdateCameraOffsetWithMouse(savedCamPos, e.X, e.Y, glControl1.Width, glControl1.Height, camMtx)
Else
MyCamera.UpdateCameraMatrixWithMouse(e.X, e.Y, camMtx)
End If
glControl1.Invalidate()
glControlHost.Invalidate()
End If
End Sub
@@ -286,31 +286,29 @@ Namespace PreviewN
Dim allowCamMove As Boolean = Not (IsMouseDown AndAlso IsShiftPressed)
If allowCamMove Then
Dim state As KeyboardState = Keyboard.GetState
If state(Key.W) Then
If Keyboard.IsKeyDown(Key.W) Then
'camera.Move(moveSpeed, moveSpeed, camMtx)
MyCamera.UpdateCameraMatrixWithScrollWheel(moveSpeed, camMtx)
savedCamPos = MyCamera.Position
End If
If state(Key.S) Then
If Keyboard.IsKeyDown(Key.S) Then
'camera.Move(-moveSpeed, -moveSpeed, camMtx)
MyCamera.UpdateCameraMatrixWithScrollWheel(-moveSpeed, camMtx)
savedCamPos = MyCamera.Position
End If
If state(Key.A) Then
If Keyboard.IsKeyDown(Key.A) Then
'camera.Move(-moveSpeed, 0, camMtx)
MyCamera.UpdateCameraOffsetDirectly(-moveSpeed, 0, camMtx)
End If
If state(Key.D) Then
If Keyboard.IsKeyDown(Key.D) Then
'camera.Move(moveSpeed, 0, camMtx)
MyCamera.UpdateCameraOffsetDirectly(moveSpeed, 0, camMtx)
End If
If state(Key.E) Then
If Keyboard.IsKeyDown(Key.E) Then
'camera.Move(0, -moveSpeed, camMtx)
MyCamera.UpdateCameraOffsetDirectly(0, -moveSpeed, camMtx)
End If
If state(Key.Q) Then
If Keyboard.IsKeyDown(Key.Q) Then
'camera.Move(0, moveSpeed, camMtx)
MyCamera.UpdateCameraOffsetDirectly(0, moveSpeed, camMtx)
End If