Instanced Models

Jan 1, 2009 at 12:16 PM
Hello! It's me again D:

I am wondering, how better to draw instanced models?
In tutorials there instanced geometry and i thought it will be easy to convert it to models, but...
Tutorial using DrawBatch function, and model don't have it.
So i've opened Sphere's DrawBatch func and it's looke like that:
         state.DrawBatch(verts, inds, PrimitiveType.TriangleList, CanDrawItem, instances, instanceCount);

But when i've opened model's draw func i was shocked! :D
So, looks like solution is to write drawbatch method. But maybe there is better way? :D

PS. In AlphaBlendState there no AlphaBlend state, only Alpha. Is this ok? :) Or your typing err? :))
Jan 1, 2009 at 12:20 PM
Ah! Looks like rewriting is pretty easy, actualy drawing code is:
geom.Vertices.Draw(state, geom.Indices, PrimitiveType.TriangleList);
So, i need  change it to: state.DrawBatch.
Yes? :)
Jan 1, 2009 at 12:55 PM
Oh, going that way i have error:
Field (Byte) Byte.m_value value mapping cannot be determined. Either set the VertexElementFormat with a [VertexElement()] attribute, or change the declaration to a supported type.

I dunno how to fix that :(

Jan 1, 2009 at 10:14 PM
Edited Jan 1, 2009 at 10:18 PM


Model instancing is something I'm looking into, but not heavily. As you are discovering, it's a bit more complex than it may first appear. And there are even bigger gotchas when you got deeper (eg batching is not compatible with animation, etc)

The 'Byte' error is actually quite complex. The XNA content pipeline only provides raw byte data for vertices, so the model data is stored as raw bytes (whereas all other vertex buffers in xen are stored as their original structure). Because of this, the buffers have be handled internally in a slightly different way. It's quite possible that they are not handled correctly when using batching.

Internally in xen, vertex declarations are generated for vertex types as they are needed, based on the vertex structure (this makes sure that you never have to deal with declarations, and that they are always correct). However, because the model is raw data, I have to use the vertex declaration that the XNA content pipeline generates. I'm guessing what is going wrong, is that you are using batching, which will be wrapping the model's vertex buffer in a VerticesGroup (multi-stream vertex buffers), this requires a new declaration to be generated (to map both streams) and it's then trying to map the vertex structure of the raw data (which is just bytes). This is failing...

Basically, it will take a bunch of effort on my part to fix this. It's simply a limit of the content pipeline black boxing it's vertex data, which I didn't correctly account for when using VerticesGroup(). Sorry about that.