Android OpenGL - ES纹理出血

时间:2010-10-18 18:58:07

标签: android opengl-es render textures

我正在编写一个小应用程序,目前会生成随机的纹理贴图。

我将此地图绘制为10 x 15组“四边形”,其实际上是所有三角形条带。我使用“map”来获取一个int,然后我将其作为textureAtlas中此方块的纹理位置。所以例如0是左下角的“瓦片”。图集为128 x 128,分为32个像素图块。

然而,我似乎得到了一些奇怪的文物,其中来自一个瓷砖的纹理爬进到下一个瓷砖。我想知道它是否是图像本身,但据我所知,像素正好在它们应该的位置。然后我查看了我指定的纹理坐标,但它们看起来都很精确(0.0,0.25,0.5,0.75,1.0-将它分成我期望的4行和列)。

奇怪的是,如果我在模拟器上运行它,我就不会得到任何文物。

我是否缺少会导致1像素出血的设置?它似乎只是垂直的 - 这可能与手机有关我在这个方向“拉伸”图像,因为手机的屏幕在那个方向上比正常大。

我像这样加载纹理:

//Get a new ID
    int id = newTextureID(gl);

    //We will need to flip the texture vertically
    Matrix flip = new Matrix();
    flip.postScale(1f, -1f);

    //Load up and flip the texture
    Bitmap temp = BitmapFactory.decodeResource(context.getResources(), resource);

    //Store the widths for the texturemap
    int width = temp.getWidth();
    int height = temp.getHeight();
    Bitmap bmp = Bitmap.createBitmap(temp, 0, 0, width, height, flip, true);
    temp.recycle();

    //Bind
    gl.glBindTexture(GL10.GL_TEXTURE_2D, id);

    //Set params
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR);

    //Push onto the GPU
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);

    TextureAtlas atlas = new TextureAtlas(id, width, height, tileSize);
    return atlas;

然后我就这样呈现:

gl.glBindTexture(GL10.GL_TEXTURE_2D, currentAtlas.textureID);
    //Enable the vertices buffer for writing and to be used during our rendering
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    //Specify the location and data format of an array of vertex coordinates to use
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

    //Enable the texture buffer
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);

我会拍照,但我不确定如何通过手机获得屏幕截图...

如果有人知道如何捕捉当前帧并将其放入文件中,我会这样做,如果它有助于解释发生了什么!

期待您的回复。

编辑:这是一个屏幕保护程序 - 请注意我在横向上运行应用程序但是上限是纵向的。也可以原谅可怕的纹理:D他们只是一个占位符/乱搞。

ScreenCap

1 个答案:

答案 0 :(得分:11)

好吧,在和朋友说话后,我设法解决了这个小问题。

事实证明,如果你想拥有精确的像素完美纹理,你必须指定纹理的边缘到像素的一半。

为此,我只需添加半个像素或减去半个像素来测量纹理坐标。

像这样:

//Top Left
            textureCoords[textPlace] = xAdjust*currentAtlas.texSpaceWidth + currentAtlas.halfPixelAdjust;        textPlace++;
            textureCoords[textPlace] = (yAdjust+1)*currentAtlas.texSpaceHeight - currentAtlas.halfPixelAdjust;    textPlace++;

这是在加载纹理图集时简单计算的:

(float)(0.5 * ((1.0f / numberOfTilesInAtlasRow) / pixelsPerTile));

虽然如果高度与每个图块的宽度不同(可能发生),您需要单独计算它们。

这解决了所有工件,所以我可以继续。希望它可以帮助别人!