ngdg's picture

Picking/Selection using Vb.NET

Hello. I've been developing a VB.Net Windows Form Application, learning and using OpenTK from the scratch, with which I'm actually getting by, thanks to your excelent support and documentation.
I started using the GameWindow style, then I moved to GLControl in order to fulfill my needs of an interface. I'm now familiar with the whole drawing thing, including mouse and keyboard inputs, although I'm facing a problem now that I wanted to add a picking support to my application, but I have no clue about how I could do it. I've also been googling and reading this forum, and I've actually found some examples, most of then written in C# and using a certain lib called "Glu" which as far as I'm concerned has been discontinued. My question is: Is there any example (written in VB) from which I could take some ideas? Or is anyone willing to give me some guidelines in order to do it?

Thanks in advance!


Comments

Comment viewing options

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

c# or Vb.NET doesn't matter. just take a look a opentk's picking example (in the example browser) and translate it from c# to vb.net.

ngdg's picture

Do you mean in the project gallery? I can't seem to find one...

avc81's picture

OpenTK\1.0\Source\Examples\OpenGL\1.x\Picking.cs

bhairav's picture

Hi, I checked that example, but it uses color information for picking. Extending the logic for box selection drawn by user may not be all that easy. Can anybody please give a working example of picking using selection?

I googled and tried various solutions proposed, but I think I am going wrong somewhere in the projection matrices.

The code I am using is as follows:

    Public Sub StartPicking(x As Double, y As Double)
 
        Dim viewport(4) As Integer
        ReDim selectBuffer(128 * 4)
        GL.SelectBuffer(128 * 4, selectBuffer)
        GL.RenderMode(RenderingMode.Select)
 
 
        GL.MatrixMode(MatrixMode.Projection)
        GL.PushMatrix()
        GL.LoadIdentity()
 
        SetupViewport()
        GL.GetInteger(GetPName.Viewport, viewport)
        OpenTK.Graphics.Glu.PickMatrix(x, viewport(3) - y, 4, 4, viewport)
 
 
        GL.MatrixMode(MatrixMode.Modelview)
        GL.InitNames()
        GL.PushName(0)
 
 
    End Sub
 
    Public Function GetSelected() As Integer
        Dim SelectedID As Integer
        '---> Render()
        Draw()
        GL.Flush()
 
        'restoring the original projection matrix
        GL.MatrixMode(MatrixMode.Projection)
        GL.PopMatrix()
        GL.MatrixMode(MatrixMode.Modelview)
        GL.PopMatrix()
 
 
        Dim hits As Integer
 
        'Returning to normal rendering mode
        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(selectBuffer(i * 4 + 1))
 
            If closest >= Distance Then
                closest = Distance
                selectedId = selectBuffer(i * 4 + 3)
            End If
        Next
 
        Return selectedId
    End Function

I am also allowing rotation by arcball (just in case if it matters). Can anybody please help?

Thanks