Opengl es Android 3D finger Touch Rotate Object

Source: Internet
Author: User

Clear principle:

First make sure that the coordinates in OpenGL are: The x-axis is in the right direction for the screen, the y-axis is the screen upward, the z-axis is in the direction of the screen from the inside out.

When the finger moves in the x-axis direction, the object in the picture should rotate around the y-axis, or GL.GLROTATEF (angle,0,1,0);

When the finger moves in the y-axis direction, the object in the picture should rotate around the x-axis, or GL.GLROTATEF (angle,1,0,0);

In addition, the function used by the screen response gesture movement in Android is: Ontouchevent.

The following shows the code:

private final float touch_scale_factor = 180.0f/320;
renderer = new Newrender ();
Public Booleanontouchevent (motionevent e) {Switch(E.getaction ()) { CaseMotionEvent.ACTION_DOWN:ypos=e.gety (); Xpos=E.getx (); return true; CaseMotionevent.action_move:floatDY = e.gety ()-ypos;//Calculate the Pen y offset floatDX = E.getx ()-xpos;//Calculate pen x offsetRenderer.center.mAngleX = dy * touch_scale_factor;//set the angle of rotation along the x axisRenderer.center.mAngleY = dx * TOUCH_SCALE_FACTOR;//set rotation angle along Y axisRequestrender ();//Redraw the screen return true; } return true; }

Newrender () is a class that I inherited from renderer.

Renderer.center.mAngleX = dy * touch_scale_factor; // set the angle  of rotation along the x axis Renderer.center.mAngleY = dx * TOUCH_SCALE_FACTOR; // set rotation angle  along y axis

Center.manglex and Center.mangley refer to the angle of rotation of my object (center).

In center, my rotation angle is set to:

Gl.glrotatef (Manglex, 1, 0, 0); // Rotate   Gl.glrotatef (Mangley, 0, 1, 0);  

Opengl es Android 3D finger Touch Rotate Object

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.