3

I am trying Rajawali library for Android to draw some basic 3d objects on the scene. There is a sample 2000 planes, which show how to render huge amount of vertexes very "cheaply" with one shader. I can pass array of vertexes to the shader and draw them.

I want to upgrade this sample and move some vertexes independently from each other.

I've tried glBufferSubData, but i cant change anything on the scene. The one way i've found is to change data and recreate buffer, but i hope there is a simpler way to only change the necessary data.

For example, I only want to change some positions from already created and binded buffer.

initialize positions

    float[] planePositions = new float[numVertices * 3];
    ....

create buffer

    mGeometry.createBuffer(mPlanePositionsBufferInfo, BufferType.FLOAT_BUFFER, mPlanePositions,     GLES20.GL_ARRAY_BUFFER);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

set positions

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, planePosBufferHandle);
    GLES20.glEnableVertexAttribArray(maPlanePositionHandle);
    fix.android.opengl.GLES20.glVertexAttribPointer(maPlanePositionHandle, 3, GLES20.GL_FLOAT,
            false, 0, 0);   

and how can i modify position of, for example, the first vertex of the first plane ? (planePositions[0] = a;planePositions[1] = b; planePositions[1] = c... and now i have to pass this modified array to opengl but don't know how).

2
  • Do you need to lock the buffer before modifying it? I am very new to this too and using Ogre3d with c++ however the principle should still be the same. You should be able to get the current vertex buffer, lock it, modify it, then unlock it. I don't think you have to do anything else but I may be completely wrong. Sorry I'm not much help. Commented Jul 17, 2012 at 11:12
  • glBufferSubData should work just fine, can you show what you tried that didn't work? Commented Jul 17, 2012 at 15:23

1 Answer 1

3

All you need to do is modify the buffer data then call mGeometry.changeBufferData() from the PlanesGalore class. I put the following code in an update method which is called during the onDrawFrame() event of my Renderer class.

for (int i = 0; i < vertices.length; i += 3) {
   mPlanePositions= mPlanePositions.put(i, mPlanePositions.get(i) - 0.01f);
}

mGeometry.changeBufferData(mCubePositionsBufferInfo, mPlanePositions, 0);

This is the best way I could find for accomplishing the movements. Additionally, I have proposed a change in this issue that would let you specify what part of the buffer changed which should provide increased performance.

EDIT

My change to the engine has been approved so you can now optimize this even further like so.

// The offset position should be vertices.length * object #. In my use case this is 72*x
// allowing me to update the XYZ coordinates of all 24 vertices of a cube.
int offsetPosition = 0;
mGeometry.changeBufferData(mCubePositionsBufferInfo, mPlanePositions, offsetPosition, offsetPosition + vertices.length);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.