How to get physical coordinates in android -
i have drawn map on opengl want whenever user touches screen should coordinates relatvie opngl maps not screen coordinates.
following piece of code have tried not getting correct coordinates
// initialize auxiliary variables. pointf worldpos = new pointf(); // auxiliary matrix , vectors // deal ogl. float[] invertedmatrix, transformmatrix, normalizedinpoint, outpoint; invertedmatrix = new float[16]; transformmatrix = new float[16]; normalizedinpoint = new float[4]; outpoint = new float[4]; // invert y coordinate, android uses // top-left, , ogl bottom-left. int ogltouchy = (int) (scrheigth - touch.y);
/* transform screen point clip space in ogl (-1,1) */ normalizedinpoint[0] = (float) ((touch.x) * 2.0f / scrwidth - 1.0); normalizedinpoint[1] = (float) ((ogltouchy) * 2.0f / scrheigth - 1.0); normalizedinpoint[2] = - 1.0f; normalizedinpoint[3] = 1.0f;
/* obtain transform matrix , inverse. */
matrix.multiplymm( transformmatrix, 0, mprojmatrix, 0, mmvpmatrix, 0); matrix.invertm(invertedmatrix, 0, transformmatrix, 0);
/* apply inverse point in clip space */ matrix.multiplymv( outpoint, 0, invertedmatrix, 0, normalizedinpoint, 0);
if (outpoint[3] == 0.0) { // avoid /0 error. log.e("world coords", "error!"); return worldpos; }
for view override ontouch method. motionevent object use getx() , gety() methods
Comments
Post a Comment