问题
So I have this board, to play a boardgame... The problem is, how do I know in which square did the user click on? I know the x and y coordinates, but how do I automate it, without the need to hunt down the specific locations of each square. (Also the size of the board is variable...so it can have lots of squares, or very few, but the board has always the same height and width, that is, the same number of squares running from left to right as the number of squares running from top to bottom).
Thank you so much stack overflow :)
回答1:
There are a lot of options offered at: http://www.opengl.org/resources/faq/technical/selection.htm
回答2:
The problem you're talking about is called "picking" and there are several solutions for that:
- occlusion query-based picking (quite modern, but I haven't tried it yet - there's some rant on that topic here),
- geometric picking (mathematics, and independent of OpenGL),
- color-based picking (a bit old and hackish, but very simple to implement).
I have previously described the last approach here:
OpenGL GL_SELECT or manual collision detection?
来源:https://stackoverflow.com/questions/4199358/how-would-you-solve-this-opengl-necessity-in-c-involving-knowing-in-which-squa