1
\$\begingroup\$

I have a vertex and color VBO that render a plane (2 triangles) just fine. However, when I attempt to use an IBO to render the plane, the vertices are completely off. My nice square looks all crumpled up...

I have experience in the past where an incorrect setting on the size parameter for glVertexAttribPointer skews ALL of the buffered data - color and vertex. It looks as if the IBO is doing the same thing somehow.

My code:

float[] colors = {    1.0f, 0.0f, 0.0f, 
      0.0f, 1.0f, 0.0f, 
       0.0f, 0.0f, 1.0f, 
       5.0f, 0.0f, 0.0f};

float[] dataBuffer = {-1,-1,1,
      1,-1, 1,
      1,1,1,
      -1,1,1};

int[] idxBuffer = {1,2,3,3,4,1};

//**** INIT ROUTINE *****
  vboHandles = new int[3];
  gl.glGenBuffers(3, vboHandles, 0);

  //populate the position buffer
  FloatBuffer fbData = Buffers.newDirectFloatBuffer(dataBuffer);
  gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, vboHandles[0]); //the vertex data
  gl.glBufferData(GL4.GL_ARRAY_BUFFER, fbData.capacity() * 4, 
          fbData, GL4.GL_STATIC_DRAW);

  //populate the color buffer
  FloatBuffer fbColors = Buffers.newDirectFloatBuffer(colors);
  gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, vboHandles[1]);
  gl.glBufferData(GL4.GL_ARRAY_BUFFER, fbColors.capacity() * (Float.SIZE / Byte.SIZE), 
          fbColors, GL4.GL_STATIC_DRAW);

//index buffer IBO Vertex
  IntBuffer dtaIndc = Buffers.newDirectIntBuffer(idxBuffer);
  gl.glBindBuffer(GL4.GL_ELEMENT_ARRAY_BUFFER, vboHandles[2]); 
  gl.glBufferData(GL4.GL_ELEMENT_ARRAY_BUFFER, dtaIndc.capacity() * (Integer.SIZE / Byte.SIZE), 
          dtaIndc, GL4.GL_STATIC_DRAW);

  //set vertex array index
  IntBuffer intBuffer = BufferUtil.newIntBuffer(1);
  gl.glGenVertexArrays(1, intBuffer);
  iVao = intBuffer.get(0);
  gl.glBindVertexArray(iVao);

  gl.glEnableVertexAttribArray(0);
  gl.glEnableVertexAttribArray(1);

  // Select the VBO, GPU memory data, to use for colors
  gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, vboHandles[COLOR_IDX]);
  gl.glVertexAttribPointer(0, 3, GL4.GL_FLOAT, false, 0, 0);

  gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, vboHandles[VERTICES_IDX]); //the vertex data
  // Associate Vertex attribute 1 with the last bound VBO
  gl.glVertexAttribPointer(1,3,GL4.GL_FLOAT, false,  0, 0 );

  gl.glBindBuffer(GL4.GL_ELEMENT_ARRAY_BUFFER, vboHandles[2]);
  gl.glVertexAttribPointer(2, 1, GL4.GL_INT, false, 0, 0);


//***************The render routine*****************
gl.glBindVertexArray(iVao);
//works great when I skip IBO and just use glDrawArrays
//gl.glDrawArrays(GL4.GL_TRIANGLES, 0, 6);

//vertices are all wrong when I use glDrawElements....
gl.glDrawElements(
       GL4.GL_TRIANGLES,      // mode
       idxBuffer.length,    // count
       GL4.GL_UNSIGNED_INT,   // type
       0           // element array buffer offset
);

Does anyone have any suggestion as to how I might resolve this? Obviously I'm thinking the IBO is offsetting the vertex data somehow but I don't know how I'd resolve that. Tried calling glGenBuffers separately for vertex, color and index but made no difference. Tried using Float for index (grasping I know...) just to keep the data types the same but threw an exception. I was also wondering if it is incorrect to use glVertexAttribPointer to associate the bound IBO? Is there a different function for that perhaps?

EDIT:

Without these two lines:

gl.glBindBuffer(GL4.GL_ELEMENT_ARRAY_BUFFER, vboHandles[2]);
gl.glVertexAttribPointer(2, 1, GL4.GL_INT, false, 0, 0);

I get the following error:

A fatal error has been detected by the Java Runtime Environment:

EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000005ca0e6c0, pid=5196, tid=5112

JRE version: 6.0_21-b07 Java VM: Java HotSpot(TM) 64-Bit Server VM (17.0-b17 mixed mode windows-amd64 ) Problematic frame: C [nvoglv64.DLL+0xb7e6c0]

An error report file with more information is saved as: C:\&&&&&&&&&&&&&&&&&&\hs_err_pid5196.log

If you would like to submit a bug report, please visit:
http://java.sun.com/webapps/bugreport/crash.jsp The crash happened outside the Java Virtual Machine in native code. See problematic frame for where to report the bug.

I accept that the call to glVertexAttribPointer is incorrect as it obviously messes up the buffer and simply masks the above error. Could this error be caused by my index buffer being of type int and the drawelements requires uint?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

You don't need to call glVertexAttribPointer for the indexes; the indexes are found from the bound GL_ELEMENT_ARRAY_BUFFER during the drawElements call (which is saved in the VAO state IIRC).

However indexes start from 0 so the contents of your index buffer are flawed they should be: {0,1,2,2,3,0}

\$\endgroup\$
2
  • \$\begingroup\$ I actually added that call to glVertexAttribPointer in because without it I get a nasty error. I've added it to the post. Embarrassing about the array but I had seen an example where it was one based. Makes sense that it be 0 based.. \$\endgroup\$ Commented Oct 16, 2014 at 12:41
  • \$\begingroup\$ Sure enough removing gl.glVertexAttribPointer(2, 1, GL4.GL_INT, false, 0, 0); fixed the problem. \$\endgroup\$ Commented Oct 17, 2014 at 3:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .