Examples of OpenGL rotating triangles

Source: Internet
Author: User

In Android, we use glsurfaceview to display OpenGL views. glsurfaceview is a very important class, which is located in Android. the OpenGL package is used to manage a special surface that can be the memory of a composite view robot system. Manage An EGL display that visualizes OpenGL. Allows a user to provide input render objects for display. A dedicated thread rendering interface is implemented from the UI thread to achieve 3D performance.So here we will first build our own Renderer class,

 
Public class helloopengles10renderer implements glsurfaceview. Renderer {}

In the helloopengles10renderer class, three Abstract methods of the Renderer interface must be implemented: ondrawframe, onsurfacechanged, onsurfacecreated

The following is an example:

First, we need to create a glsurfaceview:

 
Class helloopengles10surfaceview extends glsurfaceview {public helloopengles10surfaceview (context) {super (context); setrenderer (New helloopengles10renderer ());}}

@ Override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); glsurfaceview view = new helloopengles10surfaceview (this); setcontentview (View );}

Next, our core class helloopengles10renderer implements the Renderer interface:

Public class helloopengles10renderer implements glsurfaceview. renderer {float rotatetri, rotatequad; private floatbuffer trianglevb; // The four vertices of a square, private floatbuffer quatebuffer; /*** convert the float array and store it in the byte buffer array * @ Param arr * @ return */private void initquate () {float [] Four = {-0.5f,-0.5f, 0.5f, 0.5f,-0.5f, 0.5f,-0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f,}; bytebuffer vBB = bytebuffer. allocatedirect (// allocate buffer space. A float occupies 4 bytes of four. length * 4); vBB. order (byteorder. nativeorder (); // you can specify byteorder. nativeorder () is to obtain the local byte order quatebuffer = vBB. asfloatbuffer (); // convert to float quatebuffer. put (four); // Add data quatebuffer. position (0); // set the starting position of the array} private void initshapes () {float trianglecoords [] = {// X, Y, Z-0.5f,-0.25f, 0, 0.5f, -0.25f, 0, 0.0f, 0.559016994f, 0}; bytebuffer vBB = bytebuffer. allocatedirect (trianglecoords. length * 4); vBB. order (byteorder. nativeorder (); trianglevb = vBB. asfloatbuffer (); trianglevb. put (trianglecoords); trianglevb. position (0);} public void onsurfacecreated (gl10 GL, eglconfig config) {// set the background color GL. glclearcolor (0.5f, 0.5f, 0.5f, 1.0f); initshapes (); initquate (); GL. glableclientstate (gl10.gl _ vertex_array);} // all plotting operations are performed in this method. Public void ondrawframe (gl10 GL) {// clear the screen and depth cache GL. glclear (gl10.gl _ color_buffer_bit | gl10.gl _ depth_buffer_bit); // switch to the model observation matrix GL. glmatrixmode (gl10.gl _ modelview); // reset the current model observation matrix GL. glloadidentity (); // you can specify the view position. glulookat (GL, 0, 0,-5, 0f, 0f, 0f, 0f, 1.0f, 0.0f); // create a rotating triangle to rotate GL along the Y axis. glrotatef (rotatetri, 0.0f, 1.0f, 0.0f); // draw a triangle // set the current color GL. glcolor4f (0.61161875f, 0.76953125f, 0.22265625f, 0.0f); GL. glvertexpointer (3, gl10.gl _ float, 0, trianglevb); GL. gldrawarrays (gl10.gl _ triangles, 0, 3); // move 1 unit GL along the X axis square. gltranslatef (1.0f, 0.0f, 0.0f); // create a rotated square to rotate GL along the X axis. glrotatef (rotatequad, 1.0f, 0.0f, 0.0f); // draw a square GL. glcolor4f (0.61161875f, 0.76953125f, 0.22265625f, 0.0f); GL. glvertexpointer (3, gl10.gl _ float, 0, quatebuffer); GL. gldrawarrays (gl10.gl _ triangle_strip, 0, 4); // change the Rotation Angle rotatetri + = 0.5f; rotatequad-= 0.5f;} public void onsurfacechanged (gl10 GL, int width, int height) {// set the OpenGL scenario size GL. glviewport (0, 0, width, height); float ratio = (float) width/height; GL. glmatrixmode (gl10.gl _ projection); // sets the projection matrix GL. glloadidentity (); // sets the matrix as the unit matrix, which is equivalent to Resetting the matrix GL. glfrustumf (-ratio, ratio,-1, 1, 3, 7); // apply the projection matrix }}

The above methods are basically commented out, and I believe everyone can understand them,

Finally, let's take a look at the running effect!

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.