android OpenGLES開発第5課テクスチャマッピング


ここでは、色の代わりにテクスチャを追加して異なる表面の内容を設定します.前の授業とは異なり、Polygonクラスにいくつかのテクスチャの設定を追加しました.以下で説明します.コードは以下の通りです.
public class Polygon {
	//      FloatBuffer

	private FloatBuffer[] textureBuffer;

	//          
	private int[] textures = new int[8];

	//            

	private float[][] texture = {
			new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, 1.0f, 0.5f, 0.0f,
					1.0f, 1.0f, 1.0f,

			},
			new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, 1.0f, 0.5f, 0.0f,
					1.0f, 1.0f, 1.0f,

			}, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,

			}, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,

			}, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,

			}, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,

			}, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,

			}, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,

			} };

	public Polygon() {
		ByteBuffer bb;
		//         FloatBuffer

		textureBuffer = new FloatBuffer[8];
		for (int i = 0; i < 8; i++) {
			bb = ByteBuffer.allocateDirect(texture[i].length * 4);
			bb.order(ByteOrder.nativeOrder());
			textureBuffer[i] = bb.asFloatBuffer();
			textureBuffer[i].put(texture[i]);
			textureBuffer[i].position(0);
		}

	}

	public void draw(GL10 gl) {
		gl.glFrontFace(GL10.GL_CW);

		for (int i = 0; i < 8; i++) {
			// Generate one texture pointer...
			// ...and bind it to our array

			//         ,           
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]);
			// Point to our buffers
			//       

			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer[i]);
			// Enable the texture state
			//          

			gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			// Disable the client state before leaving
			//          

			gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		}

	}

	//           
	public void loadTexture(GL10 gl, Context context) {
		// define the resourcesId
		 int[] resourcesIds = { R.drawable.gou, R.drawable.hou,
		 R.drawable.laohu, R.drawable.laoshu, R.drawable.tuzi,
		 R.drawable.xiaolong, R.drawable.xiaoniu, R.drawable.zhu };
		gl.glGenTextures(8, textures, 0);
		// Get the texture from the Android resource directory
		for (int i = 0; i < 8; i++) {
			Bitmap bitmap = loadBitmap(context, resourcesIds[i]);
			// Generate one texture pointer...
			// ...and bind it to our array
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]);

			// Create Nearest Filtered Texture
			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_LINEAR);
			// Use the Android GLUtils to specify a two-dimensional texture
			// image from our bitmap
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
			// Clean up
			bitmap.recycle();
		}
	}

	//   bitmap

	public Bitmap loadBitmap(Context context, int resourceid) {

		InputStream is = context.getResources().openRawResource(resourceid);
		try {
			// BitmapFactory is an Android graphics utility for images
			return BitmapFactory.decodeStream(is);

		} finally {
			// Always clear and close
			try {
				is.close();
				is = null;
			} catch (IOException e) {
			}
		}
	}
}

 
ネット上で256 x 256の画像を探して、文の中の画像資源の代わりにすることができます.