Android: GLSurfaceView: bitmap image and glViewport Adjustment

Source: Internet
Author: User

Android: GLSurfaceView: bitmap image and glViewport Adjustment

First, let's take a look at how GLSurfaceView is drawn. As described in the android development documentation, we need to create a new GLSurfaceView object and set an object that implements the Renderer interface. We need to write a MyRender class to implement the Renderer method. Let's take a look at the Activity writing method. It does not need a layout file. We can use the GLSurfaceView object as the contentview of the activity. As follows:

package net.mobctrl.glsurfaceviewbmp;import java.util.Random;import android.annotation.SuppressLint;import android.app.Activity;import android.opengl.GLSurfaceView;import android.os.Bundle;import android.view.WindowManager;/** *  * @author Zheng Haibo * @web http://www.mobctrl.net * */public class MainActivity extends Activity {private GLSurfaceView glView; // Use GLSurfaceViewRandom rnd = new Random();@SuppressLint("NewApi")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setTheme(android.R.style.Theme_Translucent_NoTitleBar);getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);glView = new GLSurfaceView(this); // Allocate a GLSurfaceViewglView.setRenderer(new MyGLRenderer(this)); // Use a custom rendererglView.getAlpha();this.setContentView(glView); }@Overrideprotected void onPause() {super.onPause();glView.onPause();}@Overrideprotected void onResume() {super.onResume();glView.onResume();android.provider.Settings.System.putInt(this.getContentResolver(),android.provider.Settings.System.SCREEN_BRIGHTNESS,rnd.nextInt(256));}}

Next, let's take a look at the MyGLRender class writing method, which implements the Renderer interface, as follows:

Package net. mobctrl. glsurfaceviewbmp; import java. util. random; import javax. microedition. khronos. opengles. GL10; import android. content. context; import android. opengl. GLSurfaceView. renderer; import android. opengl. GLU;/*** @ date 2:49:17 on January 1, October 20, 2014 * @ author Zheng Haibo * @ Description: TODO */public class MyGLRenderer implements Renderer {Context context; // Application's contextRandom r = new Random (); // private Square square; private GLBitmap glBitmap; private int width = 0; private int height = 0; private long frameSeq = 0; private int viewportOffset = 0; private int maxOffset = 400; public MyGLRenderer (Context context) {this. context = context; // square = new Square (); glBitmap = new GLBitmap () ;}@ Overridepublic void onSurfaceChanged (GL10 gl, int width, int height) {if (height = 0) {// Prevent A Divide By Zero Byheight = 1; // Making Height Equal One} this. width = width; this. height = height; gl. glViewport (0, 0, width, height); // Reset The // Current // Viewportgl. glMatrixMode (GL10.GL _ PROJECTION); // Select The Projection Matrixgl. glLoadIdentity (); // Reset The Projection Matrix // Calculate The Aspect Ratio Of The WindowGLU. gluPerspective (gl, 45.0f, (float) width/(float) height, 0.1f, 100366f); gl. glMatrixMode (GL10.GL _ MODELVIEW); // Select The Modelview Matrixgl. glLoadIdentity ();}/*** call once every 16 Ms */@ Overridepublic void onDrawFrame (GL10 gl) {gl. glClear (GL10.GL _ COLOR_BUFFER_BIT | GL10.GL _ DEPTH_BUFFER_BIT); // Reset the Modelview Matrixgl. glLoadIdentity (); gl. glTranslatef (0.0f, 0.0f,-5.0f); // move 5 units INTO the screen is // the same as moving the camera 5 // units away // square. draw (gl); glBitmap. draw (gl); changeGLViewport (gl);}/*** obtain ** @ param gl */private void changeGLViewport (GL10 gl) {System by changing the gl perspective. out. println ("time =" + System. currentTimeMillis (); frameSeq ++; viewportOffset ++; // The // Currentif (frameSeq % 100 = 0) {// reset gl at every 100 frames. glViewport (0, 0, width, height); viewportOffset = 0;} else {int k = 4; gl. glViewport (-maxOffset + viewportOffset * k,-maxOffset + viewportOffset * k, this. width-viewportOffset * 2 * k + maxOffset * 2, this. height-viewportOffset * 2 * k + maxOffset * 2); }}@ Overridepublic void onSurfaceCreated (GL10 gl, javax. microedition. khronos. egl. EGLConfig arg1) {glBitmap. loadGLTexture (gl, this. context); gl. glable (GL10.GL _ TEXTURE_2D); // Enable Texture Mapping (NEW) gl. glShadeModel (GL10.GL _ SMOOTH); // Enable Smooth Shadinggl. glClearColor (0.0f, 0.0f, 0.0f, 0.5f); // Black Backgroundgl. glClearDepthf (1.0f); // Depth Buffer Setupgl. glable (GL10.GL _ DEPTH_TEST); // Enables Depth Testinggl. glDepthFunc (GL10.GL _ LEQUAL); // The Type Of Depth Testing To Dogl. glHint (GL10.GL _ PERSPECTIVE_CORRECTION_HINT, GL10.GL _ NICEST );}}

Note: here we mainly implement the following three methods: onSurfaceCreated, onSurfaceChanged and onDrawFrame. The key is the onDrawFrame method. Its rendering mechanism is within 1 s, the onDrawFrame method is called 60 times every 16 Ms. Let's take a look at the glBitmap object. The changeGLViewport method will be available soon.

Package net. mobctrl. glsurfaceviewbmp; import java. nio. byteBuffer; import java. nio. byteOrder; import java. nio. floatBuffer; import javax. microedition. khronos. opengles. GL10; import net. mobctrl. glsurfaceviewbmp. r; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. opengl. GLUtils;/*** @ date 3:09:33 on April 9, October 20, 2014 * @ author Zheng Haibo * @ Description: TODO */public class GLBitmap {private FloatBuffer textureBuffer; // buffer holding the texture coordinatesprivate float texture [] = {// Mapping coordinates for the vertices0.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)}; private FloatBuffer vertexBuffer; // buffer holding the verticesprivate float vertices [] = {-1.0f,-1.0f, 0.0f, // V1-bottom left-1.0f, 1.0f, 0.0f, // V2-top left1.0f, -1.0f, 0.0f, // V3-bottom right1.0f, 1.0f, 0.0f // V4-top right}; public GLBitmap () {ByteBuffer byteBuffer = ByteBuffer. allocateDirect (vertices. length * 4); byteBuffer. order (ByteOrder. nativeOrder (); vertexBuffer = byteBuffer. asFloatBuffer (); vertexBuffer. put (vertices); vertexBuffer. position (0); byteBuffer = ByteBuffer. allocateDirect (texture. length * 4); byteBuffer. order (ByteOrder. nativeOrder (); textureBuffer = byteBuffer. asFloatBuffer (); textureBuffer. put (texture); textureBuffer. position (0);}/** The texture pointer */private int [] textures = new int [1]; public void loadGLTexture (GL10 gl, Context context) {// loading textureBitmap bitmap = BitmapFactory. decodeResource (context. getResources (), R. drawable. plane_image); // generate one texture pointergl. glGenTextures (1, textures, 0 );//... and bind it to our arraygl. glBindTexture (GL10.GL _ TEXTURE_2D, textures [0]); // create nearest filtered texturegl. 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); // Use Android GLUtils to specify a two-dimen1_texture image from // our bitmapGLUtils. texImage2D (GL10.GL _ TEXTURE_2D, 0, bitmap, 0); // Clean upbitmap. recycle ();} public void draw (GL10 gl) {// bind the previusly generated texturegl. glBindTexture (GL10.GL _ TEXTURE_2D, textures [0]); // Point to our buffersgl. glableclientstate (GL10.GL _ VERTEX_ARRAY); gl. glableclientstate (GL10.GL _ TEXTURE_COORD_ARRAY); // Set the face rotationgl. glFrontFace (GL10.GL _ CW); // Point to our vertex buffergl. glVertexPointer (3, GL10.GL _ FLOAT, 0, vertexBuffer); gl. glTexCoordPointer (2, GL10.GL _ FLOAT, 0, textureBuffer); // Draw the vertices as triangle stripgl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 0, vertices. length/3); // Disable the client state before leavinggl. glDisableClientState (GL10.GL _ VERTEX_ARRAY); gl. glDisableClientState (GL10.GL _ TEXTURE_COORD_ARRAY );}}


From GLBitmap, we can see that we first need to load an image resource and then generate texture. Here, we will particularly describe the changeGLViewport method. If this method is removed from MyRender, the running result is that GLSurfaceView draws an image. After this method is added, you must periodically draw an animation from far to near ". Note that this kind of animation does not change the image itself, and it is highly efficient because it cannot be adjusted from the perspective. Its principle is like taking a camera to take a picture on the wall. when the camera is getting closer and closer, the picture is magnified, and the wall itself is not changing.


-------------------------------------------------------------------

For more information, the Android Development Alliance's QQ group: 272209595


Related Article

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.