glDrawArrays throwing exception.

Problems with Canvas, OpenGL, etc...

glDrawArrays throwing exception.

Postby dgutie01 » Tue Jul 27, 2010 12:43 am

Hey guys..I've been banging my head against the wall with this for a few hours, so maybe you guys can help me out.
The code I've written works perfectly fine in the emulator, but when I try running it on my DroidX it crashes with the exception "ArrayIndexOutOfBounds".

Here is some code:
GL Initialization:
Code: Select all
      /*
       * Set the projection up for 2D rendering.
       */

      gl.glMatrixMode(GL10.GL_PROJECTION);

      // gl.glLoadIdentity();
      gl.glOrthof(0, width, 0, height, 0.01f, 100.0f);
      gl.glViewport(0, 0, width, height);

      gl.glMatrixMode(GL10.GL_MODELVIEW);
      // gl.glLoadIdentity();

      /*
       * Enable 2D textures, and alpha blending
       */
      gl.glEnable(GL10.GL_TEXTURE_2D);
      gl.glEnable(GL10.GL_BLEND);
      gl.glEnable(GL10.GL_DEPTH_TEST);
      gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);


The Draw:
Code: Select all
/*
       * Load up the buffers
       */
      ByteBuffer vbb = ByteBuffer
            .allocateDirect(localCoords.getData().length * 4);
      vbb.order(ByteOrder.nativeOrder());
      FloatBuffer vertexBuffer = vbb.asFloatBuffer();
      vertexBuffer.put(localCoords.getData());
      //vertexBuffer.position(0);
      vertexBuffer.flip();

      ByteBuffer tbb = ByteBuffer
            .allocateDirect(texSrcCoords.getData().length * 4);
      tbb.order(ByteOrder.nativeOrder());
      FloatBuffer textureBuffer = tbb.asFloatBuffer();
      textureBuffer.put(texSrcCoords.getData());
      textureBuffer.flip();
      //textureBuffer.position(0);
      
      
      gl.glPushMatrix();

      gl.glTranslatef(dimensions.getX(), dimensions.getY(), -layer);
      gl.glRotatef(rotationAngle, 0, 0, 1);

      gl.glScalef(scaleAmnt.getX(), scaleAmnt.getY(), 1.0f);

      gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

      gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
      gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

      if (OpenGLRenderer.curBoundTexID == 0
            || OpenGLRenderer.curBoundTexID != texID) {
         gl.glBindTexture(GL10.GL_TEXTURE_2D, texID);
         OpenGLRenderer.curBoundTexID = texID;
      }
      
   
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
      int err = gl.glGetError();
      

      gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

      gl.glPopMatrix();


The exception:
07-26 19:28:36.847 10995 11002 E AndroidRuntime: java.lang.IndexOutOfBoundsExcep
tion
07-26 19:28:36.847 10995 11002 E AndroidRuntime: at java.nio.FloatToByteBufferAdapter.get(FloatToByteBufferAdapter.java:155)


The exception is thrown after glDrawArrays goes and tries to do it's thing.

The array localCoords.getData() and texSrcCoords.getData() both return a float[8].

Like I said before, works fine on emulator and the only way I can get it to stop from crashing is by using the commented out "array.position(0)" calls in place of the "array.flip()" calls and by increasing the byte buffers capacity from "*4" to anything bigger then 4. But unfortunately, this just stops it from crashing and still fails to draw the texture just showing a white square.

I have no idea what's wrong, any ideas? Also, the texture is 32x32, no alpha channels, 32-bit png.

Here is the code used to load it:
Code: Select all
if (glObj == null)
         return -1;

      IntBuffer buf = IntBuffer.allocate(1);
      glObj.glGenTextures(1, buf);

      // Load up the texture:
      InputStream is = context.getResources().openRawResource(
            textureResourceID);
      Bitmap temp = null;
      try {
         temp = BitmapFactory.decodeStream(is);

      } finally {
         // Always clear and close
         try {
            is.close();
            is = null;
         } catch (IOException e) {
         }
      }
      Bitmap bmp = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(), temp
            .getHeight(), null, false);

      glObj.glBindTexture(GL10.GL_TEXTURE_2D, buf.get(0));

      GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);

      bmp.recycle();
      temp.recycle();



Thanks in advance for any help!
dgutie01
Freshman
Freshman
 
Posts: 2
Joined: Tue Jul 27, 2010 12:31 am

Top

Re: glDrawArrays throwing exception.

Postby dgutie01 » Tue Jul 27, 2010 7:03 pm

I've discovered that this issue only applies to Android devices running anything lower then 2.2. Any ideas what I'm using that isn't supported in the older OS's?
dgutie01
Freshman
Freshman
 
Posts: 2
Joined: Tue Jul 27, 2010 12:31 am

Top

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

Who is online

Users browsing this forum: No registered users and 1 guest