如何更有效地绘制Opengl元素

时间:2013-04-17 18:53:48

标签: android performance object opengl-es-2.0

我发布了我的绘图方法,称为每帧。

我更改每帧的顶点以移动对象(基本上是一个精灵/纹理四边形)。

正如你所看到的那样,我最初每帧都在创建一个数组,但我现在已经改变了这一点,我最初创建数组并且每帧都更新它,但是我想知道我是否可以做更多的事来提高效率? (虽然我得到大约90fps的精灵不会一直平稳地移动,但它偶尔会暂停一瞬间)。我看不到垃圾收集器运行,但我猜它是由于分配)。

当我添加更多的精灵/四边形时,抽搐变得更糟,但事件在100+四边形,虽然光滑度几乎消失了,我的帧率仍然在60fps左右,所以我无法理解是什么减慢了这个? / p>

我还添加了Allocation Tracker的屏幕截图

任何帮助都将不胜感激。

public void drawTest(float x, float y, float[] mvpMatrix){

//Convert Co-ordinates

//Left
xPlotLeft = (-MyGLRenderer.ratio)+((x)*MyGLRenderer.coordStepAmountWidth);
//Top
yPlotTop = +1-((y)*MyGLRenderer.coordStepAmountHeight);
//Right
xPlotRight = xPlotLeft+((quadWidth)*MyGLRenderer.coordStepAmountWidth);
//Bottom
yPlotBottom = yPlotTop-((quadHeight)*MyGLRenderer.coordStepAmountHeight);


//    Following has been changed as per below. I am now declaring the array initially and just updating it every frame.
//  float[] vertices = {

        //Top Left
//          xPlotLeft,yPlotTop,0, 0,0,
        //Top Right
//          xPlotRight,yPlotTop,0, 1,0,
        //Bottom Left
//          xPlotLeft,yPlotBottom,0, 0,1,
        //Bottom Right
//          xPlotRight,yPlotBottom,0, 1,1
//          };

vertices[0]=xPlotLeft;
vertices[1]=yPlotTop;
vertices[2]=0;
vertices[3]=0;
vertices[4]=0;

vertices[5]=xPlotRight;
vertices[6]=yPlotTop;
vertices[7]=0;
vertices[8]=1;
vertices[9]=0;

vertices[10]=xPlotLeft;
vertices[11]=yPlotBottom;
vertices[12]=0;
vertices[13]=0;
vertices[14]=1;

vertices[15]=xPlotRight;
vertices[16]=yPlotBottom;
vertices[17]=0;
vertices[18]=1;
vertices[19]=1;

 vertexBuf = ByteBuffer.allocateDirect(vertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
 vertexBuf.put(vertices).position(0);
    //GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
//Bind texture
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texID);      
//Use program
GLES20.glUseProgram(iProgId);
// Combine the rotation matrix with the projection and camera view
    Matrix.multiplyMM(mvpMatrix2, 0, mvpMatrix, 0,  mRotationMatrix, 0);
// get handle to shape's transformation matrix
    mMVPMatrixHandle = GLES20.glGetUniformLocation(iProgId, "uMVPMatrix");
    // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix2, 0);
    //Set starting position for vertices (0 for position)
vertexBuf.position(0);
//Specify attributes for vertex
GLES20.glVertexAttribPointer(iPosition, 3, GLES20.GL_FLOAT, false, 5 * 4, vertexBuf);
//Enable attribute for position
GLES20.glEnableVertexAttribArray(iPosition);
//Set starting position for vertices (3 for texture)
vertexBuf.position(3);
//Specify attributes for vertex
GLES20.glVertexAttribPointer(iTexCoords, 2, GLES20.GL_FLOAT, false, 5 * 4, vertexBuf);
//Enable attribute for texture
GLES20.glEnableVertexAttribArray(iTexCoords);
//Enable Alpha blending and set blending function
GLES20.glEnable(GLES20.GL_BLEND); 
GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA);
//Draw
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
//Disable Alpha blending
GLES20.glDisable(GLES20.GL_BLEND);

}

enter image description here

1 个答案:

答案 0 :(得分:1)

ByteBuffer.allocateDirect()每帧在内存中分配一个新缓冲区,您可以创建一个初始缓冲区并覆盖内容。只需在put()之前使用rewind()position(0)

为了进一步改善问题,请使用VBO(顶点缓冲对象,有许多在线教程,以及关于此主题的若干问题)和glBufferSubData来更新缓冲区。