Reprint The Android platform OpenGL preliminary

Source: Internet
Author: User

Please see the original OpenGL on Android platform

This article focuses only on how to implement OpenGL on an Android platform step-by-step.

1, Glsurfaceview

Glsurfaceview is an important part of implementing OpenGL drawing in Android applications. A surface is encapsulated in the Glsurfaceview. And the reality of images on the Android platform is almost always done by surface.

2, Renderer

With the Glsurfaceview, it is equivalent to the paper we have drawing. Now all we need to do is draw on this piece of paper. So we need a pen.

Renderer is the internal static interface of Glsurfaceview, which is equivalent to a pen painted on this glsurfaceview. We "paint" by implementing this interface. Finally, the Glsurfaceview setrenderer (Glsurfaceview.renderer Renderer) method can be used to correlate the pen and paper.

Implementing renderer requires three interfaces to implement it: onsurfacecreated (GL10 gl, EGLConfig config), onsurfacechanged (GL10 gl, int width, int height), Ondrawframe (GL10 GL). Here is a brief introduction to the specific meanings of these three interfaces.

2.1, onsurfacecreated

This method looks at the name and knows that it was called when surface was created. So we can do some initialization work in the implementation of this function. For example, remove four treasures, lay out the canvas, adjust the paint, and so on. Its function prototype is as follows:

public abstract void onsurfacecreated (GL10 gl, EGLConfig config)

The second parameter does not have any public methods and fields in the document about it. So we don't have to take care of it.

The first parameter is of great importance. If the renderer is a brush, then this GL parameter, can be said to be our hands. How to operate this brush, it is the boss! So most of the time we are directing renderer drawing through this hand called GL.

2.2 onsurfacechanged

When the Glsurfaceview size changes, the corresponding surface size also changes. It's worth noting that when surface was first created, its size was actually 0, meaning it would be called once before the first picture was drawn. (And for many times, the size of your surface doesn't change, so this function is called only once when it's created)

The prototype is as follows:

public abstract void Onsurfacechanged (GL10 gl, int width, int height)

Similarly, the hand of drawing is required.

It is also worth noting that it tells us how wide this piece of paper is. This is important. Because we do not know the width of the paper at the time of onsurfacecreated, so some and long-width related initialization work has to be done in this function.

2.3 Ondrawframe

Well, our initialization work is almost done, so now is the time for real thing to draw! This function is really for you to draw. Draw a picture every time you call. Perhaps the question is when will this function be called? It must have been called at the very beginning. There are two different modes for you to choose from:

1.rendermode_continuously

2.rendermode_when_dirty

First mode (rendermode_continuously):

A continuous brush, draw a picture and draw the next one immediately. This pattern is obviously used to draw animations;

Second mode (Rendermode_when_dirty):

Draw the next picture only when you need to redraw it. This model is more CPU-and GPU-friendly, and is suitable for painting situations that do not need to be refreshed very often. One more word, how does the system know that a redraw is needed? Of course you have to tell it ...

Call Glsurfaceview's Requestrender () method and tell it that you are dirty.

Where are these two modes set? Glsurfaceview the Setrendermode (int rendermode) method. Lets you set the refresh mode you need.

Let's take a look at the prototype of this function: public abstract void Ondrawframe (GL10 gl) is simple and hands-only.

3. The basic process of OpenGL drawing under Android:

Let's start by drawing a triangle:

3.1 Myrender

After the previous introduction, we should know what needs to be done now, is to write good renderer three interface methods.

We need to rewrite a class to implement it, and then override these three methods.

Class Myrender implements Glsurfaceview.renderer

OK, the pen has been taken. "Paving the paper" is a critical step. Although we say that Glsurfaceview is the paper we draw, but "paving" this piece of paper, but also very important.

Let's focus on how to shop this paper. OpenGL this piece of paper is not a regular piece of paper. At the very least, it is three-dimensional. However, the actual display screen is a plane. So this paper is not good "shop".

First of all, not necessarily the whole piece of paper is used to paint, surface is not necessarily all used (of course, we are generally used). So we need to plan what part of the area is used to paint.

Gl.glviewport (0, 0, width, height);

According to the width and height of the parameters here, we can know that the wide height needs to be learned in the onsurfacechanged.

Then, this step is critical. How to draw three-dimensional coordinates on a planar point or graph? OpenGL has a coordinate system, such as:

We need to map this coordinate system to the surface of our glsurfaceview.

Glmatrixmode (gl10.gl_projection);

Gl.glloadidentity ();

GL.GLFRUSTUMF ( -400, -240, 0.3f, 100);

Glmatrixmode (gl10.gl_projection); Is that what we're changing now is the mapping of the coordinate system to surface (the projection matrix).

Next sentence gl.glloadidentity (); is to clear the previous changes (before any changes to the projection matrix).

GLFRUSTUMF (float left, float right, float bottom, float top, float znear, float Zfar) This function is very powerful. It implements the mapping between surface and the coordinate system. It is mapped in a perspective projection way.

See the meaning of perspective projection:

Map Description:

1, the front of a rectangle represents the scope of our plane mapping. That is, surface's drawing range. It has four sides, and we will temporarily name them Edgeleft, Edgeright, Edgetop, Edgebottom.

2. Left, right, bottom, top of the GLFRUSTUMF parameter is the position of the four edge of the drawing range in OpenGL x =-400. The values of the top, right, and bottom represent the positions of Edgeright, Edgetop, and Edgebottom in the coordinate system.

3, the above 2nd set out the scope of the mapping of the X and Y range. So for the 3D OpenGL this paper, we also need to set the range of Z. First, imagine where the camera or eye is in the coordinate system.

The default eye position is at the origin of the OpenGL coordinates (0,0,0). The direction of sight is parallel to the z axis.

Near represents the distance from the eye to the drawing plane (absolute value Oh!) Far represents the plane range of the eye to the farthest visible point. So, by default, Z's drawing range is in the-near to-far position.

4, well, we have determined the X, Y, z direction of the drawing range. Back, you can find that this "three-dimensional" paper, a square vertebral body cut off the head of the flat-head body. The coordinates of the objects that we draw will be visible to us (that is, drawn on the screen). OK, at this point, we have finally paved the paper.

The options (parameters) of the Glmatrixmode function have the following three kinds: Gl_projection,gl_modelview and gl_texture;

    • Gl_projection, is the projection of the meaning, is to be related to the projection operation, that is, the object projection to a plane, as we take pictures, the 3-dimensional object to the 2-dimensional plane. In this way, the following statements can be functions related to perspective, such as glfrustum () or gluperspective ();
    • Gl_modelview, is the operation of the Model view, the next statement depicts a model-based adaptation, so to set parameters, the next is to use a function like Glulookat ();
    • Gl_texture, is to manipulate the texture related;

By the way, OpenGL inside the operation, a lot is based on the operation of the Matrix, such as displacement, rotation, scaling, so, here is actually said the specification is Glmatrixmode is used to specify which matrix is the current matrix, and its parameters represent the target to operate:

    • Gl_projection is the operation of the projection matrix;
    • Gl_modelview is the operation of the Model view matrix;
    • Gl_texture is the subsequent operation of the texture matrix;

3.2 Composition before drawing

The OpenGL mapping of Android, unlike the general drawing, we have to sigh with emotion. It separates the data from the picture completely. For example, we want to draw a triangle. Obviously, there are three points in a triangle. We need to make a composition first before drawing, such as where each point is. We put this data in an array buffer, and after we put the data together, we can draw it in unity.

Here's how to put the vertex data and color data into an array buffer that fits the Android OpenGL.

The first thing to understand is that OpenGL is a very low-level drawing interface that uses a buffer storage structure that is not the same as our Java program. Java is the big-endian byte-order (Bigedian), and the data that OpenGL needs is the small-endian byte order (Littleedian). So, we need to do some work when we convert the Java buffers into OpenGL-usable buffers.

Byte Data buffer

Regardless of whether our data is integer or floating point, we need a bytebuffer in order to complete the Bigedian to Littleedian conversion. We use it to get a buffer that can change the byte order.

Bytebuffer Mbuffer = Bytebuffer.allocatedirect (pointcount*dimension*4);

Mbuffer.order (Byteorder.nativeorder ());

Note that we should use "Allocatedirect" to allocate memory space, because this will allow order ordering. Finally, we can pass:

Resultbuffer = Mbuffer.asfloatbuffer ();

Resultbuffer = Mbuffer.asintbuffer ();

Converts a byte buffer to an integer or floating-point buffer.

3.3 is finally drawing!

With all the preparatory work ahead, there is a good news to tell you that we can finally draw a picture! Moreover, with so much work ahead, the real work of drawing is actually quite simple.

3.3.1, clean up your paper.

As we said before, before drawing, be sure to clean up the paper:

Gl.glclear (Gl10.gl_color_buffer_bit);

In addition, we used Glmatrixmode (gl10.gl_projection) when we mapped the coordinate system, to specify that the projection matrix should be changed. So now we're going to draw, so we need to specify that the "view Matrix" is changed:

Gl.glmatrixmode (Gl10.gl_modelview);

Gl.glloadidentity ();

3.3.2, enabling arrays

As we have said before, the data of the drawing is placed in the array buffer, and then it comes together to paint. So we'll first tell OpenGL what arrays we need to use. For example, we need vertex arrays and color arrays:

Gl.glenableclientstate (Gl10.gl_vertex_array);

Gl.glenableclientstate (Gl10.gl_color_array);

3.3.3, specifying array data

We have already constructed our data buffer, Floatbuffer (or Intbuffer) in front of us. Now all we need to do is bind this data buffer with the corresponding function:

Gl.glvertexpointer (3, gl10.gl_float, 0, VertexBuffer);

Gl.glcolorpointer (4, gl10.gl_float, 0, Colorbuffer);

These two sentences are bound to the vertex data array and the color data array respectively. Where the first parameter represents a few coordinates for each point. For example, vertices have x, y, z values, so it is 3, and the color is R, G, B, a value, so it is 4.

3.3.4, Draw!

Gl.gldrawarrays (gl10.gl_triangles, 0, 3);

The first parameter indicates the type of paint-the triangle (Android seems to only support drawing triangles, dots, lines, and does not support drawing polygons). The next two parameters indicate which vertex to draw from and how many vertices to draw.

Ok! At this point, our first triangle is drawn, let's see the effect.

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.