bhairav's picture

Picking Problem using OpenTK with VB.NET

Dear All,

I am having a nightmarish time trying to find out the bug in my picking program. I have made a simple program using the GLControl. I am trying to pick the objects drawn, but the picking output appears to be random, without any pattern. Also, even if I pick in empty space, I get hits for all objects drawn! The code used is as follows:

The mousedown event in form:

   Private Sub p_MouseDown(sender As Object, e As MouseEventArgs) Handles p.MouseDown
        If Control.ModifierKeys = Keys.Control Then
 
            p.SimplySelect(e.X, e.Y)
 
            p.LoadName(1)
            p.BeginMode(PariGL.DrawingModes.Triangles)
            p.SetColor(Color.Red)
            p.AddVertex(0, 0, 0)
            p.SetColor(Color.Green)
            p.AddVertex(1, 0, 0)
            p.SetColor(Color.Pink)
            p.AddVertex(0, 1, 0)
            p.EndMode()
 
 
            p.LoadName(3)
 
            p.BeginMode(PariGL.DrawingModes.Triangles)
            p.SetColor(Color.Orange)
            p.AddVertex(3, 0, 0)
            p.AddVertex(4, 0, 0)
            p.AddVertex(3, 1, 0)
            p.EndMode()
 
 
            p.LoadName(5)
            p.DrawPyramid(0, 3, 0, 0, 2, 0, 1, 2, 0, 1, 2, -1, 0, 2, -1)
 
            Me.Text = p.ProcessHits().ToString
        End If
 
    End Sub

The setting up of the GLControl is regular (p is the name of inherited control). Selection code on the inherited GLControl side are:

    Const BUFSIZE As Integer = 512
    Private selectBuf(BUFSIZE) As UInteger
 
    Public Sub SimplySelect(x As Integer, y As Integer)
        Dim viewport(4) As Integer
        GL.GetInteger(GetPName.Viewport, viewport)
 
        GL.SelectBuffer(BUFSIZE, selectBuf)
        GL.RenderMode(RenderingMode.Select)
 
        GL.InitNames()
        GL.PushName(0)
 
        GL.MatrixMode(MatrixMode.Projection)
        GL.PushMatrix()
        GL.LoadIdentity()
 
        'create 5x5 pixel picking region near cursor location
 
        GLU.PickMatrix(x, (viewport(3) - y), 1, 1, viewport)
 
        Dim aspect_ratio As Double = Width / Height
        Dim perspective As OpenTK.Matrix4 = OpenTK.Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, CSng(aspect_ratio), 1, 64)
        GL.MatrixMode(MatrixMode.Projection)
        GL.LoadMatrix(perspective)
 
 
    End Sub
 
    Public Function ProcessHits() As Integer
        Dim hits As Integer
        Dim SelectedID As Integer = -1
 
        GL.MatrixMode(MatrixMode.Projection)
        GL.PopMatrix()
        GL.Flush()
 
        hits = GL.RenderMode(RenderingMode.Render)
 
        Dim closest As UInteger
        closest = UInteger.MaxValue
        Dim Distance As UInteger
        Dim i As Integer
        For i = 0 To hits - 1
 
            Distance = CUInt(selectBuf(i * 4 + 1))
 
            If closest >= Distance Then
                closest = Distance
                SelectedID = CInt(selectBuf(i * 4 + 3))
            End If
        Next
 
        Return SelectedID
    End Function

I am sure the problem would be obvious to expert eyes. Could you please advise where am I going wrong?

Bhairav


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Cinder's picture

Thinking that picking should pick the closest point, and given your initial condition using closest = UInteger.MaxValue, then maybe the line

If closest >= Distance Then

should be

If closest < Distance Then

otherwise it won't monotonically select better candidate points.