OpenglES2.0 for Android: A wave of various transformations

Source: Internet
Author: User

OpenglES2.0 for Android: Various transformations to a wave of listening screen events before making various transformations, let's look at the events of how to listen to the screen. The following transformations need to be shown in cubes, so we continue to use the contents of the previous section of the drawing cube to create a new project Opengeschange, and copy the code from the previous section about drawing the cube. In front we've been using Android.opengl.GLSurfaceView in the first article we already know the role of this class, in order to listen to screen events, we create a class that inherits from that class, overriding its Ontouchevent method. At this point the class code is as follows: (Mysurfaceview.java):
Package Com.cumt.opengeschange;import Com.cumt.render.myrender;import Android.content.context;import Android.opengl.glsurfaceview;import Android.view.motionevent;public class Mysurfaceview extends GLSurfaceView { Private Myrender Myrender;public Mysurfaceview (context context) {super (context);//TODO Auto-generated constructor Stubmyrender = new Myrender (context); this.seteglcontextclientversion (2); This.setrenderer (myrender);// Set rendering mode to render This.setrendermode actively (glsurfaceview.rendermode_continuously);} @Overridepublic boolean ontouchevent (Motionevent event) {//TODO auto-generated method Stubreturn super.ontouchevent ( event);}}

We can detect a variety of events in the Ontouchevent method we can also use Glsurfaceview's setontouchlistener to listen to the touch events of the view, the code is as follows (Mysurfaceview.java):
Package Com.cumt.opengeschange;import Com.cumt.render.myrender;import Android.content.context;import Android.opengl.glsurfaceview;import Android.view.motionevent;import Android.view.view;public class MySurfaceView Extends Glsurfaceview {private Myrender myrender;public Mysurfaceview (context context) {super (context);//TODO auto-generated constructor Stubmyrender = new Myrender (context); this.seteglcontextclientversion (2); this.setrenderer (myrender);//Set render mode as active render This.setrendermode (glsurfaceview.rendermode_continuously); This.setontouchlistener (new Ontouchlistener () {public boolean OnTouch (View V, motionevent event) {//TODO auto-generated method Stubreturn false;}}); }}


At this point the mainactivity also has a slight change (Mainactivity.java):
package Com.cumt.opengeschange;import Android.app.activity;import Android.content.pm.activityinfo;import Android.os.bundle;import Android.view.window;import Android.view.windowmanager;public class MainActivity extends Activity {private Mysurfaceview glsurfaceview; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate);//Set to Full screen requestwindowfeature (window.feature_no_title); GetWindow (). SetFlags ( Windowmanager.layoutparams.flag_fullscreen,windowmanager.layoutparams.flag_fullscreen);// Set to horizontal screen mode setrequestedorientation (activityinfo.screen_orientation_landscape); glsurfaceview = new MySurfaceView (this) ; Setcontentview (Glsurfaceview);} @Overrideprotected void OnPause () {//TODO auto-generated method Stubsuper.onpause (); Glsurfaceview.onpause ();} @Overrideprotected void Onresume () {//TODO auto-generated method Stubsuper.onresume (); Glsurfaceview.onresume ();}} 


At this point, our pre-work is OK.

The translation transformation first looks at the translation transformation matrix:
The three parameters in the matrix represent the displacements along the X-y,z axis, respectively.

The translation matrix M is multiplied by the vector of the current P-point to get the vector of the translated P ' point.
Now we want to do this on the basis of the original cube: Our cube moves to the left or right for a distance when the finger moves left or right on the screen. First we need to modify the code in the Matrixstate, create a new translation transformation matrix and initialize it to the unit matrix, then make a method call to set the pan, and finally modify our original Getfinalmatrix method to multiply the translation matrix in the method. The procedure is shown in the following code (MATRIXSTATE.JAVA):

Package Com.cumt.utils;import android.opengl.matrix;//Storage-system matrix State Class-public class Matrixstate {private static float[] Mprojmatrix = new float[16];//4x4 matrix storage projection matrix private static float[] Mvmatrix = new float[16];//camera position toward 9 parameter matrix/* First step: New Translation Transform Matrix */private static float[] Mtmatrix = new float[16];//translation transformation Matrix/* * Second step: Initialize the unit matrix */static{//to initialize the unit matrix MATRIX.SETIDENTITYM (MT Matrix, 0);} /* Third Step: Translation transform method common externally used */public static void translate (float x,float y,float z)//set to move along the XYZ axis {Matrix.translatem (Mtmatr    IX, 0, X, y, z); }//set camera public static void Setcamera (float CX,//Camera position xfloat CY,//Camera position yfloat CZ,//Camera position zfloat TX,//Camera target point xfloat t Y,//Camera target point yfloat TZ,//Camera target point zfloat upx,//Camera up vector x component float upy,//camera up vector y component float UPZ//camera up vector z component) {Matrix.setlooka TM (Mvmatrix, 0, CX, CY, CZ, TX, Ty, TZ, UPX, Upy, Upz);} Set perspective projection parameters public static void Setprojectfrustum (float left,//near face leftfloat right,//Rightfloat bottom on near face,//near Face Bottomfloat top,//near face topfloat,//face distance float FAR/FAR face distance) {Matrix.frustumm (Mprojmatrix, 0, left, right, bottom, top, near, far);} Get the total transformation matrix of a specific object static float[] Mmvpmatrix = new FLOAT[16];p ublic static float[] Getfinalmatrix () {/* * Fourth step: Multiply the translation transformation matrix */ma Trix.multiplymm (Mmvpmatrix, 0, Mvmatrix, 0, Mtmatrix, 0); Matrix.multiplymm (Mmvpmatrix, 0, Mprojmatrix, 0, Mmvpmatrix, 0); return mmvpmatrix;}}

Here we use the static code snippet to initialize the translation matrix to the unit matrix, if the user does not set the translation transformation matrix, it will not affect the original shape.
Then we listen to the user's screen movement event in Mysurfaceview, the code is as follows (Mysurfaceview.java):
Package Com.cumt.opengeschange;import Com.cumt.render.myrender;import Com.cumt.utils.matrixstate;import Android.content.context;import Android.opengl.glsurfaceview;import Android.view.motionevent;import Android.view.view;public class Mysurfaceview extends Glsurfaceview {private Myrender myrender;private float MPREVIOUSX ///Last touch position x coordinate public Mysurfaceview (context context) {super (context);//TODO auto-generated Constructor Stubmyrender = New Myrender (context); this.seteglcontextclientversion (2); This.setrenderer (myrender);// Set the render mode to render This.setrendermode actively (glsurfaceview.rendermode_continuously); This.setontouchlistener (new Ontouchlistener () {public boolean OnTouch (View V, motionevent event) {//TODO auto-generated method Stubfloat x = event.ge TX ();//current touch position x-coordinate switch (event.getaction ()) {Case motionevent.action_move://detects move event when float dx = x-mpreviousx;if (dx > 0) {matrixstate.translate (0.1f, 0, 0);} Else{matrixstate.translate ( -0.1f, 0, 0);}} Mpreviousx=x;return true;}});}}

Look at the effect of the operation:

Rotation transformation to see the rotation matrix:


Indicates that the point P is rotated around the vector Utheta degree. In OpenGL we usevoid Android.opengl.Matrix.rotateM (Float[] m,int Moffset,Float A,float x,Float y,Float z) method to set the rotation, the first parameter represents the returned rotation matrix, the moffset represents the offset, the general setting is 0, a is the angle, and X y z represents the z and y components of the rotation axis corresponding vector
Let's implement a feature that lets our cubes rotate 30 degrees along their Y-axis every time you click on the screen.
First, in our original Matrixstate class, a new method to set the rotation, the new code is as follows:
Rotation transform public static void rotate (float angle, float x, float y, float z) {//Set move around XYZ axis matrix.rotatem (mtmatrix, 0, Angle, x, Y, z);}

Then set the listener event and rotation in the Mysurfaceview class, at which point the Mysurfaceview.java code is as follows:
Package Com.cumt.opengeschange;import Com.cumt.render.myrender;import Com.cumt.utils.matrixstate;import Android.content.context;import Android.opengl.glsurfaceview;import Android.view.motionevent;import Android.view.view;public class Mysurfaceview extends Glsurfaceview {private Myrender myrender;public Mysurfaceview ( Context context) {super (context);//TODO auto-generated constructor stubmyrender = new Myrender (context); This.seteglcontextclientversion (2); This.setrenderer (myrender);//Set render mode for active rendering This.setrendermode ( glsurfaceview.rendermode_continuously) This.setontouchlistener (new Ontouchlistener () {public boolean onTouch (View V , Motionevent event) {//TODO auto-generated method Stubswitch (Event.getaction ()) {case motionevent.action_down:// Matrixstate.rotate (30, 0, 1, 0) when a click event is detected;} return true;}});}}

We run to look at the effect:

Scale Transformation Scaling matrix:

The three parameters in the above matrix represent the zoom rate in the direction of the X, Y, Z axis in the scaling transformation.
Let's do the following: Click on the screen to do a certain proportion of the zoom operation first add the following code in the Matrixstate.java:
Scale transform public static void scale (float x,float y,float z)    {    Matrix.scalem (mtmatrix,0, x, Y, z);    }

Then set the listener events and scaling in the Mysurfaceview class, at which point the Mysurfaceview.java code is as follows:

Package Com.cumt.opengeschange;import Com.cumt.render.myrender;import Com.cumt.utils.matrixstate;import Android.content.context;import Android.opengl.glsurfaceview;import Android.view.motionevent;import Android.view.view;public class Mysurfaceview extends Glsurfaceview {private Myrender myrender;public Mysurfaceview ( Context context) {super (context);//TODO auto-generated constructor stubmyrender = new Myrender (context); This.seteglcontextclientversion (2); This.setrenderer (myrender);//Set render mode for active rendering This.setrendermode ( glsurfaceview.rendermode_continuously) This.setontouchlistener (new Ontouchlistener () {public boolean onTouch (View V , Motionevent event) {//TODO auto-generated method Stubswitch (Event.getaction ()) {case motionevent.action_down:// Matrixstate.scale (0.4f, 1.5f, 0.6f) when a click event is detected,//xyz three directions scaled by their scaling factor}return true;}});}}


Operating effect:


Finally I wish you a happy Dragon boat Festival ~

OpenglES2.0 for Android: A wave of various transformations

Related Article

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.