Hi all, i’m new in this forum and i’m new with OpenGL (and my english is terrible, sorry).
I have a problem using the gluPickMatrix to grab the mouse picked elements in a OpenGL window.
My problem is that the X and Y mouse coordinates i use to define the gluPickMatrix does not match the object correctly, i mean if i pass to the function the real mouse coordinates in the window (X and Height-Y) it works correctly over the center of the window, but not correctly when i click in ln some other parts.
Infact if i want it works almost correctly i have to moltiply X and Y for a certain factor (different between x and y) that i found by attempts.
So, i say “it works almost fine”, because when i resize my window and i zoom my view those factors should be corrected… i think because the perspective deformation.
This is the code of the rendering routine:
Public Sub Refresh()
Dim V(3) As Integer
UpdateLights()
Gl.glInitNames()
Gl.glPushName(0)
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT Or Gl.GL_DEPTH_BUFFER_BIT Or Gl.GL_STENCIL_BUFFER_BIT)
Gl.glLoadIdentity()
If GrabSelection = True Then
Gl.glGetIntegerv(Gl.GL_VIEWPORT, V)
'*********************************************
'this is what i have to add to my code:
Dim smx, smy As Double
smx = 12.2
smy = 9.11
Dim dblX As Double = (GrabX - V(2) / 2) * smx + V(2) / 2
Dim dblY As Double = (GrabY - V(3) / 2) * smy + V(3) / 2
'it would be only:
'Dim dblX as Double = CDbl(GrabX)
'Dim dblX as Double = CDbl(GrabX)
'*********************************************
Glu.gluPickMatrix(dblX, dblY, 5, 5, V)
Gl.glRenderMode(Gl.GL_SELECT)
End If
'Rotate system
Gl.glTranslatef(0, 0, -100)
Gl.glTranslatef(Dx, Dy, Dz)
Gl.glRotatef(Ax, 1, 0, 0)
Gl.glRotatef(Ay, 0, 1, 0)
Gl.glRotatef(Az, 0, 0, 1)
Gl.glScalef(Zoom, Zoom, Zoom)
'draw elements on active layers
For I = 0 To UBound(List)
If List(I).Visible = True Then
'draw list
Gl.glPushName(I + 1)
Gl.glCallList(I + 1)
End If
Next
If GrabSelection = True Then
GrabSelection = False
Dim h As Integer = Gl.glRenderMode(Gl.GL_RENDER)
If h > 0 Then ParseBuffer(h)
Gl.glPopMatrix()
Else
'present draw
Gl.glFlush()
Gdi.SwapBuffers(hDC)
End If
End Sub
This sub is called by a timer, and when the user click on the window i set the GrabSelection boolean to TRUE, so one frame is lost but at the end of the rendering i parse the grabbed selectionbuffer.
In the code i indicated the part that puzzles me.
Any idea to solve this problem?
Thanks in advance.