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