OpenGL ES:一个VBO - 几个精灵 - 分别翻译



我想知道如何单独访问和操作每个精灵,当这些精灵的顶点存储在同一个 VBO 中时。出于性能原因,我为我的精灵使用纹理图集,然后将其映射到顶点数组。我尝试将精灵拆分为单独的顶点数组,并将它们放在单独的 VBO 中。但是,如果我绘制 100 个精灵,这会影响性能,因为每次调用都必须进行大量单独的抽奖。相反,我想使用相同的 VBO,并从中分别翻译每个精灵。这可能吗?

源代码

     @Override
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
    vertexBuffer = GLData.createVertices(nSprites, vertices);
    GLData.createUvsData(nSprites, alienUvs, uvBufferAlien);
    //drawListBuffer = GLData.createIndices(indices, nSprites);
    GLData.createVertexBufferObject(vertexBuffer,nSprites, uvBufferAlien, bufferId.length, bufferId);
    createCamera();
    GLES20.glEnable(GLES20.GL_CULL_FACE);
    GLES20.glEnable(GLES20.GL_BLEND);
    GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA);
    // get shadersource
    final String vertexShader = getVertexShader();
    final String fragmentShader = getFragmentShader();
    final int vertexShaderHandle = ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, vertexShader);
    final int fragmentShaderHandle = ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);
    mProgramHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle,
            new String[] {"a_Position",  "a_Color", "a_Normal", "a_TexCoordinate"});
    //create texture
    textureHandle = TextureHelper.loadTexture(context);
    GLES20.glUseProgram(mProgramHandle);
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix");
    mMVMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVMatrix");
    //mColorHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Color");
    mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_TexCoordinate");
    mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position");
}
@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
    game_width = width;
    game_height = height;
    GLES20.glViewport(0, 0, width, height);

    // Create a new perspective projection matrix. The height will stay the same
    // while the width will vary as per aspect ratio.
    final float ratio = (float) width / height;
    final float left = -ratio;
    final float right = ratio;
    final float bottom = -1.0f;
    final float top = 1.0f;
    final float near = 1f;
    final float far = 20.0f;
    Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
}

@Override
public void onDrawFrame(GL10 unused) {
    Matrix.setIdentityM(mModelMatrix, 0);
    Matrix.translateM(mModelMatrix, 0, 2, 0f, -7f);
    draw();
}
private void draw() {
    //uvs
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, bufferId[0]);
    GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);
    GLES20.glVertexAttribPointer(mTextureCoordinateHandle, mTextureCoordinateDataSize, GLES20.GL_FLOAT, false, 0, 0);

    //vertices
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, bufferId[1]);
    GLES20.glEnableVertexAttribArray(mPositionHandle);
    GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, 0);

    // Clear the currently bound buffer (so future OpenGL calls do not use this buffer).
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

                         /* MATRIX */
    // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
    // (which currently contains model * view).
    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
    // Pass in the modelview matrix.
    GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0);
    // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
    // (which now contains model * view * projection).
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
    // Pass in the combined matrix.
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
    // Draw the sprites
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6 * nSprites);
}

顶点着色器

 attribute vec4 a_Position;
 uniform mat4 u_MVMatrix;
 uniform mat4 u_MVPMatrix;
 void main()       {
    gl_Position = u_MVPMatrix * a_Position;
 }

您可以上传一个统一的矩阵数组,并将索引作为每个精灵的每个顶点属性上传到其中,以便为每个精灵提供唯一的矩阵(用于外观骨骼动画模型的技术,但在这里也可以(。

但是,对于简单的精灵,上传矩阵的成本几乎与更新位置数组一样昂贵,并且您需要担心的统一数组的大小有限制。您最好只是在软件中对它们进行动画处理,并在每一帧中为批处理中的每个顶点上传一个新位置。将位置拆分为与纹理坐标等分开的缓冲区,以最大程度地减少上传每帧的带宽。

最新更新