我正在尝试将简单的纹理四边形渲染为VBO。当不使用VBO时,一切都按预期工作(但是,对于静态对象,我仍在每帧更新我的顶点数据,这似乎毫无意义,因此切换到VBO)。
但是,我必须做错事,因为我什么都没有,绝对是zilch,呈现在屏幕上。
以下是我的相关代码:
构造
public VBO() {
updateVertexData();
vertexBuf = ByteBuffer.allocateDirect(vertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
vertexBuf.put(vertices);
vertexBuf.position(0);
//Create buffers array for VBO (I'm using interleaved data so only 1 needed here??
final int buffers[] = new int[1];
GLES20.glGenBuffers(1, buffers, 0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexBuf.capacity() * 4, vertexBuf, GLES20.GL_STATIC_DRAW);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
mBufferIdx = buffers[0];
vertexBuf.limit(0);
vertexBuf = null;
}
顶点更新
public void updateVertexData(){
vertices[0]=-1;
vertices[1]=1f;
vertices[2]=0;
vertices[3]=0;
vertices[4]=0;
vertices[5]=1;
vertices[6]=1f;
vertices[7]=0;
vertices[8]=1;
vertices[9]=0;
vertices[10]=-1;
vertices[11]=-1f;
vertices[12]=0;
vertices[13]=0;
vertices[14]=1;
vertices[15]=1;
vertices[16]=-1f;
vertices[17]=0;
vertices[18]=1;
vertices[19]=1;
}
渲染
public void drawVBO(){
GLES20.glUseProgram(iProgId);
mMVPMatrixHandle = GLES20.glGetUniformLocation(iProgId, "uMVPMatrix");
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mBufferIdx);
GLES20.glEnableVertexAttribArray(iPosition);
GLES20.glVertexAttribPointer(iPosition, 3, GLES20.GL_FLOAT, false, 5 * 4, 0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mBufferIdx);
GLES20.glEnableVertexAttribArray(iTexCoords);
GLES20.glVertexAttribPointer(iTexCoords, 2, GLES20.GL_FLOAT, false, 5 * 4, 3);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
}
就像我说的,如果我删除VBO代码并将代码从 updateVertexData 移回我的draw方法,那么每个都可以正常工作。如果有人能解释我在这里做错了什么,我将不胜感激....
答案 0 :(得分:0)
问题是第二个' glVertexAttribPointer '线的偏移量。
原创
GLES20.glVertexAttribPointer(iTexCoords, 2, GLES20.GL_FLOAT, false, 5 * 4, 3);
'offset'参数(结尾的'3')错误。 3是位置数据(x,y和z)的大小,这需要乘以4(每个浮点数的字节数)。所以......
更正代码
//For readability
final int POSITION_SIZE = 3;
final int BYTES_PER_FLOAT = 4;
final int TEXTURE_SIZE = 2;
final int STRIDE = (POSITION_SIZE+TEXTURE_SIZE)*BYTES_PER_FLOAT
//Correct line
GLES20.glVertexAttribPointer(iTexCoords, 2, GLES20.GL_FLOAT, false, STRIDE, (POSITION_SIZE*BYTES_PER_FLOAT));