Sign in to follow this  

Saving DirectX Meshes

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok what I am doing is loading an .X file into a very basic directX program (it only contains the object). Now after being created with D3DXCreateTextureFromFile the mesh is stored into a LPD3DXMESH variable. I have a wrapper class called mesh that stores the mesh and its x, y and z coordinates. I would like to store the mesh, and the three floats to a file and be able to read this file to create the "scene" again. I generally know about scene graphs and pak files so if this relates to them that is fine. The only problem I have is im not sure exactly how you do this. I am loading an .X file into the program. It is now a LPD3DXMESH. Do I save the LPD3DXMESH variable to the file? or.. use the SaveToXFile (the function name is something like that) from directx to create the scene by reloading the .X file again.. ( I was thinking that if you did it this way the loading of.. several .X files would create an incredibly slow program) Basically how do I save the mesh thats in memory to a file to be read later? (The most "efficient" way possible)

Share this post


Link to post
Share on other sites
Ultimately I think that you'll probably want to keep the mesh data (your X-file) separate from any per-instance data (in this case, the position). Think about it: for one set of mesh data, you can have many instances of this mesh all with different positions. Therefore it doesn't make sense to store the mesh data over and over again for every new position. Instead, it makes more sense to just have an X-file for each mesh and one file or a series of files specifying the position of each mesh instance and the filename of the corresponding x-file.

If you really really want all of the mesh data in one file with the position, it will be more difficult. One approach is you can create an x-file using D3DXSaveMeshToX, load the binary data out of the file, and then save that all to your new custom file format along with the position. Then, when loading that file you could load all of the x-file data into memory and then use D3DXLoadMeshFromXInMemory to load the mesh. The other option is to manually extract all of the mesh data (vertices, indices, attributes, effect instances) and then write all of that data to the file. Then when loading, you could reassemble the ID3DXMesh from that daya. Note that you can't just save a variable of type LPD3DXMESH to a file, that's just a pointer to a COM interface. It's a single 32-bit (or 64-bit) integer that points to where the actual class is allocated, which is a value that's useless when you're loading a mesh from file.

Share this post


Link to post
Share on other sites
Thanks MJP I would just like a little more clarification as to how to do this correctly.

Yes you would not want to load an .X file for each instance thats fine but anything to understand how to do it will work for now even if it is incredibly inefficient. The mesh can be just saved in say a .PAK file that holds multiple meshes correct? Then another file say .SCENE file that holds the location of any instances of that mesh. That would be loaded into the program to create the scene with the mesh in it at the correct location.

The overall idea is just to get the location and the mesh its self saved somehow. The scene could contain the coordinates of the single mesh im working with at the moment and perhaps the location.. or name of the .X file I would like to load?

Save what .X file (location, name, or the mesh its self) you want to use in the scene and its coorrdinates in a file named say "myScene.scene"?

So which would be more efficient saving the mesh to a .X file and loading that .X file every time the scene is opened.. or saving each bit of data from the mesh (vertices, attributes ect) then recreating that from a custom file type each time the scene is opened?

Share this post


Link to post
Share on other sites
You're "SCENE" approach sounds just fine: in the scene file store the position of the mesh and the name of x-file, and then load up the mesh from the actual x-file (which could be in a .PAK or wherever else you store it).

I would suggest just sticking with D3DXLoadMeshFromX for loading your X meshes. The code is already written for you and there's no reason to re-write it, unless you develop features in your engine that require a mesh file format with more capabilities than the X-format (this is very unlikely, at least while you're starting out). Never be too quick to ditch tested and proven code in favor of writing something yourself!

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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