多个OpenTK控件:为什么只有一个最初渲染?

时间:2012-06-25 15:10:37

标签: vb.net opengl opentk

我正在尝试实施2个以上的独立OpenTK GLControl。在表单加载时,我希望它们在加载时都被渲染和显示。在下图中,您将看到我的问题。一个是正​​确渲染,但另一个是未渲染的。

GL mouse_drag

之前的截图

GL Screenshot before mouse_drag

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

在mouse_drag

之后的GL屏幕截图

GL Screenshot after mouse_drag

以下是我的主要表单的代码。它创建并添加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

1 个答案:

答案 0 :(得分:0)

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

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

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

我希望它有所帮助。