The example of this article describes the method of implementing OpenGL ES texture mapping. Share it for everyone for your reference. The specifics are as follows:
1. Glrenderer.java file:
package network.obviam.opndl; Import Javax.MicroEdition.khronos.eglconfig; Import Javax.microedition.khronos.gl10; Import Android .content.context; Import Android.opngl.glu; Import Android.Opengl.glsurfaceView. Renderer; Public Class Glrender IMPLEMEREMENTS Renderer {Private Square Square; // The Square Private Context; /** Constructor to Set the Handed Ovetext */ Public Glrenderer (Context Context) {this.Context = context; // initialis the square this.square = New Square ();}} @Override Public Void ODRAWFRAME (GL10 GL) {// Clear Screen and DEPTH BUFFER GL.GLCLEAR (GL10.gL_BUFFER_BIT | GL10 .Gl_depth_buffer_bit); // reset the modelView Matrix Gl.glloadidentity ( ); // Drawing GL.GLTRANSLATEF (0.0F, 0.0F, -5.0F); // Move 5 Units Into the Screen // is the tas moving the carera 5 units away // GL.GLSCALEF (0.5F, 0. 5 f, 0.5F); // Scale the square to 50% // Otherwise It will be too large square.draw (GL); // Draw the triangle} @Override Public Void (GL1 0 GL, Int Width, int Height) {if (height == 0) {// Prevent a divide by Zero by Height = 1; // Making Height Equal One} GL.GLVIEWPORT (0, 0, Width, Height); ewport gl.glmatrixmode (GL10.gl_Projection); // Select The Project Matrix Gl.glloAdIdIdIntity (); // Reset the Project Matrix // Calculating the Aspect Ratio of the Window GL U.gluperSpective (GL, 45.0F, (Float) width / (float) Height, 0.1F, 100.0F); GL.GLMATRIXMODE (GL10.gl_modelview); // Select The Modelview Matrix Gl.glloadidedntity (); // Resise The ModelView Matrix} Override Public Void OverfaceCreated (GL10 GL, EglConfig Config) { // Load the texture for the square square.loadgltexture (GL, this.Context); GL.GLENABLE (GL10.gl_texture_2D); // Enable Texture Mapping (NewsHad Emodel (GL10.gl_smooth); // Enable Smoolh Shading GL.GLCLEARCOLOR (0.0F, 0.0F, 0.0F, 0.5F); // Black Background Gl.glCleardepthf (1.0F); // depth buffer setup gl.gLenable (GL10.gl_Depth_teest); // E. Nables depth testing gl. gldepthfunc (GL10.gl_Lequal); // The type of depth testing to do // Really Nice Perspective Calculations , Gl10.gl_nicest);}}2. Square.java file:
package network.obviam.opengl; Import Java.nio.Bytebuffer; Import Java.nio.Byteorder; Import Java.nio.floatbuffer; Import Onos.opengles.gl10; Import Android.Content.context; Import android.graphics .Bitmap; Import Android.graphics.BitmapFactory; Import Android.Opengl.glutils; Public Class Square {Private Floatbuffer Vertexbuffer; // BUFF Er Holding The Vertices Private Float Vertices [] = {-1.0F, -1.0F, 0.0F, / / / V1 -Bottom Left -1.0F, 1.0F, 0.0F, // V2 -TOP Left 1.0F, -1.0F, 0.0F, // V3 -Bottom Right 1.0F, 1.0F // v4 -TOP Right}; Private Floatbuffer TextureBuffer; // Buffer Holding The Texture Coordinates Private Float Texture [] = {// mapping coordinates for the vertices 0.0F, 1. 0F, // TOP Left (V2) 0.0F, 0.0F, // Bottom Left (v1) 1.0F, 1.0F, // Top Right (V4) 1.0F, 0.0F // Bottom Right (v3)}; / ** The texture pointr* / private int [] text Int [1] ; Public Square () {// A Float Has 4 bytes so we allocate for each coordinal 4 bytebuffer bytebuffer = bytebuffer.alLocatedirect (vertices.Length * 4) ; bytebuffer.Order (byteorder.nativeorder ()); // Allocates the Memory from the byte buffer vertexbuffer = bytebuffer.asfloatbuffer (); // Fill the vertexbuffer with the vertices vertexbuffer.put (vertices); // set the C Ursor posted to the beginning of the buffer vertexbuffer.position (0); bytebuffer = bytebuffer .Allocatedirect (texture.Length * 4); bytebuffer.order (byteorder.natives ()); Texturebuffer = bytebuffer.asfloatbuffer (); Ture); TextureBuffer.position (0);} /*** load the Texture for the Square * @Param GL * @Param Context */ Public Void Loadgltexture (GL10 GL, Context Context) {// Loading Texture bitmap = Bitmapfa ctory.decodeReSource (context.getResources (), r.Drawable.android); / / General One Texture Pointer Gl.glgentextures (1, Textures, 0); // ... and Bind it to our array gl.glbindTexture (gl10.gl_texture_2d, textures [0]); // Create arest filed texture gl.gltexparameterf (GL10.gl_texture_2D, GL10.gl_Texture_min_FILTER, GL10.gl_nearest); GL.GLTEXPARAMETERF (GL10.gl_texture_2D, GL10.gl_texture_mag_filter, GL10. GL_LINEAR); // Different Possible Texture Parameters, Eg GL10.gl_CLAMP_TO_EDGE // Gl.gltexparameterf (GL10. GL_Texture_2D, GL10.gl_Texture_wrap_S, GL10.gl_repeat); // GL.GLTEXPARAMETERF (GL10.gl_Texture_2D, GL10.gl_Texture_WRAP_T, GL10.gl_Repeat); // Use Android Glutils to Specify A Two-Dimensional Texture IMAGE from Our Bitmap Glutils.teximage2D ( GL10.gl_Texture_2D, 0, Bitmap, 0); // Clean up bitmap.recycle ();} / ** The Draw Method for the Square with the GL Context* / Public Void Draw (GL10 GL) {// bind the previously Generated Texture Gl.glbindTexture (GL10.gl_Texture_2D, Textures [0]); // Point to our buffers gl.glenableClientState (gl10.gl_vertex_array); GL.GLENABLEC liveState (gl10.gl_texture_coord_array); // set the face rotation gl.glfrontFace ( GL10.gl_CW); // Point to our vertex buffer gl.glvertexpointer (3, GL10.gl_float, 0, Vertexbuffer); GL.GLTEXCOORDPOINTER (2, GL10.gl_float, 0, TEXTUREBUFUF fer); // Draw the vertices as triangle Strip GL.GLDRAWARAYS (GL10.gl_triangle_Strip, 0, Vertices.length /3); // Disable the Client State Before Leaving ); ;gldisableClientState (gl10.gl_texture_coord_array);}}3. Triangle.java file:
package network.obviam.opengl; Import Java.nio.Bytebuffer; Import Java.nio.Byteorder; Import Java.nio.floatbuffer; Import Onos.opengles.gl10; Public Class Triangle {Private Floatbuffer Vertexbuffer; // Buffer Holding The Vertices Private Float Vertices [] = {-0.5F, -0.5F, 0.0F, // V1 -FIRST VERTEX (x, Y, Z) 0.5F, -0.5F, 0.0F, // V2 -Second Vertex 0.0F, 0.5F, 0.0F // V3 -Third Vertex // 1.0F, 0.5F, 0.0F // V3 -Third Vertex}; Public Triangle () {// A Float Has 4 by We AlLoch COORDINATE 4 bytes bytebuffer vertexbytebuffer = bytebuffer.allocatedirect (vertices.Length * 4); vertexbytebuffer.Order (byteorder.nativeRorder ()); He memory from the byte buffer vertexbuffer = vertexbytebuffer.asfloatbuffer (); // Fill the vertexbuffer with The vertices vertexbuffer.put (vertices); // set the cursor position to the beginning of the buffer vertexbuffer.positation (0);} /** the draw method for the Triangle with the GL Context */ Public Void Draw (GL10 GL ) {GL.GLENABLEClientState (GL10.gl_VERTEX_ARAY); // Set The Color for the Background // GL.GLCLERCOLOR (0.0F, 0.0F, 0.5F); // to show the color ( Paint the screen) we Need to clear the color buffer // gl.glclear (GL10.gl_COLOR_BUFFER_BIT); // set the color for the triangle gl.glcolor4f (0.0F, 1.0F, 0.5F); // Point to Our vertex buffer gl .glvertexpoIinter (3, GL10.gl_float, 0, Vertexbuffer); // Draw the vertices as triangle Strip Gl.gldrawarrays (GL10.gl_triangle_Strip, 0, Vertices.Length / / 3); // Disable The Client State Before Leaving Gl.gldisableClientState (GL10.gl_VERTEX_ARAY);}}4. Run.java file:
package network.obviam.opengl; Import android.app.Activity; Import Android.Opengl.glSurfaceView; Import Android.os.bundle; Import Android.vi ew.window; Import Android.View.windowManager; Public Class Run Extends Activity { /* *The OpenGL View */ Private GlsurfaceView GlsurfaceView;/ ** Called when the activity is first set Created. */ @Override Public Voice (Bundle SaVE dinstancestate) {Super.Oncreate (SaveDInstancestate); // Requesting to Turn the Title Office Window. Feature_no_TITLE); // Making It Full Screen GetWindow (). Setflags (WindowManager.Layoutparams.flag_FullScreen FullScreen); // Internation the Open Gl View and // Create An Instance with this action glsurfaceView = New GlsurfaceVieww (This); // Set Our Renderer to be the main renderer with // The Current Activity Context GLSURFACEVIEW.SetRenderer (this); SetContentView (GL SurfaceView);} / ** * Remember to resume the glsurface * / @ Override Protected Void Onresume () {Super.onResume (); GlsurfaceView.onResume ();} / ** * Also Pause the glsurface * / @Override Protected onpause () {super.onpause (); glsurfaceView.onpause (); }}It is hoped that this article is helpful to everyone's Java program design.