How to change the content of vertexbuffer used in glDrawArrays method in opengles
I have some triangle polygons and drawing them in a traditional way: (android-java code)
gl.glDrawArrays(GL10.GL_TRIANGLES, i, j);
I want to update the vertex coordinates of the triangles. All of 开发者_运维百科the tutorials I've found use an initial vertex data then only apply transformations to them. I need to change each vertex coordinate independently.
I change the content of the array which is used to create the related vertexbuffer but it doesn't make any change on the screen. Rebuilding the vertexbuffer on eachframe doesn't seem to be right way I guess.
Can you point out any example source code at least, if you know any?
You seem to be looking for glBufferSubData
. Basically, you update the contents of your array just as you've described, then you call glBufferSubData
to update the vertex buffer object with the new values.
This assumes you're modifying only a relatively small subset of the data. If you're modifying most of the data, it's generally better to just call glBufferData
again instead.
I found out that most part of my problem is java-android related. @jerry proposed the right solution to the main idea in my question but I'll adress the java-android related parts of the problems:
First; method signatures in Renderer interface of Android offers you a GL10 object as parameter but some of the methods you need are in opengl-es1.1 so you need to cast gl object.
public void onDrawFrame(GL10 gl) {
GL11 gl11 = (GL11)gl;
Ofcourse if the device doesn't support opengl-es1.1 your code won't work but that's out of our topic.
My other problem was about the differences in java implementation of opengl-es. In java you create a related Buffer object, which is a native java class in java.nio package, and fill inside of this buffer with an array you specified.
public static FloatBuffer createFloatBuffer(float[] array){
ByteBuffer byteBuf = ByteBuffer.allocateDirect(array.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
FloatBuffer fbuf = byteBuf.asFloatBuffer();
fbuf.put(array);
fbuf.position(0);
return fbuf;
}
After this part you have nothing to do with the array object. You need to update the buffer object. You can update the content of buffer by using put(index, value)
method of related buffer class.
Here is the draw() method of an object we want to draw. vertexBuffer
is a FloatBuffer
object. You can get the idea I think from this snippet.
public void draw(GL11 gl) {
vertexBuffer.put(index, floatValue);
gl.glBufferSubData(GL11.GL_ARRAY_BUFFER, 0, vertexBuffer.capacity(),vertexBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
You can correct me if there are misleading points..
I think what you want to do is create multiple vertex buffers on initialization and then just bind to different ones using glBindBuffer()
精彩评论