Android VBO implementation.

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

Android VBO implementation.

Postby Resilient » Wed May 19, 2010 4:22 am

Its my understanding that there is a lot to be gained by using VBO's but am having little luck in finding any information about implementing these on the Android. Does anyone know of any example code or documentation that talks about implementing VBO's?

Thanks
Justin
Resilient
Junior Developer
Junior Developer
 
Posts: 15
Joined: Tue May 18, 2010 1:33 am

Top

Re: Android VBO implementation.

Postby Resilient » Wed May 19, 2010 7:43 am

Alright, I am almost up and running following the information in this thread:
viewtopic.php?p=27227

But, I am having a texture issue. I got a helicopter, it works fine with old school standard non-VBO style implementation.

But, with the VBO implementation the texture gets hosed. I can change the order of the verticies and salvage 3 out of 4 corner of the texture, but at best one corner of the texture is missing or corrupted.

Here is where I load the buffers. This texCoords array works fine in non-VBO mode but totally fails to show the texture in VBO mode. Also I have tried shading the quad with a color in VBO mode and it looks fine, so the shape is coming through fine. Its just the texture that is messed up.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Sprite{
  2.         public Sprite(Context context, GL10 gl, int resourceId){
  3.                 GL11 gl11 = (GL11)gl;
  4.  
  5.                 xPos = 0.25f;
  6.                 yPos = 0.25f;
  7.                 short[] mIndicesArray = {1,0,2,3};
  8.                
  9.                 float[] coords = {
  10.                                 -0.1f, -0.1f, 0f, //0 bottom left
  11.                                 -0.1f, 0.1f, 0f, //1 top left
  12.                                 0.1f, 0.1f, 0f, //2 top right
  13.                                 0.1f, -0.10f, 0f //3 bottom right
  14.                 };
  15.                 mNrOfVertices = 4;
  16.                
  17.                 float[] texCoords = {
  18.                                 0.0f, 0.0f,
  19.                                 1.0f, 0.0f,
  20.                                 1.0f, 1.0f,
  21.                                 0.0f, 1.0f
  22.                 };
  23.                
  24.                 mContext = context;
  25.                 loadTexture(gl,resourceId);
  26.                 ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
  27.                 vbb.order(ByteOrder.nativeOrder());
  28.                 mVertexBuffer = vbb.asFloatBuffer();
  29.                
  30.                 ByteBuffer ibb = ByteBuffer.allocateDirect(mIndicesArray.length * 2);
  31.                 ibb.order(ByteOrder.nativeOrder());
  32.                 mIndexBuffer = ibb.asShortBuffer();
  33.                
  34.                 ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
  35.         tbb.order(ByteOrder.nativeOrder());
  36.         mTexBuffer = tbb.asFloatBuffer();
  37.        
  38.  
  39.                 mVertexBuffer.put(coords);
  40.                 mIndexBuffer.put(mIndicesArray);
  41.                 mTexBuffer.put(texCoords);
  42.                
  43.                 mVertexBuffer.position(0);
  44.                 mIndexBuffer.position(0);
  45.                 mTexBuffer.position(0);
  46.                
  47.                 int[] buffer = new int[1];
  48.                 gl11.glGenBuffers(1, buffer, 0);
  49.                 vertexBufferIndex = buffer[0];
  50.                 gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexBufferIndex);
  51.                 gl11.glBufferData(GL11.GL_ARRAY_BUFFER, 12 * 4, mVertexBuffer, GL11.GL_STATIC_DRAW);
  52.                
  53.                 gl11.glGenBuffers(1, buffer, 0);
  54.                 textureBufferIndex = buffer[0];
  55.                 gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, textureBufferIndex);
  56.                 gl11.glBufferData(GL11.GL_ARRAY_BUFFER, 8 * 4, mTexBuffer, GL11.GL_STATIC_DRAW);
  57.                
  58.                 gl11.glGenBuffers(1, buffer, 0);
  59.                 indexBufferIndex = buffer[0];
  60.                 gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexBufferIndex);
  61.                 gl11.glBufferData(GL11.GL_ELEMENT_ARRAY_BUFFER, 4 * 2, mIndexBuffer, GL11.GL_STATIC_DRAW);
  62.                
  63.                 gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);
  64.                 gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
  65.         }
  66.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


And here is the portion where the quad gets drawn. currently the non-VBO lines are commented out.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void draw(GL10 gl, float angle, float x, float y) {
  2.                 gl.glEnable(GL10.GL_TEXTURE_2D);
  3.                 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  4.                 gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  5.  
  6.         gl.glPushMatrix();
  7.         gl.glTranslatef(x, y, 0f);
  8.         gl.glRotatef(angle, 0.0f,0.0f, 1.0f);
  9.  
  10.         GL11 gl11 = (GL11)gl;
  11.         gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexBufferIndex);
  12.         gl11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
  13.         //gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
  14.  
  15.                 gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, textureBufferIndex);
  16.         gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexBufferIndex);
  17.         //gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);
  18.         gl11.glTexCoordPointer(3, GL11.GL_FLOAT, 0, 0);
  19.                 //gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexBuffer);
  20.  
  21.                 //gl.glDrawElements(GL11.GL_TRIANGLE_STRIP, mNrOfVertices, GL11.GL_UNSIGNED_SHORT, mIndexBuffer);
  22.                 gl11.glDrawElements(GL11.GL_TRIANGLE_STRIP, mNrOfVertices, GL11.GL_UNSIGNED_SHORT, 0);
  23.                 gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);
  24.                 gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);
  25.                
  26.                 gl.glPopMatrix();
  27.                
  28.         }
  29.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Any help here would be appreciated. I know the code is kind of messy and could be a lot better, but this is just a first pass trying to get anything to display correctly.
Resilient
Junior Developer
Junior Developer
 
Posts: 15
Joined: Tue May 18, 2010 1:33 am

Re: Android VBO implementation.

Postby Resilient » Mon May 24, 2010 2:23 am

This line was the only problem:

gl11.glTexCoordPointer(3, GL11.GL_FLOAT, 0, 0);

The texture array only had 2 coordinates per vertex. For some reason, this didn't break the non VBO version (but I think it should have) but it did break the VBO version.

This line should have been:
gl11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);
Resilient
Junior Developer
Junior Developer
 
Posts: 15
Joined: Tue May 18, 2010 1:33 am

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests