Android OpenGL ES concise development tutorial 02

Source: Internet
Author: User

It is very simple to construct an OpenGL view on the andorid platform. There are two main tasks:

Glsurfaceview

The OpenGL es api provided by the Android platform is mainly defined in the package android. openGL, javax. microedition. khronos. EGL, javax. microedition. khronos. opengles, Java. among several packages such as NiO, glsurfaceview is the core class in these packages:

  • It serves as a bridge between OpenGL ES and Android's view hierarchy.
  • Makes the open gl es library suitable for the activity lifecycle of the anndroid system.
  • This makes it easy to select the appropriate frame buffer pixel format.
  • Create and manage independent drawing threads for smooth animation.
  • It provides easy-to-use debugging tools to track OpenGL ES function calls to help check errors.

Therefore, the starting point for compiling OpenGL ES applications is from glsurfaceview. To set glsurfaceview, you only need to call a method to set glsurfaceview. Renderer used by openglview.

public void  setRenderer(GLSurfaceView.Renderer renderer)

Glsurfaceview. Renderer

Glsurfaceview. Renderer defines an interface for unified graphic rendering. It defines the following three interface functions:

// Called when the surface is created or recreated.public void onSurfaceCreated(GL10 gl, EGLConfig config)// Called to draw the current frame.public void onDrawFrame(GL10 gl)// Called when the surface changed size.public void onSurfaceChanged(GL10 gl, int width, int height)
  • Onsurfacecreated: This method is mainly used to set parameters that do not change frequently during painting, such as background color and whether to enable Z-buffer.
  • Ondrawframe: defines the Actual Drawing operation.
  • Onsurfacechanged: if the device supports horizontal and vertical screen switching, this method will occur in vertical swapping. In this case, you can reset the aspect ratio.

With the basic definition above, you can write out a general framework of OpenGL ES applications.

Create a new Android project: openglestutorial. Add two classes in the project: tutorialparti. Java and openglrenderer. java.

The Code is as follows:

Tutorialparti. Java

public class TutorialPartI extends Activity { // Called when the activity is first created.  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); // (NEW) getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // (NEW) GLSurfaceView view = new GLSurfaceView(this); view.setRenderer(new OpenGLRenderer()); setContentView(view); }}

Openglrenderer. Java

public class OpenGLRenderer implements Renderer { public void onSurfaceCreated(GL10 gl, EGLConfig config) { // Set the background color to black ( rgba ). gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);  // OpenGL docs. // Enable Smooth Shading, default not really needed. gl.glShadeModel(GL10.GL_SMOOTH);// OpenGL docs. // Depth buffer setup. gl.glClearDepthf(1.0f);// OpenGL docs. // Enables depth testing. gl.glEnable(GL10.GL_DEPTH_TEST);// OpenGL docs. // The type of depth testing to do. gl.glDepthFunc(GL10.GL_LEQUAL);// OpenGL docs. // Really nice perspective calculations. gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, // OpenGL docs. GL10.GL_NICEST); } public void onDrawFrame(GL10 gl) { // Clears the screen and depth buffer. gl.glClear(GL10.GL_COLOR_BUFFER_BIT | // OpenGL docs. GL10.GL_DEPTH_BUFFER_BIT); } public void onSurfaceChanged(GL10 gl, int width, int height) { // Sets the current view port to the new size. gl.glViewport(0, 0, width, height);// OpenGL docs. // Select the projection matrix gl.glMatrixMode(GL10.GL_PROJECTION);// OpenGL docs. // Reset the projection matrix gl.glLoadIdentity();// OpenGL docs. // Calculate the aspect ratio of the window GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f); // Select the modelview matrix gl.glMatrixMode(GL10.GL_MODELVIEW);// OpenGL docs. // Reset the modelview matrix gl.glLoadIdentity();// OpenGL docs. }}

After compilation, the screen displays a black full screen. These two Classes define the most basic classes and methods of the android OpenGL ES application. They can be seen as the "Hello, world" Application of OpenGL ES, we will gradually enrich this example to draw a 3D image later.

Download framework code: it can be used as your own OpenGL
3D initial code.

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.