我无法理解我的 VBO 代码是否正确。下面是我的代码,它正在工作,但不是 VBO,然后我展示了我在尝试让 VBO 工作时所做的工作,我已经取得了一些成功,因为我的 Sprites/Quads 被绘制了,但是当它们是绘制,我没有看到任何性能提升。有人可以建议我是否正确执行此操作,只有两个专门针对 Java 中 Android 上的 VBO 的 tuts 之一(我看过 C 示例,但对它们没有任何兴趣)。
我不确定将一些代码放入构造函数中是否正确 - 每次绘制精灵时都不应调用此代码吗?
任何帮助,将不胜感激。
标准(非 VBO 代码、注意、X/YplotLeft/Right/Top/Bottom 已正确定义)
//This code is the from the drawSprite method of my VBO Class.
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;
//Reset Byte Buffer position
vertexBuf.rewind();
vertexBuf.put(vertices).position(0);
//Bind the texture
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texID);
//Use the program created earlier
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
vertexBuf.position(0);
//Specify attributes for vertex (X,Y,Z)
GLES20.glVertexAttribPointer(iPosition, 3, GLES20.GL_FLOAT, false, 5 * 4, vertexBuf);
//Enable attribute for position
GLES20.glEnableVertexAttribArray(iPosition);
//Set starting position for vertices (texture)
vertexBuf.position(3);
//Specify attributes for vertex (S T for textures)
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);
VBO 代码
在类级别定义:
//Buffer object for VBO (We will copy data from floatBuffer into this)
final int buffers[] = new int[1];
我已将以下代码添加到我的 VBO 类的构造函数中:
//Generate buffers as needed
GLES20.glGenBuffers(1, buffers, 0);
// Bind to the buffer. Future commands will affect this buffer specifically.
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexBuf.capacity() * 4,vertexBuf, GLES20.GL_STATIC_DRAW);
//drawSprite方法的主要VBO代码:
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;
//Reset Byte Buffer position
vertexBuf.rewind();
vertexBuf.put(vertices).position(0);
// Transfer data from client memory
GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, 0, vertexBuf.capacity() * 4,vertexBuf);
//Bind the 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);
//Specify attributes for vertex (X,Y,Z)
GLES20.glVertexAttribPointer(iPosition, 3, GLES20.GL_FLOAT, false, 5 * 4, 0);
//Enable attribute for position
GLES20.glEnableVertexAttribArray(iPosition);
//Specify attributes for vertex S & T
GLES20.glVertexAttribPointer(iTexCoords, 2, GLES20.GL_FLOAT, false, 5 * 4, 3 * 4);
//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);
}