-1

I learned the method glMultiDrawArraysIndirect() from OpenGL, and I want to use this method call with JOGL. In the sample code, a C++ struct is used and the data in that struct is stored in a buffer by using glMapBufferRange(). I was wondering how it is down with JOGL? It would be great if there is any example.

Here is the sample code using OpenGL:

struct DrawArraysIndirectCommand
{
    GLuint  count;
    GLuint  primCount;
    GLuint  first;
    GLuint  baseInstance;
};

load_shaders();

object.load("media/objects/asteroids.sbm");

glGenBuffers(1, &indirect_draw_buffer);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, indirect_draw_buffer);
glBufferData(GL_DRAW_INDIRECT_BUFFER,
             NUM_DRAWS * sizeof(DrawArraysIndirectCommand),
             NULL,
             GL_STATIC_DRAW);

DrawArraysIndirectCommand * cmd = (DrawArraysIndirectCommand *)
    glMapBufferRange(GL_DRAW_INDIRECT_BUFFER,
                     0,
                     NUM_DRAWS * sizeof(DrawArraysIndirectCommand),
                     GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);

for (i = 0; i < NUM_DRAWS; i++)
{
    object.get_sub_object_info(i % object.get_sub_object_count(),
                               cmd[i].first,
                               cmd[i].count);
    cmd[i].primCount = 1;
    cmd[i].baseInstance = i;
}

glUnmapBuffer(GL_DRAW_INDIRECT_BUFFER);
Oscar
  • 1,993
  • 2
  • 18
  • 27
  • 1
    I doubt JOGL has a `glMapBuffer` binding. Try looking for `glBuffer[Sub]Data` instead. – Colonel Thirty Two Mar 11 '15 at 12:54
  • @ColonelThirtyTwo [it does actually](http://jogamp.org/deployment/webstart/javadoc/jogl/javadoc/javax/media/opengl/GL.html#glMapBufferRange%28int,%20long,%20long,%20int%29) returning a ByteBuffer but you can push `int` into it. – ratchet freak Mar 11 '15 at 12:57
  • @ratchetfreak Ah, okay. I wasn't sure how `glMapBuffer` would work given Java's memory model. – Colonel Thirty Two Mar 11 '15 at 12:58
  • @ColonelThirtyTwo: Yeah, it uses what's known as a "direct" buffer in Java. There's a little bit going on under the hood outside the normal Java memory management. But then again, you come to expect that in a framework that wraps an API written in another language ;) A lot of GL would not work if `java.nio.Buffer` did not offer this. – Andon M. Coleman Mar 11 '15 at 13:07

1 Answers1

1

You can see the struct is just 4 gluint one after the other, you will need to imitate that:

ByteBuffer cmd = gl.glMapBufferRange(GL_DRAW_INDIRECT_BUFFER,
                 0,
                 NUM_DRAWS * 4 * 4,
                 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);

for (int i = 0; i < NUM_DRAWS; i++)
{
    int first = object.get_sub_object_info_first(i % object.get_sub_object_count());
    int count = object.get_sub_object_info_count(i % object.get_sub_object_count());
    int primCount = 1;
    int baseInstance = i;

    cmd.putInt(count);
    cmd.putInt(primCount);
    cmd.putInt(first);
    cmd.putInt(basInstance);

}
ratchet freak
  • 47,288
  • 5
  • 68
  • 106