Sign in to follow this  
Knight Chat X

OpenGL Object Picking Problem in Selection Mode? (Source Code Included.)

Recommended Posts

I think I've got a problem with the gluPickMatrix function, I have a triangle draw to the selection buffer and displayed on screen, when you click on the viewport it enables selection mode, you can see the object's selection boundaries when you move the cursor around the triangle, the hit count is displayed in the upper left corner and shows a 2 when a hit is detected, 1 when it's not. The pick matrix seems a bit funky on the detection of the cursor and I don't know what the problem is other than that, I've included a simple source code sample with commenting and my lite Opengl .Net rendering DLL to display and render. Related Source Code Sample Download Link: http://knight-technologies.us/objectpicking.zip Some of the code below: #Region "Object Selection" Private Const BUFFERSIZE As Integer = 1024 '512 Private SelectionBuffer(BUFFERSIZE) As UInteger Private blnPicking As Boolean = False Private hits As Integer = 0 Private cursorX As Integer Private cursorY As Integer #End Region #Region "Main Form" Private Sub frmMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove cursorX = e.X cursorY = e.Y End Sub Private Sub frmMain_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown blnPicking = True 'Mouse clicked. End Sub #End Region #Region "OpenGL Render Window Lite Events" Private Sub OGLRenderWindow_RenderWindow_Initialized() Handles OGLRenderWindow.RenderWindow_Initialized ChangeViewportSize(350, 200) 'Adjust viewport here to assure correct Default View... End Sub Private Sub OGLRenderWindow_RenderWindow_SceneRender() Handles OGLRenderWindow.RenderWindow_SceneRender '################################################################################# 'GO INTO SELECTION MODE IF PICKING IS ENABLED VIA MOUSE CLICK. '################################################################################# If blnPicking = True Then StartPicking() 'If mouse clicked on render window go into selection mode. '################################################################################# '################################################################################# 'CREATE NEW MATRIX SO CAMERA VIEW MATRIX DOESN'T GET MULTIPLIED AND THE CURRENT CAMERA VIEW REMAINS CONSISTENT. '################################################################################# glLoadIdentity() 'Load new matrix to maintain consistant camera view without multiplying the matrix. '################################################################################# '################################################################################# 'DRAW TRIANGLE TO NORMAL VIDEO DISPLAY/DRAW BUFFER MODE. (DISPLAY'S TRIANGLE.) '################################################################################# 'glPushMatrix() 'glPushName(1) glTranslatef(0.0F, 0.0F, -4.0F) 'Positon triangle. glBegin(enumGLDrawingMethod.GL_TRIANGLES) 'Set drawing mode to triangles. glVertex3f(0.0F, 1.0F, 0.0F) 'Top Center. glVertex3f(-1.0F, -1.0F, 0.0F) 'Bottom Left. glVertex3f(1.0F, -1.0F, 0.0F) 'Bottom Right. glEnd() 'Drawing of Triangle Finished. 'glPopName() 'glPopMatrix() '################################################################################# '################################################################################# 'END SELECTION MODE OR RETURN DRAW GRAPHICS DATA TO THE SCREEN/DISPLAY. '################################################################################# If blnPicking = True Then 'If we are currently picking and in select buffer mode then stop picking. StopPicking() Else 'Otherwise, if we're in normal drawing mode then swap buffers and draw directly to the screen/display. OGLRenderWindow.SwapNormalBuffers() End If '################################################################################# End Sub #End Region #Region "Current Test Code" Public Sub ChangeViewportSize(ByVal w1 As Integer, ByVal h1 As Integer) Dim ratio As Single Dim h As Integer = h1 Dim w As Integer = w1 'Prevent a divide by zero, when window is too short '(you cant make a window of zero width). If h = 0 Then h = 1 'Set ratio. ratio = 1.0F * w / h - 1 'ratio = w / h 'Reset the coordinate system before modifying. glMatrixMode(enumGLMatrixMode.GL_PROJECTION) glLoadIdentity() 'Set the viewport to be the entire window. glViewport(0, 0, w, h) 'Set the clipping volume. gluPerspective(90, ratio, 0.1, 1000) 'Setting the camera view now. glMatrixMode(enumGLMatrixMode.GL_MODELVIEW) End Sub Public Sub StartPicking() Dim viewport(3) As Integer Dim ratio As Single '################################################################################# 'GO INTO SELECT BUFFER MODE. '################################################################################# glSelectBuffer(BUFFERSIZE, SelectionBuffer) glGetIntegerv(enumGLOther.GL_VIEWPORT, viewport) glRenderMode(enumGLOther.GL_SELECT) glInitNames() '################################################################################# 'DRAW IN SELECTION MODE ANY OBJECTS HERE WHICH NEED CLICK DETECTION... '################################################################################# glPushName(1) glTranslatef(0.0F, 0.0F, -1.0F) 'Move Left 1.5 Units And Into The Screen 6.0. glBegin(enumGLDrawingMethod.GL_TRIANGLES) glVertex3f(0.0F, 1.0F, 0.0F) 'Top. glVertex3f(-1.0F, -1.0F, 0.0F) 'Bottom Left. glVertex3f(1.0F, -1.0F, 0.0F) 'Bottom Right. glEnd() 'Finished Drawing The Triangle. glPopName() '################################################################################# '################################################################################# 'SWITCH BACK TO PROJECTION MODE AND PUSH A NEW MATRIX ONTO THE STACK. '################################################################################# glMatrixMode(enumGLMatrixMode.GL_PROJECTION) glPushMatrix() glLoadIdentity() '################################################################################# 'CREATE A NEW PICK MATRIX FOR CLICK DETECTION. (PROBLEM IS HERE I BELIEVE....) '################################################################################# 'gluPickMatrix(cursorX, viewport(3) - cursorY, 5, 5, viewport) 'gluPickMatrix(cursorX, viewport(3) - cursorY, 1, 1, viewport) gluPickMatrix(CType(cursorX, Double), CType(viewport(3) - cursorY, Double), 1.0F, 1.0F, viewport) 'gluPickMatrix(Windows.Forms.Cursor.Position.X, viewport(3) - Windows.Forms.Cursor.Position.Y, 5, 5, viewport) '################################################################################# '################################################################################# 'SETUP PERSPECTIVE RATIO. '################################################################################# ratio = 1.0F * Me.Size.Width / Me.Size.Height - 1 '(viewport(2) + 0.0) / viewport(3) gluPerspective(90, ratio, 0.1, 1000) glMatrixMode(enumGLMatrixMode.GL_MODELVIEW) End Sub Public Sub StopPicking() glMatrixMode(enumGLMatrixMode.GL_PROJECTION) glPopMatrix() glMatrixMode(enumGLMatrixMode.GL_MODELVIEW) glFlush() '################################################################################# 'RETURN TO RENDER/NORMAL VIDEO BUFFER DRAWING MODE AND GET OBJECT CLICK/HIT INFORMATION. '################################################################################# hits = glRenderMode(enumGLOther.GL_RENDER) If hits > 0 Then lblHits.Text = hits 'Display hit count. Else blnPicking = False End If '################################################################################# End Sub #End Region [Edited by - Knight Chat X on September 27, 2006 11:58:25 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this