Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

c++ - OpenGL - Picking (fastest way)

I would like to implement scene picking (for mouse clicks, moves, etc.). What is the best, fastest way? I used to use glSelectBuffer and glRenderMode with GL_SELECT and then render whole scene again (of course with boost, without textures, heavy shaders, etc.).

It's too slow render recursively all nodes and geometries for one number (geometry ID) on every mouse click, move and drag. Does anybody know better way?

EDIT: Solution with reading depth buffer seems fain, but what if I don't want to pick some geometries and they are already drown to buffer? Finding box which contains point can be imprecisely, since point can be in multiple boxes.

Is it possible to write into two frame buffers, depth buffers in one draw? Then I can write geometry id to one buffer through shader. And what about speed?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the past I've used glReadPixels( xf, yf, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &zf); to read the value of the depth buffer at a point (xf, yf) in screen space. You can then unproject this point back into world coordinates (multiply by the inverse of the modelview and projection matrices. This can be done with gluUnProject provided you're happy including the GLU library in your application.

This gives you a coordinate, you then need to search through your objects to find one that has a bounding box that contains this coordinate. This object is the one selected.

I'd recommend searching for information on Unprojecting if you want more information about this, also https://www.opengl.org/archives/resources/faq/technical/selection.htm is helpful (and suggests other techniques you could use).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...