简体   繁体   中英

Low FPS with OpenGL on Android

I contact because, I try to use openGL with android, in order to make a 2D game :)

Here is my way of working:

I have a class GlRender

public class GlRenderer implements Renderer

In this class, on onDrawFrame I do GameRender() and GameDisplay()

And on gameDisplay() I have:

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
            // Reset the Modelview Matrix
            gl.glMatrixMode(GL10.GL_PROJECTION);    //Select The Modelview Matrix
            gl.glLoadIdentity();                    //Reset The Modelview Matrix


            // Point to our buffers
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

            // Set the face rotation
            gl.glFrontFace(GL10.GL_CW);

            for(Sprites...)
            {
                sprite.draw(gl, att.getX(), att.getY());
            }
            //Disable the client state before leaving
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

And in the draw method of sprite I have:

    _vertices[0] = x;
    _vertices[1] = y;

    _vertices[3] = x;
    _vertices[4] = y + height;

    _vertices[6] = x + width;
    _vertices[7] = y;   

    _vertices[9] = x + width;
    _vertices[10] = y + height; 


    if(vertexBuffer != null)
    {
        vertexBuffer.clear();
    }
    // fill the vertexBuffer with the vertices
    vertexBuffer.put(_vertices);
    // set the cursor position to the beginning of the buffer
    vertexBuffer.position(0);


    // bind the previously generated texture
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

    // Point to our vertex buffer
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer.mByteBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer.mByteBuffer);

    // Draw the vertices as triangle strip
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, _vertices.length / 3);

My problem is that I have a low frame rate, even at 30 FPS I loose some frame sometimes with only 1 sprite (but it is the same with 50)

Am I doing something wrong? How can I improve FPS?

In general, you should not be changing your vertex buffer for every sprite drawn. And by "in general", I pretty much mean "never," unless you're making a particle system. And even then, you would use proper streaming techniques, not write a quad at a time.

For each sprite, you have a pre-built quad. To render it, you use shader uniform s to transform the sprite from a neutral position to the actual position you want to see it on screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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