Android OpenGL Getting Started sample

Source: Internet
Author: User

The support for OpenGL on Android is seamless, so there are a lot of 3D effects so realistic game, in some of the camera process is also useful to glsurfaceview situation. This article records OpenGL's entry-level example on Android, drawing a triangle and a square. Although the function is simple, but I churn several nights, a large number of online articles on the code is a bit of a problem, not to be drawn out is hanging.

First file: Mainactivity.java

Package Com.example.opentest;import Android.opengl.glsurfaceview;import android.os.bundle;import Android.app.activity;import Android.view.Menu; Public classMainactivity extends Activity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Glsurfaceview Glsurfaceview=NewGlsurfaceview ( This); Glsurfaceview.setrenderer (NewOpenglrender ());        Setcontentview (Glsurfaceview); //Setcontentview (r.layout.activity_main);} @Override Publicboolean Oncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}

Second file: Bufferutil.java is responsible for turning the array into buffer

Package Com.example.opentest;import java.nio.bytebuffer;import java.nio.byteorder;import java.nio.FloatBuffer; Import Java.nio.IntBuffer; Public classBufferutil { Public StaticFloatbuffer Mbuffer;  Public StaticFloatbuffer Floattobuffer (float[] a) {        //first initialize buffer, the length of the array, because a float takes up 4 bytesBytebuffer MBB = Bytebuffer.allocatedirect (a.length*4); //array sorting with NativeorderMbb.order (Byteorder.nativeorder ()); Mbuffer=Mbb.asfloatbuffer ();        Mbuffer.put (a); Mbuffer.position (0); returnMbuffer; }         Public StaticIntbuffer Inttobuffer (int[] a)        {Intbuffer intbuffer; //first initialize buffer, the length of the array, because a float takes up 4 bytesBytebuffer MBB = Bytebuffer.allocatedirect (a.length*4); //array sorting with NativeorderMbb.order (Byteorder.nativeorder ()); Intbuffer=Mbb.asintbuffer ();        Intbuffer.put (a); Intbuffer.position (0); returnIntbuffer; }}

Third file: Openglrender.java This is the most core, responsible for the matching glsurfaceview to draw things

Package Com.example.opentest;import Java.nio.floatbuffer;import javax.microedition.khronos.egl.EGLConfig; Import Javax.microedition.khronos.opengles.gl10;import android.opengl.GLSurfaceView.Renderer; Public classOpenglrender implements Renderer {Private float[] Mtrianglearray ={0f,1f,0f,-1f,-1f,0f, 1f,-1f,0f}; PrivateFloatbuffer Mtrianglebuffer; Private float[] mcolorarray={1f,0f,0f,1f,//Red0F,1F,0F,1F,//Green0f,0f,1f,1f//Blue    }; PrivateFloatbuffer Mcolorbuffer; //four vertices of a square    PrivateFloatbuffer Quatebuffer; Private float[] Mquatearray = {            -1F,-1f, 0f, 1f,-1f, 0f,-1f, 1f, 0f, 1f, 1f, 0f,}; @Override Public voidondrawframe (GL10 gl) {//TODO auto-generated Method StubGl.glclear (gl10.gl_color_buffer_bit|gl10.gl_depth_buffer_bit); //using arrays as colorsGl.glcolorpointer (4, Gl10.gl_float,0, Mcolorbuffer); //draw a small trianglegl.glloadidentity (); Gl.gltranslatef (-1.5f,0.0f, -6.0f); Gl.glvertexpointer (3, Gl10.gl_float,0, Mtrianglebuffer);//array points to triangle vertex bufferGl.gldrawarrays (Gl10.gl_triangles,0,3);//gl.gldisableclientstate (gl10.gl_color_array);Gl.glfinish (); //Draw a squaregl.glloadidentity (); Gl.gltranslatef (1.5f,0.0f, -6.0f);//gl.glcolor4f (1.0f, 0.0f, 0.0f, 1.0f);Gl.glvertexpointer (3, Gl10.gl_float,0, Quatebuffer); Gl.gldrawarrays (Gl10.gl_triangle_strip,0,4);    Gl.glfinish (); } @Override Public voidOnsurfacechanged (GL10 GL,intWinth) {//TODO auto-generated Method StubGl.glviewport (0,0, W, h); floatRatio = (float) w/h;        Gl.glmatrixmode (gl10.gl_projection);        Gl.glloadidentity (); GL.GLFRUSTUMF (-ratio, ratio,-1,1,1,Ten);        Gl.glmatrixmode (Gl10.gl_modelview);    Gl.glloadidentity (); } @Override Public voidonsurfacecreated (GL10 gl, EGLConfig config) {//TODO auto-generated Method StubGl.glshademodel (Gl10.gl_smooth); Gl.glclearcolor (1.0f,1.0f,1.0f, 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.glenableclientstate (Gl10.gl_vertex_array);        Gl.glenableclientstate (Gl10.gl_color_array); Mtrianglebuffer=Bufferutil.floattobuffer (Mtrianglearray); Mcolorbuffer=Bufferutil.floattobuffer (Mcolorarray); Quatebuffer=Bufferutil.floattobuffer (Mquatearray); }}

Development Highlights:

1, Glsurfaceview can be directly new, can also be placed in the layout, this example is the first method.

2, a glsurfaceview to support a renderer, this renderer is an interface, there are three functions. This is like Surfaceview. In particular, ondrawframe () can be likened to the OnDraw () function of the view in Android.

3. Draw the theme in the Ondrawframe () function, use the following code to draw the triangle:

 //  draw a small triangle  -1.5f , 0.0f ,-6.0f   3 , Gl10.gl_float , 0 , Mtrianglebuffer); //  buffergl.gldrawarrays (gl10.gl_triangles, 0 , );  //  Gl.gldisableclientstate (Gl10.gl_color_array);  Gl.glfinish (); 

It is important to note that in some tutorials it is written that you want to gl.gldisableclientstate (Gl10.gl_vertex_array) When you finish drawing, and it is a mistake to clear the vertices you set. once this sentence is called, then what painting does not come out!!!

Then draw the square:

// Draw a square gl.glloadidentity (); Gl.gltranslatef (1.5f0.0f,-6.0f); // gl.glcolor4f (1.0f, 0.0f, 0.0f, 1.0f); Gl.glvertexpointer (3004); Gl.glfinish ();

Note that the Color:gl.glColorPointer (4, gl10.gl_float, 0, Mcolorbuffer) have been loaded before the triangle was drawn;

If you call Gl.gldisableclientstate (Gl10.gl_color_array) after drawing, you can see that the triangles are flashing and the squares are not visible . This piece is also a misunderstanding! If you follow the gl.glcolor4f (1.0f, 0.0f, 0.0f, 1.0f), and then specify the color again, you can see that the triangles and squares are drawn in this color. All in all, this ondrawframe () and the View OnDraw () are very similar, in the OnDraw do not give paint color, just draw. or after the painting, but also to the color set to transparent, the result must be nothing to see. Do not understand why so many tutorials on the drawing of the small triangle after not to take:

Gl.gldisableclientstate (Gl10.gl_vertex_array);

Gl.gldisableclientstate (Gl10.gl_color_array);

These two sentences bug!!!

The process involved in the code itself is not explained, the reference link is very clear.

4, if the square four vertex coordinate order is replaced, the drawing will not be a square.

SOURCE Link: http://download.csdn.net/detail/yanzi1225627/7484793

Reference: Link 1 Link 2

As shown below:


Android OpenGL Getting Started sample

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.