我在使用纹理图集时遇到了OpenGL中的物体纹理问题,我正在创建一个2D游戏,知道如何将POT位图纹理化到对象上,但是,出于性能原因,我无法找到使用纹理图集的教程。这里是我当前工作中对象创建和纹理实现的代码。
public void createTexture(Bitmap bmp, GL10 gls, int texturename)
{
this.gl = (GL11) gls;
this.TextureName = texturename;
bombBmp = bmp;
VertexBuffer = null;
TextureBuffer = null;
IndexBuffer = null;
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(12 * 4);
byteBuffer.order(ByteOrder.nativeOrder());
VertexBuffer = byteBuffer.asFloatBuffer();
VertexBuffer.put(new float[] { 0, 0, 0.0f, 0, -h, 0.0f, w, 0, 0.0f, w, -h, 0.0f });
VertexBuffer.position(0);
byteBuffer = ByteBuffer.allocateDirect(8 * 4);
byteBuffer.order(ByteOrder.nativeOrder());
TextureBuffer = byteBuffer.asFloatBuffer();
TextureBuffer.put(new float[] { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f });
TextureBuffer.position(0);
byteBuffer = ByteBuffer.allocateDirect(6 * 2);
byteBuffer.order(ByteOrder.nativeOrder());
IndexBuffer = byteBuffer.asShortBuffer();
IndexBuffer.put(new short[] { 0, 1, 2, 1, 3, 2 });
IndexBuffer.position(0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, this.TextureName);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bombBmp, 0);
bombBmp.recycle();
bombBmp = null;
}
我从genTexture生成我的Texturename,并将POT位图传递给此函数。
gl.glGenTextures(1, textures, 0);
bomb.createTexture(bombBmp, gl, textures[0]);
这是我假定的纹理位图。