void TerrainTessellator::init(const GraphicsDevice& device, unsigned size, unsigned numPatches, unsigned stripeSize)
{
	this->size = size;
	this->stripeSize = stripeSize;
	this->numPatches = numPatches;

	numVertices = (size + 3) * (size + 3);

	unsigned lod = (unsigned)MathHelper::log2(size) + 1;

	patchIndexBuffers = IndexBufferCollection(lod);
	skirtIndexBuffers = IndexBufferCollection(lod);

	for (unsigned i = 0; i < lod; ++i)
	{
		patchIndexBuffers[i] = createPatchIndexBuffer(device, i);
		skirtIndexBuffers[i] = createSkirtIndexBuffer(device, i);
	}

	VertexCollection vertices(numVertices);

	for (int row = -1; row <= (int)size + 1; ++row)
		for (int col = -1; col <= (int)size + 1; ++col)
		{
			int r = MathHelper::clamp(row, 0, (int)size);
			int c = MathHelper::clamp(col, 0, (int)size);

			float y = (r == row && c == col) ? 0.0f : -1.0f;

			vertices[getIndex(row, col)].position = Vector3((float)c, y, (float)r);
		}

	vertexBuffer = device.createVertexBuffer(sizeof(TerrainVertex) * numVertices, TerrainVertex::fvf, D3DUSAGE_WRITEONLY);

	vertexBuffer.setData(vertices);

	instanceVertexDeclaration = device.createVertexDeclaration(TerrainGeometry::vertexElements);
}
void TerrainTessellator::onResetDevice(const GraphicsDevice& device)
{
	instanceVertexBuffer = device.createVertexBuffer(sizeof(TerrainInstance) * numPatches, 0, D3DUSAGE_DYNAMIC, D3DPOOL_DEFAULT);
}
void FBXModel::onResetDevice(const GraphicsDevice& device)
{
	instanceVertexBuffer = device.createVertexBuffer(sizeof(InstanceType) * maxInstances, 0, D3DUSAGE_DYNAMIC, D3DPOOL_DEFAULT);
	effect.onResetDevice();
}