I've got a simple app that draws 3D cubes using instance rendering and I'm implementing Shadow Volume. Each cube's vertex, uv, normal and index data are statically stored in 4 vertex array buffers. On each frame, I draw 10 cubes by uploading 10 world-view-projection matrices (one for each cube) to a 4th dynamic vertex array buffer (using vertex attr divisor) and then calling glDrawElementsInstanced once with 10 as primecount.

All my cubes are render properly in 1 draw call \o/

Now... I want to draw their shadow volumes in one draw call too! (for 1 directional light). I'm on Mac so I've acces to OpenGL 4.1. Here's my progress so far:

  • I upload all vertices twice (once with the w attribute to 1 and once to 0). This will help me drawing the extruded silhouette in my vertex shader.
  • On the CPU, I find each cube's silhouette (according to their orientation) and I create one list of indices for each cube (so 10 lists total) representing their shadow volume.
  • And then I'm stuck...

I can't find a way to upload multiple list of index (one for each instance)... so far my vertex shader can work with each cube's matrix but can only work on 1 index array (the uniq GL_ELEMENT_ARRAY_BUFFER).

I've been googling a lot but could not find a solution. So I'm asking here: does anybody know how to implement Instanced Rendering with multiple indice lists? or does anybody know a better approach to render shadow volumes of multiple instances of the same object in one draw call?

  • \$\begingroup\$ Why do you specifically want one draw call? Just out of curiosity. Also the last time i rendered 'instanced' cubes on such a highly feature rich API i used a geometry shader ... i still have code for this somewhere if you will find it useful. (i used it to raytrace thousands of spinning noisy heightmap covered spheres). this enabled me to submit a single point and three rotations for each cube and building the matrices from this in shader. (And it occurs to me that by encoding edges similarly you can render all of the shadow volume faces with a similar approach) \$\endgroup\$ – jheriko Nov 5 '15 at 23:52
  • \$\begingroup\$ I must admit that there's not specific reason appart from curiosity. It looks like it should be possible to achieve. If so, it could be an interesting optimisation since I'll have as many shadow volumes to draw as batched cubes. I managed to upload a list of indices and issue a draw call of each cube's shadow volume, but the idea would be to upload all cube's shadow volumes indices at once and issue only one draw call. Thanks a lot for your comment! I would gladly look at the code you're talking about (maybe you can share it on a gist). \$\endgroup\$ – XPac27 Nov 6 '15 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.