This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.

Join us in building a kind, collaborative learning community via our updated
Code of Conduct.

I want to draw lots of spheres in different locations and orientations with Opengl4 and JOGL. As the vertexes and colours are the same for all of them, I have just one array for vertexes and another for colours. For the positions and orientations, I have another big matrix where I have all data for all spheres.

In principle, drawing one with glDrawArrays is not a problem but for severals, I have read that I should use glDrawArraysInstanced instead. My problem is that I am a bit confused about how to apply each transformation for my particles. How should I introduce this array into the shader? Should I send the matrix model after doing the transformations in the cpu or should I send the positions and orientations and transform them inside the shader? How do I connect the data to the shader? How should the shader look like?

3 Answers
3

For instanced rendering there's GLSL variable called gl_InstanceID, which stores the id of the current instance. You can do 2 things:

1.) You create an array of matrices and you load each transformation matrix in it, then use the instance id to get the correct one. This way you don't have to create matrices on the fly (you only have to do it once, not as for each vertex), but you'll need to have a max number of spheres, because OpenGL doesn't support dynamic arrays.

2.) You load the positions and rotations as vec3-s in a dynamic buffer, then you reconstruct the matrices on the fly. If the spheres don't have too many vertices, then this may be a better solution.

Note that you will want to put the per-instance data in the VBO, after all the normal model (per-vertex) data, which in my code sample has size 'off'.

Typically, you will want to change the values for these transformation each frame. This means you need to update a part of your VBO and put in the new versions of the matrices. This is done with glBufferSubData()

Here, numverts is the number of vertices in the model itself, not the total nr of verts drawn for all instances.

To sum it all up: the instance transformation matrix is treated as a per vertex attribute in your GLSL vertex shader. Yet, the data is fed not as a value per vertex, but only a new value per (typically 1) instance using glVertexAttribDivisor() which you need to call four times, once for each row in the matrix.