Android OpenGL ES (eight) draws point points:

Source: Internet
Author: User

The previous article describes several basic geometries that OpenGL ES can draw: points, lines, triangles. Examples of these basic geometries will be presented separately. For the sake of convenience, these geometries are plotted on the same plane for the time being, and after the coordinate system and coordinate transformations of OpenGL ES are introduced later, a true 3D graphics rendering method is introduced.

In Android OpenGL ES (vi): Create instance Application the Opengldemos program framework creates a program framework for the sample application and provides a "Hello world" example.

To avoid some duplication of code, define a base class openglesactivity for all the sample code, which is defined as follows:

Package Com.example.drawpointer;import Javax.microedition.khronos.opengles.gl10;import Android.opengl.glsurfaceview;import Android.os.bundle;import Android.app.activity;import Android.view.Menu;import Android.view.window;import Android.view.WindowManager; Public classmainactivity extends activityimplements iopengldemo{/** Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); This. Requestwindowfeature (Window.feature_no_title); GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_ Fullscreen,windowmanager.layoutparams.flag_fullscreen); Mglsurfaceview=NewGlsurfaceview ( This); Mglsurfaceview.setrenderer (NewOpenglrenderer ( This) ); Setcontentview (Mglsurfaceview);} Public voidDrawscene (GL10 gl) {Gl.glclearcolor (0.0f,0.0f,0.0f,0.0f);//Clears the screen and depth buffer.Gl.glclear (Gl10.gl_color_buffer_bit|gl10.gl_depth_buffer_bit);} @Overrideprotected voidOnresume () {//ideally a game should implement Onresume () and OnPause ()//To take appropriate action when the activity looses focusSuper.onresume (); Mglsurfaceview.onresume ();} @Overrideprotected voidOnPause () {//ideally a game should implement Onresume () and OnPause ()//To take appropriate action when the activity looses focussuper.onpause (); Mglsurfaceview.onpause ();}protectedGlsurfaceview Mglsurfaceview;}
    • Create a Glsurfaceview Mglsurfaceview in the OnCreate method and set the screen to full screen. and set render for Mglsurfaceview.
    • Onresume, OnPause handles pauses and restores of Glsurfaceview.
    • Drawscene use Black to clear the screen.

OpenGL ES stores graphics data in Buffer with color, DEPTH (depth information), and so on, before drawing the graphic only generally need to empty the color and DEPTH buffer.

This example draws 3 dots on the screen using red. Create a drawpoint as a subclass of the openglesactivity and define the coordinates of the 3 vertices:

Package Com.example.drawpointer;import Java.nio.bytebuffer;import java.nio.byteorder;import Java.nio.floatbuffer;import Javax.microedition.khronos.opengles.gl10;import Android.os.Bundle; Public classDrawpoint extends mainactivityimplements iopengldemo{float[] Vertexarray =New float[]{-0.8f, -0.4f*1.732f,0.0f ,0.8f, -0.4f*1.732f,0.0f ,0.0f,0.4f*1.732f,0.0f ,};/** Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);} Public voidDrawscene (GL10 gl) {super.          Drawscene (GL); Bytebuffer VBB= Bytebuffer.allocatedirect (vertexarray.length*4);     Vbb.order (Byteorder.nativeorder ()); Floatbuffer Vertex=Vbb.asfloatbuffer ();     Vertex.put (Vertexarray); Vertex.position (0); GL.GLCOLOR4F (1.0f,0.0f,0.0f,1.0f);     Gl.glpointsize (8f);     Gl.glloadidentity (); Gl.gltranslatef (0,0, -4);          Gl.glenableclientstate (Gl10.gl_vertex_array); Gl.glvertexpointer (3, Gl10.gl_float,0, vertex); Gl.gldrawarrays (Gl10.gl_points,0,3);         Gl.gldisableclientstate (Gl10.gl_vertex_array); }}
  • The first is to use Floatbuffer to store three vertex coordinates.
  • Use glcolor4f (float red, float green, float blue, float alpha) to set the current color to red.
  • The glpointsize (float size) can be used to set the size of the drawing point.
  • Use Glenableclientstate to open pipeline Vectex vertex "switch"
  • Use Glvertexpointer to notify OpenGL ES graphics library vertex coordinates.
  • Use gl_points mode to draw 3 vertices using gldrawarrays.

Other Required Classes

Iopengldemo.java

Package Com.example.drawpointer;import javax.microedition.khronos.opengles.GL10;  Public Interface Iopengldemo {     publicvoid  drawscene (GL10 gl);         }

Openglrenderer.java

Package Com.example.drawpointer;import Javax.microedition.khronos.opengles.gl10;import Android.opengl.eglconfig;import Android.opengl.glsurfaceview.renderer;import Android.opengl.GLU; Public classOpenglrenderer implements Renderer {Privatefinal Iopengldemo Opengldemo;  PublicOpenglrenderer (Iopengldemo demo) {Opengldemo=demo; }           Public voidonsurfacecreated (GL10 gl, EGLConfig config) {//Set the background color to black (RGBA).Gl.glclearcolor (0.0f,0.0f,0.0f,0.5f); //Enable Smooth Shading, default not really needed.Gl.glshademodel (Gl10.gl_smooth); //Depth buffer setup.GL.GLCLEARDEPTHF (1.0f); //enables depth testing.gl.glenable (gl10.gl_depth_test); //The type of depth testing to do.Gl.gldepthfunc (gl10.gl_lequal); //really nice perspective calculations.Gl.glhint (Gl10.gl_perspective_correction_hint, gl10.gl_nicest); }           Public voidondrawframe (GL10 gl) {if(opengldemo!=NULL) {Opengldemo.drawscene (GL); }          }           Public voidOnsurfacechanged (GL10 GL,intWidthintheight) {     //sets the current view port to the new size.Gl.glviewport (0,0, width, height); //Select the projection matrixGl.glmatrixmode (gl10.gl_projection); //Reset the projection matrixgl.glloadidentity (); //Calculate The aspect ratio of the windowGlu.gluperspective (GL,45.0f,     (float) Width/(float) Height,0.1f,100.0f); //Select the Modelview matrixGl.glmatrixmode (Gl10.gl_modelview); //Reset the Modelview matrixgl.glloadidentity (); } @Override Public voidonsurfacecreated (GL10 arg0, Javax.microedition.khronos.egl.EGLConfig arg1) {//TODO auto-generated Method Stub            }    }

Final Result:

Android OpenGL ES (eight) draws point points:

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.