Android中的渲染效率 - OpenGL ES 10

时间:2014-12-03 20:25:33

标签: android opengl-es

目前正在编写代码以在OpenGL()10中创建背景和3D对象。虽然在更改了单位矩阵后渲染了对象,但我遇到了问题。无论出于何种原因,代码都是非常低效的,并且对象根本不会顺利移动,它会逐渐跳跃。

public void onDrawFrame(GL10 gl) {
    // Clear the screen to black
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
   background.loadTexture(gl, mContext, R.drawable.room);
    // Position model so we can see it
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glTranslatef(0, 0, -20.0f);
    gl.glScalef(7.5f, 7.5f, 0);

    //gl.glTranslatef(tempX, tempY, 0);

    //Set rotation
    gl.glRotatef(0, 0, 0, 0); //glRotatef(angle, x, y, z) rotates each axis by a certain angle
    background.draw(gl);

    // Position model so we can see it
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glTranslatef(tempX, tempY, -10f);

    gl.glScalef(counter*1f,counter*1f,counter*1f);
    System.out.println("Z value is now 1f*"+counter+" and tempX is "+tempX+" and tempY is "+tempY +"*****************************");
    gl.glPushMatrix();

    //gl.glTranslatef(tempX, tempY, 0);

    //Set rotation
    gl.glRotatef(-60, 1, 1, 0); //glRotatef(angle, x, y, z) rotates each axis by a certain angle

    cube.loadTexture(gl, mContext, R.drawable.wood);
    // Draw the model
    cube.draw(gl);
}

立方体类如下所示:

class MyCube {
    private final IntBuffer vertexBuffer;          //Buffers store vertex data for improved performance
    private final IntBuffer textureBuffer;         //Buffers store texture data for improved performance
    static int one = 65536;                            //Length of a side
    int half = one / 2;

//Cube data
public MyCube() {

    int vertices[] = {                          //Sets coordinates of the vertices.
            // FRONT
            -half, -half, half, half, -half, half, //Sets x, y and z coordinates for each of the four vertices of the front face
            -half, half, half, half, half, half,
            // BACK
            -half, -half, -half, -half, half, -half,
            half, -half, -half, half, half, -half,
            // LEFT
            -half, -half, half, -half, half, half,
            -half, -half, -half, -half, half, -half,
            // RIGHT
            half, -half, -half, half, half, -half,
            half, -half, half, half, half, half,
            // TOP
            -half, half, half, half, half, half,
            -half, half, -half, half, half, -half,
            // BOTTOM
            -half, -half, half, -half, -half, -half,
            half, -half, half, half, -half, -half,};

    int texCoords[] = {                             //Sets values of texture coordinates. (Only u and v. Not x, y, and z.)
            // FRONT
            0, one, one, one, 0, 0, one, 0,
            // BACK
            one, one, one, 0, 0, one, 0, 0,
            // LEFT
            one, one, one, 0, 0, one, 0, 0,
            // RIGHT
            one, one, one, 0, 0, one, 0, 0,
            // TOP
            one, 0, 0, 0, one, one, 0, one,
            // BOTTOM
            0, 0, 0, one, one, 0, one, one,};

    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asIntBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    ByteBuffer tbb = ByteBuffer.allocateDirect(vertices.length * 4);
    tbb.order(ByteOrder.nativeOrder());
    textureBuffer = tbb.asIntBuffer();
    textureBuffer.put(texCoords);
    textureBuffer.position(0);
}

//Draws cube
public void draw(GL10 gl) {
    gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertexBuffer);         //glVertexPointer(size (number coords per vertex), type (data type of coords, stride (byte offset between consecutive vertices), pointer (pointer to first coord in the array)

    gl.glEnable(GL10.GL_TEXTURE_2D); // workaround bug 3623
    gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, textureBuffer);


    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);                  //Render primitives from array data. (mode (specifies kind of primitives, first (specifies starting index of arrays), count (number of indices to be rendered))
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);
}


static void loadTexture(GL10 gl, Context context, int resource) {
    Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resource);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    bmp.recycle();
}

}

关于代码的任何指针?

1 个答案:

答案 0 :(得分:0)

看起来你每帧都在调用cube.loadTexture()。这可能非常慢,在初始化期间只执行一次就足够了。

另外,我不确定您是否正确加载纹理。通常,在加载纹理之前,生成纹理ID(使用glGenTextures)并将其绑定(使用glBindTexture)。您可能也想研究这个问题。