我正在尝试实现 2+ 独立的 OpenTK GLControls。在表单加载时,我希望它们在加载时都被渲染和显示。在下图中,您会看到我的问题。一个被正确渲染,但另一个未被渲染。
mouse_drag 之前的 GL 屏幕截图
对错误控件执行 mouse_drag 操作将使其正确呈现。
mouse_drag 后的 GL 屏幕截图
这是我的主要表单的代码。它创建并添加 GLControl 对象:
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Threading.Tasks
Imports OpenTK
Imports OpenTK.Graphics
Imports OpenTK.Graphics.OpenGL
Public Class MultipleDisplay
Private Sub MultipleDisplay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Creating and adding the controls.
'The load event prompts the user to choose a file to build the objects.
Dim task2 As GLControlTask = New GLControlTask()
task2.MakeCurrent()
task2.Width = 400
task2.Height = 400
task2.Top = 0
task2.Left = 450
Me.Controls.Add(task2)
'The load event prompts the user to choose a file to build the objects.
Dim task1 As GLControlTask = New GLControlTask()
task1.MakeCurrent()
task1.Width = 400
task1.Height = 400
task1.Top = 0
task1.Left = 0
Me.Controls.Add(task1)
'INITIAL PAINT
task2.MakeCurrent()
task2.Invalidate()
task1.MakeCurrent()
task1.Invalidate()
End Sub
End Class
这是扩展 GLControl 类的 GLControlTask 类的代码:
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Threading.Tasks
Imports OpenTK
Imports OpenTK.Graphics
Imports OpenTK.Graphics.OpenGL
Public Class GLControlTask
Inherits GLControl
Private arm As Arm
Private rotator As ArcBall
Private glLoaded As Boolean = False
Private cameraAngleX As Single
Private cameraAngleY As Single
Private cameraAngleZ As Single
Private prevMouseX As Integer
Private prevMouseY As Integer
Private glMouseDown As Boolean
Private lightAngleX As Single
Private lightAngleY As Single
Private lightDistance As Single
'Perspective Attributes
Private eyeOffset As Vector3
Private boxRadius As Single
Private boxOffset As Vector3
Private isPerspective As Boolean
Private hasLighting As Boolean
Private hasFog As Boolean
Public Sub New()
arm = New Arm()
arm.loadFromXML()
'MsgBox(arm.ToString())
isPerspective = False
hasLighting = True
hasFog = False
eyeOffset.X = 0
eyeOffset.Y = 0
eyeOffset.Z = 1000.0F
lightDistance = 10000.0F
lightAngleX = 0.0F
lightAngleY = 0.0F
boxRadius = 2.5F
End Sub
Private Sub Me_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
glLoaded = True
GL.ClearColor(Color.Black)
rotator = New ArcBall(Me)
Me_Resize(Nothing, New EventArgs())
End Sub
Private Sub Me_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Resize
If glLoaded = False Then
Return
End If
GL.Viewport(0, 0, Me.Width, Me.Height)
rotator.windowResized()
Me.Invalidate()
End Sub
'PAINT CODE REMOVED
Private Sub On_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
glMouseDown = True
Me.MakeCurrent()
prevMouseX = e.X
prevMouseY = e.Y
rotator.mousePressed(e.X, e.Y)
End Sub
Private Sub Me_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
glMouseDown = False
End Sub
Private Sub Me_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If glMouseDown = False Then
Return
End If
If e.Button = MouseButtons.Left Then
rotator.mouseDragged(e.X, e.Y)
Me.Invalidate()
End If
prevMouseX = e.X
prevMouseY = e.Y
End Sub
Private Function deg2Rad(ByVal degrees As Double) As Double
Return degrees * Math.PI / 180
End Function
End Class