colladaImport.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 "console/engineAPI.h"
  24. #include "core/volume.h"
  25. #include "ts/collada/colladaUtils.h"
  26. #include "ts/collada/colladaAppNode.h"
  27. #include "ts/collada/colladaShapeLoader.h"
  28. #include "gui/controls/guiTreeViewCtrl.h"
  29. // Helper struct for counting nodes, meshes and polygons down through the scene
  30. // hierarchy
  31. struct SceneStats
  32. {
  33. S32 numNodes;
  34. S32 numMeshes;
  35. S32 numPolygons;
  36. S32 numMaterials;
  37. S32 numLights;
  38. S32 numClips;
  39. SceneStats() : numNodes(0), numMeshes(0), numPolygons(0), numMaterials(0), numLights(0), numClips(0) { }
  40. };
  41. // Recurse through the <visual_scene> adding nodes and geometry to the GuiTreeView control
  42. static void processNode(GuiTreeViewCtrl* tree, domNode* node, S32 parentID, SceneStats& stats)
  43. {
  44. stats.numNodes++;
  45. S32 nodeID = tree->insertItem(parentID, _GetNameOrId(node), "node", "", 0, 0);
  46. // Update mesh and poly counts
  47. for (S32 i = 0; i < node->getContents().getCount(); i++)
  48. {
  49. domGeometry* geom = 0;
  50. const char* elemName = "";
  51. daeElement* child = node->getContents()[i];
  52. switch (child->getElementType())
  53. {
  54. case COLLADA_TYPE::INSTANCE_GEOMETRY:
  55. {
  56. domInstance_geometry* instgeom = daeSafeCast<domInstance_geometry>(child);
  57. if (instgeom)
  58. {
  59. geom = daeSafeCast<domGeometry>(instgeom->getUrl().getElement());
  60. elemName = _GetNameOrId(geom);
  61. }
  62. break;
  63. }
  64. case COLLADA_TYPE::INSTANCE_CONTROLLER:
  65. {
  66. domInstance_controller* instctrl = daeSafeCast<domInstance_controller>(child);
  67. if (instctrl)
  68. {
  69. domController* ctrl = daeSafeCast<domController>(instctrl->getUrl().getElement());
  70. elemName = _GetNameOrId(ctrl);
  71. if (ctrl && ctrl->getSkin())
  72. geom = daeSafeCast<domGeometry>(ctrl->getSkin()->getSource().getElement());
  73. else if (ctrl && ctrl->getMorph())
  74. geom = daeSafeCast<domGeometry>(ctrl->getMorph()->getSource().getElement());
  75. }
  76. break;
  77. }
  78. case COLLADA_TYPE::INSTANCE_LIGHT:
  79. stats.numLights++;
  80. tree->insertItem(nodeID, _GetNameOrId(node), "light", "", 0, 0);
  81. break;
  82. }
  83. if (geom && geom->getMesh())
  84. {
  85. const char* name = _GetNameOrId(node);
  86. if ( dStrEqual( name, "null" ) || dStrEndsWith( name, "PIVOT" ) )
  87. name = _GetNameOrId( daeSafeCast<domNode>(node->getParent()) );
  88. stats.numMeshes++;
  89. tree->insertItem(nodeID, name, "mesh", "", 0, 0);
  90. for (S32 j = 0; j < geom->getMesh()->getTriangles_array().getCount(); j++)
  91. stats.numPolygons += geom->getMesh()->getTriangles_array()[j]->getCount();
  92. for (S32 j = 0; j < geom->getMesh()->getTristrips_array().getCount(); j++)
  93. stats.numPolygons += geom->getMesh()->getTristrips_array()[j]->getCount();
  94. for (S32 j = 0; j < geom->getMesh()->getTrifans_array().getCount(); j++)
  95. stats.numPolygons += geom->getMesh()->getTrifans_array()[j]->getCount();
  96. for (S32 j = 0; j < geom->getMesh()->getPolygons_array().getCount(); j++)
  97. stats.numPolygons += geom->getMesh()->getPolygons_array()[j]->getCount();
  98. for (S32 j = 0; j < geom->getMesh()->getPolylist_array().getCount(); j++)
  99. stats.numPolygons += geom->getMesh()->getPolylist_array()[j]->getCount();
  100. }
  101. }
  102. // Recurse into child nodes
  103. for (S32 i = 0; i < node->getNode_array().getCount(); i++)
  104. processNode(tree, node->getNode_array()[i], nodeID, stats);
  105. for (S32 i = 0; i < node->getInstance_node_array().getCount(); i++)
  106. {
  107. domInstance_node* instnode = node->getInstance_node_array()[i];
  108. domNode* dNode = daeSafeCast<domNode>(instnode->getUrl().getElement());
  109. if (dNode)
  110. processNode(tree, dNode, nodeID, stats);
  111. }
  112. }
  113. DefineEngineFunction( enumColladaForImport, bool, (const char * shapePath, const char * ctrl, bool loadCachedDts), ("", "", true),
  114. "(string shapePath, GuiTreeViewCtrl ctrl) Collect scene information from "
  115. "a COLLADA file and store it in a GuiTreeView control. This function is "
  116. "used by the COLLADA import gui to show a preview of the scene contents "
  117. "prior to import, and is probably not much use for anything else.\n"
  118. "@param shapePath COLLADA filename\n"
  119. "@param ctrl GuiTreeView control to add elements to\n"
  120. "@param loadCachedDts dictates if it should try and load the cached dts file if it exists"
  121. "@return true if successful, false otherwise\n"
  122. "@ingroup Editors\n"
  123. "@internal")
  124. {
  125. GuiTreeViewCtrl* tree;
  126. if (!Sim::findObject(ctrl, tree))
  127. {
  128. Con::errorf("enumColladaScene::Could not find GuiTreeViewCtrl '%s'", ctrl);
  129. return false;
  130. }
  131. // Check if a cached DTS is available => no need to import the collada file
  132. // if we can load the DTS instead
  133. Torque::Path path(shapePath);
  134. if (loadCachedDts && ColladaShapeLoader::canLoadCachedDTS(path))
  135. return false;
  136. // Check if this is a Sketchup file (.kmz) and if so, mount the zip filesystem
  137. // and get the path to the DAE file.
  138. String mountPoint;
  139. Torque::Path daePath;
  140. bool isSketchup = ColladaShapeLoader::checkAndMountSketchup(path, mountPoint, daePath);
  141. // Load the Collada file into memory
  142. domCOLLADA* root = ColladaShapeLoader::getDomCOLLADA(daePath);
  143. if (!root)
  144. {
  145. TSShapeLoader::updateProgress(TSShapeLoader::Load_Complete, "Load complete");
  146. return false;
  147. }
  148. if (isSketchup)
  149. {
  150. // Unmount the zip if we mounted it
  151. Torque::FS::Unmount(mountPoint);
  152. }
  153. // Initialize tree
  154. tree->removeItem(0);
  155. S32 nodesID = tree->insertItem(0, "Shape", "", "", 0, 0);
  156. S32 matsID = tree->insertItem(0, "Materials", "", "", 0, 0);
  157. S32 animsID = tree->insertItem(0, "Animations", "", "", 0, 0);
  158. SceneStats stats;
  159. // Query DOM for shape summary details
  160. for (S32 i = 0; i < root->getLibrary_visual_scenes_array().getCount(); i++)
  161. {
  162. const domLibrary_visual_scenes* libScenes = root->getLibrary_visual_scenes_array()[i];
  163. for (S32 j = 0; j < libScenes->getVisual_scene_array().getCount(); j++)
  164. {
  165. const domVisual_scene* visualScene = libScenes->getVisual_scene_array()[j];
  166. for (S32 k = 0; k < visualScene->getNode_array().getCount(); k++)
  167. processNode(tree, visualScene->getNode_array()[k], nodesID, stats);
  168. }
  169. }
  170. // Get material count
  171. for (S32 i = 0; i < root->getLibrary_materials_array().getCount(); i++)
  172. {
  173. const domLibrary_materials* libraryMats = root->getLibrary_materials_array()[i];
  174. stats.numMaterials += libraryMats->getMaterial_array().getCount();
  175. for (S32 j = 0; j < libraryMats->getMaterial_array().getCount(); j++)
  176. {
  177. domMaterial* mat = libraryMats->getMaterial_array()[j];
  178. tree->insertItem(matsID, _GetNameOrId(mat), "", "", 0, 0);
  179. }
  180. }
  181. // Get images count
  182. for (S32 i = 0; i < root->getLibrary_images_array().getCount(); i++)
  183. {
  184. const domLibrary_images* libraryImages = root->getLibrary_images_array()[i];
  185. for (S32 j = 0; j < libraryImages->getImage_array().getCount(); j++)
  186. {
  187. domImage* img = libraryImages->getImage_array()[j];
  188. S32 materialID = tree->findItemByName(_GetNameOrId(img));
  189. if (materialID == 0)
  190. continue;
  191. tree->setItemValue(materialID, img->getInit_from()->getValue().str().c_str());
  192. }
  193. }
  194. // Get animation count
  195. for (S32 i = 0; i < root->getLibrary_animation_clips_array().getCount(); i++)
  196. {
  197. const domLibrary_animation_clips* libraryClips = root->getLibrary_animation_clips_array()[i];
  198. stats.numClips += libraryClips->getAnimation_clip_array().getCount();
  199. for (S32 j = 0; j < libraryClips->getAnimation_clip_array().getCount(); j++)
  200. {
  201. domAnimation_clip* clip = libraryClips->getAnimation_clip_array()[j];
  202. tree->insertItem(animsID, _GetNameOrId(clip), "animation", "", 0, 0);
  203. }
  204. }
  205. if (stats.numClips == 0)
  206. {
  207. // No clips => check if there are any animations (these will be added to a default clip)
  208. for (S32 i = 0; i < root->getLibrary_animations_array().getCount(); i++)
  209. {
  210. const domLibrary_animations* libraryAnims = root->getLibrary_animations_array()[i];
  211. if (libraryAnims->getAnimation_array().getCount())
  212. {
  213. stats.numClips = 1;
  214. tree->insertItem(animsID, "ambient", "animation", "", 0, 0);
  215. break;
  216. }
  217. }
  218. }
  219. // Extract the global scale and up_axis from the top level <asset> element,
  220. F32 unit = 1.0f;
  221. domUpAxisType upAxis = UPAXISTYPE_Z_UP;
  222. if (root->getAsset()) {
  223. if (root->getAsset()->getUnit())
  224. unit = root->getAsset()->getUnit()->getMeter();
  225. if (root->getAsset()->getUp_axis())
  226. upAxis = root->getAsset()->getUp_axis()->getValue();
  227. }
  228. TSShapeLoader::updateProgress(TSShapeLoader::Load_Complete, "Load complete");
  229. // Store shape information in the tree control
  230. tree->setDataField(StringTable->insert("_nodeCount"), 0, avar("%d", stats.numNodes));
  231. tree->setDataField(StringTable->insert("_meshCount"), 0, avar("%d", stats.numMeshes));
  232. tree->setDataField(StringTable->insert("_polygonCount"), 0, avar("%d", stats.numPolygons));
  233. tree->setDataField(StringTable->insert("_materialCount"), 0, avar("%d", stats.numMaterials));
  234. tree->setDataField(StringTable->insert("_lightCount"), 0, avar("%d", stats.numLights));
  235. tree->setDataField(StringTable->insert("_animCount"), 0, avar("%d", stats.numClips));
  236. tree->setDataField(StringTable->insert("_unit"), 0, avar("%g", unit));
  237. if (upAxis == UPAXISTYPE_X_UP)
  238. tree->setDataField(StringTable->insert("_upAxis"), 0, "X_AXIS");
  239. else if (upAxis == UPAXISTYPE_Y_UP)
  240. tree->setDataField(StringTable->insert("_upAxis"), 0, "Y_AXIS");
  241. else
  242. tree->setDataField(StringTable->insert("_upAxis"), 0, "Z_AXIS");
  243. char shapesStr[16];
  244. dSprintf(shapesStr, 16, "%i", stats.numMeshes);
  245. char materialsStr[16];
  246. dSprintf(materialsStr, 16, "%i", stats.numMaterials);
  247. char animationsStr[16];
  248. dSprintf(animationsStr, 16, "%i", stats.numClips);
  249. tree->setItemValue(nodesID, StringTable->insert(shapesStr));
  250. tree->setItemValue(matsID, StringTable->insert(materialsStr));
  251. tree->setItemValue(animsID, StringTable->insert(animationsStr));
  252. return true;
  253. }