View Full Version : picking with vertex arrays
01-18-2002, 01:21 AM
I am programming a nature simulator and in this simulator I am rendering a landscape.To do this I use Vertex arrays with triangle strips. Anyway, the problem is this: I need to be able to pick any of the vertexes with my mouse. How do I do this with vertex arrays. You need to give each vertex its own name, but since I am using vertex arrays this is a problem. Is it possible to put the names in an array and attach the names to the vertex array as I would attach say the colors of the vertexes?
Somebody please help...
I wouldn't try to use the GL selection mechanism for this one (I can see some of the frequent contributors laughing http://www.opengl.org/discussion_boards/ubb/wink.gif !).
Here is what I would do:
1) When the user moves the mouse, get X and Y from Windows and get the Z coordinate from the GL depth buffer (first problem: if nothing has been drawn at this X,Y, you don't have a Z coordinate ! See 3)...).
2) Use gluUnproject to get the 3D coordinates of the point. Now, find the closest vertex in your arrays, based upon distance.
3) If nothing has been drawn (i.e. you do not have a proper Z coordinate), project your vertices on the screen (using gluProject) and find the closest one to (X,Y).
Using this kind of algorithm brutally will probably result in very slow response. You might want to find some ways to result the number of vertices to test (there are specialists of BSP/Quadtrees/Octrees here who may help you but I don't know whether this techniques apply easily to landscapes).
You may think that the GL selection mechanism could be better but keep in mind that it is most probably not an HW-accelerated function...
Anyway, I am quite interested to see what the others can propose to help you.
[This message has been edited by Eric (edited 01-18-2002).]
01-18-2002, 01:53 AM
But my problem is using your technique with vertex arrays. Is it at all possible?
Kyllan, the technique I gave you is independant of the way you render the landscape.
I understand you are using the OpenGL Vertex Arrays mechanism to render but you still have a copy of your arrays in RAM (at least you should, when you call glVertexPointer !).
When I say "find the closest vertex", I mean find it in this array you have in RAM.
Does that make things clearer ? If it doesn't, please tell me where these explanations are unclear.
Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.