使用Android和OpenGL ES 1.0,为什么我的1440 x 320像素背景缩小到我的Frustum Width和Frustum Height(480 x 320)?

时间:2015-07-13 20:06:30

标签: android opengl-es

使用Android和OpenGL ES 1.0,为什么我的1440 x 320像素背景缩小到我的Frustum Width和Frustum Height(480 x 320)?

background = new Texture(game, "scrolling_background.png");
backgroundRegion = new TextureRegion(background, 0, 0, 1440, 320);

batcher.beginBatch(Assets.background);
batcher.drawSprite(cam.position.x, cam.position.y, FRUSTUM_WIDTH, FRUSTUM_HEIGHT, Assets.backgroundRegion);
batcher.endBatch();

cam.position.x = 240
cam.position.y = 160
FRUSTUM_WIDTH = 480
FRUSTUM_HEIGHT = 320

我的目的是仅在每个屏幕上显示我背景的三分之一,最终目标是实现2D滚动背景。我原来的理由是因为视锥体/视口只有480像素宽,而我的原始图像宽度为1440像素,每张屏幕截图我只会看到1440/480或1/3的背景。

但是,整个图像似乎缩小以适合视口。我认为这与我的默认加载纹理方法对缩小和放大滤镜使用G10.GL_NEAREST这一事实有关:

private void load () {
    GL10 gl = glGraphics.getGL();
    int[] textureIds = new int[1];
    gl.glGenTextures(1, textureIds, 0);
    textureId = textureIds[0];

    InputStream in = null;

    try {
        in = fileIO.readAsset(fileName);
        Bitmap bitmap = BitmapFactory.decodeStream(in);
        width = bitmap.getWidth(); // added by me to fix TextureRegion
        height = bitmap.getHeight(); // added by me to fix TextureRegion
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
        setFilters(GL10.GL_NEAREST, GL10.GL_NEAREST);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
    }
    catch (IOException e) {
        throw new RuntimeException("Couldn't load texture '" + fileName + "'", e);
    }
    finally {
        if (in != null) {
            try {
                in.close();
            }
            catch (IOException e) {
            }
        }
    }
}

public void reload () {
    load();
    bind();
    setFilters(minFilter, magFilter);
    glGraphics.getGL().glBindTexture(GL10.GL_TEXTURE_2D, 0);
}

所以我决定将缩小和放大滤镜设置为GL10.GL_LINEAR,认为这样可以通过这样做来制作1:1贴图的纹理:

background = new Texture(game, "scrolling_background.png");
background.setFilters(GL10.GL_LINEAR, GL10.GL_LINEAR);
background.reload();

我确定这很简单。任何指导或帮助将不胜感激。

这是spritebatcher.draw原型:

public void drawSprite(float x,float y,float width,float height,TextureRegion region){

    float halfWidth = width / 2;
    float halfHeight = height / 2;

    float x1 = x - halfWidth;
    float y1 = y - halfHeight;
    float x2 = x + halfWidth;
    float y2 = y + halfHeight;

    // bottom left

    verticesBuffer[bufferIndex++] = x1;
    verticesBuffer[bufferIndex++] = y1;
    verticesBuffer[bufferIndex++] = region.u1;  
    verticesBuffer[bufferIndex++] = region.v2;

    // bottom right

    verticesBuffer[bufferIndex++] = x2;
    verticesBuffer[bufferIndex++] = y1;
    verticesBuffer[bufferIndex++] = region.u2;
    verticesBuffer[bufferIndex++] = region.v2;

    // top right

    verticesBuffer[bufferIndex++] = x2;
    verticesBuffer[bufferIndex++] = y2;
    verticesBuffer[bufferIndex++] = region.u2;
    verticesBuffer[bufferIndex++] = region.v1;

    // top left

    verticesBuffer[bufferIndex++] = x1;
    verticesBuffer[bufferIndex++] = y2;
    verticesBuffer[bufferIndex++] = region.u1;
    verticesBuffer[bufferIndex++] = region.v1;

    numSprites++;



}

0 个答案:

没有答案