How to get coordinates of touch screen points in ogl?

3.4k views Asked by At

I am drawing map using OpenGL. I am getting map drawn after reading XML files and setting corresponding buffer. This map contains streets, highways and boundary. What i want is whenever i touch the map, the color of the specific layer should be changed.

The issue i am facing is this whenever i touch on the screen i am just getting the the screen pixel of the point where i touched. I want to convert this point into OpenGL coordinates so that i can match this point with the Map drawn and can highlight the selected point.

How to convert this point into OpenGL coordinates?

3

There are 3 answers

3
Sergey K. On BEST ANSWER

You need to unproject screen point into an OpenGL world space:

vec3 UnProjectPoint( const vec3& Point, const max4& Projection, const mat4& ModelView )
{
    vec4 R( Point, 1.0f );

    R.x = 2.0f * R.x - 1.0f;
    R.y = 2.0f * R.y - 1.0f;
    R.y = -R.y;

    R.z = 1.0f;

    R = Projection.GetInversed() * R;
    R = ModelView.GetInversed() * R;

    return R.ToVec3();
}
1
Anup Cowkur On

You can transform screen co-ords to opengl using a transform matrix and your camera position. See: https://stackoverflow.com/a/11716990/1369222

1
Amrish Kumar On

Better override onTouchEvent(MotionEvent e) of GLSurfaceView class and use the code below in the Renderer class in onSurfaceChanged(GL10 gl, int width, int height) method.

GLU.gluOrtho2D(gl,0,width,0,height);

The above code will map the screen coordinates to the openGL SurfaceView screen and you can put the points easily on the screen. But this will be only in the 2D view.