OpenGL ES texture ing

Source: Internet
Author: User

The following are all converted from the android game programming entry-level classic. For more information, see the source.

To map a bitmap to a triangle, you need to add texture coordinates (texture coordinates) for each vertex of the triangle ). Texture coordinates map a point in the texture (uploaded Bitmap) to a vertex in the triangle. Texture ing is usually 2D.

The relative coordinates are usually X, Y, Z, and the texture coordinates are generally called u, v, S, and T. S is equivalent to X coordinate in the standard coordinate system, and T is equivalent to Y coordinate. The S axis points to the right, and the T axis points to the bottom. Loading any image, no matter how wide or high the pixel is, will be embedded into this coordinate system. The upper left corner of the image is (0, 0), and the lower right corner is always (1, 1), even if the width and height are different.

int VERTEX_SIZE = (2 + 2) * 4;ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3 * VERTEX_SIZE);byteBuffer.order(ByteOrder.nativeOrder());vertices = byteBuffer.asFloatBuffer();vertices.put(new float[]{0.0f,   0.0f,   0.0f,   1.0f,  319.0f,   0.0f,   1.0f,   1.0f,  160.0f, 479.0f,   0.5f,   0.0f});vertices.flip();

OpenGL ES dynamically blends the filled color between vertices with the texture color of the triangle ing part. In this case, you also need to adjust the buffer size and vertex_size constants (for example, (2 + 4 + 2) * 4 ). To tell OpenGL ES vertices to contain texture coordinate data, call the glableclientstate () and gltexcoordpointer () Methods again.

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);vertices.position(0);gl.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);vertices.position(2);gl.glTexCoordPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);

Create a texture object:

GL10.glGenTextures(int numTextures, int[] ids, int offset)

At this time, the texture object is still empty, which means it does not have any data. Upload bitmap below. First, you need to bind the texture. Binding an object in OpenGL ES means that the specified object will be used in all subsequent calls until the binding is changed again. In this case, you need to bind a texture object and use the glbindtexture () method. Once a texture is bound, you can perform operations on its attributes.

gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);

There is one more thing to do before using a texture object. The area on the screen where the triangle is located may be much larger or smaller than the Texture projection area. In each case, we need to tell OpenGL ES whether to zoom in or out the texture. The zoom-in and zoom-out operations are also called the zoom-in and zoom-out filters in OpenGL ES. These filters are attributes of texture objects. Before setting them, make sure that the texture object has been bound through the glbindtexture () call. If it has been bound, you can set it as follows:

gl.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_NEAREST);

The gl10.gltexparameterf () method is used for both calls. This method sets texture attributes. The first call specifies the zoom-in filter; the second call uses the zoom-in filter. The last parameter specifies the filter type. There are two options: gl10.gl _ nearest and gl10.gl _ linear.

The first filter always selects the closest texture element mapped to the pixel texture ing. The second filter type samples the four lines closest to the triangle pixel to be processed, and obtains the final color of the pixel after the average value. If you want to get a pixel image, use the first filter. If you want to get a smooth image, use the second filter.


Once the texture definition is completed, it is common to cancel texture binding. When bitmap is no longer needed, it should also be recycled.

gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);bitmap.recycle();

0 is a special ID that tells OpenGL ES to unbind the current bound object.

Comprehensive instance:

package org.example.androidgames.glbasics;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.ByteOrder;import java.nio.FloatBuffer;import javax.microedition.khronos.opengles.GL10;import org.example.androidgames.framework.Game;import org.example.androidgames.framework.Screen;import org.example.androidgames.framework.impl.GLGame;import org.example.androidgames.framework.impl.GLGraphics;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.opengl.GLUtils;import android.util.Log;public class TexturedTriangleTest extends GLGame {@Overridepublic Screen getStartScreen() {// TODO Auto-generated method stubreturn new TexturedTriangleScreen(this);}class TexturedTriangleScreen extends Screen{final int VERTEX_SIZE = (2 + 2) * 4;GLGraphics glGraphics;FloatBuffer vertices;int textureId;public TexturedTriangleScreen(Game game) {super(game);glGraphics = ((GLGame)game).getGLGraphics();ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3 * VERTEX_SIZE);byteBuffer.order(ByteOrder.nativeOrder());vertices = byteBuffer.asFloatBuffer();vertices.put(new float[]{0.0f,   0.0f,   0.0f,   1.0f,  319.0f,   0.0f,   1.0f,   1.0f,  160.0f, 479.0f,   0.5f,   0.0f});vertices.flip();textureId = loadTexture("bobrgb888.png");}public int loadTexture(String fileName){try{Bitmap bitmap =BitmapFactory.decodeStream(game.getFileIO().readAsset(fileName));GL10 gl = glGraphics.getGL();int textureIds[] = new int[1];gl.glGenTextures(1, textureIds, 0);int textureId = textureIds[0];gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);gl.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_NEAREST);gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);bitmap.recycle();return textureId;}catch(IOException e){Log.d("TexturedTriangleTest", "couldn't load asset 'bobrgb888.png'");throw new RuntimeException("couldn't load asset '" + fileName + "'");}}@Overridepublic void update(float deltaTime) {// TODO Auto-generated method stub}@Overridepublic void present(float deltaTime) {// TODO Auto-generated method stubGL10 gl = glGraphics.getGL();gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());gl.glClear(GL10.GL_COLOR_BUFFER_BIT);gl.glMatrixMode(GL10.GL_PROJECTION);gl.glLoadIdentity();gl.glOrthof(0, 320, 0, 480, 1, -1);gl.glEnable(GL10.GL_TEXTURE_2D);gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);vertices.position(0);gl.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);vertices.position(2);gl.glTexCoordPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);}@Overridepublic void pause() {// TODO Auto-generated method stub}@Overridepublic void resume() {// TODO Auto-generated method stub}@Overridepublic void dispose() {// TODO Auto-generated method stub}}}

Running 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.