Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android OpenGL 2.0与VBOs_Android_Performance_Opengl Es_Buffer - Fatal编程技术网

Android OpenGL 2.0与VBOs

Android OpenGL 2.0与VBOs,android,performance,opengl-es,buffer,Android,Performance,Opengl Es,Buffer,我正在用Android OpenGL 2.0制作游戏。我需要每帧刷新UV和顶点缓冲区。我使用以下代码在曲面上绘制: GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,mTexture); GLES20.glEnableVertexAttribArray(mPositionHandle); GLES20.

我正在用Android OpenGL 2.0制作游戏。我需要每帧刷新UV和顶点缓冲区。我使用以下代码在曲面上绘制:

         GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
         GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,mTexture);

         GLES20.glEnableVertexAttribArray(mPositionHandle);
         GLES20.glEnableVertexAttribArray ( mTexCoordLoc );

         GLES20.glVertexAttribPointer(mPositionHandle, 3,
                                         GLES20.GL_FLOAT, false,
                                         0, vertexBuffer);
         GLES20.glVertexAttribPointer ( mTexCoordLoc, 2, GLES20.GL_FLOAT,
                        false,
                        0, uvBuffer);

          GLES20.glUniformMatrix4fv(mtrxhandle, 1, false, matrix, 0);
          GLES20.glUniform1i ( mSamplerLoc, 0);

          GLES20.glDrawElements(GLES20.GL_TRIANGLES, mIndices.length,
                   GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

          GLES20.glDisableVertexAttribArray(mPositionHandle);
          GLES20.glDisableVertexAttribArray(mTexCoordLoc);
使用以下命令刷新缓冲区:

private void updateVerticesBuffer()
        {
            ByteBuffer bb = ByteBuffer.allocateDirect(mVertices.length * 4);
            bb.order(ByteOrder.nativeOrder());
            vertexBuffer = bb.asFloatBuffer();
            vertexBuffer.put(mVertices);
            vertexBuffer.position(0);
        }

        private void updateUvsBuffer()
        {
            ByteBuffer bb = ByteBuffer.allocateDirect(mUvs.length * 4);
            bb.order(ByteOrder.nativeOrder());
            uvBuffer = bb.asFloatBuffer();
            uvBuffer.put(mUvs);
            uvBuffer.position(0);
        }
并使用此着色器:

/* SHADER Solid
 * 
 * This shader is for rendering a colored primitive.
 * 
 */
public static final String vs_SolidColor =
    "uniform    mat4        uMVPMatrix;" +
    "attribute  vec4        vPosition;" +
    "void main() {" +
    "  gl_Position = uMVPMatrix * vPosition;" +
    "}";

public static final String fs_SolidColor =
    "precision mediump float;" +
    "void main() {" +
    "  gl_FragColor = vec4(0.5,0,0,1);" +
    "}"; 

/* SHADER Image
 * 
 * This shader is for rendering 2D images straight from a texture
 * No additional effects.
 * 
 */
public static final String vs_Image =
    "uniform mat4 uMVPMatrix;" +
    "attribute vec4 vPosition;" +
    "attribute vec2 a_texCoord;" +
    "varying vec2 v_texCoord;" +
    "void main() {" +
    "  gl_Position = uMVPMatrix * vPosition;" +
    "  v_texCoord = a_texCoord;" +
    "}";

public static final String fs_Image =
    "precision mediump float;" +
    "varying vec2 v_texCoord;" +
    "uniform sampler2D s_texture;" +
    "void main() {" +
    "  gl_FragColor = texture2D( s_texture, v_texCoord );" +
    "}"; 
是否可以更快地刷新缓冲区?VBO比这快吗?
如何在此代码中使用VBOs?我在网上搜索,但没有找到任何好的教程。我需要将此代码转换为使用VBOs。

如果您在每一帧更新顶点,VBOs很可能不会更快,但您仍然可以尝试。创建VBO时您面临什么问题?这不是火箭科学,你所需要做的就是创建缓冲区,将数据发送给它。然后在绘制之前绑定缓冲区,并记住将NULL设置为属性指针。代码的其余部分应该大致相同。因此,不保存vertexBuffer,只需保存生成时收到的缓冲区ID即可。我是新手,需要一些示例代码来理解。你知道如何提高缓冲区的性能吗?我看到安德林的缓冲区比正常速度快5倍,但我不知道如何实现这个缓冲区。