Android game guidance (4. Simple texture textures)

Source: Internet
Author: User


Android game guidance (4. Simple texture textures)

This section describes OpenGL's texture technology and involves simple texture knowledge. I am busy with annual leave.

Download source code: Click me

Table of contents
  • 1 texture

    • 1.1 texture coordinate and texture ing
    • 1.2 enable texture ing in OpenGL
    • 1.3 create a texture
    • 1.4 specify texture
    • 1.5 Delete texture
    • 1.6 bind texture
    • 1.7 set a filter
    • 1.8 texture ing
  • 2. Several Common Problems
    • 2.1 texture white
    • 2.2 image distortion
  • 3. Code Implementation
  • 4. Map a robot
1 texture

A Texture defines the surface structure of an object, such as a pattern, pattern, or wrinkle. With textures, the model world will be more colorful. For example, for a spherical model, we map the texture of a football. This is a football model, and map the texture of a earth to it. In addition, if you map a quadrilateral to a wall texture, This is the wall. Otherwise, we need to build a brick and a brick. In this section, we refer to the narrow texture: image Textures (corresponding functional textures-textures defined by mathematical functions ).

Texture is actually a two-dimensional array with some color values. Each element is called a texture pixel ). A Texture object is an internal data type that stores texture data. You cannot directly access a texture object, but you can use an integer ID to trace it as its handle. With this handle, you can use it as the texture currently used (called texture binding), or you can delete this texture object from the memory, you can also assign a value to a texture (it is called a specified texture to load some texture data to the associated texture ).

Generally, the process of texture ing is as follows:

  1. Create a texture object. Is to obtain a new texture Handle ID.
  2. Specify the texture. In this step, the image data is formally loaded into the ID's texture object.
  3. Set the filter. Defines the effects of OpenGL realistic images, such as mosaic elimination during texture enlargement.
  4. Bind a texture object. The ID texture is used as the texture for the following operations.
  5. Texture ing. Draw the texture data to the screen. In this step, you can see the effect of the texture.

1.1 texture coordinate and texture ing

A Texture object has its own coordinate system. In the lower left corner, the upper right corner is ):

We need to draw a part of an image to the screen, called texture ing, that is, to calculate the texture coordinates of each point of the image to be drawn based on the above coordinate system, then correspond to the coordinates on the screen one by one (in the coordinate system, the upper left corner is the origin ):

1.2 enable texture ing in OpenGL

In the default settings, texture ing is disabled and the enabled parameters are gltexture2d. Other parameters include gl_texture_1d, gl_texture_3d, and gl_texture_cube_map. We only use 2D textures.

1.3 create a texture

Create a texture by using the glgentextures () function. The function returns the ID of the newly created texture. This function can create n textures and put texture IDs in textures:

Void glgentextures (int n, intbuffer textures)



Intbuffer = intbuffer. Allocate (1); Gl. glgentextures (1, intbuffer); int textureid = intbuffer. Get (); // texture ID


1.4 specify texture

OpenGL provides three functions to specify textures: glteximage1d (), glteximage2d (), and glteximage3d (). these three versions are used for the texture of the corresponding dimension. We use the 2D version: glteximage2d ().

Void glteximage2d (INT target, int level, int internalformat, int width, int height, int border, int format, int type, buffer pixels)

If there are too many parameters, you can use the teximage2d () function in glutils to directly use bitmap data as the parameter:

Void teximage2d (INT target, int level, Bitmap bitmap, int Border)


The target type of the operation. Set it to gl_texture_2d.
Texture level. This section does not cover it. Set it to 0.
Border, usually set to 0

GLUtils.texImage2D (GL10.GL_TEXTURE_2D, 0, mBitmap, 0);
1.5 Delete texture

Delete the texture. The third parameter specifies the step size of the texture ID in the textures array of the second parameter, which is usually saved in a compact sequence and set to 0.

Void gldeletetextures (int n, int [] textures, int offset)
1.6 bind texture

After binding, the texture is active. When you bind a texture object for the first time, it will adapt a series of initial values to your application. Binding is relatively simple. Use the glbindtexture () function ():

Void glbindtexture (INT target, int texture)

The first parameter is the Texture type. We use a 2D texture. The parameter is set to gl_texture_2d, and the second parameter is the ID of the texture object.

1.7 set a filter

There are two versions: float and Int.

Void gltexparameterf (INT target, int pname, float PARAM)
Void gltexparameterx (INT target, int pname, int PARAM)

Generally, we set two parameters: gl_texture_mag_filter for one amplifier and gl_texture_min_filter for the other amplifier.

The following two lines tell OpenGL that when displaying an image, when it is larger than the enlarged original texture (gl_texture_mag_filter) or smaller than the original texture (gl_texture_min_filter) openGL adopts the filtering method. I usually use gl_linear in both cases. This allows the texture to be smoothly displayed from a very distance to a very close to the screen. Using gl_linear requires more operations on the CPU and video card. If your machine is slow, you may need to use gl_nearest. When the filtered texture is enlarged, it looks very mottled (MOSAIC ). You can also combine these two filtering methods. Use gl_linear at the nearest point, and gl_nearest at the distance.

Gltexparameteri (gl_texture_2d, gl_texture_min_filter, gl_linear); // Linear Filter gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_linear); // Linear Filter

1.8 texture ing

Use the gltexcoordpointer function to specify the texture coordinate array,

Void gltexcoordpointer (INT size, int type, int stride, buffer pointer)

This function is disabled by default, so you need to enable it:

Gl. glableclientstate ( _ texture_coord_array); //... // disable Gl. gldisableclientstate ( _ texture_coord_array );
2. Common issues: 2.1 texture rendering white

Possible causes:

  • The gl_texture_2d option is not enabled. Please useGlenable ()AndGldisable ()Function.
  • The texture object has no data. UseGlutils. teximage2d ()Before specifyingGlbindtexture ()Activate the current texture.

2.2 image distortion

Possible causes:

  • Whether the correspondence between texture coordinates and vertex coordinates is correct.
  • The image size is not the power of 2. Solution: Internally regenerate an image of the power of 2 and adjust the UV coordinates.

3. Code Implementation

First, define a texture object. Its basic interfaces include:

  • Create + specify. Constructor completed
  • BIND.
  • Draw.

@ Note: In order to process the power of 2, an internal re-creates an image that is not the power of 2 for the original image. For details, see the code.

Public class texture2d {private int mwidth; private int mheight; private int mpow2width; private int mpow2height; private float maxu = 1.0f; private float maxv = 1.0f; private bitmap mbitmap = NULL; private int textureid = 0; // Delete the texture data public void Delete (gl10 GL) {If (textureid! = 0) {Gl. gldeletextures (1, new int [] {textureid}, 0); textureid = 0 ;}// bitmap if (mbitmap! = NULL) {If (mbitmap. isrecycled () mbitmap. recycle (); mbitmap = NULL;} public static int pow2 (INT size) {int small = (INT) (math. log (double) size)/math. log (2.0f); If (1 <small)> = size) return 1 <small; else return 1 <(small + 1);} // construct, deferred until the first binding: Public texture2d (bitmap BMP) {// mbitmap = BMP; mwidth = BMP. getwidth (); mheight = BMP. getheight (); mpow2height = pow2 (mheight); mpow2wid Th = pow2 (mwidth); maxu = mwidth/(float) mpow2width; maxv = mheight/(float) mpow2height; Bitmap bitmap = bitmap. createbitmap (mpow2width, mpow2height, BMP. hasalpha ()? Bitmap. config. argb_8888: bitmap. config. rgb_565); canvas = new canvas (Bitmap); canvas. drawbitmap (BMP, 0, 0, null); mbitmap = bitmap;} // the first time the texture data is loaded, public void BIND (gl10 GL) {If (textureid = 0) {int [] textures = new int [1]; GL. glgentextures (1, textures, 0); textureid = textures [0]; GL. glbindtexture ( _ texture_2d, textureid); GL. gltexparameterx ( _ texture_2d, _ texture_min_filter, _ linear); GL. gltexparameterx ( _ texture_2d, _ texture_mag_filter, _ linear); glutils. teximage2d ( _ texture_2d, 0, mbitmap, 0); mbitmap. recycle (); mbitmap = NULL;} GL. glbindtexture ( _ texture_2d, textureid);} // draw to the public void draw (gl10 GL, float X, float y) {GL. glable ( _ texture_2d); GL. glableclientstate ( _ texture_coord_array); GL. glableclientstate ( _ vertex_array); // bind this. BIND (GL); // floing floatbuffer verticlebuffer = floatbuffer. wrap (new float [] {X, Y, x + mwidth, 0, x, y + mheight, x + mwidth, Y + mheight,}); floatbuffer coordbuffer = floatbuffer. wrap (new float [] {0, 0, maxu, 0, 0, maxv, maxu, maxv,}); GL. gltexcoordpointer (2, _ float, 0, coordbuffer); GL. glvertexpointer (2, _ float, 0, verticlebuffer); GL. gldrawarrays ( _ triangle_strip, 0, 4); GL. gldisableclientstate ( _ vertex_array); GL. gldisableclientstate ( _ texture_coord_array); GL. gldisable ( _ texture_2d);} public void draw (gl10 GL, float X, float y, float width, float height) {GL. glable ( _ texture_2d); GL. glableclientstate ( _ texture_coord_array); GL. glableclientstate ( _ vertex_array); // BIND (GL); // ing // floing floatbuffer verticlebuffer = floatbuffer. wrap (new float [] {X, Y, x + width, 0, x, y + height, x + width, Y + height,}); floatbuffer coordbuffer = floatbuffer. wrap (new float [] {0, 0, maxu, 0, 0, maxv, maxu, maxv,}); GL. gltexcoordpointer (2, _ float, 0, coordbuffer); GL. glvertexpointer (2, _ float, 0, verticlebuffer); GL. gldrawarrays ( _ triangle_strip, 0, 4); GL. gldisableclientstate ( _ vertex_array); GL. gldisableclientstate ( _ texture_coord_array); GL. gldisable ( _ texture_2d); GL. gldisableclientstate ( _ vertex_array); GL. gldisableclientstate ( _ texture_coord_array); GL. gldisable ( _ texture_2d );}}
4. Map a robot

The code is simple. Draw a texture2d file in the draw () of the scenario scene. For more information about the download code, see:

Public class androidscene extends globject {texture2d texture; Public androidscene () {super (); // use androida.jpg bitmap androidbitmap = gamesystem in the assets folder. getinstance (). getbitmapfromassets ("androida.jpg"); texture = new texture2d (androidbitmap);} public void draw (gl10 GL) {texture. draw (GL, 0, 0 );}}

This section is boring and enjoyable.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.