Step 1: Create a glrender class and implement the Renderer interface. At the same time, implement the three methods of the Renderer interface.
Step 2: Perform initialization in the onsurfacechanged () method body. The Code is as follows:
Public void onsurfacechanged (gl10 GL, int width, int height) {<br/> log. D (this. getclass (). getname (), "onsurfacechanged ......... "); <br/> float ratio = (float) width/height; <br/> // set the size of the OpenGL scenario <br/> GL. glviewport (0, 0, width, height); <br/> // sets the projection matrix <br/> GL. glmatrixmode (gl10.gl _ projection); <br/> // reset the projection matrix <br/> GL. glloadidentity (); <br/> // set the View Size <br/> GL. glfrustumf (-ratio, ratio,-1, 1, 1, 10); <br/> // select the model observation matrix <br/> GL. glmatrixmode (gl10.gl _ modelview); <br/> // reset the model observation matrix <br/> GL. glloadidentity (); <br/>}
Step 3: Describe the triangle and quadrilateral.
A triangle is composed of three vertices, And the OpenGL coordinate system is also three-dimensional. Therefore, each vertex coordinate is composed of three values: X, Y, and Z.
In this example, the image is drawn at the origin of the Z axis, so the 3rd values (Z axis) of all coordinates are 0.
Triangle code:
// Triangle three vertices <br/> private intbuffer triggerbuffer = intbuffer. wrap (New int [] {<br/> 0, one, 0, // top vertex <br/>-one,-one, 0, // lower left corner <br/> one,-one, 0,}); // lower right corner
Quadrilateral code:
// Four vertices of the square <br/> private intbuffer quaterbuffer = intbuffer. wrap (New int [] {<br/> One, one, 0, <br/>-One, one, 0, <br/> one,-one, 0, <br/>-one,-one, 0 });
Step 4: In this example, the left side of the mobile phone screen is a triangle and the right side is a quadrilateral. In this way, the coordinate origin is moved to the left when the triangle is drawn, and the coordinate origin is moved to the right when the quadrilateral is drawn.
The moving method is to use Gl. gltranslatef (1.5f, 0.0f,-6.0f) to extend the coordinate origin to 1.5 units to the right of the X axis. The Y axis does not understand, and the Z axis moves to 6 units on the screen.
Note that the origin point of each movement is the origin point of the three-dimensional coordinate system. Triangle and quadrilateral have their own coordinate system origins.
Step 5: Set vertices for both triangle and quadrilateral drawing. Gl. glableclientstate (gl10.gl _ vertex_array );
Step 6: before creating a polygon, you must specify the vertex and vertex information. As follows:
// Set the triangle <br/> GL. glvertexpointer (3, gl10.gl _ fixed, 0, triggerbuffer); <br/> // draw a triangle <br/> GL. gldrawarrays (gl10.gl _ triangles, 0, 3); </P> <p> // square <br/> // set and draw a square <br/> GL. glvertexpointer (3, gl10.gl _ fixed, 0, quaterbuffer); <br/> GL. gldrawarrays (gl10.gl _ triangle_strip, 0, 4 );
The
---- The third parameter represents the dimension of the coordinate system (this parameter is not the size of the coordinate array ). Since OpenGL is a three-dimensional coordinate system, this parameter value is 3.
---- 2nd parameters indicate the vertex type
---- The second parameter indicates the step size.
---- The first parameter indicates the vertex cache (that is, the two coordinate arrays defined in step 3)
Red text for reference: http://www.cocoachina.com/gamedev/openal/2010/0430/1286.html (thanks to Lotus lovenature for giving me a friendly tip)
Parameters:
Size: specifies the number of coordinates corresponding to each vertex. It can only be one of 2, 3, and 4. The default value is 4.
Type: Specifies the Data Type of each vertex coordinate in the array. The recommended constants include gl_byte, gl_short, gl_fixed, and gl_float;
Stride: Specifies the byte arrangement between consecutive vertices. If it is 0, the vertices in the array are considered to be in a compact arrangement. The default value is 0.
Pointer: Specifies the first address of the first vertex in the array. The default value is 0. for Android, you can leave it alone. Generally, you can use intbuffer.
When render starts, glvertexpointer uses an array to specify the coordinates of each vertex,
The first parameter: Size specifies the number of coordinates of each vertex.
The second parameter: Type specifies the Data Type of each coordinate. (Monk note: note that different data types have different meanings. If gl_fixed is selected, 0x10000 indicates the unit length, if gl_float is selected, 1.0f indicates the unit degree .)
The third parameter: stride specifies the data arrangement between a vertex and the next vertex, whether it is a "single array" or "distributed number group ", the storage of a single array is usually more efficient. After a vertex array is specified, size, type, stride, and pointer are stored as client-side.
When gldrawarrays or gldrawelements is called, if the vertex array is available, it will be used. You can use glenableclientstate or gldisableclientstate to activate or disable a vertex array, vertex arrays are unavailable by default and cannot be used by gldrawarrays and gldrawelements functions.
Finally, use the gldrawarrays method to draw the triangle and quadrilateral.
Complete all the code in steps and methods as follows:
Package COM. geolo. android. GL; <br/> Import Java. NIO. intbuffer; <br/> Import javax. microedition. khronos. EGL. eglconfig; <br/> Import javax. microedition. khronos. opengles. gl10; <br/> Import android. openGL. glsurfaceview. renderer; <br/> Import android. util. log; <br/> public class glrender implements Renderer {<br/> int one = 0x10000; </P> <p> // three triangle vertices <br/> private intbuffer triggerbuffer = intbuffer. wrap (New int [] {<br/> 0, one, 0, // top vertex <br/>-one,-one, 0, // bottom left <br/> one,-one, 0 ,}); // bottom right vertex <br/> // four vertices of the square <br/> private intbuffer quaterbuffer = intbuffer. wrap (New int [] {<br/> One, one, 0, <br/>-One, one, 0, <br/> one,-one, 0, <br/>-one,-one, 0}); </P> <p> Public void ondrawframe (gl10 GL) {<br/> log. D (this. getclass (). getname (), "ondrawframe ......... "); <br/> // clear the screen and depth cache <br/> GL. glclear (gl10.gl _ color_buffer_bit | gl10.gl _ depth_buffer_bit); </P> <p> // triangle <br/> // reset the current model observation matrix <br/> GL. glloadidentity (); <br/> // shifts 1.5 units to the left and 6.0 to the screen. <br/> GL. gltranslatef (-1.5f, 0.0f,-6.0f); <br/> // allows vertex Settings <br/> GL. glableclientstate (gl10.gl _ vertex_array); <br/> // set the triangle <br/> GL. glvertexpointer (3, gl10.gl _ fixed, 0, triggerbuffer); <br/> // draw a triangle <br/> GL. gldrawarrays (gl10.gl _ triangles, 0, 3); </P> <p> // square <br/> // reset the current model observation matrix <br/> GL. glloadidentity (); <br/> // shifts 1.5 units to the left and 6.0 to the screen. <br/> GL. gltranslatef (1.5f, 0.0f,-6.0f); <br/> // set and draw squares <br/> GL. glvertexpointer (3, gl10.gl _ fixed, 0, quaterbuffer); <br/> GL. gldrawarrays (gl10.gl _ triangle_strip, 0, 4); </P> <p> // cancel vertex Settings <br/> GL. gldisableclientstate (gl10.gl _ vertex_array); </P> <p >}< br/> Public void onsurfacechanged (gl10 GL, int width, int height) {<br/> log. D (this. getclass (). getname (), "onsurfacechanged ......... "); <br/> float ratio = (float) width/height; <br/> // set the size of the OpenGL scenario <br/> GL. glviewport (0, 0, width, height); <br/> // sets the projection matrix <br/> GL. glmatrixmode (gl10.gl _ projection); <br/> // reset the projection matrix <br/> GL. glloadidentity (); <br/> // set the View Size <br/> GL. glfrustumf (-ratio, ratio,-1, 1, 1, 10); <br/> // select the model observation matrix <br/> GL. glmatrixmode (gl10.gl _ modelview); <br/> // reset the model observation matrix <br/> GL. glloadidentity (); <br/>}< br/> Public void onsurfacecreated (gl10 GL, eglconfig config) {<br/> log. D (this. getclass (). getname (), "onsurfacecreated ......... "); <br/> // enable shadow smoothing <br/> GL. glshademodel (gl10.gl _ smooth); <br/> // black background <br/> GL. glclearcolor (0, 0, 0, 0); <br/> // set the deep cache <br/> GL. glcleardepthf (1.0f); <br/> // enable deep test <br/> GL. glable (gl10.gl _ depth_test); <br/> // type of the deep test <br/> GL. gldepthfunc (gl10.gl _ lequal); <br/> // notify the system to correct the perspective. <br/> GL. glhint (gl10.gl _ perspective_correction_hint, gl10.gl _ fastest); <br/>}< br/>
Package COM. geolo. android; <br/> Import COM. geolo. android. GL. glrender; <br/> Import android. app. activity; <br/> Import android. openGL. glsurfaceview; <br/> Import android. openGL. glsurfaceview. renderer; <br/> Import android. OS. bundle; <br/> public class androidopendgl extends activity {<br/> Renderer mrenderer = new glrender (); <br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> glsurfaceview mglsurfaceview = new glsurfaceview (this); <br/> mglsurfaceview. setrenderer (mrenderer); <br/> setcontentview (mglsurfaceview); <br/>}< br/>}