1

我正在尝试实现 2+ 独立的 OpenTK GLControls。在表单加载时,我希望它们在加载时都被渲染和显示。在下图中,您会看到我的问题。一个被正确渲染,但另一个未被渲染。

mouse_drag 之前的 GL 屏幕截图

mouse_drag 之前的 GL 屏幕截图

对错误控件执行 mouse_drag 操作将使其正确呈现。

mouse_drag 后的 GL 屏幕截图

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
4

1 回答 1

0

我正在制作的项目中遇到类似错误,问题在于 windows7-openTK 兼容性。首先,在使用 openTK 时,您似乎永远不应该使用 Windows7 航空主题。就我而言,这不是唯一的解决方案。您还必须尝试更新您的 GPU 的最新驱动程序(我在 OpenTK 论坛上看到过一些帖子,人们修复了类似的错误绘图问题)但在我的情况下仍然没有工作。因为我有一个非常旧的 GPU,所以我需要购买一个新的,这样我就解决了我所有的图形问题。

可能不是这样,但我在您的代码中找不到任何错误。如果您可以在 XP 机器上测试您的项目,并且如果它在那里正常工作,请尝试更新您的图形驱动程序。

我也看到人们使用 glColor4 函数而不是 GLcolor3 来解决这个问题。

我希望它有所帮助。

于 2012-07-04T15:48:20.150 回答