Working with Packed Arrays
Despite its various crimes against programming, Object Oriented Programming gives us a nice way of accessing data fields.
When we move away from OOP and start dealing with large arrays of raw numbers, it can be a little disorienting at first.
The point of this tutorial is to explain this different way of programming.
Consider an array of position components.
This array holds two objects, a and b, (x,y,z) are positions and (ex,ey,ez) are the euler angles around the various axes.
Padding is added for the benefit of the compute shaders, for more info see the compute shader tutorial. Now say we want to
double all the positions (admitedly this might not be the most creative example). A naive implementation would look something
like this.
Now in this small example it's easy enough to look at the diagram and deduce how this is working, but in larger programs
things can quickly get messy. To make things clearer, I'll introduce two concepts: stride and offset.
Stride
The stride of an array is the number of elements we'll have to step to reach the next object. In this case, including padding,
a transform component has a stride of 8. e.g. To get to the next x coordinate, I'll have to look at the element 8 steps
after my current position. We can improve code readability a little.
Offset
When we multiply an integer by the stride, it tells us the index of the beginning of an object in memory, in order to
access attributes of the object, we then add offsets to the starting index. As with stride, this can be made more readable.
And that's the way we deal with packed arrays! Happy programming!