DOM guide: Integration templates: Difference between revisions

From COLLADA Public Wiki
Jump to navigation Jump to search
SteveT (talk | contribs)
SteveT (talk | contribs)
No edit summary
 
(25 intermediate revisions by 2 users not shown)
Line 1: Line 1:
When you [[DOM guide: Loading and saving documents|load]] a [[COLLADA XML instance document]] into the [[COLLADA DOM]], it creates a DOM object for each XML element in the COLLADA document. An application can then use DOM methods to read and manipulate the DOM objects. However, your application might already have its own data structures designed to represent the data coming from COLLADA XML elements and its own tools for manipulating those data structures. In that case, you can implement the COLLADA DOM's '''integration templates''', which, when loading a COLLADA document, convert DOM objects into your application's data structures, and back again when saving. The integration templates cannot be used to create new COLLADA documents based on your runtime data structures.
When you [[DOM guide: Working with documents|load]] a [[COLLADA XML instance document]] into the [[COLLADA DOM]], it creates a DOM object for each XML element in the COLLADA document. An application can then use DOM methods to read and manipulate the DOM objects. However, your application might already have its own data structures designed to represent the data coming from COLLADA XML elements and its own tools for manipulating those data structures. In that case, you can implement the COLLADA DOM's '''integration templates''', which, when loading a COLLADA document, convert DOM objects into your application's data structures, and back again when saving. The integration templates cannot be used to create new COLLADA documents based on your runtime data structures.


This article provides a step-by-step example of how to integrate application data structures with the COLLADA runtime infrastructure ([[COLLADA DOM]]). Detailed knowledge of the COLLADA DOM architecture is not necessary to understand this example or to successfully integrate your own application-specific data structures.
This article provides a step-by-step example of how to integrate application data structures with the COLLADA runtime infrastructure ([[COLLADA DOM]]). Detailed knowledge of the COLLADA DOM architecture is not necessary to understand this example or to successfully integrate your own application-specific data structures.


==Overview==
==Overview==
The [[COLLADA Object Model]] is a set of runtime COLLADA DOM objects that correspond to elements in a [[COLLADA XML instance document]] (a ''COLLADA document'''). For importing, the COLLADA DOM builds these objects when it parses a COLLADA document. For exporting into a COLLADA  document, you need to provide code to build the appropriate DOM objects.  
The [[COLLADA Object Model]] is a set of runtime COLLADA DOM objects that correspond to elements in a [[COLLADA XML instance document]] (a ''COLLADA document''). For importing, the COLLADA DOM builds these objects when it parses a COLLADA document. For exporting into a COLLADA  document, you need to provide code to build the appropriate DOM objects.  


To convert data contained in COLLADA DOM objects into your own application's data structures, the DOM provides integration templates for every object. The integration templates include plug-in points where you can insert conversion code (otherwise, the templates do nothing). After a COLLADA document has been imported by the DOM into COLLADA DOM objects, the DOM calls your conversion code to convert between the DOM objects and the application's data structures.  
To convert data contained in COLLADA DOM objects into your own application's data structures, the DOM provides integration templates for every object. The integration templates include plug-in points where you can insert conversion code (otherwise, the templates do nothing). After a COLLADA document has been imported by the DOM into COLLADA DOM objects, the DOM calls your conversion code to convert between the DOM objects and the application's data structures.  
Line 15: Line 15:
#Application registers its application-specific integration libraries.
#Application registers its application-specific integration libraries.
#When importing a COLLADA document:
#When importing a COLLADA document:
##Application begins the import by calling '''load'''.
##Application begins the import by calling <code>load</code>.
##The DOM reads the COLLADA document and places its elements into runtime COLLADA DOM objects.
##The DOM reads the COLLADA document and places its elements into runtime COLLADA DOM objects.
##The COLLADA runtime calls the conversion methods in the integration libraries to convert the content of COLLADA DOM objects into their associated data structures.
##The COLLADA runtime calls the conversion methods in the integration libraries to convert the content of COLLADA DOM objects into their associated data structures.
#When exporting to a COLLADA document:
#When exporting to a COLLADA document:
##Application begins the export of a COLLADA DAE structure by calling '''save'''.
##Application begins the export of a COLLADA DAE structure by calling <code>save</code>.
##The COLLADA runtime calls registered conversion methods in the integration libraries to convert the content of the application's data structures into COLLADA DOM objects.
##The COLLADA runtime calls registered conversion methods in the integration libraries to convert the content of the application's data structures into COLLADA DOM objects.
##The DOM exports the COLLADA document.
##The DOM exports the COLLADA document.


==COLLADA DOM Integration Templates==
==COLLADA DOM Integration Templates==
Integration templates are used to convert DOM objects to your run-time data structures. Each DOM object has its own integration template consisting of a header (.h) file and a code (.cpp) file. For example, the DOM object for the COLLADA <node> element is  '''domNode''', and the template files to convert '''domNode''' objects to or from application data structures are '''intNode.cpp''' and '''intNode.h'''.  
Integration templates are used to convert DOM objects to your run-time data structures. Each DOM object has its own integration template consisting of a header (.h) file and a code (.cpp) file. For example, the DOM object for the COLLADA <node> element is  <code>domNode</code>, and the template files to convert <code>domNode</code> objects to or from application data structures are <code>intNode.cpp</code> and <code>intNode.h</code>.  


Copy into your application directory the template files for the elements that you want to convert from DOM objects into application-specific data structures. These copies are the starting points for your customized integration libraries. You can then add code to these integration libraries to do the conversion.
Copy into your application directory the template files for the elements that you want to convert from DOM objects into application-specific data structures. These copies are the starting points for your customized integration libraries. You can then add code to these integration libraries to do the conversion.


The plug-in points for your code are identified in comments in the template source.
The plug-in points for your code are identified in comments in the template source, which is available in the <code>integrationTemplates</code> folder.
 
The COLLADA DOM provides two sets of integration templates:
*Simple integration templates: These templates are in the templates/integrationSimple directory, and provide plug-in points for a basic set of COLLADA elements. These templates do not provide plug-in points for nested COLLADA elements, such as the '''<author>''' element found in '''<asset>''' that is defined by the '''domAuthor''' DOM object class.
*Full integration templates: These templates are in the templates/integrationFull directory, and provide plug-in points for all COLLADA elements.


==Integration Objects==
==Integration Objects==
Your integration library provides the code that defines an integration object for each DOM object. The integration object serves as the focal point for all information about the conversion. It defines methods that perform the conversion, and it provides data members that bind the DOM object with the application-specific data structure being converted to.
Your integration library provides the code that defines an integration object for each DOM object. The integration object serves as the focal point for all information about the conversion. It defines methods that perform the conversion, and it provides data members that bind the DOM object with the application-specific data structure being converted to.


Integration objects are represented with the '''daeIntegrationObject''' class. Every class derived from '''daeElement''' provides for an integration object through the data member '''daeElement::_intObject'''.
Integration objects are represented with the <code>daeIntegrationObject</code> class. Every class derived from <code>daeElement</code> provides for an integration object through the data member <code>daeElement::_intObject</code>.


The integration object class for each element is defined with the prefix "int". For example, the '''domGeometry''' class provides an '''intGeometry''' integration object.
The integration object class for each element is defined with the prefix "int". For example, the <code>domGeometry</code> class provides an <code>intGeometry</code> integration object.


When you define integration code for a DOM object, your code binds the DOM object with your application-specific object, via the '''_element''' and '''_object''' data members of the integration object. You can use methods '''daeIntegrationObject::getElement''' and '''getObject''' to access these data members.
When you define integration code for a DOM object, your code binds the DOM object with your application-specific object, via the <code>_element</code> and <code>_object</code> data members of the integration object. You can use methods <code>daeIntegrationObject::getElement</code> and <code>getObject</code> to access these data members.


When you load a COLLADA instance document, the DOM automatically creates the appropriate integration objects.  
When you load a COLLADA instance document, the DOM automatically creates the appropriate integration objects.  


To get the integration object for an element, use the method '''daeElement::getIntObject''', which also initiates the conversion process for any objects that have not yet been converted.
To get the integration object for an element, use the method <code>daeElement::getIntObject</code>, which also initiates the conversion process for any objects that have not yet been converted.


==Integration Template Plugin Points==
==Integration Template Plugin Points==
Line 51: Line 47:


The methods are:
The methods are:
*'''createFrom''': Defines the code to create the application-specific data structure associated with the DAE class for this template. This method sets up the integration object for the DAE class.
*<code>createFrom</code>: Defines the code to create the application-specific data structure associated with the DAE class for this template. This method sets up the integration object for the DAE class.
*'''fromCOLLADA''': Defines the code to covert a DOM object into your application-specific data structure.
*<code>fromCOLLADA</code>: Defines the code to covert a DOM object into your application-specific data structure.
*'''fromCOLLADAPostProcess''': Defines any post-processing code that must execute after the basic conversion to your data structure.
*<code>fromCOLLADAPostProcess</code>: Defines any post-processing code that must execute after the basic conversion to your data structure.
*'''createTo''': Defines code to create the DOM object associated with the DAE class for this template if such a structure does not already exist (for example, if it was not created while importing).
*<code>createTo</code>: Defines code to create the DOM object associated with the DAE class for this template if such a structure does not already exist (for example, if it was not created while importing).
*'''toCOLLADA''': Defines the code to covert the content of your application’s data structures into DOM objects.
*<code>toCOLLADA</code>: Defines the code to covert the content of your application’s data structures into DOM objects.
*'''toCOLLADAPostProcess''': Defines any post-processing code that must execute after the basic conversion from your application’s data structure.
*<code>toCOLLADAPostProcess</code>: Defines any post-processing code that must execute after the basic conversion from your application’s data structure.


==Geometry Integration Example==
==Geometry Integration Example==
This example demonstrates integration with the '''<geometry>''' element.
This example demonstrates integration with the <geometry> element.
   
   
Creating the example has three basic steps, summarized as follows:
Creating the example has three basic steps, summarized as follows:
Line 67: Line 63:


===Copying Integration Templates===
===Copying Integration Templates===
The first step in the process of creating conversion code is to copy the relevant integration template files from the integration subdirectory into your application’s directory. For this example, the relevant integration templates are '''intGeometry.cpp''' and '''intGeometry.h'''. You will modify the copied files to contain your conversion code.
The first step in the process of creating conversion code is to copy the relevant integration template files from the integration subdirectory into your application’s directory. For this example, the relevant integration templates are <code>intGeometry.cpp</code> and <code>intGeometry.h</code>. You will modify the copied files to contain your conversion code.


===Registering Integration Libraries===
===Registering Integration Libraries===
For the DOM to create integration objects during parsing or before exporting, you must register the integration classes with the infrastructure. To do this, call the '''registerElement''' method on each integration class that you are using. A function to register these classes with the COLLADA DOM is defined in '''intRegisterElements.cpp''', which is also provided in the template directory. You can copy this function into your application's directory, though you should only register those classes that you'll be using, which in this example is intGeometry. The relevant code for is shown here:  
For the DOM to create integration objects during parsing or before exporting, you must register the integration classes with the infrastructure. To do this, call the <code>registerElement</code> method on each integration class that you are using. A function to register these classes with the COLLADA DOM is defined in <code>intRegisterElements.cpp</code>, which is also provided in the template directory. You can copy this function into your application's directory, though you should only register those classes that you'll be using, which in this example is <code>intGeometry</code>. The relevant code is shown here:  
  void intRegisterElements()
  void intRegisterElements()
  {
  {
     intGeometry::registerElement();
     intGeometry::registerElement();
  }
  }
The integration file intGeometry.cpp provides the definition for the '''intGeometry''' class. Its contents are explained in a later step.
The integration file <code>intGeometry.cpp</code> provides the definition for the <code>intGeometry</code> class. Its contents are explained in a later step.
 
After the '''intRegisterElements''' function is defined, pass a handle to this function into the '''DAE::setIntegrationLibrary''' method, as described in the “Invoking Integration Libraries Registration” section.


===Setting up the Integration Header File===
After the <code>intRegisterElements</code> function is defined, pass a handle to this function into the <code>DAE::setIntegrationLibrary</code> method, as described in the “Invoking Integration Libraries Registration” section.
Now edit the new integration class. Start with the '''intGeometry.h''' file copied from the original '''intGeometry.h''' template. The template must be modified to add information about the application data structures to which the DOM object will be converted. For the header, declare the class to be converted to:
// class myGeometry is fully defined in an application header file.
class myGeometry;
Also add code to define the relevant structures and provide a method to return those structures. These definitions fall within the body of the '''intGeometry''' integration object declaration:
class intGeometry : public daeIntegrationObject
{
    // intGeometry template declarations provided here
    . . .
public: // USER CODE
    virtual ~intGeometry();
    // define the accessor for the myGeometry object
    myGeometry *getGeometry() { return _object; }
private: // USER CODE
    // declare the types for the integration object data members
    myGeometry *_object;
    daeElement *_element;
};


===Defining Your Application Data Structure===
===Defining Your Application Data Structure===
Within the integration library files, provide the code to create your application-specific data structure for the COLLADA DOM objects that you want to convert.
Within the integration library files, provide the code to create your application-specific data structure for the COLLADA DOM objects that you want to convert.


For this example, the application's  data structure used to represent geometry is defined in an application header file, in this case, '''myGeometry.h''':
For this example, the application's  data structure used to represent geometry is defined in an application header file, in this case, <code>myGeometry.h</code>:
  // Definition of application's myPologon class also included here
  // Definition of application's myPologon class also included here
  class myGeometry
  class myGeometry
Line 112: Line 89:


===Provide the Plug-in Code to Create an Application Object for Importing===
===Provide the Plug-in Code to Create an Application Object for Importing===
The plug-in method relevant to this step for importing is the '''createFrom()''' method. Within the '''createFrom()''' method, add code to create a new ''myGeometry''' object, initialize it, and initialize the '''intGeometry''' integration object data members. Within '''intGeometry.cpp''':  
The plug-in method relevant to this step for importing is the <code>createFrom</code> method. Within the <code>createFrom</code> method, add code to create a new <code>myGeometry</code> object, initialize it, and initialize the <code>intGeometry</code> integration object data members. Within <code>intGeometry.cpp</code>:  
  void intGeometry::createFrom(daeElementRef element)
  void intGeometry::createFrom(daeElementRef element)
  {
  {
Line 122: Line 99:
     _element = element;
     _element = element;
  }
  }
Now, when a COLLADA instance document is loaded  and a '''domGeometry''' object is encountered, the '''createFrom()''' method automatically creates a new '''myGeometry''' object, because the '''intGeometry''' integration library has been registered with the DOM.
Now, when a COLLADA instance document is loaded  and a <code>domGeometry</code> object is encountered, the <code>createFrom</code> method automatically creates a new <code>myGeometry</code> object, because the <code>intGeometry</code> integration class has been registered with the DOM.


===Build Conversion Code for Importing===
===Build Conversion Code for Importing===
Now add code to the integration library to translate the data stored in a DOM object to its associated application data structure. The '''fromCOLLADA()''' method is the plug-in point for the basic application-specific conversion code. The '''fromCOLLADAPostProcess()''' method provides additional flexibility for the conversion process.
Now add code to the integration library to translate the data stored in a DOM object to its associated application data structure. The <code>fromCOLLADA</code> method is the plug-in point for the basic application-specific conversion code. The <code>fromCOLLADAPostProcess</code> method provides additional flexibility for the conversion process.


Depending on the COLLADA DOM object and the application data structure, the code may vary significantly. For this example, we use the '''intGeometry::fromCOLLADA()''' method to create new vertex buffers from the COLLADA DAE geometry object.
Depending on the COLLADA DOM object and the application data structure, the code may vary significantly. For this example, we use the <code>intGeometry::fromCOLLADA</code> method to create new vertex buffers from the COLLADA DOM geometry object.


The following code retrieves the mesh object from the COLLADA '''domGeometry''' object:
The following code retrieves the mesh object from the COLLADA <code>domGeometry</code> object:
  // Get the geometry element from this integration object
  // Get the geometry element from this integration object
  domGeometry* geomElement = (domGeometry*)(domElement*)getElement();
  domGeometry* geomElement = (domGeometry*)(domElement*)getElement();
  domMesh *meshEl = geomElement->getMesh();
  domMesh *meshEl = geomElement->getMesh();
When we have the mesh, we can construct our application-specific data structure iVertices for the myGeometry object. The following code shows how to create the new vertex buffer.
When we have the mesh, we can construct our application-specific data structure <code>iVertices</code> for the <code>myGeometry</code> object. The following code shows how to create the new vertex buffer.
  // Get a pointer to the application-defined geometry object that was
  // Get a pointer to the application-defined geometry object that was
  // automatically created during load by calling createFrom.
  // automatically created during load by calling createFrom.
  myGeometry *local = (myGeometry *)_object;
  myGeometry *local = (myGeometry *)_object;
  // Get a pointer to the domPolygons in this domMesh. To simplify this example,
  // Get a pointer to the domPolygons in this domMesh. To simplify this example,
  // we will handle only a domMesh that has a single domPolygons.
  // we will handle only a domMesh that has a single domPolygons.
Line 145: Line 123:
  }
  }
  domPolygons *polygons = meshElement->getPolygons_array()[0];
  domPolygons *polygons = meshElement->getPolygons_array()[0];
  int polygonCount = polygons->getCount();
   
  // To simplify this example, we assume the domPolygons has only one domInput.
  // To simplify this example, we assume the domPolygons has only one domInput.
  if(polygons->getInput_array().getCount() != 1)
  if(polygons->getInput_array().getCount() != 1)
Line 152: Line 130:
     return;
     return;
  }
  }
  // Loop over all the polygons in the domPolygons element
  // Loop over all the polygons in the domPolygons element
int polygonCount = polygons->getCount();
  for (int i=0;i<polygonCount;i++)
  for (int i=0;i<polygonCount;i++)
  {
  {
Line 174: Line 154:
     local->_vPolygons.push_back(myPoly);
     local->_vPolygons.push_back(myPoly);
  }
  }
  // Copy the vertices we are going to use into myGeometry. To keep things simple,
  // Copy the vertices we are going to use into myGeometry. To keep things simple,
  // we will assume there is only one domSource and domFloatArray in the domMesh,
  // we will assume there is only one domSource and domFloatArray in the domMesh,
Line 186: Line 167:
  }
  }
  domSource *source = meshElement->getSource_array()[0];
  domSource *source = meshElement->getSource_array()[0];
  if(source->getFloat_array().getCount() != 1)
  if(source->getFloat_array().getCount() != 1)
  {
  {
Line 191: Line 173:
  }
  }
  domFloat_array *floatArray = source->getFloat_array()[0];
  domFloat_array *floatArray = source->getFloat_array()[0];
  // Assume there are 3 values per vertex with a stride of 3.
  // Assume there are 3 values per vertex with a stride of 3.
  local->_iVertexCount = floatArray->getCount()/3;
  local->_iVertexCount = floatArray->getCount()/3;
  local->_pVertices = new float[local->_iVertexCount*3];
  local->_pVertices = new float[local->_iVertexCount*3];
  // Copy the vertices into my structure one-by-one
  // Copy the vertices into my structure one-by-one
  // (converts from COLLADA's doubles to floats).
  // (converts from COLLADA's doubles to floats).
Line 204: Line 188:
Now put together the application code that registers the integration libraries, loads the COLLADA elements into the in-memory DOM objects, and accesses the converted data.  
Now put together the application code that registers the integration libraries, loads the COLLADA elements into the in-memory DOM objects, and accesses the converted data.  


====Invoke Integration Libraries Registration====
====Invoke Integration Library Registration====
You defined the '''intRegisterElements()''' function as described in “Register Integration Libraries with the DOM.” Now you must pass a handle to this function into the '''DAE::setIntegrationLibrary()''' method.
You defined the <code>intRegisterElements</code> function as described in “Register Integration Libraries with the DOM.” Now you must pass a handle to this function into the <code>DAE::setIntegrationLibrary</code> method.


With this step, the elements that you want to convert are registered with the DOM, and are converted from COLLADA DOM objects to application-specific structures when a COLLADA document is loaded, or the reverse when an object is saved.
With this step, the elements that you want to convert are registered with the DOM, and are converted from COLLADA DOM objects to application-specific structures when a COLLADA document is loaded, or the reverse when an object is saved.
Line 211: Line 195:
For example, your main application code could pass a handle to the integration library registration function as follows:
For example, your main application code could pass a handle to the integration library registration function as follows:
  // Instantiate the reference implementation
  // Instantiate the reference implementation
  daeObject = new DAE;
  collada_dom = new DAE;
  //register the integration objects
  //register the integration objects
  daeObject->setIntegrationLibrary(&intRegisterElements);
  collada_dom->setIntegrationLibrary(&intRegisterElements);


====Parse a COLLADA File====
====Parse a COLLADA File====
Now parse a COLLADA document into the run-time COLLADA Object Model. The load step creates the integration objects and invokes the '''createFrom()''' and '''romCOLLADA()''' methods to convert the data from the COLLADA DOM objects into the application-defined data structures.
Now parse a COLLADA document into the run-time COLLADA Object Model. The load step creates the integration objects and invokes the <code>createFrom</code> and <code>fromCOLLADA</code> methods to convert the data from the COLLADA DOM objects into the application-defined data structures.
  //load the COLLADA file
  //load the COLLADA file
  int res = daeObject->load(filename);
  int res = collada_dom->load(filename);


====Access a COLLADA Object====
====Access a COLLADA Object====
With the COLLADA file parsed and loaded into in-memory objects, you can now request objects from the database {{editor | what=  I've removed previous mentions of the database because seemed not relevant earlier but clearly need somewhere to mention runtime database's role.}}. Here we request that the database return the first geometry element, placing it into '''pElem'''.
Now we'll find a <code>domGeometry</code> object to get at our application-specific data. We'll use the database for this. Here we request that the database return the first geometry element, placing it into <code>domGeom</code>.
 
:'''''Note:''' See [[DOM guide: Working with elements#Querying Database Elements|Querying Database Elements]] for additional details on how to use the database to retrieve COLLADA DOM objects.''
 
  //query the runtime to retrieve an element
  //query the runtime to retrieve an element
  int res = daeObject->getDatabase()->getElement
domGeometry* domGeom = 0;
((daeElement**)&pElem,0,NULL,COLLADA_ELEMENT_GEOMETRY);
  int res = collada_dom->getDatabase()->getElement(
  (daeElement**)&domGeom, 0, NULL, COLLADA_TYPE_GEOMETRY);


====Acquire the Converted Application Object====
====Acquire the Converted Application Object====
In the following code, the '''myGeometry''' class represents the application data structure containing geometry data. The COLLADA DOM object retrieved above into '''pElem''' contains a reference to its associated integration object (thanks to its earlier registration). The '''intGeometry''' class converts the COLLADA data {{editor | what= which collada data are we talking about here?}} and returns the converted data as a '''myGeometry''' instance via the '''getGeometry()method''', which was defined in '''importGeometry.h'''.
In the following code, the <code>myGeometry</code> class represents the application data structure containing geometry data. The <code>domGeometry</code> object retrieved above into <code>domGeom</code> contains a reference to its associated integration object (thanks to its earlier registration). The <code>intGeometry</code> class converts the <code>domGeometry</code> data into a <code>myGeometry</code> instance, which can then be retrieved with the <code>getObject</code> method.
  // Get the integration object from the element
  // Get the integration object from the element
  daeIntegrationObject *pIntegrationObj = pElem->getIntObject();
  intGeometry *intGeom = (intGeometry*)domGeom->getIntObject();
intGeometry *importGeometry =(intGeometry *)pIntegrationObj;
  // Extract the user data from the integration object
  //extract the user data from the integration object
  myGeometry* myGeom = (myGeometry*)intGeom->getObject();
  myGeometry geom = importGeometry->getGeometry();


==Exporting Using Integration==
==Exporting Using Integration==
Line 238: Line 225:


If you modify data values and want to write it to a COLLADA instance document, the process is similar to reversing the import-and-convert process, although there are some differences.  
If you modify data values and want to write it to a COLLADA instance document, the process is similar to reversing the import-and-convert process, although there are some differences.  
===Modifying a COLLADA DOM Object Structure===
In some cases, your application might modify the structure of your application objects that you loaded from a COLLADA DOM object. If you want to export the data back to a COLLADA instance document, you must change the structure of the COLLADA DOM object to match your changes before converting from your application-specific structures. The example code does not show how to do this.


===Creating a New COLLADA DOM Object Using createTo===
===Creating a New COLLADA DOM Object Using createTo===
In some cases, you might need to export your data to an entirely new COLLADA document, such as when the data originated in your application rather than by being imported from an existing COLLADA instance document. In this cases, to use the integration methods to convert from your application structure to a DOM object and then export to a new COLLADA instance document, you must create the matching COLLADA DOM structure before you can convert from your application’s structure.
The functionality for creating new COLLADA DOM documents with the integration templates via the <code>createTo</code> method has always been broken, and wouldn't have been very useful even if it did work. Creating new COLLADA documents via the integration templates is now officially deprecated and won't be supported in the future. See [https://collada.org/public_forum/viewtopic.php?t=777&sid=2d247be12d1d1924baa2922a44f956b2 this thread] in the COLLADA forums for more details.
 
Compare with the import process: When your application loads a COLLADA instance document, the COLLADA DOM automatically creates DOM objects in which to load the COLLADA data, then calls your customized createFrom for each element that has an integration class, which creates your application-specific objects. Then it calls fromCOLLADA, which copies the data from the DOM objects to your application objects.
 
If you create a new application object, there are no COLLADA elements (no DOM objects) associated with it. If you simply save the file, this new data is not written. The purpose of createTo is to create these COLLADA elements and associate them with the application object. createTo is not called automatically when a new application object is created; you must call it explicitly. After createTo has been called, the rest of the saving process is automatic, that is, if you have registered your integration library, when you call save, toCOLLADA is called for every COLLADA element with an integration class to copy the data from the application objects into the COLLADA objects.
 
This example code does not show how to do this. The createTo method looks like this:
void intGeometry::createTo(void *userData)
{
    // This function would create new COLLADA elements from an
    // application- defined object
    // It is NOT called automatically by the COLLADA DOM.
    // The user needs to call this when creating a new
    // application-specific object.
}


===Exporting Modified Data Values===
===Exporting Modified Data Values===
If you have modified only the values of the COLLADA data and want to export to a revised COLLADA instance document, this example shows how you might convert the data.
If you have modified the values of your application object and want to save to a revised COLLADA instance document, you'll need to update the associated COLLADA DOM objects before saving. This example shows how you might do that.


The saving process for this example is automatic because you have registered your integration library. When you call '''save''', the DOM calls '''toCOLLADA''' for every COLLADA element with an integration class. This copies the data from the application objects into the COLLADA ones.
When you call <code>save</code>, the DOM calls <code>toCOLLADA</code> for every COLLADA element with an integration class. In the <code>toCOLLADA</code> method we'll copy the data from our application objects to the COLLADA objects. The code is essentially the reverse of the <code>fromCOLLADA</code> code in the importing example. We make several assumptions about the nature of the changes to make the code simpler. These assumptions are explained in the comments.


Note that the code is essentially the reverse of the '''fromCOLLADA''' code in the importing example.
  void intGeometry::toCOLLADA()
  void intGeometry::toCOLLADA()
  {
  {
    // INSERT CODE TO TRANSLATE TO YOUR RUNTIME HERE
    // The following lines are example code from the template:
    // myRuntimeClassType* local = (myRuntimeClassType*)_object;
    // element->foo = local->foo;
    // element->subelem[0]->bar = local->bar;
     // This code takes data from an application-defined object and
     // This code takes data from an application-defined object and
     // puts it back into the appropriate collada objects.
     // puts it back into the appropriate collada objects.
     // Get a pointer to the COLLADA domGeometry element
     // Get a pointer to the COLLADA domGeometry element
     // (this is the element that called us)
     // (this is the element that called us)
     domGeometry* geometryElement = (domGeometry*)(domElement*)_element;
     domGeometry* geometryElement = (domGeometry*)_element;
     // Get a pointer to the domGeometry's domMesh element
     // Get a pointer to the domGeometry's domMesh element
     domMesh *meshElement = geometryElement->getMesh();
     domMesh *meshElement = geometryElement->getMesh();
     // Get a pointer to my object that's assocated with this collada object
     // Get a pointer to my object that's assocated with this collada object
     myGeometry *local = (myGeometry *)_object;
     myGeometry *local = (myGeometry *)_object;
     // Get a pointer to the domPolygons in this domMesh.
     // Get a pointer to the domPolygons in this domMesh.
     // To simplify this example,
     // To simplify this example, we will handle only a domMesh  
    // we will handle only a domMesh that has a single domPolygons
    // that has a single domPolygons
     if(meshElement->getPolygons_array().getCount() != 1)
     if(meshElement->getPolygons_array().getCount() != 1)
     {
     {
Line 290: Line 257:
     }
     }
     domPolygons *polygons = meshElement->getPolygons_array()[0];
     domPolygons *polygons = meshElement->getPolygons_array()[0];
    int polygonCount = local->_vPolygons.size();
     // To simplify this example, we assume that the domPolygons has
     // To simplify this example, we assume that the domPolygons has
     // only one domInput
     // only one domInput.
     if(polygons->getInput_array().getCount() != 1)
     if(polygons->getInput_array().getCount() != 1)
     {
     {
Line 298: Line 265:
         return;
         return;
     }
     }
     // Loop over all the polygons in the domPolygons element and
     // Loop over all the polygons in the domPolygons element and
     // put the data from
     // put the data from myGeometry back into it. For the purposes  
    // myGeometry back into it. For the purposes of the example,
    // of the example, assume the number of polygons and indices  
    // assume the number of polygons and indices hasn't changed
    // hasn't changed so we can just update the values in place.
    // so we can just update the values in place
    int polygonCount = local->_vPolygons.size();
     polygons->setCount(polygonCount);
     polygons->setCount(polygonCount);
     for (int i=0;i<polygonCount;i++)
     for (int i=0;i<polygonCount;i++)
Line 312: Line 280:
             poly->getValue()[j] = local->_vPolygons[i]._pIndexes[j] ;
             poly->getValue()[j] = local->_vPolygons[i]._pIndexes[j] ;
     }
     }
     // Now copy the vertices from myGeometry back to the source.
     // Now copy the vertices from myGeometry back to the source.
     // Assume that the number of
     // Assume that the number of vertices hasn't changed so we can  
    // vertices hasn't changed so we can just update them in place.
    // just update them in place.
     if(meshElement->getSource_array().getCount() != 1)
     if(meshElement->getSource_array().getCount() != 1)
     {
     {
Line 321: Line 290:
     }
     }
     domSource *source = meshElement->getSource_array()[0];
     domSource *source = meshElement->getSource_array()[0];
     if(source->getFloat_array_array().getCount() != 1)
     if(source->getFloat_array_array().getCount() != 1)
     {
     {
Line 326: Line 296:
     }
     }
     domFloat_array *floatArray = source->getFloat_array()[0];
     domFloat_array *floatArray = source->getFloat_array()[0];
     // Copy the vertices into from myGeometry back into the
    // COLLADA float array
     // Copy the vertices from myGeometry back into the COLLADA float array
     floatArray->setCount(local->_iVertexCount*3);
     floatArray->setCount(local->_iVertexCount*3);
     for ( unsigned int i = 0; i < local->_iVertexCount*3; i++ )  
     for ( unsigned int i = 0; i < local->_iVertexCount*3; i++ )  
Line 337: Line 307:
==Using integration versus using the DOM directly==  
==Using integration versus using the DOM directly==  


Integration classes provide a SAX-like approach to using the DOM. The biggest problem with this is that the interdependent links between COLLADA elements has become very complex. Parsing a DOM structure lends itself better to handle these problems. Creating an integration class to process, for example, materials may take the same amount of work as accessing the DOM structures itself. Here's a comparison.
Integration classes provide a SAX-like approach to using the DOM. The biggest problem with this is that the interdependent links between COLLADA elements have become very complex. Parsing a DOM structure lends itself better to handling these problems. Creating an integration class to process <material>s, for example, may take the same amount of work as accessing the DOM structures directly. Here's a comparison.


To do with integration class:
Import <material> with an integration class:
#Create class that inherits from '''daeIntegrationObject'''.
#Create class that inherits from <code>daeIntegrationObject</code>.
#Implement the '''fromCOLLADA''' code to do the conversion.
#Implement the <code>fromCOLLADA</code> code to do the conversion.
#Implement the '''fromCOLLADAPostProcess''' code to do the processing needed to resolve links to other elements, for example, effect.
#Implement the <code>fromCOLLADAPostProcess</code> code to do the processing needed to resolve links to other elements, for example, <effect>.
#Make your integration library register with the DOM.
#Register your integration library with the DOM.


To do with DOM access:
Import <material> without an integration class:
#Get DOM by calling '''getDOM()''' or '''getDOMRoot''' from the '''daeDocument'''.
#Get DOM by calling <code>getDOM</code> or <code>getDOMRoot</code> from the <code>daeDocument</code>.
#*For each material library
#For each <material> in each <library_materials>, call your conversion function (which would be the same as your fromCOLLADA). Your conversion function can even do the postprocess linking since all the data required by the other elements would be available.
#*For each material
#Call your conversion function (which would be the same as your fromCOLLADA). Your conversion function can even do the post-processing linking needed since all the data required by the other elements would be available.


Also it is a lot harder to go from custom classes to the COLLADA DOM with the integration objects than it would be just with the DOM structures.
Also you can't create COLLADA documents from scratch with an integration library.


DOM structures give you finer-grained control over what to convert; for example, you can spend time converting only the geometries in the scene and not the possible dozens (or hundreds or thousands) of others, while the integration classes convert all of them. This is especially useful to know if you have documents that cross-reference to other documents. For example, imagine loading a document that contains a visual scene where all the <instance_geometry> elements reference a geometry from a separate document. This document has only one <library_geometries> and contains all the geometry elements used for all your scenes, not just the one being loaded. Because the COLLADA DOM, by default, loads external documents to resolve the URIs, the integration classes will convert every geometry found in the second document. There could be lots of geometries and that could take a long time to process, especially since there will be geometries that never get used in the scene.
DOM structures give you finer-grained control over what to convert; for example, you can spend time converting only the <geometry> elements used by a specific <visual_scene> and not the possibly dozens (or hundreds or thousands) of other <geometry> elements, while the integration classes convert all of them. This is especially useful if you have documents that cross-reference other documents. For example, imagine loading a document that contains a <visual_scene> where all the <instance_geometry> elements reference a <geometry> from a separate document. This document has only one <library_geometries> and contains all the <geometry> elements used for all your scenes, not just the one being loaded. Because the COLLADA DOM, by default, loads external documents to resolve the URIs, the integration classes will convert every <geometry> found in the second document. There could be lots of <geometry> elements and that could take a long time to process. This is especially wasteful since many <geometry> elements won't ever get used in the scene you're loading.


{{DOM tutorial}}
{{DOM navigation}}


[[Category:DOM tutorial|Integration templates]]
[[Category:COLLADA DOM|Integration templates]]
[[Media:Example.ogg]]

Latest revision as of 23:47, 6 February 2008

When you load a COLLADA XML instance document into the COLLADA DOM, it creates a DOM object for each XML element in the COLLADA document. An application can then use DOM methods to read and manipulate the DOM objects. However, your application might already have its own data structures designed to represent the data coming from COLLADA XML elements and its own tools for manipulating those data structures. In that case, you can implement the COLLADA DOM's integration templates, which, when loading a COLLADA document, convert DOM objects into your application's data structures, and back again when saving. The integration templates cannot be used to create new COLLADA documents based on your runtime data structures.

This article provides a step-by-step example of how to integrate application data structures with the COLLADA runtime infrastructure (COLLADA DOM). Detailed knowledge of the COLLADA DOM architecture is not necessary to understand this example or to successfully integrate your own application-specific data structures.

Overview

The COLLADA Object Model is a set of runtime COLLADA DOM objects that correspond to elements in a COLLADA XML instance document (a COLLADA document). For importing, the COLLADA DOM builds these objects when it parses a COLLADA document. For exporting into a COLLADA document, you need to provide code to build the appropriate DOM objects.

To convert data contained in COLLADA DOM objects into your own application's data structures, the DOM provides integration templates for every object. The integration templates include plug-in points where you can insert conversion code (otherwise, the templates do nothing). After a COLLADA document has been imported by the DOM into COLLADA DOM objects, the DOM calls your conversion code to convert between the DOM objects and the application's data structures.

The following figure shows what happens during a load when integration templates are used.

The basic steps during integration are the following:

  1. Application initializes the COLLADA DOM by creating a DAE object.
  2. Application registers its application-specific integration libraries.
  3. When importing a COLLADA document:
    1. Application begins the import by calling load.
    2. The DOM reads the COLLADA document and places its elements into runtime COLLADA DOM objects.
    3. The COLLADA runtime calls the conversion methods in the integration libraries to convert the content of COLLADA DOM objects into their associated data structures.
  4. When exporting to a COLLADA document:
    1. Application begins the export of a COLLADA DAE structure by calling save.
    2. The COLLADA runtime calls registered conversion methods in the integration libraries to convert the content of the application's data structures into COLLADA DOM objects.
    3. The DOM exports the COLLADA document.

COLLADA DOM Integration Templates

Integration templates are used to convert DOM objects to your run-time data structures. Each DOM object has its own integration template consisting of a header (.h) file and a code (.cpp) file. For example, the DOM object for the COLLADA <node> element is domNode, and the template files to convert domNode objects to or from application data structures are intNode.cpp and intNode.h.

Copy into your application directory the template files for the elements that you want to convert from DOM objects into application-specific data structures. These copies are the starting points for your customized integration libraries. You can then add code to these integration libraries to do the conversion.

The plug-in points for your code are identified in comments in the template source, which is available in the integrationTemplates folder.

Integration Objects

Your integration library provides the code that defines an integration object for each DOM object. The integration object serves as the focal point for all information about the conversion. It defines methods that perform the conversion, and it provides data members that bind the DOM object with the application-specific data structure being converted to.

Integration objects are represented with the daeIntegrationObject class. Every class derived from daeElement provides for an integration object through the data member daeElement::_intObject.

The integration object class for each element is defined with the prefix "int". For example, the domGeometry class provides an intGeometry integration object.

When you define integration code for a DOM object, your code binds the DOM object with your application-specific object, via the _element and _object data members of the integration object. You can use methods daeIntegrationObject::getElement and getObject to access these data members.

When you load a COLLADA instance document, the DOM automatically creates the appropriate integration objects.

To get the integration object for an element, use the method daeElement::getIntObject, which also initiates the conversion process for any objects that have not yet been converted.

Integration Template Plugin Points

The integration class for each DOM object provides six plug-in points into which you can add conversion code. The plug-in points are implemented as methods; you provide the code for the method bodies. You need to implement the body of the methods only for those plug-in points that are relevant to your application.

The methods are:

  • createFrom: Defines the code to create the application-specific data structure associated with the DAE class for this template. This method sets up the integration object for the DAE class.
  • fromCOLLADA: Defines the code to covert a DOM object into your application-specific data structure.
  • fromCOLLADAPostProcess: Defines any post-processing code that must execute after the basic conversion to your data structure.
  • createTo: Defines code to create the DOM object associated with the DAE class for this template if such a structure does not already exist (for example, if it was not created while importing).
  • toCOLLADA: Defines the code to covert the content of your application’s data structures into DOM objects.
  • toCOLLADAPostProcess: Defines any post-processing code that must execute after the basic conversion from your application’s data structure.

Geometry Integration Example

This example demonstrates integration with the <geometry> element.

Creating the example has three basic steps, summarized as follows:

  1. Copy the corresponding integration templates to application-specific versions.
  2. Register these classes with the COLLADA DOM.
  3. In the application runtime, add code to initialize the COLLADA DOM, call the file load, and request application objects from the integration classes.

Copying Integration Templates

The first step in the process of creating conversion code is to copy the relevant integration template files from the integration subdirectory into your application’s directory. For this example, the relevant integration templates are intGeometry.cpp and intGeometry.h. You will modify the copied files to contain your conversion code.

Registering Integration Libraries

For the DOM to create integration objects during parsing or before exporting, you must register the integration classes with the infrastructure. To do this, call the registerElement method on each integration class that you are using. A function to register these classes with the COLLADA DOM is defined in intRegisterElements.cpp, which is also provided in the template directory. You can copy this function into your application's directory, though you should only register those classes that you'll be using, which in this example is intGeometry. The relevant code is shown here:

void intRegisterElements()
{
    intGeometry::registerElement();
}

The integration file intGeometry.cpp provides the definition for the intGeometry class. Its contents are explained in a later step.

After the intRegisterElements function is defined, pass a handle to this function into the DAE::setIntegrationLibrary method, as described in the “Invoking Integration Libraries Registration” section.

Defining Your Application Data Structure

Within the integration library files, provide the code to create your application-specific data structure for the COLLADA DOM objects that you want to convert.

For this example, the application's data structure used to represent geometry is defined in an application header file, in this case, myGeometry.h:

// Definition of application's myPologon class also included here
class myGeometry
{
public:
    unsigned int _iVertexCount;
    float *_pVertices;
    std::vector<myPolygon> _vPolygons;
};

Provide the Plug-in Code to Create an Application Object for Importing

The plug-in method relevant to this step for importing is the createFrom method. Within the createFrom method, add code to create a new myGeometry object, initialize it, and initialize the intGeometry integration object data members. Within intGeometry.cpp:

void intGeometry::createFrom(daeElementRef element)
{
    // create class to hold geometry information and
    // initialize the new object as empty
    _object = new myGeometry();
    _object->pVertices = NULL;
    // set up the _element data member of the integration object
    _element = element;
}

Now, when a COLLADA instance document is loaded and a domGeometry object is encountered, the createFrom method automatically creates a new myGeometry object, because the intGeometry integration class has been registered with the DOM.

Build Conversion Code for Importing

Now add code to the integration library to translate the data stored in a DOM object to its associated application data structure. The fromCOLLADA method is the plug-in point for the basic application-specific conversion code. The fromCOLLADAPostProcess method provides additional flexibility for the conversion process.

Depending on the COLLADA DOM object and the application data structure, the code may vary significantly. For this example, we use the intGeometry::fromCOLLADA method to create new vertex buffers from the COLLADA DOM geometry object.

The following code retrieves the mesh object from the COLLADA domGeometry object:

// Get the geometry element from this integration object
domGeometry* geomElement = (domGeometry*)(domElement*)getElement();
domMesh *meshEl = geomElement->getMesh();

When we have the mesh, we can construct our application-specific data structure iVertices for the myGeometry object. The following code shows how to create the new vertex buffer.

// Get a pointer to the application-defined geometry object that was
// automatically created during load by calling createFrom.
myGeometry *local = (myGeometry *)_object;

// Get a pointer to the domPolygons in this domMesh. To simplify this example,
// we will handle only a domMesh that has a single domPolygons.
if(meshElement->getPolygons_array().getCount() != 1)
{
    fprintf(stderr, "This example supports only one domPolygons per domMesh\n");
    return;
}
domPolygons *polygons = meshElement->getPolygons_array()[0];

// To simplify this example, we assume the domPolygons has only one domInput.
if(polygons->getInput_array().getCount() != 1)
{
    fprintf(stderr, "This example supports only one domInput per domPolygons\n");
    return;
}

// Loop over all the polygons in the domPolygons element
int polygonCount = polygons->getCount();
for (int i=0;i<polygonCount;i++)
{
    myPolygon myPoly;
    // Get pointer to this polygon (domP).
    domPolygons::domP *poly = polygons->getP_array()[i];
    // Get the number of indices from the domP and save it in my structure.
    myPoly._iIndexCount = poly->getValue().getCount();
    // You can modify the data as you copy it from
    // the COLLADA object to your object.
    // Here we repeat the first index in list as the last index,
    // to form a closed loop that can be drawn as a line strip.
    myPoly._iIndexCount++;
    myPoly._pIndexes = new unsigned short[myPoly._iIndexCount];
    // Copy all the indices from the domP into my structure.
    for (int j=0;j<myPoly._iIndexCount-1;j++)
    myPoly._pIndexes[j] = poly->getValue()[j];
    // Repeat the first index at the end of the list to create a closed loop.
    myPoly._pIndexes[j] = myPoly._pIndexes[0];
    // Push this polygon into the list of polygons in my structure.
    local->_vPolygons.push_back(myPoly);
}

// Copy the vertices we are going to use into myGeometry. To keep things simple,
// we will assume there is only one domSource and domFloatArray in the domMesh,
// that it is the array of vertices, and that it is in X, Y, Z format. A real
// app would find the vertices by starting with domPolygons and following
// the links through the domInput, domVertices, domSource, domFloat_array,
// and domTechnique.
if(meshElement->getSource_array().getCount() != 1)
{
    fprintf(stderr, "This example supports only one source array per domMesh\n");
    return;
}
domSource *source = meshElement->getSource_array()[0];

if(source->getFloat_array().getCount() != 1)
{
    fprintf(stderr, "This example supports only one float array per source\n");
}
domFloat_array *floatArray = source->getFloat_array()[0];

// Assume there are 3 values per vertex with a stride of 3.
local->_iVertexCount = floatArray->getCount()/3;
local->_pVertices = new float[local->_iVertexCount*3];

// Copy the vertices into my structure one-by-one
// (converts from COLLADA's doubles to floats).
for ( unsigned int i = 0; i < local->_iVertexCount*3; i++ ) 
{
    local->_pVertices[i] = floatArray->getValue()[i];
}

Access COLLADA Objects from the Application

Now put together the application code that registers the integration libraries, loads the COLLADA elements into the in-memory DOM objects, and accesses the converted data.

Invoke Integration Library Registration

You defined the intRegisterElements function as described in “Register Integration Libraries with the DOM.” Now you must pass a handle to this function into the DAE::setIntegrationLibrary method.

With this step, the elements that you want to convert are registered with the DOM, and are converted from COLLADA DOM objects to application-specific structures when a COLLADA document is loaded, or the reverse when an object is saved.

For example, your main application code could pass a handle to the integration library registration function as follows:

// Instantiate the reference implementation
collada_dom = new DAE;
//register the integration objects
collada_dom->setIntegrationLibrary(&intRegisterElements);

Parse a COLLADA File

Now parse a COLLADA document into the run-time COLLADA Object Model. The load step creates the integration objects and invokes the createFrom and fromCOLLADA methods to convert the data from the COLLADA DOM objects into the application-defined data structures.

//load the COLLADA file
int res = collada_dom->load(filename);

Access a COLLADA Object

Now we'll find a domGeometry object to get at our application-specific data. We'll use the database for this. Here we request that the database return the first geometry element, placing it into domGeom.

Note: See Querying Database Elements for additional details on how to use the database to retrieve COLLADA DOM objects.
//query the runtime to retrieve an element
domGeometry* domGeom = 0;
int res = collada_dom->getDatabase()->getElement(
  (daeElement**)&domGeom, 0, NULL, COLLADA_TYPE_GEOMETRY);

Acquire the Converted Application Object

In the following code, the myGeometry class represents the application data structure containing geometry data. The domGeometry object retrieved above into domGeom contains a reference to its associated integration object (thanks to its earlier registration). The intGeometry class converts the domGeometry data into a myGeometry instance, which can then be retrieved with the getObject method.

// Get the integration object from the element
intGeometry *intGeom = (intGeometry*)domGeom->getIntObject();
// Extract the user data from the integration object
myGeometry* myGeom = (myGeometry*)intGeom->getObject();

Exporting Using Integration

The preceding sections showed how to set up your integration objects, import a COLLADA instance document, and ensure that the data is converted into data structures specific to your application.

If you modify data values and want to write it to a COLLADA instance document, the process is similar to reversing the import-and-convert process, although there are some differences.

Creating a New COLLADA DOM Object Using createTo

The functionality for creating new COLLADA DOM documents with the integration templates via the createTo method has always been broken, and wouldn't have been very useful even if it did work. Creating new COLLADA documents via the integration templates is now officially deprecated and won't be supported in the future. See this thread in the COLLADA forums for more details.

Exporting Modified Data Values

If you have modified the values of your application object and want to save to a revised COLLADA instance document, you'll need to update the associated COLLADA DOM objects before saving. This example shows how you might do that.

When you call save, the DOM calls toCOLLADA for every COLLADA element with an integration class. In the toCOLLADA method we'll copy the data from our application objects to the COLLADA objects. The code is essentially the reverse of the fromCOLLADA code in the importing example. We make several assumptions about the nature of the changes to make the code simpler. These assumptions are explained in the comments.

void intGeometry::toCOLLADA()
{
    // This code takes data from an application-defined object and
    // puts it back into the appropriate collada objects.
    // Get a pointer to the COLLADA domGeometry element
    // (this is the element that called us)
    domGeometry* geometryElement = (domGeometry*)_element;

    // Get a pointer to the domGeometry's domMesh element
    domMesh *meshElement = geometryElement->getMesh();

    // Get a pointer to my object that's assocated with this collada object
    myGeometry *local = (myGeometry *)_object;

    // Get a pointer to the domPolygons in this domMesh.
    // To simplify this example, we will handle only a domMesh 
    // that has a single domPolygons
    if(meshElement->getPolygons_array().getCount() != 1)
    {
        fprintf(stderr, "this example supports only one domPolygons per domMesh\n");
        return;
    }
    domPolygons *polygons = meshElement->getPolygons_array()[0];

    // To simplify this example, we assume that the domPolygons has
    // only one domInput.
    if(polygons->getInput_array().getCount() != 1)
    {
        fprintf(stderr, "this example supports only one domInput per domPolygons\n");
        return;
    }

    // Loop over all the polygons in the domPolygons element and
    // put the data from myGeometry back into it. For the purposes 
    // of the example, assume the number of polygons and indices 
    // hasn't changed so we can just update the values in place.
    int polygonCount = local->_vPolygons.size();
    polygons->setCount(polygonCount);
    for (int i=0;i<polygonCount;i++)
    {
        // Get pointer to this polygon (domP)
        domPolygons::domP *poly = polygons->getP_array()[i];
        // Copy all the indices from my structure back to the domP
        for (int j=0;j< local->_vPolygons[i]._iIndexCount-1;j++)
            poly->getValue()[j] = local->_vPolygons[i]._pIndexes[j] ;
    }

    // Now copy the vertices from myGeometry back to the source.
    // Assume that the number of vertices hasn't changed so we can 
    // just update them in place.
    if(meshElement->getSource_array().getCount() != 1)
    {
        fprintf(stderr, "this example supports only one source array per domMesh\n");
        return;
    }
    domSource *source = meshElement->getSource_array()[0];

    if(source->getFloat_array_array().getCount() != 1)
    {
        fprintf(stderr, "this example supports only one float array per source\n");
    }
    domFloat_array *floatArray = source->getFloat_array()[0];

    // Copy the vertices from myGeometry back into the COLLADA float array
    floatArray->setCount(local->_iVertexCount*3);
    for ( unsigned int i = 0; i < local->_iVertexCount*3; i++ ) 
    {
        floatArray->getValue()[i] = local->_pVertices[i];
    }
}

Using integration versus using the DOM directly

Integration classes provide a SAX-like approach to using the DOM. The biggest problem with this is that the interdependent links between COLLADA elements have become very complex. Parsing a DOM structure lends itself better to handling these problems. Creating an integration class to process <material>s, for example, may take the same amount of work as accessing the DOM structures directly. Here's a comparison.

Import <material> with an integration class:

  1. Create class that inherits from daeIntegrationObject.
  2. Implement the fromCOLLADA code to do the conversion.
  3. Implement the fromCOLLADAPostProcess code to do the processing needed to resolve links to other elements, for example, <effect>.
  4. Register your integration library with the DOM.

Import <material> without an integration class:

  1. Get DOM by calling getDOM or getDOMRoot from the daeDocument.
  2. For each <material> in each <library_materials>, call your conversion function (which would be the same as your fromCOLLADA). Your conversion function can even do the postprocess linking since all the data required by the other elements would be available.

Also you can't create COLLADA documents from scratch with an integration library.

DOM structures give you finer-grained control over what to convert; for example, you can spend time converting only the <geometry> elements used by a specific <visual_scene> and not the possibly dozens (or hundreds or thousands) of other <geometry> elements, while the integration classes convert all of them. This is especially useful if you have documents that cross-reference other documents. For example, imagine loading a document that contains a <visual_scene> where all the <instance_geometry> elements reference a <geometry> from a separate document. This document has only one <library_geometries> and contains all the <geometry> elements used for all your scenes, not just the one being loaded. Because the COLLADA DOM, by default, loads external documents to resolve the URIs, the integration classes will convert every <geometry> found in the second document. There could be lots of <geometry> elements and that could take a long time to process. This is especially wasteful since many <geometry> elements won't ever get used in the scene you're loading.


COLLADA DOM - Version 2.4 Historical Reference
List of main articles under the DOM portal.
User Guide chapters:  • Intro  • Architecture  • Setting up  • Working with documents  • Creating docs  • Importing docs  • Representing elements  • Working with elements  • Resolving URIs  • Resolving SIDs  • Using custom COLLADA data  • Integration templates  • Error handling

Systems:  • URI resolver  • Meta  • Load/save flow  • Runtime database  • Memory • StringRef  • Code generator
Additional information:  • What's new  • Backward compatibility  • Future work
Terminology categories:  • COLLADA  • DOM  • XML