Clicking into a 3D world
BlitzMax Forums/OpenGL Module/Clicking into a 3D world
| ||
I need to know if there are any helper functions built into opengl which will give me the 3D location of my cursor (which obviously moves over the 'surface' of the 2D screen), so it is possible to interact with models using the mouse? I understand that I will be given a 3D vector, which I can use to figure out my result. Failing that, how exactly do you untangle openGL's projection system so that I can find this vector through hard math? Any tutorials would be most welcome. Thanks. |
| ||
GL has a `select` feature where you can get feedback about what is at a given location. Also you might look into first rendering a color-coded flat-shaded view of the scene, using it to pick the pixel color beneath the mouse, and then this ties in exactly with which polygon you clicked on ... then render the scene properly. You give a unique color to each polygon and then you can tell exactly which polygon you're clicking on. |
| ||
Or you can use gluUnproject, which is explained here: http://nehe.gamedev.net/data/articles/article.asp?article=13 |
| ||
Excellent, thankyou both. |
| ||
Here's the gluUnProject code translated into BlitzMax: ***** Method selectPiece(x, y) Print "selectPiece(" + x + "," + y + ")" Local viewport:Int[4] Local modelview:Double[16] Local projection:Double[16] Local winX:Float, winY:Float, winZ:Float Local posX:Double, posY:Double, posZ:Double glGetDoublev( GL_MODELVIEW_MATRIX, modelview ) glGetDoublev( GL_PROJECTION_MATRIX, projection ) glGetIntegerv( GL_VIEWPORT, viewport ) winX = Float( MouseX() ) winY = Float( viewport[3] - MouseY() ) glReadPixels( x, Int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, Varptr winZ ) gluUnProject( winX, winY, winZ, modelview, projection, viewport, Varptr posX, Varptr posY, Varptr posZ ) Print "You clicked on " + posX +","+ posY +","+ posZ End Method ***** I think I like the Selection Mode method better, though. This method requires you to figure out what model is located at that 3D point, while using Selection Mode can tell you what model (or group of polys, or whatever) was rendered at the location the user clicked on. |