Android OpenGL10 Input Response <6>

Source: Internet
Author: User

OpenGL is a glsurfaceview, and the Glsurfaceview class is similar to view in many ways, it also has some treatment with view, such as the ability to respond to keyevent,motionevent events and so on, from the previous few, There are many cubes of graphics can be redrawn each time, set different positions and rotation angle, you can achieve the position of the graph changes, here can be touched by the event generated by the coordinates of the position, so that the graphics follow the direction of the touch movement or transformation.

The whole is not very difficult.

<1>: New Android Studio project, project named Pumpkinbasicinput, drawing the cube to draw on the previous article.

Main class:

Package Org.pumpkin.pumpkinbasicinput;import Android.app.activity;import Android.app.actionbar;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.Menu;import Android.view.menuitem;import Android.view.view;import Android.view.viewgroup;import Android.os.Build;public class Pumpkinmainactivity extends Activity {    @Override    protected void onCreate (Bundle savedinstancestate) {        Super.oncreate (savedinstancestate);        Inputsurfaceview inputsurfaceview=new Inputsurfaceview (this);        Setcontentview (inputsurfaceview/*r.layout.activity_pump_kin_main*/);    }}

Inherit the Glsurfaceview class:

Package Org.pumpkin.pumpkinbasicinput;import Android.content.context;import Android.opengl.glsurfaceview;import Android.view.keyevent;import android.view.motionevent;/** * Created by Administrator on 2016/5/6.    */public class Inputsurfaceview extends Glsurfaceview {private final float touch_scale_factor = 180.0f/320.0f;    private float Previousx;    private float previousy;    Private Inputrenderer renderer;        Public Inputsurfaceview (Context context) {super (context);        Renderer=new inputrenderer (context);        This.setrenderer (renderer);        This.requestfocus ();    This.setfocusableintouchmode (TRUE);  } @Override public Boolean onKeyDown (Int. keycode,keyevent event) {switch (event.getaction ()) {case                KeyEvent.KEYCODE_DPAD_LEFT:renderer.speedY-= 0.1f;            Break                Case KeyEvent.KEYCODE_DPAD_RIGHT:renderer.speedY + = 0.1f;            Break Case KEYEVENT.KEYCODE_DPAD_DOWN:renderer.speedX-= 0.1f;            Break                Case KeyEvent.KEYCODE_DPAD_UP:renderer.speedX + = 0.1f;            Break        Case KeyEvent.KEYCODE_DPAD_CENTER:break;    } return True;//super.onkeydown (keycode,event);        } @Override public boolean ontouchevent (Motionevent event) {Float CurrentX = Event.getx ();        float currenty = event.gety ();        float DeltaX, DeltaY;            Switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:break;            Case MotionEvent.ACTION_UP:break;                Case MotionEvent.ACTION_MOVE:deltaX = Currentx-previousx;                DeltaY = Currenty-previousy;                Renderer.anglex + = DeltaY * Touch_scale_factor;                Renderer.angley + = DeltaX * Touch_scale_factor;        Break        } Previousx = CurrentX;        Previousy = CurrentY; return true;Super.ontouchevent (event); }}

The following is the drawing of the cube:

Package Org.pumpkin.pumpkinbasicinput;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.opengl.glutils;import Java.nio.bytebuffer;import Java.nio.ByteOrder ; Import Java.nio.floatbuffer;import java.nio.intbuffer;import javax.microedition.khronos.opengles.gl10;/** * Created by Administrator on 2016/5/5.    */public class Multexturecube {private int[] textures;    Private bitmap[] Mbitmap=new bitmap[6];    Private Floatbuffer VertexBuffer;    Private Floatbuffer Texbuffer; Private float[] vertices={ -1.0f, -1.0f, 0.0f,//0. Left-bottom-front 1.0f, -1.0f, 0.0f,//1. Ri ght-bottom-front-1.0f, 1.0f, 0.0f,//2. Left-top-front 1.0f, 1.0f, 0.0f//3.    Right-top-front};            Float[] texcoords={0.0f, 1.0f,//A. Left-bottom (new) 1.0f, 1.0f,//B. Right-bottom (New) 0.0f, 0.0f,///C. Left-top (NEW) 1.0f, 0.0f//D. Right-top (NEW)};    Int[] Textureids=new int[1]; Public Multexturecube (Context context) {mbitmap[0] = Bitmapfactory.decoderesource (Context.getresources (), R.DRAWABL        E.GH);        MBITMAP[1] = Bitmapfactory.decoderesource (Context.getresources (), r.drawable.ghh);        MBITMAP[2] = Bitmapfactory.decoderesource (Context.getresources (), r.drawable.gtr);        MBITMAP[3] = Bitmapfactory.decoderesource (Context.getresources (), R.DRAWABLE.GHR);        MBITMAP[4] = Bitmapfactory.decoderesource (Context.getresources (), R.drawable.yel);        MBITMAP[5] = Bitmapfactory.decoderesource (Context.getresources (), R.DRAWABLE.HJ);        Bytebuffer Vbb=bytebuffer.allocatedirect (vertices.length*4);        Vbb.order (Byteorder.nativeorder ());        Vertexbuffer=vbb.asfloatbuffer ();        Vertexbuffer.put (vertices);        Vertexbuffer.position (0);        Bytebuffer Tbb=bytebuffer.allocatedirect (texcoords.length*4);        Tbb.order (Byteorder.nativeorder ());       Texbuffer=tbb.asfloatbuffer (); Texbuffer.put (texcoords);    Texbuffer.position (0);        } public void Loadtexture (GL10 gl) {Intbuffer textbuffer=intbuffer.allocate (6);        Gl.glgentextures (6,textbuffer);        Textures=textbuffer.array ();        Gl.glbindtexture (Gl10.gl_texture_2d,textures[0]);        Glutils.teximage2d (gl10.gl_texture_2d,0,mbitmap[0],0);        Gl.gltexparameterf (gl10.gl_texture_2d,gl10.gl_texture_mag_filter,gl10.gl_nearest);        Gl.gltexparameterf (gl10.gl_texture_2d,gl10.gl_texture_min_filter,gl10.gl_nearest);        Gl.glbindtexture (Gl10.gl_texture_2d,textures[1]);        Glutils.teximage2d (gl10.gl_texture_2d,0,mbitmap[1],0);        Gl.gltexparameterf (gl10.gl_texture_2d,gl10.gl_texture_mag_filter,gl10.gl_nearest);        Gl.gltexparameterf (gl10.gl_texture_2d,gl10.gl_texture_min_filter,gl10.gl_nearest);        Gl.glbindtexture (gl10.gl_texture_2d,textures[2]);        Glutils.teximage2d (gl10.gl_texture_2d,0,mbitmap[2],0); Gl.gltexparameterf (gl10.gl_texture_2d,gl10.gl_textuRe_mag_filter,gl10.gl_nearest);        Gl.gltexparameterf (gl10.gl_texture_2d,gl10.gl_texture_min_filter,gl10.gl_nearest);        Gl.glbindtexture (Gl10.gl_texture_2d,textures[3]);        Glutils.teximage2d (gl10.gl_texture_2d,0,mbitmap[3],0);        Gl.gltexparameterf (gl10.gl_texture_2d,gl10.gl_texture_mag_filter,gl10.gl_nearest);        Gl.gltexparameterf (gl10.gl_texture_2d,gl10.gl_texture_min_filter,gl10.gl_nearest);        Gl.glbindtexture (Gl10.gl_texture_2d,textures[4]);        Glutils.teximage2d (gl10.gl_texture_2d,0,mbitmap[4],0);        Gl.gltexparameterf (gl10.gl_texture_2d,gl10.gl_texture_mag_filter,gl10.gl_nearest);        Gl.gltexparameterf (gl10.gl_texture_2d,gl10.gl_texture_min_filter,gl10.gl_nearest);        Gl.glbindtexture (Gl10.gl_texture_2d,textures[5]);        Glutils.teximage2d (gl10.gl_texture_2d,0,mbitmap[5],0);        Gl.gltexparameterf (gl10.gl_texture_2d,gl10.gl_texture_mag_filter,gl10.gl_nearest); Gl.gltexparameterf (Gl10.gl_texture_2d,gl10.gl_texture_min_filter, gl10.gl_nearest);        } public void Draw (GL10 gl) {gl.glfrontface (GL10.GL_CCW);        Gl.glenable (Gl10.gl_cull_face);        Gl.glcullface (Gl10.gl_back);        Gl.glenableclientstate (Gl10.gl_vertex_array);        Gl.glvertexpointer (3,gl10.gl_float,0,vertexbuffer);        Gl.glenableclientstate (Gl10.gl_texture_coord_array);        Gl.gltexcoordpointer (2,gl10.gl_float,0,texbuffer);        Front Gl.glpushmatrix ();        Gl.gltranslatef (0.0f, 0.0f, 1.0f);        Gl.glbindtexture (Gl10.gl_texture_2d,textures[0]);        Gl.gldrawarrays (gl10.gl_triangle_strip,0,4);        Gl.glpopmatrix ();        Left Gl.glpushmatrix ();        Gl.glrotatef (270.0f,0.0f,1.0f,0.0f);        Gl.gltranslatef (0.0f,0.0f,1.0f);        Gl.glbindtexture (Gl10.gl_texture_2d,textures[1]);        Gl.gldrawarrays (gl10.gl_triangle_strip,0,4);        Gl.glpopmatrix ();        Back Gl.glpushmatrix ();        Gl.glrotatef (180.0f, 0.0f, 1.0f, 0.0f);  Gl.gltranslatef (0.0f, 0.0f, 1.0f);      Gl.glbindtexture (gl10.gl_texture_2d,textures[2]);        Gl.gldrawarrays (Gl10.gl_triangle_strip, 0, 4);        Gl.glpopmatrix ();        Right Gl.glpushmatrix ();        Gl.glrotatef (90.0f, 0.0f, 1.0f, 0.0f);        Gl.gltranslatef (0.0f, 0.0f, 1.0f);        Gl.glbindtexture (Gl10.gl_texture_2d,textures[3]);        Gl.gldrawarrays (Gl10.gl_triangle_strip, 0, 4);        Gl.glpopmatrix ();        Top Gl.glpushmatrix ();        Gl.glrotatef (270.0f, 1.0f, 0.0f, 0.0f);        Gl.gltranslatef (0.0f, 0.0f, 1.0f);        Gl.glbindtexture (Gl10.gl_texture_2d,textures[4]);        Gl.gldrawarrays (Gl10.gl_triangle_strip, 0, 4);        Gl.glpopmatrix ();        Bottom Gl.glpushmatrix ();        Gl.glrotatef (90.0f, 1.0f, 0.0f, 0.0f);        Gl.gltranslatef (0.0f, 0.0f, 1.0f);        Gl.glbindtexture (Gl10.gl_texture_2d,textures[5]);        Gl.gldrawarrays (Gl10.gl_triangle_strip, 0, 4);        Gl.glpopmatrix ();       Gl.gldisableclientstate (Gl10.gl_texture_coord_array); Gl.gldisableclientstate (Gl10.gl_vertex_array);    Gl.gldisable (Gl10.gl_cull_face); }}


Renderer:

Package Org.pumpkin.pumpkinbasicinput;import Android.content.context;import Android.opengl.glsurfaceview;import Android.opengl.glu;import Javax.microedition.khronos.egl.eglconfig;import javax.microedition.khronos.opengles.gl10;/** * Created by Administrator on 2016/5/6.    */public class Inputrenderer implements Glsurfaceview.renderer {private Context mcontext;    Private Multexturecube Multexturecube;    float AngleX = 0;    float Angley = 0;    float Speedx = 0;    float SpeedY = 0;    float z = -6.0f;        Public Inputrenderer (Context context) {Mcontext=context;    Multexturecube=new Multexturecube (context);        } @Override public void onsurfacecreated (GL10 gl, EGLConfig config) {gl.glclearcolor (0.0f,0.0f,0.0f,1.0f);        GL.GLCLEARDEPTHF (1.0f);        Gl.glenable (gl10.gl_depth_test);        Gl.gldepthfunc (gl10.gl_lequal);        Gl.glhint (gl10.gl_perspective_correction_hint,gl10.gl_nicest);        Gl.glshademodel (Gl10.gl_smooth); Gl.gldisable (GL10.GL_dither);        Multexturecube.loadtexture (GL);    Gl.glenable (gl10.gl_texture_2d); } @Override public void onsurfacechanged (GL10 gl, int width, int height) {if (height==0) {height=1        ;        } float aspect= (float) width/height;        Gl.glviewport (0,0,width,height);        Gl.glmatrixmode (gl10.gl_projection);        Gl.glloadidentity ();        Glu.gluperspective (GL, Aspect, 0.1f, 100.0f);        Gl.glmatrixmode (Gl10.gl_modelview);    Gl.glloadidentity (); } @Override public void Ondrawframe (GL10 gl) {gl.glclear (Gl10.gl_color_buffer_bit |        Gl10.gl_depth_buffer_bit);        Gl.glloadidentity ();        Gl.gltranslatef (0.0f, 0.0f, z);        Gl.glrotatef (AngleX, 1.0f, 0.0f, 0.0f);        Gl.glrotatef (Angley, 0.0f, 1.0f, 0.0f);        Multexturecube.draw (GL);        AngleX + = Speedx;    Angley + = SpeedY; }}


Also run, swipe on the screen to see the cube as it changes.

The result is no longer.












Android OpenGL10 Input Response <6>

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.