2
0

colladaImport.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "core/volume.h"
  24. #include "ts/collada/colladaUtils.h"
  25. #include "ts/collada/colladaAppNode.h"
  26. #include "ts/collada/colladaShapeLoader.h"
  27. #include "gui/controls/guiTreeViewCtrl.h"
  28. // Helper struct for counting nodes, meshes and polygons down through the scene
  29. // hierarchy
  30. struct SceneStats
  31. {
  32. S32 numNodes;
  33. S32 numMeshes;
  34. S32 numPolygons;
  35. S32 numMaterials;
  36. S32 numLights;
  37. S32 numClips;
  38. SceneStats() : numNodes(0), numMeshes(0), numPolygons(0), numMaterials(0), numLights(0), numClips(0) { }
  39. };
  40. // Recurse through the <visual_scene> adding nodes and geometry to the GuiTreeView control
  41. static void processNode(GuiTreeViewCtrl* tree, domNode* node, S32 parentID, SceneStats& stats)
  42. {
  43. stats.numNodes++;
  44. S32 nodeID = tree->insertItem(parentID, _GetNameOrId(node), "node", "", 0, 0);
  45. // Update mesh and poly counts
  46. for (S32 i = 0; i < node->getContents().getCount(); i++)
  47. {
  48. domGeometry* geom = 0;
  49. const char* elemName = "";
  50. daeElement* child = node->getContents()[i];
  51. switch (child->getElementType())
  52. {
  53. case COLLADA_TYPE::INSTANCE_GEOMETRY:
  54. {
  55. domInstance_geometry* instgeom = daeSafeCast<domInstance_geometry>(child);
  56. if (instgeom)
  57. {
  58. geom = daeSafeCast<domGeometry>(instgeom->getUrl().getElement());
  59. elemName = _GetNameOrId(geom);
  60. }
  61. break;
  62. }
  63. case COLLADA_TYPE::INSTANCE_CONTROLLER:
  64. {
  65. domInstance_controller* instctrl = daeSafeCast<domInstance_controller>(child);
  66. if (instctrl)
  67. {
  68. domController* ctrl = daeSafeCast<domController>(instctrl->getUrl().getElement());
  69. elemName = _GetNameOrId(ctrl);
  70. if (ctrl && ctrl->getSkin())
  71. geom = daeSafeCast<domGeometry>(ctrl->getSkin()->getSource().getElement());
  72. else if (ctrl && ctrl->getMorph())
  73. geom = daeSafeCast<domGeometry>(ctrl->getMorph()->getSource().getElement());
  74. }
  75. break;
  76. }
  77. case COLLADA_TYPE::INSTANCE_LIGHT:
  78. stats.numLights++;
  79. tree->insertItem(nodeID, _GetNameOrId(node), "light", "", 0, 0);
  80. break;
  81. }
  82. if (geom && geom->getMesh())
  83. {
  84. const char* name = _GetNameOrId(node);
  85. if ( dStrEqual( name, "null" ) || dStrEndsWith( name, "PIVOT" ) )
  86. name = _GetNameOrId( daeSafeCast<domNode>(node->getParent()) );
  87. stats.numMeshes++;
  88. tree->insertItem(nodeID, name, "mesh", "", 0, 0);
  89. for (S32 j = 0; j < geom->getMesh()->getTriangles_array().getCount(); j++)
  90. stats.numPolygons += geom->getMesh()->getTriangles_array()[j]->getCount();
  91. for (S32 j = 0; j < geom->getMesh()->getTristrips_array().getCount(); j++)
  92. stats.numPolygons += geom->getMesh()->getTristrips_array()[j]->getCount();
  93. for (S32 j = 0; j < geom->getMesh()->getTrifans_array().getCount(); j++)
  94. stats.numPolygons += geom->getMesh()->getTrifans_array()[j]->getCount();
  95. for (S32 j = 0; j < geom->getMesh()->getPolygons_array().getCount(); j++)
  96. stats.numPolygons += geom->getMesh()->getPolygons_array()[j]->getCount();
  97. for (S32 j = 0; j < geom->getMesh()->getPolylist_array().getCount(); j++)
  98. stats.numPolygons += geom->getMesh()->getPolylist_array()[j]->getCount();
  99. }
  100. }
  101. // Recurse into child nodes
  102. for (S32 i = 0; i < node->getNode_array().getCount(); i++)
  103. processNode(tree, node->getNode_array()[i], nodeID, stats);
  104. for (S32 i = 0; i < node->getInstance_node_array().getCount(); i++)
  105. {
  106. domInstance_node* instnode = node->getInstance_node_array()[i];
  107. domNode* node = daeSafeCast<domNode>(instnode->getUrl().getElement());
  108. if (node)
  109. processNode(tree, node, nodeID, stats);
  110. }
  111. }
  112. ConsoleFunction( enumColladaForImport, bool, 3, 3,
  113. "(string shapePath, GuiTreeViewCtrl ctrl) Collect scene information from "
  114. "a COLLADA file and store it in a GuiTreeView control. This function is "
  115. "used by the COLLADA import gui to show a preview of the scene contents "
  116. "prior to import, and is probably not much use for anything else.\n"
  117. "@param shapePath COLLADA filename\n"
  118. "@param ctrl GuiTreeView control to add elements to\n"
  119. "@return true if successful, false otherwise\n"
  120. "@ingroup Editors\n"
  121. "@internal")
  122. {
  123. GuiTreeViewCtrl* tree;
  124. if (!Sim::findObject(argv[2], tree))
  125. {
  126. Con::errorf("enumColladaScene::Could not find GuiTreeViewCtrl '%s'", argv[2]);
  127. return false;
  128. }
  129. // Check if a cached DTS is available => no need to import the collada file
  130. // if we can load the DTS instead
  131. Torque::Path path(argv[1]);
  132. if (ColladaShapeLoader::canLoadCachedDTS(path))
  133. return false;
  134. // Check if this is a Sketchup file (.kmz) and if so, mount the zip filesystem
  135. // and get the path to the DAE file.
  136. String mountPoint;
  137. Torque::Path daePath;
  138. bool isSketchup = ColladaShapeLoader::checkAndMountSketchup(path, mountPoint, daePath);
  139. // Load the Collada file into memory
  140. domCOLLADA* root = ColladaShapeLoader::getDomCOLLADA(daePath);
  141. if (!root)
  142. {
  143. TSShapeLoader::updateProgress(TSShapeLoader::Load_Complete, "Load complete");
  144. return false;
  145. }
  146. if (isSketchup)
  147. {
  148. // Unmount the zip if we mounted it
  149. Torque::FS::Unmount(mountPoint);
  150. }
  151. // Initialize tree
  152. tree->removeItem(0);
  153. S32 nodesID = tree->insertItem(0, "Shape", "", "", 0, 0);
  154. S32 matsID = tree->insertItem(0, "Materials", "", "", 0, 0);
  155. S32 animsID = tree->insertItem(0, "Animations", "", "", 0, 0);
  156. SceneStats stats;
  157. // Query DOM for shape summary details
  158. for (S32 i = 0; i < root->getLibrary_visual_scenes_array().getCount(); i++)
  159. {
  160. const domLibrary_visual_scenes* libScenes = root->getLibrary_visual_scenes_array()[i];
  161. for (S32 j = 0; j < libScenes->getVisual_scene_array().getCount(); j++)
  162. {
  163. const domVisual_scene* visualScene = libScenes->getVisual_scene_array()[j];
  164. for (S32 k = 0; k < visualScene->getNode_array().getCount(); k++)
  165. processNode(tree, visualScene->getNode_array()[k], nodesID, stats);
  166. }
  167. }
  168. // Get material count
  169. for (S32 i = 0; i < root->getLibrary_materials_array().getCount(); i++)
  170. {
  171. const domLibrary_materials* libraryMats = root->getLibrary_materials_array()[i];
  172. stats.numMaterials += libraryMats->getMaterial_array().getCount();
  173. for (S32 j = 0; j < libraryMats->getMaterial_array().getCount(); j++)
  174. {
  175. domMaterial* mat = libraryMats->getMaterial_array()[j];
  176. tree->insertItem(matsID, _GetNameOrId(mat), _GetNameOrId(mat), "", 0, 0);
  177. }
  178. }
  179. // Get animation count
  180. for (S32 i = 0; i < root->getLibrary_animation_clips_array().getCount(); i++)
  181. {
  182. const domLibrary_animation_clips* libraryClips = root->getLibrary_animation_clips_array()[i];
  183. stats.numClips += libraryClips->getAnimation_clip_array().getCount();
  184. for (S32 j = 0; j < libraryClips->getAnimation_clip_array().getCount(); j++)
  185. {
  186. domAnimation_clip* clip = libraryClips->getAnimation_clip_array()[j];
  187. tree->insertItem(animsID, _GetNameOrId(clip), "animation", "", 0, 0);
  188. }
  189. }
  190. if (stats.numClips == 0)
  191. {
  192. // No clips => check if there are any animations (these will be added to a default clip)
  193. for (S32 i = 0; i < root->getLibrary_animations_array().getCount(); i++)
  194. {
  195. const domLibrary_animations* libraryAnims = root->getLibrary_animations_array()[i];
  196. if (libraryAnims->getAnimation_array().getCount())
  197. {
  198. stats.numClips = 1;
  199. tree->insertItem(animsID, "ambient", "animation", "", 0, 0);
  200. break;
  201. }
  202. }
  203. }
  204. // Extract the global scale and up_axis from the top level <asset> element,
  205. F32 unit = 1.0f;
  206. domUpAxisType upAxis = UPAXISTYPE_Z_UP;
  207. if (root->getAsset()) {
  208. if (root->getAsset()->getUnit())
  209. unit = root->getAsset()->getUnit()->getMeter();
  210. if (root->getAsset()->getUp_axis())
  211. upAxis = root->getAsset()->getUp_axis()->getValue();
  212. }
  213. TSShapeLoader::updateProgress(TSShapeLoader::Load_Complete, "Load complete");
  214. // Store shape information in the tree control
  215. tree->setDataField(StringTable->insert("_nodeCount"), 0, avar("%d", stats.numNodes));
  216. tree->setDataField(StringTable->insert("_meshCount"), 0, avar("%d", stats.numMeshes));
  217. tree->setDataField(StringTable->insert("_polygonCount"), 0, avar("%d", stats.numPolygons));
  218. tree->setDataField(StringTable->insert("_materialCount"), 0, avar("%d", stats.numMaterials));
  219. tree->setDataField(StringTable->insert("_lightCount"), 0, avar("%d", stats.numLights));
  220. tree->setDataField(StringTable->insert("_animCount"), 0, avar("%d", stats.numClips));
  221. tree->setDataField(StringTable->insert("_unit"), 0, avar("%g", unit));
  222. if (upAxis == UPAXISTYPE_X_UP)
  223. tree->setDataField(StringTable->insert("_upAxis"), 0, "X_AXIS");
  224. else if (upAxis == UPAXISTYPE_Y_UP)
  225. tree->setDataField(StringTable->insert("_upAxis"), 0, "Y_AXIS");
  226. else
  227. tree->setDataField(StringTable->insert("_upAxis"), 0, "Z_AXIS");
  228. return true;
  229. }