我想知道当这些精灵的顶点存储在同一个VBO中时,如何单独访问和操作每个精灵。出于性能原因,我为我的精灵使用了textureatlas,然后将它们映射到顶点阵列。我尝试将精灵分成不同的顶点数组,并将它们放在单独的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;
}
答案 0 :(得分:2)
您可以上传一个统一的矩阵数组,并将其作为每个精灵的每顶点属性的索引,为每个精灵提供一个独特的矩阵(用于蒙皮骨骼动画模型的技术,但也适用于此)。 / p>
然而,对于简单的精灵来说,上传矩阵的成本几乎与更新位置数组一样昂贵,并且您需要担心的统一阵列的大小有限。您可能最好只在软件中为它们设置动画,并在每个帧中为批处理中的每个顶点上传新位置。将位置分成单独的缓冲区,例如,纹理坐标,以最小化每帧上传的带宽。