My code has a LWJGL and Vertex Array problem.

I have the following code in my test app and I can see a square as expected when I run. However when I enable the color array bits, my square disapears, has any got any idea why?


    float[] lvColours  = {0f, 1f, 0.5f, 1.0f, 0f, 1f, 0.5f, 1.0f, 0f, 1f, 0.5f, 1.0f, 0f, 1f, 0.5f, 1.0f};
    float[] lvVertices = {-150f, -150f, -50f, -150f, 150f, -50f, 150f, 150f, -50f, 150f, -150f, -50f};

    GL.glEnableClientState(GL.GL_VERTEX_ARRAY);
//    GL.glEnableClientState(GL.GL_COLOR_ARRAY);

    GL.glVertexPointer(3, GL.GL_FLOAT, 0, lvVertices);
//    GL.glColorPointer (4, GL.GL_FLOAT, 0, lvColours);

    GL.glDrawArrays(GL.GL_QUADS, 0, lvVertices.length / 3);

    GL.glDisableClientState(GL.GL_VERTEX_ARRAY);
//    GL.glDisableClientState(GL.GL_COLOR_ARRAY);

I read somewhere that the only valid value for the size parameter on the glColorPointer method is 4, but I have also tried it with the value of 3 and removed the alpha values from my colour array, but still nothing.

Thanks,

Andy.

PS: I have some nice wrapper classes that use a scratch buffer so that I can pass float[] to my methods rather than have the buffer conversions all over the place.

I point the finger of blame here at the wrapper. Let’s see it.

Cas :slight_smile:

I borrowed this code from the forums on the puppygames.net website.


  private static       ByteBuffer scratch;
  private static final int        FLOAT_SIZE = 4;
  private static final int        BUFFER_SIZE = 8192 * 1024 * FLOAT_SIZE;

  static {
    scratch = ByteBuffer.allocateDirect(BUFFER_SIZE);
    scratch.order( ByteOrder.nativeOrder() );
  }
   
  private static FloatBuffer toBuffer( float[] params ) {
    scratch.clear();
    FloatBuffer buff = scratch.asFloatBuffer();
    buff.put( params );
    buff.flip();
    return buff;
  }
   
  public static void glVertexPointer( int size, int type, int stride, float[] buffer ) {
    glVertexPointer( size, stride, toBuffer( buffer ) );
  }

  public static void glColorPointer( int size, int type, int stride, float[] buffer ) {
    glColorPointer( size, stride, toBuffer( buffer ) );
  }

Any thoughts would be appreciated.

Thanks,

Andy.

Duh :slight_smile: You are simply overwriting your vertex data with colour data ::slight_smile: You need another buffer for the colour data. Or to interleave your data.

Cas :slight_smile:

Yeah, the gl*Pointer methods tell the GL where to find the data when needed, they don’t upload it at that time. When you get to glDrawArrays it then spools all the data from the buffers you mentioned.

Easiest fix, maintain a separate scratch buffer for each data type.

Thanks for the tip, I had the impression that when I called glVertexPointer and glColorPointer that the data would be referenced and used immidiately, not later when I call glDrawArray.

I guess the clue to the answer for this one could have been in the method names gl*Pointer

Mulitple buffers it is then!

Thanks,

Andy.