VBO & NIO issues

Hi,

I have some problems with VBOs and indexed geometry. If I use simple VBOs without index arrays everything works like expected. But if I try to build indexed geometry with unique vertices, the geometry is not drawn correctly. Only if i make the FloatBuffer 4 times larger everything works (3/4 of the vertex buffer is empty!!!).

I know that 1 float consists of 4 bytes but this is already managed by the BufferUtil. Perhaps some code makes it easyer to understand as my explanation attempt :).

This code should draw a rectangular field of triangles near the origin.

 
package engine.geometry.test;

import static javax.media.opengl.GL.*;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.media.opengl.GL;
import com.sun.opengl.util.BufferUtil;


public class TestIndexedGeometry {

 private FloatBuffer vb;
 private IntBuffer ib;
 private int[] buffer = new int[2];
 private boolean useIndexBuffer = false;
    
    
 public TestIndexedGeometry() {

     int size = 40; // size*size = number of vertices
     
     vb = BufferUtil.newFloatBuffer(size*size*3);  // <<<<<<<<<<<<< *4
     ib = BufferUtil.newIntBuffer((size-1)*(size-1)*2*3);
     
     // init indices
     //    0---3
     //    | \ |
     //    1---2
     for (int h = 0; h < size-1; h++) {
         for (int w = 0; w < size-1; w++) {
             
             // triangle 0 - 3 - 2
             ib.put(w + (h  )*(size)    );
             ib.put(w + (h  )*(size) + 1);
             ib.put(w + (h+1)*(size) + 1);
                 
             // triangle 0 - 2 - 1
             ib.put(w + (h  )*(size)    );
             ib.put(w + (h+1)*(size) + 1);
             ib.put(w + (h+1)*(size)    );
             
         }   
     }
     // init vertices
     for (int w = 0; w < size; w++)
         for (int h = 0; h < size; h++)
             vb.put(w*20).put(h*20).put(0);
     
     vb.rewind();
     ib.rewind();
 }

  
  public void initGL(GL gl) {
      
      gl.glGenBuffers(buffer.length, buffer, 0);
      
      gl.glBindBuffer(GL_ARRAY_BUFFER, buffer[0]); // vertex buffer
      gl.glBufferData(GL_ARRAY_BUFFER, vb.capacity(), vb, GL_STATIC_DRAW);
          
      gl.glBindBuffer(GL_ARRAY_BUFFER, 0);
      
      gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer[1]); // index buffer
      gl.glBufferData(GL_ELEMENT_ARRAY_BUFFER, ib.capacity(), ib, GL_STATIC_DRAW);
      
      gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  }

  public void render(GL gl) {
    
      gl.glColor3f(1, 0, 0);
    
      gl.glEnableClientState(GL_VERTEX_ARRAY);
    
      gl.glBindBuffer(GL_ARRAY_BUFFER, buffer[0]); // vertex buffer
      gl.glVertexPointer(3, GL_FLOAT, 0, 0);
    
    
      if(useIndexBuffer) {
        gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer[1]);
        gl.glDrawElements(GL_TRIANGLES, ib.capacity(), GL_UNSIGNED_INT, 0);
      }else{        
        gl.glDrawElements(GL_TRIANGLES, ib.capacity(), GL_UNSIGNED_INT, ib);
      }

      gl.glDisableClientState(GL_VERTEX_ARRAY);
  }
}

thank you in advance,

bienator

Problem solved ;D

gl.glBufferData(…) requires the size of the ByteBuffer (not FloatBuffer) and
gl.glDrawElements(…) the number of elements (vertices). I made the mistake to put the size of the IntBuffer (indices) as param to glDrawElements(…)

bienator