0

I'm developing a 2D side-scrolling game.I have a tiled bitmap for background with size 2400x480.How can i scroll this bitmap?

I know that i can do it with following code:

for(int i=0;i<100;i++)
draw(bitmap,2400*i,480);

So it will scroll bitmap for 240000 pixels.

But i don't want to draw images which stays out of screen(with size 800x480).

How can i render scrolling tile?How can i give velocity to it?(For parallax scrolling after normal scrolling)

4

2 回答 2

0

下面的代码不是使用两个四边形,而是一个四边形,并相应地调整纹理坐标。请注意,我剥离了所有纹理处理代码。投影矩阵仅包含glOrthof(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);Quad2D定义一个四边形,其 tex 坐标可以使用 动画updateTexCoords

static class Quad2D {
    public Quad2D() {
        /* SNIP create buffers */
        float[] coords = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, };
        mFVertexBuffer.put(coords);
        float[] texs = { 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, };
        mTexBuffer.put(texs);
        /* SNIP reposition buffer indices */
    }

    public void updateTexCoords(float t) {
        t = t % 1.0f;
        mTexBuffer.put(0, 0.33f+t);
        mTexBuffer.put(2, 0.0f+t);
        mTexBuffer.put(4, 0.33f+t);
        mTexBuffer.put(6, 0.0f+t);
    }

    public void draw(GL10 gl) {
        glVertexPointer(2, GL_FLOAT, 0, mFVertexBuffer);
        glTexCoordPointer(2, GL_FLOAT, 0, mTexBuffer);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    }
    /* SNIP members */
}

现在怎么打电话updateTexCoords?在里面onDrawFrame,输入以下代码:

    long time = SystemClock.uptimeMillis() % 3000L;
    float velocity = 1.0f / 3000.0f;
    float offset = time * velocity;
    mQuad.updateTexCoords(offset);

由于纹理坐标超出1.0f,S 坐标的纹理环绕必须设置为GL_REPEAT

于 2012-05-02T07:02:09.233 回答
0

如果您可以使用着色器,我们可以使用更清洁且可能更有效的方式来执行此操作。

创建一个四边形覆盖整个屏幕的 VBO。宽度和高度应分别为 800 和 480。

然后将您的顶点着色器创建为:

attribute vec4 vPos;
uniform int shift;
varying mediump vec2 fTexCoord;
void main(void){
   gl_Position = vPos;
   fTexCoord = vec2((vPos.x-shift)/800.0, vPos.y/480.0);//this logic might change slightly based on which quadarnt you choose.
}

片段着色器将类似于:

precision mediump float;
uniform sampler2D texture;    
varying mediump vec2 fTexCoord;
void main(void){
   gl_FragColor = texture2D(texture, fTexCoord);
}

你就完成了。要滚动只需使用“glUniformX()”api 设置统一值。

于 2012-05-02T07:42:13.947 回答