【问题标题】:Sample texturing using Texture atlas and openGL 1.1 in android在 android 中使用 Texture atlas 和 openGL 1.1 的示例纹理
【发布时间】:2012-03-28 13:39:14
【问题描述】:

我在使用纹理图集在 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]);

这是我假设的纹理位图

【问题讨论】:

  • 纹理图集具体有什么不明白的地方?你知道纹理坐标是如何工作的吗?我认为没有人会为您重写您的代码,但如果有一些您不理解的特定概念,您可能会找到一些帮助。
  • 我不知道如何从纹理图集中绑定子图像,我只知道如何将图像作为一个整体进行绑定,例如,我必须先使用texImage2d然后使用texSubImage吗?还是我必须立即使用 texSubImage?谢谢
  • 你是如何解决这个问题的?

标签: android opengl-es textures


【解决方案1】:

响应您的评论,您不绑定 subImages。 glTexSubImage 仅用于替换存储纹理中的像素。如果您的位图已经作为图集存在,如您所示,那么您只需通过 glTexImage2d 加载整个纹理并绑定它。

如果您想访问该图像的特定部分(例如第一行左起第三个黄色圆圈炸弹),则只需修改用于访问它的纹理坐标。如果该圆圈在其自己的图像中,您只需使用 (0,0) 到 (1,1) 坐标,但由于它是另一个图像的一部分,您必须只选择它的特定部分。在您的图片中,它的 texcoords 为:

(.50,.75) //bottom left
(.75,.75) //bottom right
(.75,1.0) //top right
(.50,1.0) //top left

如果您使用这些 texcoords 绘制一个四边形,它应该看起来就像黄色圆形炸弹有自己的图像一样,但您不必一直重新绑定纹理(因此性能提高)。

【讨论】:

  • 感谢您为我澄清事情,这对我很有帮助:)
猜你喜欢
  • 1970-01-01
  • 2010-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多