Hi folks,
I'm quite new in programming OpenGl with Jogl. Actually i'm trying to develop an application which uses picking to get OpenGl points which are selected by the mouse. I've found some examples with picking in the web, but all of them refer to picking in 3D. But my application is only 2D and I'm using the following MatrixMode: ... gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); gl.glOrtho(0, 640, 480, 0, -1, 1); gl.glMatrixMode(GL.GL_MODELVIEW); .... gl.glBegin(GL.GL_POINTS); gl.glColor3f(1.0f, 0.4f, 1.0f); gl.glVertex2d(160.0,120.0); gl.glEnd(); ... gl.glEnable(GL.GL_POINT_SMOOTH); gl.glPointSize(60.0f); .... So does anyone know a good tutorial or example which explains picking in 2D? This would be great. Thanks in advance, Daniel |
Administrator
|
Hi!
There is no difference between 2D and 3D when using picking, you call the same functions. You can use our example of picking in jogl-demos and adapt it to your needs.
Julien Gouesse | Personal blog | Website
|
Hi,
thanks for your fast reply. Ok, I will check your examples and I hope I will be successful in adapting your examples. Can you give me the direct link to your examples. Unfortunately I couldn't find them... Thank you very much! Daniel |
Administrator
|
In reply to this post by Daniel S.
Hi Daniel,
In 2D, all you have to do is convert your mouse pointer coordinates to object space, then search your list of objects to see which one the point is inside. With matrices are set up as you showed, you'd do something like this: int [] aiViewport = new int [4]; gl.glGetIntegerv( GL.GL_VIEWPORT, aiViewport, 0 ); double [] adModelView = new double [16]; gl.glGetDoublev( GL.GL_MODELVIEW_MATRIX, adModelView, 0 ); double [] adProjection = new double [16]; gl.glGetDoublev( GL.GL_PROJECTION_MATRIX, adProjection, 0 ); double [] adObjectPos = new double [3]; GLU glu = new GLU(); if( !glu.gluUnProject( iX, glcanvas.getSize().y - iY, iZ, adModelView, 0, adProjection, 0, aiViewport, 0, adObjectPos, 0 ) ) log( "Couldn't map window coordinates to object coordinates." ); Where iX, iY are your mouse coordinates on screen, and iZ is just zero. Then just find the object that contains the point (adObjectPos[0], adObjectPos[1]). |
Administrator
|
In reply to this post by Daniel S.
Hi!
There is an example here: https://github.com/sgothel/jogl-demos/blob/master/src/demos/misc/Picking.java N.B: You might get strange results if you mix 2D and 3D by using different kinds of projection. You have to know that the use of OpenGL picking is not recommended according to the official OpenGL FAQ.
Julien Gouesse | Personal blog | Website
|
Hi folks,
thanks for your ints. Now I've got some points where to start. So I hope I can handle picking. Greetings, Daniel |
In reply to this post by gouessej
Hi,
I'm new in programming OpenGl with Jogl. I am trying to show a rectangle when the cursor of the mouse is around a sphere(in a specific area), and hide the rectangle when the cursor is not in that area, in fact doing something like this site: http://www.math.umn.edu/~rogness/multivar/partialderivs.shtml Thanks in advance, Sara |
Administrator
|
Hi
I already gave an example of picking here: https://github.com/sgothel/jogl-demos/blob/master/src/demos/misc/Picking.java Edit.: I think that software picking (no OpenGL picking) with bounding boxes is used in your example.
Julien Gouesse | Personal blog | Website
|
Thank you for your reply, but I do not know how to determine that the information about (hits, selectBuffer) is related to the rectangle or not for showing that. :(
|
Administrator
|
Please look at the manual of glSelectBuffer:
http://www.manpagez.com/man/3/glSelectBuffer/ Use glLoadName, glPushName and glPopName to manipulate the names used to recognize the candidates for the picking. You will find which objects are picked by looking for these names into the hits returned by gl.glRenderMode(GL2.GL_RENDER) after using the select mode. You can use gluProject to convert object coordinates into window coordinates, for example to draw a rectangle over the picked 3D object.
Julien Gouesse | Personal blog | Website
|
Thank you so much , yes I solved that by using gluProject :)
sara |
Hi,
unfortunately gluProject did not solve my problem when I zoom in or zoom out the window, So I wanna use picking code, but I do not know how to choose the values of glOrtho , I use the width and height but it did not work or other values did not work correctly ,this is my code? public void pick(GL gl) { int[] selectBuf = new int[BUFSIZE]; IntBuffer selectBuffer = BufferUtil.newIntBuffer(BUFSIZE); int hits; int viewport[] = new int[4]; gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0); gl.glSelectBuffer(BUFSIZE, selectBuffer); gl.glRenderMode(GL.GL_SELECT); gl.glInitNames(); gl.glPushName(-1); gl.glPushMatrix(); gl.glLoadIdentity(); glu.gluPickMatrix((double) pickPoint.x, (double) ( viewport[3] -pickPoint.y), 5.0, 5.0, viewport, 0); gl.glOrtho(0.0, wid, 0.0,hi, -0.5, 2.5); drawRects(gl, GL.GL_SELECT); gl.glPopMatrix(); gl.glFlush(); hits = gl.glRenderMode(GL.GL_RENDER); selectBuffer.get(selectBuf); processHits(hits, selectBuf); } I have to present this program in Friday, I really do not know how to solve? Thanks in advance, Sara |
Administrator
|
Hi
Use glOrtho when the perspective matrix is active and don't forget to select the model-view matrix before drawing the object. Ensure your object is in the viewing frustum or you won't see anything. Look at the man: http://www.manpagez.com/man/3/glOrtho/
Julien Gouesse | Personal blog | Website
|
Free forum by Nabble | Edit this page |