I’m creating a modelling tool and need an easy way of selecting objects in my scene. Any input is welcomed. I’ve basically got two strategies.
a ) Using the selection buffer. This is tiresome as I need to name every triangle in my mesh ( I need to be able to select individual triangles ). From reading the red book I can’t put the glLoadName() between a glBegin(GL_TRIANGLES) and a glEnd() correct ? This leaves me two options:
1 ) name each mesh as a whole and allow selection on the mesh basis ( not enough detail in my selection ) .
2 ) use glLoadName(i) followed by a loop through all my triangles in the mesh and use glBegin(GL_TRIANGLE) instead.
I’m not liking either of these options. Particularly as I have 4 viewports in my window and in order to do the selection I need to figure out which one was clicked and construct my viewing matrix from that, converting mouse coordinates as I go. I’ve also had problems with this method as I’m using double-buffering with MFC and the rendering context needs to be current. Anyone seen this before ?
Second strategy is to use the drawing to the back buffer method and check the color under the mouse coordinates. This seems simpler and more efficient plus would give me the detail I need but I can’t get it to work.
Does anyone have an example of this on their site ? I’ve tried searching but to no avail. Again any help is appreciated.
Rock on !!!
I had an similar problem (just look for the last posting from me, includes source code). The selection was not usable because the name stack just handles 64 names. So I used the feedbackbuffer together with glPassthrough(float).
The previous source code needs some changes, so the feedbackbuffer must be large enough to store all coordinates of the drawn vertices. The Pickmatrix window is now 1x1 pixel large, and the checkresult function returns the token of the first coordinate after a token.
In my code I draw about 622207 vertices. The “selection” via feedback takes about 0,5 sec, but this is up to your original display routine - and my one is quite slow
Alright an update: Last night I tried to implement the selection algorithm described in the red book that uses color values and the back buffer. It wasn’t pleasant but it seems to work within an error of 4 triangles. Believe me I was amazed this worked even to this degree. In any case, I believe the problem now is the encoding of the primitive indeces in the colors. Either dithering is on ( it was too late last night to attempt with it explicitly off ),
Question: What’s the default on this ? on or off ? I’m in GL_RGB mode.
Either that or there’s a loss of precision somewhere in my r,g,b value generation. I’m using floats but I’m not sure what the glColor3f function does with very small decimal numbers
Question: if I send glColor3f(0.00001,0.0,0.0) to openGL will it chop off the .00001 off ?
very probable: since most color buffers hold 8 bits of precision, so 256 intensities for component, 0.00001*256 = 0.00256, wich is less than 0 will be rounded to 0.
i would use glColor3ub() to assign precise color values.
Alright, another tremendous night of coding over and here 's the update: I played around a lot with the code and took DMY’s suggestion of using glColor3ub(). It worked to some degree of success, please read on…
I found if my machine is set to display True Color then everything works fine.
If I’m in 65536 color mode then I start seeing triangles making selection mistakes ( every 8th triangle works, all others think they are the 8th !).
In 256 and 16 color modes, forget it. Nothing works properly.
This leads me to believe that I need a true color setup in order for this to work. Although that should have been clear if I’d taken the time to think. I’m using 255 possible values for each r,g,b component. That’s
255 * 255 * 255 = a whole lot more than I need.
I’d be happy with 16 * 16 *16 = 4096 entries.
My question now becomes: How can I figure out what color depth I need AND be sure I’m going to GET that depth regardless of the computer I’m running on ?