Android 3D learning: Drawing 2D triangles Using OpenGL ES

Source: Internet
Author: User

 

I am so ashamed to myself. I have been looking for a long time to use ndk to develop openggl es learning materials and I have not found any suitable materials (if I find that I cannot understand the English language, the program is too complicated and I cannot start with it), so I should first write it in Java, then I published an article about using ndk to accomplish the same function!

The methods in the program are translated and understood on the OpenGL es api. Please forgive me for not making it clear.

This time, a 2D triangle is drawn. Android provides a glsurfaceview framework inherited from surfaceview, which makes it easier to display OpenGL views. It contains a 3D rendering interface Renderer, there are three abstract methods described in the program. Check the activity code: Import android. app. activity; <br/> Import android. openGL. glsurfaceview; <br/> Import android. OS. bundle; <br/> public class helloopengl extends activity {</P> <p> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> // create a glsurfaceview class to manage OpenGL display <br/> glsurfaceview surface = new glsurfaceview (this ); <br/> // set the Renderer of glsurfaceview to add 3D rendering <br/> surface. setrenderer (New myrenderer (); <br/> setcontentview (surface); <br/>}< br/>}Next is the myrenderer class: Import Java. NIO. floatbuffer; <br/> Import javax. microedition. khronos. EGL. eglconfig; <br/> Import javax. microedition. khronos. opengles. gl10; <br/> Import android. openGL. glsurfaceview. renderer; <br/> public class myrenderer implements Renderer {<br/> // vertex array <br/> private floatbuffer vertexbuffer = floatbuffer. wrap (new float [] {<br/>-0.5f,-0.5f, 0f, // (x1, Y1, Z1) <br/> 0.5f,-0.5f, 0f, // (X2, Y2, Z2) <B R/> 0f, 0.5f, 0f // (X3, Y3, Z3) <br/> }); <br/> // Number of vertex coordinates <br/> private int sizeofvertex = 3; <br/> // call this method when the window is created <br/> Public void onsurfacecreated (gl10 GL, eglconfig config) {</P> <p >}</P> <p> // this method is called when the window size changes. <br/> Public void onsurfacechanged (gl10 GL, int width, int height) {<br/> // position, length, and width of the final display area on the screen <br/> GL. glviewport (0, 0, width, height ); <br/>}</P> <p> // call this method when drawing the current frame <br/> Public void ondrawframe (gl1 0 GL) {<br/> // enable vertex settings. You must disable the settings later. <br/> GL. glableclientstate (gl10.gl _ vertex_array ); </P> <p> // clear the screen to the color specified by glclearcolor before drawing. <br/>/* <br/> * The four parameters are red, green, blue, transparency <br/> * value: 0.0f ~ Between 1.0f <br/> */<br/> GL. glclearcolor (0.1f, 1.0f, 0.1f, 0.5f); <br/> GL. glclear (gl10.gl _ color_buffer_bit); <br/> // set the current color, that is, the color of the triangle. <br/> GL. glcolor4f (0.3f, 0.5f, 0.7f, 0.5f ); <br/> // define vertex coordinates <br/> // parameter 1 specify the number of coordinates of each vertex <br/> // parameter 2 specifies the Data Type of each vertex coordinate <br />/// parameter 3 specifies the offset byte between each consecutive vertex <br/> // parameter 4 is the vertex array <br/> GL. glvertexpointer (sizeofvertex, gl10.gl _ float, 0, vertexbuffer); <br/> // draw a prototype from the data <br/> // parameter 1 specifies the prototype to be drawn gl_tria .. triangle <br/> // parameter 2 specifies the start position in the array <br/> // parameter 3 specifies the number of coordinates of each vertex <br/> GL. gldrawarrays (gl10.gl _ triangles, 0, 3); <br/> // disable the vertex setting function <br/> GL. gldisableclientstate (gl10.gl _ vertex_array); <br/>}</P> <p>}Although we draw 2D images, note that we draw images in 3D space! See the figure. Note that the Z axis is 0 and the arrow direction of the coordinate axis is positive. now, let's take a look: After writing this program and blog, I am familiar with the OpenGL ES development process on Android. I hope it will help you as well. In the next blog, I will share with you the program that completes the same function on the ndk. This is today. Thank you!

 

 

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.