Glsurfaceview for gallery3d code analysis

Source: Internet
Author: User
Author:Liu PengDate:This article briefly introduces how to use glsurfaceview provided by Android SDK for OpenGL ES development.
Introduction

The android. OpenGL. glsurfaceview class in the SDK provides the following functions:

  • Establish a connection between OpenGL ES and the view system;
  • So that OpenGL ES can work in the activity lifecycle;
  • Select an appropriate frame buffer pixel format;
  • Create and manage a separate rendering thread to achieve smooth animation;
  • Debugging tools and APIs are provided.
A simple glsurfaceview Application
package com.example.android.apis.graphics;import javax.microedition.khronos.egl.EGLConfig;import javax.microedition.khronos.opengles.GL10;import android.app.Activity;import android.opengl.GLSurfaceView;import android.os.Bundle;public class ClearActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mGLView = new GLSurfaceView(this);        mGLView.setRenderer(new ClearRenderer());        setContentView(mGLView);    }    @Override    protected void onPause() {        super.onPause();        mGLView.onPause();    }    @Override    protected void onResume() {        super.onResume();        mGLView.onResume();    }    private GLSurfaceView mGLView;}class ClearRenderer implements GLSurfaceView.Renderer {    public void onSurfaceCreated(GL10 gl, EGLConfig config) {        // Do nothing special.    }    public void onSurfaceChanged(GL10 gl, int w, int h) {        gl.glViewport(0, 0, w, h);    }    public void onDrawFrame(GL10 gl) {        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    }}

The function of this program is very simple. The screen is set to black when each frame is drawn. But it is a complete OpenGL program that works in the activity lifecycle. When the activity is paused, It pauses rendering; when the activity continues, it continues rendering. This program can be used as a non-interactive demo program. You can add OpenGL calls to the clearrenderer. ondrawframe () interface to draw a lot.

The glsurfaceview. Render interface has three methods:

  • Onsurfacecreated (): This method is called before rendering starts. When the rendering context of OpenGL ES is rebuilt, it is also called. When the activity is paused, the rendering context is lost. When the activity continues, the painting context is rebuilt. In addition, it is often used to create long-standing OpenGL resources (such as texture.
  • Onsurfacechanged (): This method is called when the surface size changes. Usually set viewport here. If your camera is fixed, you can set it here.
  • Ondrawframe (): This method is used to draw each frame. Call the glclear function to clear the framebuffer before calling the interface of OpenGL ES.
How to handle Input

If you develop a role-based application (such as a game), you usually need to subclass glsurfaceview to obtain input events. The following is an example:

package com.google.android.ClearTest;import javax.microedition.khronos.egl.EGLConfig;import javax.microedition.khronos.opengles.GL10;import android.app.Activity;import android.content.Context;import android.opengl.GLSurfaceView;import android.os.Bundle;import android.view.MotionEvent;public class ClearActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mGLView = new ClearGLSurfaceView(this);        setContentView(mGLView);    }    @Override    protected void onPause() {        super.onPause();        mGLView.onPause();    }    @Override    protected void onResume() {        super.onResume();        mGLView.onResume();    }    private GLSurfaceView mGLView;}class ClearGLSurfaceView extends GLSurfaceView {    public ClearGLSurfaceView(Context context) {        super(context);        mRenderer = new ClearRenderer();        setRenderer(mRenderer);    }    public boolean onTouchEvent(final MotionEvent event) {        queueEvent(new Runnable(){            public void run() {                mRenderer.setColor(event.getX() / getWidth(),                        event.getY() / getHeight(), 1.0f);            }});            return true;        }        ClearRenderer mRenderer;}class ClearRenderer implements GLSurfaceView.Renderer {    public void onSurfaceCreated(GL10 gl, EGLConfig config) {        // Do nothing special.    }    public void onSurfaceChanged(GL10 gl, int w, int h) {        gl.glViewport(0, 0, w, h);    }    public void onDrawFrame(GL10 gl) {        gl.glClearColor(mRed, mGreen, mBlue, 1.0f);        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    }    public void setColor(float r, float g, float b) {        mRed = r;        mGreen = g;        mBlue = b;    }    private float mRed;    private float mGreen;    private float mBlue;}

This application clears the screen in each frame. When you tap a screen, the screen color is changed. Queueevent () is used in clearglsurfaceview. ontouchevent (). The queueevent () method is used for communication between the UI thread and the rendering thread. You can also use its Java thread communication technology, such as the synchronized method, but queueevent is the simplest thread communication method.

Other glsurfaceview examples

There are many examples in the android sdk api demo example program:

  • Glsurfaceview
  • Kube
  • Translucent glsurfaceview: transparent background
  • Textured triangle: texture
  • Sprite text: write text on Texture and display it in 3D scenarios
  • Touch rotate: rotate a 3D object
Select a surface

Glsurfaceview provides interfaces to select the surface type. By default, glsurfaceview uses a 16-bit RGB frame buffer with 16-bit depth. You can also choose based on your own needs. For example, in the translucent glsurfaceview example, an alpha channel is required to achieve transparency. Glsurfaceview provides the seteglsurfacechooser () method to select the surface.

Select a 16-bit framebuffer of RGB (565). The interface is as follows:

setEGLConfigChooser(boolean needDepth)

To customize red, green, blue, Alpha, and depth, use the following interface:

setEGLConfigChooser(int redSize, int greenSize,int blueSize, int alphaSize,int depthSize, int stencilSize)

Use your own eglconfigchooser and use the following interface:

setEGLConfigChooser(EGLConfigChooser configChooser)
Continuous rendering mode & notification rendering Mode

Most 3D applications, such as games and simulations, are continuous rendering animations, and some 3D applications are reactive. They often wait passively first, when the user has an action, then the response is made. Continuous screen rendering is a waste of time for this application. If you develop a reactive application, you can call the following method:

GLSurfaceView.setRenderMode(RENDERMODE_WHEN_DIRTY);

Stop continuous rendering. When

GLSurfaceView.requestRender()

The program then renders the screen.

Debugging

The glsurfaceview. setdebugflags () method can activate log or error detection, which can help debug OpenGL ES calls. In actual use, in the glsurfaceview constructor, call glsurfaceview. setdebugflags () before calling setrender. The following is an example:

public ClearGLSurfaceView(Context context) {    super(context);    // Turn on error-checking and logging    setDebugFlags(DEBUG_CHECK_GL_ERROR | DEBUG_LOG_GL_CALLS);    mRenderer = new ClearRenderer();    setRenderer(mRenderer);}
Reference
  • Introducing glsurfaceview

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //

Http://www.linuxgraphics.cn/android/gallery3d_glsurfaceview.html

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.