# Using a .x file for shader instancing

This topic is 3936 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everybody, From the sdk, I've managed to get a strong grasp of instancing and been able to produce the same results in my own application. Now I'm trying to expand beyond simple geometry (cubes, planes, triangles) to instance a .x mesh. From my understanding I will have to load in the .x mesh, get all the vertex information and rewrite it again multiple times into one vertex buffer. I'm having difficulty in getting the vertex info from the x mesh and 'converting' it so that i can put it into a vertex buffer multiple times. Also, in the new vertex buffer, i will need to use a different FVF to one that has an additional TEXCOORD space to store the id of the current instance (as in the sdk). But this shouldn't be a problem :) The annoying thing is that i can pretty much get most of the info, i just can't seem to get the right structure setup for it. ie.
	D3DVERTEXELEMENT9 *mesh_decl;
meshSys->GetDeclaration(mesh_decl);


This gives me the description which, through debug stepping, i can figure out the vertex structure (which is just pos, norm, tex coord). When i lock the buffer and try to copy out this data, i can't make any sense out of it :( If anyone can help me find out how to get all the drawing data out from the .x vertex buffer i would be very grateful!!!! Thanks.

##### Share on other sites
I don't completely get what you meaning :) If you just want to render a mesh a few times, you can just draw the mesh with a regular call. Then change the WVP matrix and render again. Repeat this procedure as many times you like. It is slow because it got a relatively huge overhead though. If you have many instances you can go with hardware instancing instead. Here is a thread where I explain how it's performed.

##### Share on other sites
I'm doing shader instancing using ".x" files also, but in my case, I just use hardwired structure (that is, Position, Normal and Texture Coordinates) - these seem to be usually (always?) positioned sequentially when you load .X file.

Also make sure you move your source pointer by ID3DXBaseMesh::GetNumBytesPerVertex instead of some precalculated value. I'm almost sure that if you read position properly (that is, as three floats) and increase your source pointer by the above value, you will be able to access all positions properly. Same applies to normals and texture coords (usually two floats), although if you have tangents and binormals, you will have to use vertex declaration to figure out where each component is located.

##### Share on other sites
Hi,

51mon: Thats EXACTLY what i was looking for, ur awesome dude!

Lifepower: I tried a few approaches and yeah the structure for the .x file does seem to be the same each time. 51mon's post does what (i think :) u where describing though theres the use of some methods in the ID3DXBaseMesh class that i wasnt aware of (the ones that 'batch' the data to and from the vertex/index buffers).

Thanks alot, gamedev is wicked awesome.