I’m using openTk for a my project. I have serious problem with mouse selection.
I tried unsuccessfully a lot of examples found on the web.
So, I have to draw a grid with some coils (custom cylinders with hole):
Private Sub drawFilledCoil(ByVal sadV As Integer, ByVal sadH As Integer, ByVal sadZ As Integer, ByVal _coil As coil, ByVal renderMode As RenderingMode)
Dim _in As List(Of Vector3) = New List(Of Vector3) Dim _out As List(Of Vector3) = New List(Of Vector3)
Dim _start As Double = 1.5 * Math.PI '1.5
Dim _stop As Double = 4.1 * Math.PI '4.1
Dim v3Tmp As Vector3
Dim v2Tmp As Vector2
Dim tmpcount As Integer = 0
points2D_ = New List(Of Vector2)
Dim _newName As Integer
_newName = _name + 1
'If renderMode = RenderingMode.Select Then MsgBox("hghgh")
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''' CALCOLO PUNTI PER DISEGNO ROTOLO. ''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For i As Double = _start To _stop - 1 Step 0.2
'Diametro Esterno.
v3Tmp.X = ((sadH - 1) * (_interasseH / 100)) + (((_interasseH / 100) - _coil.diametroE) / 2) + (_coil.diametroE / 2) + CDbl(_coil.diametroE / 2 * Math.Cos(i))
v3Tmp.Y = ((_totSaddleV - sadV - 1) * (_interasseV / 100)) + (((_interasseV / 100) - _coil.larghezza) / 2) '+ (Width * y)
v3Tmp.Z = (_coil.diametroE / 2) + (_coil.diametroE / 2 * Math.Sin(i))
_out.Add(v3Tmp)
_coil.points3D.Add(v3Tmp)
v2Tmp = TreDTo2d(v3Tmp, GlControl1)
_coil.points2D.Add(v2Tmp)
'Diametro Interno.
v3Tmp.X = ((sadH - 1) * (_interasseH / 100)) + (((_interasseH / 100) - _coil.diametroI) / 2) + (_coil.diametroI / 2) + CDbl(_coil.diametroI / 2 * Math.Cos(i))
v3Tmp.Y = ((_totSaddleV - sadV - 1) * (_interasseV / 100)) + (((_interasseV / 100) - _coil.larghezza) / 2) '+ (Width * y)
v3Tmp.Z = (_coil.diametroE / 2) + (_coil.diametroI / 2 * Math.Sin(i))
_in.Add(v3Tmp)
v2Tmp = TreDTo2d(v3Tmp, GlControl1)
_coil.points2D.Add(v2Tmp)
tmpcount += 1
Next
v3Tmp.X = ((sadH - 1) * (_interasseH / 100)) + (((_interasseH / 100) - _coil.diametroE) / 2) + (_coil.diametroE / 2) + CDbl(_coil.diametroE / 2 * Math.Cos(_start))
v3Tmp.Y = ((_totSaddleV - sadV - 1) * (_interasseV / 100)) + (((_interasseV / 100) - _coil.larghezza) / 2) '+ (Width * y)
v3Tmp.Z = (_coil.diametroE / 2) + CDbl(_coil.diametroE / 2 * Math.Sin(_start))
_out.Add(v3Tmp)
_coil.points3D.Add(v3Tmp)
v2Tmp = TreDTo2d(v3Tmp, GlControl1)
_coil.points2D.Add(v2Tmp)
points2D_ = _coil.points2D
v3Tmp.X = ((sadH - 1) * (_interasseH / 100)) + (((_interasseH / 100) - _coil.diametroI) / 2) + (_coil.diametroI / 2) + CDbl(_coil.diametroI / 2 * Math.Cos(_start))
v3Tmp.Y = ((_totSaddleV - sadV - 1) * (_interasseV / 100)) + (((_interasseV / 100) - _coil.larghezza) / 2) '+ (Width * y)
v3Tmp.Z = (_coil.diametroE / 2) + CDbl(_coil.diametroI / 2 * Math.Sin(_start))
_in.Add(v3Tmp)
v2Tmp = TreDTo2d(v3Tmp, GlControl1)
_coil.points2D.Add(v2Tmp)
'Calcolo limiti punti 3D.
Dim _limits3D() As Integer = calcLimits3D(_coil)
_coil.zMin = _limits3D(0)
_coil.zMax = _limits3D(1)
_coil.center = calcCenter3D(_coil)
''''''''''''''''''''''''''''''''''''''''
'Calcolo limiti punti 2D.
Dim _limits() As Integer = calcLimits2D(_coil)
_coil.xMin = _limits(0)
_coil.xMax = _limits(1)
_coil.yMin = _limits(2)
_coil.yMax = _limits(3)
xMin = _limits(0)
xMax = _limits(1)
yMin = _limits(2) - 15
yMax = _limits(3)
''''''''''''''''''''''''''''''''''''''''
'Dictionary con tutti i rotoli.
coils.Add(_coil.id, _coil)
'coils__.Add(_coil.id, _coil)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''' DISEGNO DEL ROTOLO CON I PUNTI CALCOLATI. ''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim _color As Double
'If renderMode = RenderingMode.Select Then
' GL.LoadName(_name)
'End If
'GL.PushMatrix()
If renderMode = RenderingMode.Select Then
GL.PushName(_newName) 'GL.LoadName(_newName)
End If
'Corona Davanti.
GL.LineWidth(0.5)
GL.Begin(PrimitiveType.QuadStrip)
For i As Double = 0 To _out.Count - 1
If _coil.selected Then
GL.Color3(Color.Blue)
Else
If (i < (_out.Count / 4)) Then
_color = (i) / (_out.Count / 4) + 0.2
ElseIf (i >= (_out.Count / 4) And i < (_out.Count / 2)) Then
_color = ((_out.Count / 2) - i) / (_out.Count / 4) + 0.2
ElseIf (i >= (_out.Count / 2) And i < (_out.Count * 3 / 4)) Then
_color = (i - (_out.Count / 2)) / (_out.Count / 4) + 0.2
Else
_color = ((_out.Count / 2) - (i - (_out.Count / 2))) / (_out.Count / 4) + 0.2
End If
GL.Color4(0.901 * _color, 0.901 * _color, 0.941 * _color, _color)
End If
GL.Vertex3(_out(i).X, _out(i).Y, _out(i).Z)
GL.Vertex3(_in(i).X, _in(i).Y, _in(i).Z)
Next
GL.End()
'GL.PopMatrix()
'Corona Dietro.
'GL.PushMatrix()
GL.LineWidth(0.5)
If renderMode = RenderingMode.Select Then GL.PushName(_newName)
GL.Begin(PrimitiveType.QuadStrip)
For i As Double = 0 To _out.Count - 1
If _coil.selected Then
GL.Color3(Color.Blue)
Else
If (i < (_out.Count / 4)) Then
_color = (i) / (_out.Count / 4) + 0.4
ElseIf (i >= (_out.Count / 4) And i < (_out.Count / 2)) Then
_color = ((_out.Count / 2) - i) / (_out.Count / 4) + 0.4
ElseIf (i >= (_out.Count / 2) And i < (_out.Count * 3 / 4)) Then
_color = (i - (_out.Count / 2)) / (_out.Count / 4) + 0.4
Else
_color = ((_out.Count / 2) - (i - (_out.Count / 2))) / (_out.Count / 4) + 0.4
End If
GL.Color4(0.901 * _color, 0.901 * _color, 0.941 * _color, _color)
End If
GL.Vertex3(_out(i).X, _out(i).Y + _coil.larghezza, _out(i).Z)
GL.Vertex3(_in(i).X, _in(i).Y + _coil.larghezza, _in(i).Z)
Next
GL.End()
'GL.PopMatrix()
'Superficie Esterna.
GL.LineWidth(0.5)
'GL.PushMatrix()
If renderMode = RenderingMode.Select Then GL.PushName(_newName)
GL.Begin(PrimitiveType.QuadStrip)
For i As Double = 0 To _out.Count - 1
If _coil.selected Then
GL.Color3(Color.Blue)
Else
If (i < (_out.Count / 4)) Then
_color = (i) / (_out.Count / 4)
ElseIf (i >= (_out.Count / 4) And i < (_out.Count / 2)) Then
_color = ((_out.Count / 2) - i) / (_out.Count / 4)
ElseIf (i >= (_out.Count / 2) And i < (_out.Count * 3 / 4)) Then
_color = (i - (_out.Count / 2)) / (_out.Count / 4)
Else
_color = ((_out.Count / 2) - (i - (_out.Count / 2))) / (_out.Count / 4)
End If
GL.Color4(0.901 * _color, 0.901 * _color, 0.941 * _color, _color)
End If
GL.Vertex3(_out(i).X, _out(i).Y + _coil.larghezza, _out(i).Z)
GL.Vertex3(_out(i).X, _out(i).Y, _out(i).Z)
Next
GL.End()
'GL.PopMatrix()
'Superficie interna.
GL.LineWidth(0.5)
'GL.PushMatrix()
If renderMode = RenderingMode.Select Then GL.PushName(_newName)
GL.Begin(PrimitiveType.QuadStrip)
For i As Double = 0 To _out.Count - 1
If _coil.selected Then
GL.Color3(Color.Blue)
Else
If (i < (_out.Count / 2)) Then
_color = i / _out.Count
Else
_color = (_out.Count - i) / _out.Count
End If
GL.Color4(0.901 * _color, 0.901 * _color, 0.941 * _color, _color)
End If
GL.Vertex3(_in(i).X, _in(i).Y + _coil.larghezza, _in(i).Z)
GL.Vertex3(_in(i).X, _in(i).Y, _in(i).Z)
Next
GL.End()
'GL.PopMatrix()
End Sub
And in mouse selection:
Dim selectBuf(512) As Integer Dim _hits As Integer = 0
Dim viewport(3) As Integer
GL.SelectBuffer(512, selectBuf)
GL.RenderMode(RenderingMode.Select)
GL.GetInteger(GetPName.Viewport, viewport)
GL.PushMatrix()
GL.MatrixMode(MatrixMode.Projection)
GL.LoadIdentity()
PickMatrix(e.X, e.Y, 5, 5, viewport)
'Dim projection As Matrix4 = Matrix4.CreatePerspectiveFieldOfView(0.7853982F, 4.0F / 3.0F, 0.1F, 100.0F) '; // this projection matrix is the same as one in glControlWindow_Paint method.
Dim projection As Matrix4 = Matrix4.CreatePerspectiveFieldOfView(1.04, Me.Width / Me.Height, 100, 10000)
GL.MultMatrix(projection)
GL.MatrixMode(MatrixMode.Modelview)
GL.PushMatrix()
Try
Dim singleCoil As coil = Nothing
initCoil(singleCoil)
singleCoil.points2D = New List(Of Vector2)
singleCoil.points3D = New List(Of Vector3)
_name = 0
For i As Integer = 0 To 1 ' _Coils.Length - 1
Dim idCoil As Long = CLng(_Coils(i)(0))
If idCoil > 0 Then
singleCoil.id = idCoil
singleCoil.fila = CStr(_Coils(i)(1))
singleCoil.box = CInt(_Coils(i)(2))
singleCoil.livello = CInt(_Coils(i)(3))
singleCoil.diametroE = CInt(_Coils(i)(4)) / 100
singleCoil.diametroI = 6.1
singleCoil.larghezza = CInt(_Coils(i)(5)) / 100
If (singleCoil.id = clickedCoil) Then
singleCoil.selected = True
Else
singleCoil.selected = False
End If
drawFilledCoil(convertFila(CStr(_Coils(i)(1))), CInt(_Coils(i)(2)), CInt(_Coils(i)(3)), singleCoil, RenderingMode.Select)
GL.PopName()
GL.PopMatrix()
End If
Next
Catch ex As Exception
End Try
'GL.PopMatrix()
'GL.Flush()
_hits = GL.RenderMode(RenderingMode.Render)
_hits is Always 0…And I don’t know why
Please help me
Thank you in advance