Android OpenGL ES basics: Draw triangles

Source: Internet
Author: User

OpenGL ES introduction OpenGL-ES is a cross-platform, fully functional API for 2D and 3D graphics applications free of license fees, it is mainly designed for a variety of embedded systems, including consoles, mobile phones, handheld devices, household appliances and automobiles. It consists of a well-defined subset of desktop OpenGL, creating a flexible and powerful underlying interaction interface between software and graphics acceleration. OpenGL ES includes the description of floating point operations and fixed point operations systems, as well as EGL's local Windows system specifications for portable devices. OpenGL ES 1.X is designed for fixed-function hardware and provides acceleration support, graphic quality, and performance standards. OpenGL ES 2.X provides a fully programmable 3D graphics algorithm, including the masking technology. OpenGL ES supports three types of basic geometric graphs: points, line segments, and triangles. That is to say, OpenGL ES can only draw these three basic geometric figures. Any complex 2D or 3D images are constructed from these three types of geometric images. In Android, SurfaceView is generally used to draw 2D images, while GLSurfaceView is used to display 3D OpenGL views, and 3D images are rendered through the Renderer interface. Therefore, we first establish a class that implements the Renderer interface. The Code has a detailed note: [java] public class GLRender implements Renderer {private float rotateTri; private int one = 0x10000; // a vertex of the triangle private IntBuffer triggerBuffer = BufferUtil. fBuffer (new int [] {0, one, 0, // top vertex-one,-one, 0, // left vertex one,-one, 0 // bottom right}); private IntBuffer colorBuffer = BufferUtil. fBuffer (new int [] {one, 0, 0, one, 0, 0, one, 0, 0, one, one}); public void onDrawFrame (GL10 gl) {// clear the screen and deep cache gl. glClear (GL10.GL _ COLOR_BUFFER_BIT | GL10.GL _ DEPTH_BUFFER_BIT); // reset the current model observation matrix gl. glLoadIdentity (); // move 1.5 units to the left and 6.0 gl to the screen. glTranslatef (-1.5f, 0.0f,-6.0f); // you can specify to rotate the gl. glRotatef (rotateTri, 0.0f, 1.0f, 0.0f); // sets the fixed-point array gl. glableclientstate (GL10.GL _ VERTEX_ARRAY); // sets the color array gl. glableclientstate (GL10.GL _ COLOR_ARRAY); gl. glColorPointer (4, GL10.GL _ FIXED, 0, colorBuffer); // sets the vertex gl. glVertexPointer (3, GL10.GL _ FIXED, 0, triggerBuffer); // draw a triangle gl. glDrawArrays (GL10.GL _ TRIANGLES, 0, 3); // cancel the vertex array gl. glDisableClientState (GL10.GL _ COLOR_ARRAY); // draw the end gl of the triangle. glFinish ();} public void onSurfaceChanged (GL10 gl, int width, int height) {float ratio = (float) width/height; // set the size of gl in OpenGL scenarios. glViewport (0, 0, width, height); // sets the gl projection matrix. glMatrixMode (GL10.GL _ PROJECTION); // reset the PROJECTION matrix gl. glLoadIdentity (); // you can specify the size of the gl. glFrustumf (-ratio, ratio,-1, 1, 1, 10); // select the model observation matrix gl. glMatrixMode (GL10.GL _ MODELVIEW); // resets the model observation matrix gl. glLoadIdentity ();} public void onSurfaceCreated (GL10 gl, EGLConfig arg1) {// enable shadow smoothing gl. glShadeModel (GL10.GL _ SMOOTH); // black background, silver-gray gl. glClearColor (, 1, 0); // sets the deep cache gl. glClearDepthf (1.0f); // enable the deep test gl. glable (GL10.GL _ DEPTH_TEST); // type of the deep test gl. glDepthFunc (GL10.GL _ LEQUAL); // tells the system to modify gl for pivoting. glHint (GL10.GL _ PERSPECTIVE_CORRECTION_HINT, GL10.GL _ FASTEST);} In MainActivity: [java] public class MainActivity extends Activity {Renderer render = new GLRender (); public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); GLSurfaceView gview = new GLSurfaceView (this); gview. setRenderer (render); setContentView (gview) ;}} if the vertex array reports an error: must user a native order idrect buffer, convert the array: [java] public static IntBuffer fBuffer (int [] a) {ByteBuffer mbb = ByteBuffer. allocateDirect (. length * 4); // nativeOrder mbb is used for Array arrangement. order (ByteOrder. nativeOrder (); floatBuffer = mbb. asIntBuffer (); floatBuffer. put (a); floatBuffer. position (0); return floatBuffer;} the running effect is as follows:

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.