LWJGL Forum

Programming => OpenGL => Topic started by: MWulf on November 26, 2010, 13:41:58

Title: VBO's and glDrawElements [Solved]
Post by: MWulf on November 26, 2010, 13:41:58
I am having trouble getting index buffers going. I've searched through looking at all different examples, but can't seem to get anything going.

Setting up the buffers

       buffers = BufferUtils.createIntBuffer(NUM_BUFFERS);
       glGenBuffers(buffers);
       buffers.get(bufferHandle);
       
       float vertexInfo[] = {        // Defines vertices for triangle based pyramid
        0.0f, 1.0f, 0.0f,
        -1.0f, -1.0f, 1.0f,
        1.0f, -1.0f, 1.0f,
        0.0f, -1.0f, -1.0f
       };        int indexInfo[] = { // Defines the index to make it
        0, 2, 1,
        0, 3, 2,
        0, 1, 3,
        1, 2, 3
       };
    // Convert to passable types
       vertexBuffer = addBufferData(bufferHandle[VERTEX_BUFFER], vertexInfo);
       indexBuffer = addBufferData(bufferHandle[INDEX_BUFFER], indexInfo);
       glEnableClientState(GL_VERTEX_ARRAY);
       glVertexPointer(3, GL_FLOAT, 0, 0);


How I try to feed the buffers, with an IntBuffer version for the index array done the same...
   private FloatBuffer addBufferData(int bufferHandle, float[] data) {
       FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
       buffer.put(data);
       buffer.rewind();
       glBindBuffer(GL_ARRAY_BUFFER, bufferHandle);
       glBufferData(GL_ARRAY_BUFFER, data.length, GL_STATIC_DRAW);
       
       return buffer;
   }


The render part
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
   
    glLoadIdentity();
       glTranslatef(0.0f, 0.0f, -6.0f);

       glDrawElements(GL_TRIANGLES, 12, GL_INT, 0);


I've tried everything I can think of. I assume I am just plain getting the idea wrong?

Edit: There is a mistake in that I had data.length in my glBufferData call instead of the buffer holding the data and that the call to DrawElements should be GL_UNISIGNED_INT not GL_INT. Thanks!
Title: Re: VBO's and glDrawElements
Post by: Matthias on November 26, 2010, 13:53:34
your addBufferData() method does not upload any data.
Title: Re: VBO's and glDrawElements
Post by: ryanm on November 26, 2010, 14:00:42
Yeah, call glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW) instead of         glBufferData(GL_ARRAY_BUFFER, data.length, GL_STATIC_DRAW).

Incidentally, what does glBufferData( target, dataSize, usage ) actually do?
Title: Re: VBO's and glDrawElements
Post by: Matthias on November 26, 2010, 14:18:14
It allocates an uninitialized BO which can then be mapped to client memory using glMapBuffer or glMapBufferRange