Opengl-es Android and TouchEvents
Right now, I am trying to create a simple app where a square follows where your finger touches as you move it along the screen. I would like to do this using opengl-es. I am fairly new to it and right now I am stuck on mapping the wind开发者_开发技巧ow coordinates reported by the touch event to the cartesian plane coordinate system that the opengl-es has. Right now, to get the x and y translation, I am using:
final int x = (e.getX()-(getWidth()/2))/(getWidth()/2) * SOME_SCALING_FACTOR;
final int y = ((getHeight()/2)-e.getY())/(getHeight()/2) * SOME_SCALING_FACTOR;
The logic behind this is that I think the cartesian plane is centered around the screen so I am trying to re-map my touch event coordinates in that way. However, my results are very inaccurate. Is there another way I should be doing this?
I wouldn't use a SOME_SCALING_FACTOR there. It's better to configure OpenGL for it.
You may use code below to configure camera, then do your drawing.
gl.glMatrixMode(gl.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluOrtho2D(gl, 0, WINDOWx, 0, WINDOWy);
gl.glMatrixMode(gl.GL_MODELVIEW);
gl.glLoadIdentity();
The lower left point of screen will be origin, upper left is (0,WINDOWy) etc..
精彩评论