Hello Guest

VBO acts weird after switching to short indices

  • 2 Replies
  • 4861 Views
*

Marltoro

VBO acts weird after switching to short indices
« on: April 07, 2013, 22:41:32 »
I created a vertex buffer object that works fine when I use byte for the indices type. However, after switching to short indices the object is rendered incorrectly and the program runs extremely slowly. I have to use short indices because I'm going to be rendering heightmaps which requires a larger type than byte indices. Can anyone determine what's wrong with my code? I have a function that initializes the VBO and a function that renders the VBO.

Code: [Select]
// INSTANCE VARIABLES
float testRot = 180.0f;
int VBObuffer[] = new int[2];

// BUFFERS
ShortBuffer indicesBuffer = BufferUtils.createShortBuffer(8);
FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(24);

public void initVBO()
{
// VERTICES
// TOP
verticesBuffer.put(-50.0f);verticesBuffer.put(50.0f);verticesBuffer.put(100.0f);
verticesBuffer.put(50.0f);verticesBuffer.put(50.0f);verticesBuffer.put(100.0f);
verticesBuffer.put(-50.0f);verticesBuffer.put(-50.0f);verticesBuffer.put(100.0f);
verticesBuffer.put(50.0f);verticesBuffer.put(-50.0f);verticesBuffer.put(100.0f);
// RIGHT
verticesBuffer.put(50.0f);verticesBuffer.put(-50.0f);verticesBuffer.put(0.0f);
verticesBuffer.put(50.0f);verticesBuffer.put(50.0f);verticesBuffer.put(0.0f);
verticesBuffer.put(50.0f);verticesBuffer.put(-50.0f);verticesBuffer.put(100.0f);
verticesBuffer.put(50.0f);verticesBuffer.put(50.0f);verticesBuffer.put(100.0f);
// setup vertices
verticesBuffer.flip();

// INDICES
// TOP
indicesBuffer.put((short)0);
indicesBuffer.put((short)1);
indicesBuffer.put((short)2);
indicesBuffer.put((short)3);
// RIGHT
indicesBuffer.put((short)4);
indicesBuffer.put((short)6);
indicesBuffer.put((short)5);
indicesBuffer.put((short)7);
// setup indices
indicesBuffer.flip();

// vertices and indices
VBObuffer[0] = glGenBuffers();
VBObuffer[1] = glGenBuffers();

// setup vertice buffer
glBindBuffer(GL_ARRAY_BUFFER, VBObuffer[0]);
glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);

// setup vertices
glVertexPointer(3, GL_FLOAT, 0, 0);

// setup indice buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBObuffer[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
}

public void renderVBO()
{
// ENABLE SETTINGS
glEnableClientState(GL_VERTEX_ARRAY);

// PUSH MATRIX
glPushMatrix();
glRotatef(testRot, 1.0f, 0.0f, 0.0f);

// TOP
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, 0);
// RIGHT
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, 4);

// POP MATRIX
glPopMatrix();

// DISABLE SETTINGS
glDisableClientState(GL_VERTEX_ARRAY);
}
« Last Edit: April 08, 2013, 01:02:08 by Marltoro »

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: VBO acts weird after switching to short indices
« Reply #1 on: April 08, 2013, 07:58:52 »
The offset in glDrawElements is measured in bytes. (It is unclear as hell I know). There are 2 (I think) bytes per short so change the second one to 8. Obviously, since there is only one byte per float, you wouldn't have realized using UNSINGNED_BYTE.

*

Marltoro

Re: VBO acts weird after switching to short indices
« Reply #2 on: April 08, 2013, 14:30:26 »
Yep, that fixed it. Thank you so much.