Sign in to follow this  
jagguy2

loading X file

Recommended Posts

Hi, I can load a mesh file easy enough but have a few questions on loading X files. q1) each subset holds a texture and material for lighting purposes that are loaded with corresponding array elements. Why doesnt the x file hold data about all the lighting like ambient, as you have to set it yourself? q2)how many ways is there to load a x file as i am reading 1 method only, to load the xfile then iterate through the DXDXMATERIAL array. is this the onlt way you do it or the easiest way?

if( g_mtrlBuffer != 0 && g_numMtrls != 0 )
	{
		D3DXMATERIAL* mtrls = (D3DXMATERIAL*)g_mtrlBuffer->GetBufferPointer();

		for(int i = 0; i < g_numMtrls; i++)
		{
				mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse;

			// save the ith material
			Mtrls.push_back( mtrls[i].MatD3D );

			// check if the ith material has an associative texture
			if( mtrls[i].pTextureFilename != 0 )
			{
			// yes, load the texture for the ith subset
				IDirect3DTexture9* tex = 0;
				D3DXCreateTextureFromFile(
					g_pd3dDevice,
					mtrls[i].pTextureFilename,
					&tex);

				// save the loaded texture
				Textures.push_back( tex );
			}
			else
			{
				// no texture for the ith subset
				Textures.push_back( 0 );
			}
		}

Share this post


Link to post
Share on other sites
q2) You have D3DXLoadMeshFromX or D3DXLoadMeshFromXInMemory at your disposal, but the first is probably what you want. Are there other ways? Sure, you'd just need to write a file parser, a way to make the vertices and indices, and then create the mesh from that data.


q1) The X file does contain the diffuse, speculative, and emissive colors and power value of each material.
Where the material/texture pair is defined in the X file will look something like this: (I added the comments)
Material hull // Material name
{
0.600000;0.600000;0.600000;1.000000;; // Diffuse
0.000000; // Power
0.000000;0.000000;0.000000;; // Speculative
0.000000;0.000000;0.000000;; // Emissive

TextureFileName
{
"Craft/hull.png"; // Texture file location from the exe (or .sln in C++ VS2003/5)
}
}

To retrieve and set the values switch this code:
mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse;

With this:
mtrls[i].MatD3D.Ambient = /*Some Ambient Value*/;
mtrls[i].MatD3D.Diffuse = mtrls[i].MatD3D.Diffuse;
mtrls[i].MatD3D.Emissive = mtrls[i].MatD3D.Emissive;
mtrls[i].MatD3D.Power = mtrls[i].MatD3D.Power;
mtrls[i].MatD3D.Specular = mtrls[i].MatD3D.Specular;



As for why there is no ambient: (this is my guess on why they decided to keep it out of the file specification, so if I'm incorrect someone please correct me)

The ambient value is almost like a global constant brightness. Every face of every object no matter if the light is shining on it or not will be at least the ambient's brightness. So to make sure all faces of all objects on the screen look uniform the ambient is kept constant from object to object.

Share this post


Link to post
Share on other sites
Quote:
Original post by tolliner
q2) You have D3DXLoadMeshFromX or D3DXLoadMeshFromXInMemory at your disposal, but the first is probably what you want. Are there other ways? Sure, you'd just need to write a file parser, a way to make the vertices and indices, and then create the mesh from that data.


q1) Tlike this: (I added the comments)
Material hull // Material name
{
0.600000;0.600000;0.600000;1.000000;; // Diffuse
0.000000; // Power
0.000000;0.000000;0.000000;; // Speculative
0.000000;0.000000;0.000000;; // Emissive

TextureFileName
{
"Craft/hull.png"; // Texture file location from the exe (or .sln in C++ VS2003/5)
}
}

To retrieve and set the values switch this code:
mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse;

With this:
mtrls[i].MatD3D.Ambient = /*Some Ambient Value*/;
mtrls[i].MatD3D.Diffuse = mtrls[i].MatD3D.Diffuse;
mtrls[i].MatD3D.Emissive = mtrls[i].MatD3D.Emissive;
mtrls[i].MatD3D.Power = mtrls[i].MatD3D.Power;
mtrls[i].MatD3D.Specular = mtrls[i].MatD3D.Specular;



hi,

The material hull has a bunch of numbers so are these the values you read into the relevant variables for materials?

why do this as your just assigning it to itself so i dont understand?
mtrls[i].MatD3D.Diffuse = mtrls[i].MatD3D.Diffuse;??

Share this post


Link to post
Share on other sites
Doh my bad, i thought you were assigning each color value from each D3DXMATERIAL to a D3DMATERIAL9 but I see what you're doing. You're just assigning the diffuse to the ambient then saving that D3DMATERIAL9 defined by MatD3D... please disregard all of those assignments I had. Sorry for the confusion.

Yeah your code's correct... So it's up to you whether you want to always assign the diffuse to the ambient or have a global ambient.

so
mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse;
is fine and you can also do something like this:
mtrls[i].MatD3D.Ambient = // Some global D3DCOLORVALUE;



With the material hull I just wanted to give you a sample of what was being read in. All those numbers are stored in the MatD3D struct for you. Unless you wanted to write a X file parser yourself you wont need to worry about parsing the materials yourself

Share this post


Link to post
Share on other sites

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