I have discussed some concepts of Android OpenGL. This section describes how to draw a triangle.
1. Create an android project named opengl_lesson2.
Ii. Modify opengl_lesson2.java Code as follows:
Package COM. android. tutor; <br/> Import android. app. activity; <br/> Import android. OS. bundle; <br/> public class opengl_lesson2 extends activity {</P> <p> private openglview mopenglview; <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> mopenglview = new openglview (this); <br/> setcontentview (mopenglview ); </P> <p >}< br/>}
3. Create a new class openglview. Java inheritance and glsurfaceview. The Code is as follows:
Package COM. android. tutor; <br/> Import android. content. context; <br/> Import android. openGL. glsurfaceview; <br/> public class openglview extends glsurfaceview {</P> <p> private openglrender mopenglrender; <br/> Public openglview (context) {<br/> super (context); <br/> mopenglrender = new openglrender (); <br/> setrenderer (mopenglrender ); <br/>}</P> <p >}< br/>
4. Create an openglrender. Java class inheritance and Renderer. The Code is as follows:
Package COM. android. tutor; <br/> Import Java. NIO. bytebuffer; <br/> Import Java. NIO. byteorder; <br/> Import Java. NIO. floatbuffer; <br/> Import Java. NIO. writable buffer; <br/> Import javax. microedition. khronos. EGL. eglconfig; <br/> Import javax. microedition. khronos. opengles. gl10; <br/> Import android. openGL. glsurfaceview. renderer; <br/> public class openglrender implements Renderer {</P> <p> private float _ Red = 0f; <br/> private float _ Green = 25f; <br/> private float _ Blue = 200f; </P> <p> private writable buffer _ indexbuffer; </P> <p> private floatbuffer _ vertexbuffer; </P> <p> private short [] _ indicesarray = {0, 1, 2}; <br/> private int _ nrofvertices = 3; <br/> @ override <br/> Public void onsurfacecreated (gl10 GL, eglconfig config) {</P> <p> GL. glableclientstate (gl10.gl _ vertex_array); <br/> inittriangle (); <br/>}< br/> @ override <br/> Public void onsurfacechanged (gl10 GL, int width, int height) {</P> <p> GL. glviewport (0, 0, width, height); <br/>}</P> <p> @ override <br/> Public void ondrawframe (gl10 GL) {<br/> GL. glclearcolor (_ Red, _ Green, _ Blue, 1.0f); </P> <p> GL. glclear (gl10.gl _ color_buffer_bit); </P> <p> GL. glcolor4f (0.5f, 0f, 0f, 0.5f); <br/> GL. glvertexpointer (_ nrofvertices, gl10.gl _ float, 0, _ vertexbuffer); <br/> GL. gldrawelements (gl10.gl _ triangles, _ nrofvertices, gl10.gl _ unsigned_short, _ indexbuffer); <br/>}</P> <p> private void inittriangle () {<br/> // float has 4 bytes <br/> bytebuffer vBB = bytebuffer. allocatedirect (_ nrofvertices * 3*4); <br/> vBB. order (byteorder. nativeorder (); <br/> _ vertexbuffer = vBB. asfloatbuffer (); </P> <p> // short has 4 bytes <br/> bytebuffer Ibb = bytebuffer. allocatedirect (_ nrofvertices * 2); <br/> Ibb. order (byteorder. nativeorder (); <br/> _ indexbuffer = Ibb. asshortbuffer (); </P> <p> float [] coords = {<br/>-0.5f,-0.5f, 0f, // (x1, Y1, Z1) <br/> 0.5f,-0.5f, 0f, // (X2, Y2, Z2) <br/> 0f, 0.5f, 0f // (X3, Y3, Z3) <br/>}; </P> <p> _ vertexbuffer. put (coords); </P> <p> _ indexbuffer. put (_ indicesarray); </P> <p> _ vertexbuffer. position (0); <br/> _ indexbuffer. position (0); <br/>}< br/>
5. The following figure shows the running effect:
I will not explain it in detail this evening! Add another day! Thank you ~