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

@@ -1,5 +1,7 @@
Imports System.Windows.Forms Imports System.Windows.Forms
Imports OpenTK Imports OpenTK
Imports OpenTK.Mathematics
Namespace CameraN Namespace CameraN

View File

@@ -2,6 +2,8 @@
<PropertyGroup> <PropertyGroup>
<MyType>Windows</MyType> <MyType>Windows</MyType>
<TargetFramework>net5.0-windows7.0</TargetFramework> <TargetFramework>net5.0-windows7.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<UseWpf>true</UseWpf>
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn> <NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
<DocumentationFile>Pilz.Drawing.Drawing3D.OpenGLFactory.xml</DocumentationFile> <DocumentationFile>Pilz.Drawing.Drawing3D.OpenGLFactory.xml</DocumentationFile>
<DefineTrace>true</DefineTrace> <DefineTrace>true</DefineTrace>
@@ -25,12 +27,12 @@
<OptionInfer>On</OptionInfer> <OptionInfer>On</OptionInfer>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="OpenTK.GLWpfControl" Version="4.0.0-pre.9" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" /> <PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" /> <PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="OpenTK" Version="4.2.0" /> <PackageReference Include="OpenTK" Version="4.3.0" />
<PackageReference Include="OpenTK.GLControl" Version="3.1.0" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" /> <PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

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

View File

@@ -1,9 +1,13 @@
Imports System.Drawing Imports System.Drawing
Imports System.Threading Imports System.Threading
Imports System.Windows.Forms Imports System.Windows.Forms
Imports OpenTK Imports OpenTK
Imports OpenTK.Graphics.OpenGL Imports OpenTK.Graphics.OpenGL
Imports OpenTK.Mathematics
Imports Pilz.S3DFileParser Imports Pilz.S3DFileParser
Imports Bitmap = System.Drawing.Bitmap Imports Bitmap = System.Drawing.Bitmap
Imports Color = System.Drawing.Color Imports Color = System.Drawing.Color
Imports Image = System.Drawing.Image Imports Image = System.Drawing.Image