Subscribe to the RSS Feed Add to Google

3D Engine - Instancing

September 17, 2008

It's time for the next piece on the indie 3D engine I'm working on. You should read through the one about Basic Concepts if you haven't already, as this one builds on the concepts described in that one. This time, I will talk about instancing, which is the method we use to enable us to re-use the same models at multiple locations in the world, without needing duplicate copies of the model data.

As you remember, I defined a model as a Node, wich had a name/ID, Transform describing it's position, and a hierarchy of child nodes, which could themselves be Nodes or Meshes. A Mesh contains a bunch of vertices and indices, and a reference to a Material. And the Material contains up to 3 texturmaps and a color.

Example of a model, and how the elements are connected.

Look at the figure above (click to enlarge it): it shows the relations between the different elements, and how a model may contain multiple meshes and even reference the same mesh more than once.

Now, imagine we have a game where we want lots of cars driving around. Let's say we have three different car models, and five different texture maps, which have been created in such a way that they can be applied to any of the three car models. This would give us 15 different cars, just by using all the possible combinations. If we also consider that we can use different basic colors for the cars, we get even more variations. Just by combing the 15 different model-textur variations with a set of seven distinct color, we get over a hundred different cars.

In theory, we could create separate models for each of the 100+ cars, but that would just mean we would be wasting a lot of memory, which we could be putting to better use. And what if we want two (or more) cars in the world with the same appearence (maybe some police cars or similar)? We certainly wouldn't want to duplicate data for them...

So the solution we'll be using, is separating Definitions from Instances. In the contect of this 3D engine, a Definition is something that we typically load from an external source, and which stays constant throughout the game. It acts as a template, from which we can create working copies, or Instances, which we can modify, add to the world and manipulate. The key thing here, is that Definitions are typically large, heavy-weight structures, while Instances are very small and light-weight, containing just those elements of data which we need to modify per instance.

Node Instance

The figure above shows a Node Instance such as might have been created from the Node Definition shown in the entry on Basic Concepts. It doesn't look much different - it doesn't have a Name/ID field, but instead it's got a reference to the Node Definition it was created from. Also, it contains referenced to Node Instance and Mesh Instance rather than to the corresponding definitions (which the Node Definition referenced). This is not a problem though - all these elements (except the transform) are just pointers to other elements, so each of them is just 4 bytes in size, so we don't need to worry too much about the memory use of the Nodes.

Mesh Instance

The difference between Mesh Instance and Mesh Definition (as detailed in the entry on Basic Concepts) is a bit more interesting. As shown above, a Mesh Instance contains only a reference to a Mesh Definition (which is a lot less memory than storing a copy of all the vertices and indices) and a reference to a Material (so that we can use different materials for different instanes).

When we're using a model in the engine, we do it by loading a Model Definition from file, and then we create as many Model Instances of that model for use in the game world. We are free to manipulate any data (Transform, Material or node structure) of each instance, to suit our games needs. The Model Definition might be big, with many vertices and indices taking up a lot of memory, while each Model Instance is tiny in comparison, containing only references to the large data.

We will be getting back to the topic of instancing later - there's still plenty of details to get into when we come to implementing this... so stay tuned for the next update :-)


   Digg this Digg this    submit to reddit Add to reddit

<< STOS - Basic Language for Making Games
Making an RPG in 48 hours >>


Comments

There are no comments on this blog entry.

Write a comment








Video Games BlogRankers.com Video Games blogs Directory of Video Games Blogs Blog directory Game Blogs - BlogCatalog Blog Directory Free Page Rank Tool Bloggtoppen.se Blogg om Bloggar.Topplista.se - topplistan med de bästa bloggarna - lägg till din blogg du också! My Zimbio DigNow.net

© Copyright Mattias Gustavsson 2008. All Rights Reserved. Reproduction/republishing of any material on this site without permission is strictly prohibited.