Any suggestions on what data structure to use?

Started by
3 comments, last by swiftcoder 9 years, 9 months ago

Hi there,

I'm trying to write a rendering function for a game. I have an array of object transforms (position, rotation, scale), which should be sorted by material as to not waste time binding the same material multiple times.

The problem is that I would rather not store a reference to a material in each transform. It's such a basic type that I don't want it to get cluttered with references to every part of the game that depends on it. The transform should only store the position, rotation and scale, and the relationship to the material should be stored elsewhere.

I could instead store references to the transforms inside each material, but then sorting the array of transforms would be a lot less straightforward.

Does anyone have any suggestions on how to store this data relationship? Is there a particular data structure which is good at representing this kind of relationship?

Advertisement
What's wrong with a separate array of object transforms for each material? That seems to hit all the boxes.

You mean allocating a new array of transforms for each material, not just references to another array with all the transforms?

Seems like a good idea. The only reason I wanted to keep all of the transforms in one array was for optimal cache usage when looping through and rendering all of them. Now that I think of it though, a single cache miss when switching materials is not bad at all.

Actually, wouldn't it be better to keep the transforms separate from the materials? Because what about all of the other parts of the game that want to know about the transforms, wouldn't they then have to point to the mesh to access the transforms?

The problem seems to be that I'm not representing any sort of unified sort of game entity, it's all just separate arrays of data so it's hard to represent connections between the data.


The problem seems to be that I'm not representing any sort of unified sort of game entity, it's all just separate arrays of data so it's hard to represent connections between the data.

Your entities are a little abstract at the moment, that's all.

With your current structure, a single entity would be an index into the transforms array, plus an index into the materials array. If you set it up that way, and then sort the array of entities by the material index, you'll be golden.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement