Direct3d Learning (6): animation BASICS (2) Use of X Files

Source: Internet
Author: User
Use of X Files (complete) [convert]

These two days are not very good. If you haven't written it, let's share the same content first.

[Copy to clipboard]

Code:

Many may want to know more about X Files. This article will introduce the use of X files in a comprehensive way. I think this is a purely technical document, and I will not praise and polish the poems. I believe that reading my article is like a toothpick.) Okay, let's face it up. First, give you a complete impression-the legendary X file:
XOF 0302txt 0032

Template header {
<3d82ab43-6211611cf-ab39-0020af71e433>
DWORD Major;
DWORD minor;
DWORD flags;
}
Template frame {
<3d82ab46-6211611cf-ab39-0020af71e433>
[Frametransformmatrix]
[Mesh]
}
Header {
1;
0;
1;
}
Frame scene_root {
Frametransformmatrix {
1.000000, 0.000000, 0.000000, 0.000000,
0.000000, 1.000000, 0.000000, 0.000000,
0.000000, 0.000000, 1.000000, 0.000000,
0.000000, 0.000000, 0.000000, 1.000000 ;;
}
Frame pyramid_frame {
Frametransformmatrix {
1.000000, 0.000000, 0.000000, 0.000000,
0.000000, 1.000000, 0.000000, 0.000000,
0.000000, 0.000000, 1.000000, 0.000000,
0.000000, 0.000000, 0.000000, 1.000000 ;;
}
Mesh pyramidmesh {
5;
0.00000; 10.00000; 0.00000 ;,
-10.00000; 0.00000; 10.00000 ;,
10.00000; 0.00000; 10.00000 ;,
-10.00000; 0.00000;-10.00000 ;,
10.00000; 0.00000;-10.00000 ;;
6;
3; 0, 1, 2 ;,
3; 0, 2, 3 ;,
3; 0, 3, 4 ;,
3; 0, 4, 1 ;,
3; 2, 1, 4 ;,
3; 2, 4, 3 ;;
Meshmateriallist {
1;
6;
0, 0, 0, 0 ;;
Material material0 {
1.000000; 1.000000; 1.000000; 1.000000 ;;
0.000000;
0.050000; 0.050000; 0.050000 ;;
0.000000; 0.000000; 0.000000 ;;
}
}
}
}
}
XOF 0302txt 0032
XOF indicates that this is a real X file. 0302txt indicates the X file of DirectX used by the notification program, and the version is 3.2. txt indicates that the file is a text file, which is readable and not a binary file. 0032 indicates that the number of digits of a floating point number is 32. If you want to use a 64-bit floating point number, you can write it as 0064.
The topics are described as follows.
Declare a template: /////////// assume that the template contactentry is declared.
First, use guidgen.exe to generate a guid. The generated GUID is as follows:
// {4c9d055b-c64d-4bfe-a7d9-981f507e45ff}
Define_guid (<Name>,
0x4c9d055b, 0xc64d, 0x4bfe, 0xa7, 0xd9, 0x98, 0x1f, 0x50, 0x7e, 0x45, 0xff );
Then add the following to the program code:
# Include "initguid. H"
// At beginning of source code file-add define_guids
Define_guid (contactentry, 0x4c9d055b, 0xc64d, 0x4bfe, 0xa7, 0xd9, 0x98, 0x1f, 0x50, 0x7e, 0x45, 0xff );
Add the following to the X file:
Template contactentry {
<4c9d055b-c64d-4bfe-a7d9-981f507e45ff>
Declare the data types used by the template:
Keyword description
Word 16-bit value (short)
DWORD 32-bit value (32-bit Int or long)
Float IEEE float value (float)
Double 64-bit floating-point value (double)
Char 8-Bit Signed value (signed Char)
Uchar 8-bit unsigned value (unsigned char)
Byte 8-bit unsigned value (unsigned char)
String a null-terminated string (char [])
Array signifies an array of following data type to follow ([])
Example:
DWORD value;
Array string text [20]; // defines an array named text. Its type is string and its size is 20.
DWORD arraysize; array string Names [arraysize]; // you can set the size to a variable.
Declare a contactentry template:
Template contactentry {
<4c9d055b-c64d-4bfe-a7d9-981f507e45ff>
String name; // The contact's name
String phonenumber; // The contact's phone number
DWORD age; // The contact's age
}
Instantiate a template object:
Contactentry jimsentry {
"Jim Adams ";
"(800) 555-1212 ";
30;
}
{Jimsentry} can reference a data object in this form. For example, you can reference
A frame data object is used as its embedded data object. You can also use a reference to represent a copy of a data object without repeating it.
Write this data object.
Embedded Data Objects and template constraints: // template closedtemplate {
<4c9d055b-c64d-4bfe-a7d9-981f507e45ff>
DWORD closeddata;
}
Template opentemplate {
<4c9d055b-c64d-4bff-a7d9-981f507e45ff>
DWORD opendata;
[...]
}
Template restrictedtemplate {
<4c9d055b-c64d-4c00-a7d9-981f507e45ff>
DWORD restricteddata;
[Closedtemplate]
[Opentemplate]
}
Closedtemplate is a standard template declaration.
Opentemplate contains a [...], indicating that this is an open template. An open template allows you to embed any data objects in. For example
You can instantiate opentemplate to define an opendata variable and an instance embedded with closedtemplate.
Restrictedtemplate is the constraint template. When a constraint template is instantiated, only the data objects listed by it are allowed. For example
Restrictedtemplate contains data objects other than [closedtemplate] and [opentemplate.
Using DirectX. x standard templates: //////////////// the X file is widely used to contain a mesh information. A standard templates contains too much information.
Table 3.2: DirectX. x standard templates
Template Name Description
Animation: defines animation data for a single frame.
Animationkey: defines a single key frame for the parent animation template.
Animationoptions: Contains animation playback information.
Animationset: contains a collection of animation templates.
Boolean: holds a Boolean value.
Boolean2d: holds two boolean values.
Colorrgb: contains red, green, and blue color values.
Colorrgba: contains red, green, blue, and Alpha color values.
Coords2d: defines two coordinate values.
Floatkeys: contains an array of floating-point values.
Frametransformmatrix: holds the transformation matrix for a parent frame template.
Frame: a frame-of-reference template that defines a hierarchy.
Header: The. X file header that contains version numbers.
Indexedcolor: contains an indexed color value.
Material: contains material color values.
Matrix4x4: holds a 4x4 homogenous matrix container.
Mesh: contains a single mesh's data.
Meshface: holds a mesh's face data.
Meshfacewraps: contains the texture wrapping for mesh faces.
Meshmateriallist: contains the material for face-mapping values.
Meshnormals: Holds normals used for mesh data.
Meshtexturecoords: Holds texture coordinates used for mesh data.
Meshvertexcolors: Holds vertex color information used for mesh vertices.
Patch: defines a control patch.
Patchmesh: contains a patch mesh (much like the mesh template ).
Quaternion: holds a quaternion value.
Skinweights: contains an array of Weight Values mapped to a mesh's vertices.
Used in skinned meshes.
Texturefilename: contains the texture file name to use for a material.
Timedfloatkeys: contains an array of floatkeys templates.
Vector: holds a 3D coordinate value.
Vertexduplicationindices: informs you which vertices are duplicates of other vertices.
Xskinmeshheader: used by skinned meshes to define the number of bones contained
In a mesh.
The Macro of each template is defined in rmxfguid. H, for example:
/* {3d82ab44-6211611cf-ab39-0020af71e433 }*/
Define_guid (tid_d3drmmesh,
0x3d82ab44, 0x62da, 0x11cf, 0xab, 0x39, 0x0, 0x20, 0xaf, 0x71, 0xe4, 0x33 );
Each Template Name with the prefix tid_d3drm is the macro definition name.
To access the. X file: // to access any X file, call the directxfilecreate function to create an idirectxfile interface.
Idirectxfile * pdxfile = NULL;
Hresult result = directxfilecreate (& pdxfile); // use & pdxfile to return the pointer to the interface. Use succeeded or
The failed macro determines whether the returned value is valid.
Register a custom Ro standard template: // you can remove the template from the X file and define the template directly in the code. The idirectxfile interface supports this feature. Required
Use the idirectxfile: registertemplates function.
Hresult idirectxfile: registertemplates (
Lpvoid pvdata, // the cache of a definition template data should be accurate.
DWORD cbsize); // The number of pvdata cached bytes.
You can define a template data cache as follows:
Char * templates ="
"XOF 0303txt 0032 // standard X file header.
Template customtemplate {<4c944580-9e9a-11cf-ab43-0120af71e433> DWORD length; array DWORD values [length];} ";
Then register it with registertemplates:
Pfile-> registertemplates (templates, strlen (templates ));
Registration standard template:
First, you must include rmxfguid. h and rmxftmpl. h In the code. Rmxfguid. h defines the Gudi of each standard template. rmxftmpl. h uses
The binary data format defines the cache and the number of bytes of the standard template data. Call registertemplates to register it:
Pfile-> registertemplates (d3drm_xtemplates, d3drm_xtemplate_bytes );
Open the X file: // create the idirectxfile interface. After registering the template, open the X file and enumerate its data objects. Call
Idirectxfile: createenumobject function.
Hresult idirectxfile: createenumobject (lpvoid pvsource, //. x filename
Dxfileloadoptions dwloadoptions, // load options
Lpdirectxfileenumobject * ppenumobj); // Enum Interface
When the createenumobject function is called, pvsource is used to specify the name of a file, and ppenumobj is used to return an enumeration object interface
Needle. Use dwloadoptions to specify the load operation mode. When the dxfileload_fromfile value is specified, it tells DirectX to load
File. Dxfileload_fromresource, dxfileload_frommemory, and dxfileload_fromurl indicate
Load X files on the source, memory buffer, and Internet. When loading a file from the Internet, you must specify a complete URL for it.
The following code loads X files from a disk:
// Filename = filename to load ("test. X" for example)
Idirectxfileenumobject * penum;
Pfile-> createenumobject (lpvoid) filename, dxfileload_fromfile, & penum );
Filename points to a valid file name, And penum returns an enumeration object interface pointer.
Enumerated data object: //////////// after the template is registered, open the X file and obtain an enumerated object interface, the following data needs to be read from the X file. Enumeration object interface pointer pointing
File, because each data object may contain embedded data objects or referenced data objects
Other data objects at the same level of data objects are at the same level of data objects. For the types of child data objects
Travel inquiry.
You can use hresult idirectxfileenumobject: getnextdataobject (lpdirectxfiledata * ppdataobj) To Get
Idirectxfiledata interfaces. It has only one parameter, as shown below:
Idirectxfiledata * pdata;
Hresult hR = penum-> getnextdataobject (& pdata );
With this function, you can continuously access data object interfaces at the same level. The specific code is as follows:
While (succeeded (penum-> getnextdataobject (& pdata ))){
// Here you can operate on pdata objects.
Pdata-> release (); // Release Interface.
}
If the returned value is failed, all interfaces have been accessed. When the access value is succeeded, you need to continue to judge this data object
Whether to include sub-objects. Idirectxfileobject and hresult idirectxfiledata: getnextobject (
Lpdirectxfileobject * ppchildobj) function, the Code is as follows:
Idirectxfileobject * pobject;
While (succeeded (pdata-> getnextobject (& pobject )))
{
// If a child object exists, you need to continue to ask for it and determine whether its type is embedded data object or referenced data object.
Pobject-> release (); // Release Interface.
}
Next, ask the interface to see if it is an embedded data object:
Idirectxfiledata * psubdata;
If (succeeded (pobject-> QueryInterface (iid_idirectxfiledata, (void **) & psubdata ))){
// If the embedded data object is queried successfully, you can perform operations on the psubdata data object.
Psubdata-> release (); // Release Interface.
}
Check whether it is a reference data object:
Idirectxfiledatareference * Pref;
Idirectxfiledata * psubdata;
If (succeeded (psubobj-> QueryInterface (iid_idirectxfiledatareference, (void **) & Pref ))){
// If the referenced data object is successfully queried, the reference prototype is parsed.
Pref-> resolve (& psubdata );
// Here you can operate on pdata objects.
Pref-> release ();
Psubdata-> release (); // Release Interface.
}
Now let's sort out the following ideas: the general idea is actually very simple. First, enumerate the top-level data objects and then determine whether they have sub-objects.
A sub-object may be an embedded object or a referenced object. Ask its interface to determine the specific type.
The following are functions of the complete parse template:
Bool parse (char * filename)
{
Idirectxfile * pfile = NULL;
Idirectxfileenumobject * penum = NULL;
Idirectxfiledata * pdata = NULL;
If (failed (directxfilecreate (& pfile )))
Return false;
// Register a standard template.
If (failed (pfile-> registertemplates (lpvoid) d3drm_xtemplates, d3drm_xtemplate_bytes )))
Return false;
// Create an enumeration object interface.
If (failed (pdxfile-> createenumobject (lpvoid) filename, dxfileload_fromfile, & penum ))){
Pfile-> release ();
Return false;
}
// Traverse all top-level data objects.
While (succeeded (penum-> getnextdataobject (& pdata ))){
// Use parseobject to parse its child data objects.
Parseobject (pdata );
Pdata-> release ();
}
Penum-> release ();
Pfile-> release ();
Return true;
}
The main part of this function is parseobject (pdata), which is responsible for parsing the child data object:
Void parseobject (idirectxfiledata * pdata)
{
Idirectxfileobject * pobject = NULL;
Idirectxfiledata * psubdata = NULL;
Idirectxfiledatareference * Pref = NULL;
While (succeeded (pdata-> getnextobject (& pobject ))){
If (succeeded (pobject-> QueryInterface (iid_idirectxfiledatareference, (void **) & Pref ))){
Pref-> resolve (& psubdata );
Parseobject (psubdata );
Psubdata-> release ();
Pref-> release ();
}
If (succeeded (pobject-> QueryInterface (iid_idirectxfiledata, (void **) & psubdata ))){
Parseobject (psubdata );
Psubdata-> release ();
}
Pobject-> release ();
}
}
This is a default function that calls the function itself. Determine the sub-object category and continue parsing until the returned value is failed, indicating that no sub-object exists.
Sub-object. As we can see from the above, this function does not do anything except enumerate all objects. The following will refer to these data objects
Search data.
Retrieve data from a data object ://///////////////
When you use the idirectxfiledata interface pointer to point to a valid data object, you can call the idirectxfiledata: getname function to obtain the name of the data object. Function prototype:
Hresult idirectxfiledata: getname (
Lpstr pstrnamebuf, // Name Buffer
Lpdword pdwbuflen); // name buffer size
You can use this function as follows:
DWORD size;
Pdata-> getname (null, & size );
Char * name = new char [size];
Pdata-> getname (name, & size );
First, declare a DWORD size variable. When the getname function is called, set the first parameter to null and return the name buffer size in size. Use this size value to create a buffer for storing the name, and then call getname to return the name of the data object in the name.
After obtaining the data object name, you need to get the template guid of the data object to determine whether the data object is the data object of the template you want to use. Use the idirectxfiledata: GetType function. Its prototype is:
Hresult idirectxfiledata: GetType (const guid ** ppguid );
You can use this function as follows:
Const guid * templateguid = NULL;
Pdata-> GetType (& templateguid); // return the guid of the template corresponding to the Data Object in templateguid.
Check whether the guid matches the Data Object of the template you want to use.
If (* templateguid = tid_d3drmmeshnormals ){
// If the matching succeeds, you can continue to process the data objects of this template.
}
Finally, we will introduce the getdata function, which can be used to obtain the data of the Data Object. Its prototype is:
Hresult idirectxfiledata: getdata (
Lpcstr szmember, // set to null
DWORD * bytes size, // data size
Void ** ppvdata); // Data Pointer
The following describes how to use getdata to obtain the size of the Data Object Structure and the data of the Data Object.
Suppose there is a color template:
Template colorrgba {
<35ff44e0-6c7c-11cf-8f52-0040333594a3>
Float red;
Float green;
Float blue;
Float Alpha;
}
You can access the data of Data Objects Based on this template as follows:
DWORD datasize;
Float * dataptr;
Pdata-> getdata (null, & datasize, (void **) & dataptr );
Float Red = * dataptr ++;
Float Green = * dataptr ++;
Float Blue = * dataptr ++;
Float alpha = * dataptr ++;
After getting a pointer to the data object, it is as simple as accessing the general structure. Of course, you can do more directly:
Typedef struct {
Float red, green, blue, Alpha;
} Scolorrgba; // define a structure to facilitate access to data objects.
Scolorrgba * color;
DWORD datasize;
Pdata-> getdata (null, & datasize, (void **) & color );
In this way, access data is more direct:
Float Red = color-> red;
Float Blue = color-> blue;
Float Green = color-> green;
Float alpha = color-> alpha;
It is easy to access a single variable. Next we will continue to introduce the access to arrays or strings.
Access array:
DWORD datasize;
DWORD * dataptr;
Pdata-> getdata (null, & datasize, (void **) & dataptr );
DWORD numkeys = * dataptr ++;
For (DWORD I = 0; I <numkeys; I ++ ){
Float fvalue = * (float *) dataptr ++;
Access string:
DWORD datasize;
DWORD * dataptr;
Pdata-> getdata (null, & datasize, (void **) & dataptr );
Char * stringptr = (char *) dataptr;
MessageBox (null, stringptr, "texture FILENAME", mb_ OK );
In order to access arrays or strings, the essence is to convert pointers into matching types to facilitate pointer locating.
I will also write articles on 3D model file formats, bone animation, and terrain rendering ;)

Author: Confused xiaoya
QQ: 183237048

Credit: 12826
Post: 1632
Register: 2004-11-11 "href =" http://bbs.skonline.net/viewpro.php? Uid = 8990 "> Xiaoyao swordsman

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.