开发者

iPhone OpenGL ES - How to Pick

开发者 https://www.devze.com 2022-12-24 07:44 出处:网络
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 i开发者_如何学Cnto my opengl

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 i开发者_如何学Cnto 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 ?


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.


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};
}


Could be possible to do this with GL_MODELVIEW_MATRIX? or GL_MODLVIEW, I´m not modifing the view position, only del modelview. I´m doing this:

            glMatrixMode(GL_MODELVIEW);

// Setup model view matrix
            glLoadIdentity();
            [gAccel GetAccelerometerMatrix:(GLfloat *) matrix headingX:headx headingY: heady headingZ:headz];

            // Finally load matrix
            glLoadMatrixf((GLfloat*)matrix);

            glPushMatrix();
            glRotatef(-90, 1, 0, 0);
            glScalef(0.0005, 0.0005, 0.0005);
            glTranslatef(x, 0.0, z);

thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号