Sign in to follow this  
hburd

Any suggestions on what data structure to use?

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this