简体   繁体   English

在OpenGL ES 1.1中滚动

[英]Scrolling in OpenGL ES 1.1

I'm developing a 2D side-scrolling game.I have a tiled bitmap for background with size 2400x480.How can i scroll this bitmap? 我正在开发2D横向滚动游戏。我有一个2400x480大小的背景平铺位图,如何滚动此位图?

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. 因此它将滚动位图240000像素。

But i don't want to draw images which stays out of screen(with size 800x480). 但是我不想画出屏幕之外的图像(尺寸为800x480)。

How can i render scrolling tile?How can i give velocity to it?(For parallax scrolling after normal scrolling) 如何渲染滚动图块?如何给它提供速度?(用于正常滚动后的视差滚动)

The following code does not use two quads, but one quad, and adapts the texture coordinates accordingly. 以下代码不使用两个quad,而是使用一个quad,并相应地调整纹理坐标。 Please note that I stripped all texture handling code. 请注意,我剥离了所有纹理处理代码。 The projection matrix solely contains glOrthof(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f); 投影矩阵仅包含glOrthof(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f); . Quad2D defines a quad whose tex coords can be animated using updateTexCoords : Quad2D定义一个四边形,可以使用updateTexCoords将其tex坐标设置为动画:

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 */
}

Now how to call updateTexCoords ? 现在如何调用updateTexCoords Inside onDrawFrame , put the following code: onDrawFrame ,放置以下代码:

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

Since the texture coordinates go beyond 1.0f , the texture wrap for the S coordinate must be set to GL_REPEAT . 由于纹理坐标超过1.0f ,因此必须将S坐标的纹理环绕设置为GL_REPEAT

If you are ok working with shaders, we can have a cleaner and may be more efficient way of doing this. 如果您可以使用着色器,则可以使用更清洁的清洁器,并且这样做可能更有效。

Create a VBO with a quad covering the whole screen. 用覆盖整个屏幕的四边形创建一个VBO。 The witdth and height should be 800 and 480 respectively. 身高和身高应分别为800和480。

then create your vertex shader as: 然后将您的顶点着色器创建为:

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.
}

The fragment shader would be something like: 片段着色器将类似于:

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

and you are done. 到此为止。 To scroll just set the uniform value using "glUniformX()" apis. 要滚动,只需使用“ glUniformX()” API设置统一值即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM