Android OpenGL es Drawing Tutorial Six: Responding to touch events

Source: Internet
Author: User

making objects move according to a preset program, such as a rotating triangle, can draw attention. But what if you want users to interact with your OpenGL ES graphics? The key to making your OpenGL ES app Touch Interactive is to extend the Glsurfaceview, Ontouchevent () method to listen for touch events. This tutorial shows how to listen for a revealing event and let the user rotate an OpenGL ES object.
1. Set up a touch Listener
In order for your OpenGL ES app to respond to touch events, you must implement the Ontouchevent () method in the Glsurfaceview class, the following example shows how to listen for motionevent.action_move events, and convert them to the angle of rotation of the graph.
Private final float touch_scale_factor = 180.0f/320;
private float Mpreviousx;
private float mpreviousy;
@Override
public boolean ontouchevent (Motionevent e) {
Motionevent reports input details from the touch screen
and other input controls. In this case, you is only
Interested in events where the touch position changed.
float x = E.getx ();
Float y = e.gety ();
Switch (e.getaction ()) {
Case Motionevent.action_move:
float dx = x-mpreviousx;
float dy = y-mpreviousy;
Reverse direction of rotation above the mid-line
if (Y > GetHeight ()/2) {
DX = dx *-1;
}
Reverse direction of rotation to left of the mid-line
if (x < GetWidth ()/2) {
dy = dy *-1;
}
Mrenderer.setangle (
Mrenderer.getangle () +
(dx + dy) * touch_scale_factor));
Requestrender ();
}
Mpreviousx = x;
Mpreviousy = y;
return true;
}


Note that when the rotation angle is calculated, this method calls the Requestrender () method to tell renderer that the frame needs to be rendered, in this case the approach is optimal because the frame is redrawn only when the angle changes. However, when you set the rendering mode to Rendermode_when_dirty, you will be more efficient, so make sure that the line code in renderer is not commented out:
Public
Myglsurfaceview (context context) {
...
Render The view only if there is a change in the drawing data
Setrendermode (Glsurfaceview.rendermode_when_dirty);
}


2. Public rotation angle
The example code above requires you to expose the rotation angle parameter by adding a public member. Because the renderer code is running in a thread that is independent of the main threads, you must declare the parameter to be volatile:
Public
class Myglrenderer implements Glsurfaceview.renderer {
...
public volatile float mAngle;
public float Getangle () {
return mAngle;
}
public void Setangle (float angle) {
MAngle = angle;
}
}


3. Application angle
To apply the angle generated by the touch input, comment out the code that generated the dispatch and add the mangle generated by the touch input:
public void Ondrawframe (GL10 gl) {
...
float[] Scratch = new FLOAT[16];
Create a rotation for the triangle
Long time = Systemclock.uptimemillis ()% 4000L;
float angle = 0.090f * ((int) time);
Matrix.setrotatem (Mrotationmatrix, 0, mAngle, 0, 0, -1.0f);
Combine the rotation matrix with the projection and camera view
Note that the Mmvpmatrix factor *must is first* in order
For the matrix multiplication product to be correct.
Matrix.multiplymm (scratch, 0, Mmvpmatrix, 0, Mrotationmatrix, 0);
Draw Triangle
Mtriangle.draw (scratch);
}

When you have completed the steps described above and executed the code, you can drag the finger on the screen to rotate the triangle.


Original address: Http://developer.android.com/training/graphics/opengl/touch.html#angle

Android OpenGL es Drawing Tutorial Six: Responding to touch events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.