views:

568

answers:

2

I'm working on an OpenGL ES1 app which displays a 2D grid and allows user to navigate and scale/rotate it. I need to know the exact translation of View Touch coordinates into my opengl world and grid cell. Are there any helpers to do the reverse of last few transforms which I do for navigation ? or I should calculate and do the matrix stuff by hand ?

+2  A: 

Picking mode is not available in openGL ES. But it's easy to calculate the screen coordinate of any 3d point using the projection matrix retrieved from current openGL state. Here is how I do it (IMPoint2D and IMPoint3D are basic (x,y) and (x,y,z) structures)

+ (IMPoint2D) getScreenCoorOfPoint:(IMPoint3D)_point3D
{
    GLfloat     p[16];                                              // Where The 16 Doubles Of The Projection Matrix Are To Be Stored
    glGetFloatv(GL_PROJECTION_MATRIX, p);                           // Retrieve The Projection Matrix
/*
Multiply M * point
*/
    GLfloat _p[] = {p[0]*_point3D.x +p[4]*_point3D.y +p[8]*_point3D.z + p[12],
                    p[1]*_point3D.x +p[5]*_point3D.y +p[9]*_point3D.z + p[13],
                    p[2]*_point3D.x +p[6]*_point3D.y +p[10]*_point3D.z+ p[14],
                    p[3]*_point3D.x +p[7]*_point3D.y +p[11]*_point3D.z+ p[15]};
/*
divide by scale factor
*/
    IMPoint2D _p2D = {_p[0]/_p[3], _p[1]/_p[3]};
/*
get result in screen coordinates. In this case I'm in landscape mode
*/
    return (IMPoint2D) {_p2D.x*240.0f + 240.0f, (1.0f - _p2D.y) *160.0f};
}
Vincent Zgueb
+4  A: 

Sounds like what you're looking for is an unproject method. The glut library comes with one, but it's not available on OpenGL ES. It's possible to rewrite the glut code yourself to be iphone compatible You could also write your own code to unproject, which would involve needing to scale the touchpoint to your viewport, multiply it by the inverse of your projection*modelview matrices, and divide by w to get a 3D vector.

tjohns20