Reputation: 1254
I'm getting screen coordinates using this:
@Override
public boolean onTouchEvent(MotionEvent ev) {
x = ev.getX(0);
y = ev.getY(0);
return true;
}
And these are the verticles of my openGL 1.0 square:
private float vertices[] = {
-1.0f, -1.0f, 0.0f, // V1 - bottom left
-1.0f, 1.0f, 0.0f, // V2 - top left
1.0f, -1.0f, 0.0f, // V3 - bottom right
1.0f, 1.0f, 0.0f // V4 - top right
};
Everybody who have worked with openGL knows, that if i would paste x and y variables instead of verticles, i would get absolute nonsense. My question is: what formula should i use to convert screen coordinates x and y to world coordinates so i could use them to position my square to the touched point?
EDIT:
Oops, i forgot to say, that it's a 2D game...
Upvotes: 2
Views: 3573
Reputation: 1254
Actualy i found a way myself, and the glUnProject is not the best way on android platform... http://magicscrollsofcode.blogspot.com/2010/10/3d-picking-in-android.html
Upvotes: 3
Reputation: 153
There is a function called 'gluunproject', that can do this for you. Link is here.
http://www.opengl.org/sdk/docs/man/xhtml/gluUnProject.xml
By the way, the screen coordinates will correspond to a 3D line passing from center of camera through the screen coordinates (image plane).
The ModelView, projection and viewport inputs can be obtained by querying OpenGL the current matrices. Refer the same link (function calls are specified).
Other than the x and y screen parameters, you need the depth parameter or z parameter. You can use the depth range to place the square in a particular z plane. Or give a default value. But make sure it is inside the visible region.
Once you receive the object co-ordinates, consider it as the center of square and draw a square of required length.
Satish
Upvotes: 1