Login  Register

Re: MultiDrawElements

Posted by Martin on Sep 30, 2021; 2:11pm
URL: https://forum.jogamp.org/MultiDrawElements-tp4036014p4041326.html

Hi,

5 years later, I found this discussion instructive to understand glMultiDrawElements but I still thrive to make it work. Using JOGL 2.3.2, Mac 10.12.6, I get a JVM crash. I think I did not understand properly how to build the PointerBuffer.

Here is what I want to do

I have a collection of vertices/normals/colors in their dedicated buffers.

I want to draw quads (based on GL_TRIANGLE_FAN) that share vertex between each other. I want to do like this because input data is made of shared vertices described in an input file and I want to avoid repeating them manually.



Here is how I try to do it

I first register vertices, normals and colors with FloatBuffers. I think I do this properly since I can use glMultiDrawArrays properly with other case where vertices are repeated (hence I do not need an index since vertices of the same triangle fan will be contiguous, and all triangle fans have their start index and length defined).

Then I invoke glMultiDrawElements as follow :

gl2.glMultiDrawElements(glGeometryType, elementsCountBuffer, GL.GL_UNSIGNED_INT, elementsIndicesBuffer, elementsIndicesBuffer.capacity());

where elementsCount is filled with a 1D array as follow :

IntBuffer elementCountBuffer = Buffers.newDirectIntBuffer(elementsCount); // int[] elementsCount
elementCountBuffer.rewind();


and elementsIndices is filled with a 2D array as follow :

PointerBuffer elementIndicesBuffer = PointerBuffer.allocateDirect(size(elementsIndices)); // int[][] elementsIndices, where size(array) returns #row x #columns

for (int i = 0; i < elementsIndices.length; i++) {
  // Each i is the ID of a GL_TRIANGLE_FAN

  for (int j = 0; j < elementsIndices[i].length; j++) {
    // Each j is the index of a vertex to use in the vertex/normal/color buffers to draw the ith triangle fan

    elementIndicesBuffer.put(elementsIndices[i][j]);
  }
}
elementIndicesBuffer.rewind();


I am almost sure I do this wrong because the man page of the function states that the last argument - drawcount - should describe the length of the two buffer, hence that they should be equals.

As the purpose of this function is to draw multiple elements based on vertices that may be shared among these elements, I wonder how elementIndicesBuffer could be anything else than a 2D array? Otherwise, if it is a 1D array, how can I register in it the list of indices for the first triangle fan, then second triangle fan, etc?

Thanks for your help!