DotSceneLoader.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This program is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU General Public License as published by the Free  *
00007 * Software Foundation; either version 2 of the License, or (at your option)   *
00008 * any later version.                                                          *
00009 *                                                                             *
00010 * This program is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    *
00013 * more details.                                                               *
00014 *                                                                             *
00015 * You should have received a copy of the GNU General Public License along     *
00016 * with this program; if not, write to the Free Software Foundation, Inc., 51  *
00017 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.                   *
00018 *******************************************************************************
00019 *                            SOFA :: Applications                             *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 // http://www.ogre3d.org/wiki/index.php/New_DotScene_Loader
00028 
00029 #include "viewer/qtogre/DotSceneLoader.h"
00030 #include "tinyxml.h"
00031 #include <Ogre.h>
00032 
00033 using namespace std;
00034 using namespace Ogre;
00035 
00036 DotSceneLoader::DotSceneLoader()
00037 : mSceneMgr(0)
00038 {
00039     environment.nearClipDistance = 0.2;
00040     environment.farClipDistance = 1000;
00041     environment.backgroundColour = ColourValue(0,0,0,0);
00042     environment.ambientColour = ColourValue(0,0,0,1);
00043 }
00044 
00045 void DotSceneLoader::parseDotScene(const String &SceneName, const String &groupName, SceneManager *yourSceneMgr, SceneNode *pAttachNode, const String &sPrependNode)
00046 {
00047     // set up shared object values
00048     m_sGroupName = groupName;
00049     mSceneMgr = yourSceneMgr;
00050     m_sPrependNode = sPrependNode;
00051     staticObjects.clear();
00052     dynamicObjects.clear();
00053 
00054     TiXmlDocument   *XMLDoc = 0;
00055     TiXmlElement   *XMLRoot;
00056 
00057     try
00058     {
00059         DataStreamPtr pStream = ResourceGroupManager::getSingleton().
00060             openResource( SceneName, groupName );
00061 
00062         String data = pStream->getAsString();
00063         // Open the .scene File
00064         XMLDoc = new TiXmlDocument();
00065         XMLDoc->Parse( data.c_str() );
00066         pStream->close();
00067         pStream.setNull();
00068 
00069         if( XMLDoc->Error() )
00070         {
00071             //We'll just log, and continue on gracefully
00072             LogManager::getSingleton().logMessage("[DotSceneLoader] The TiXmlDocument reported an error");
00073             delete XMLDoc;
00074             return;
00075         }
00076     }
00077     catch(...)
00078     {
00079         //We'll just log, and continue on gracefully
00080         LogManager::getSingleton().logMessage("[DotSceneLoader] Error creating TiXmlDocument");
00081         delete XMLDoc;
00082         return;
00083     }
00084 
00085     // Validate the File
00086     XMLRoot = XMLDoc->RootElement();
00087     if( String( XMLRoot->Value()) != "scene"  ) {
00088         LogManager::getSingleton().logMessage( "[DotSceneLoader] Error: Invalid .scene File. Missing <scene>" );
00089         delete XMLDoc;      
00090         return;
00091     }
00092 
00093     // figure out where to attach any nodes we create
00094     mAttachNode = pAttachNode;
00095     if(!mAttachNode && mSceneMgr)
00096         mAttachNode = mSceneMgr->getRootSceneNode();
00097 
00098     // Process the scene
00099     processScene(XMLRoot);
00100 
00101     // Close the XML File
00102     delete XMLDoc;
00103 }
00104 
00105 void DotSceneLoader::processScene(TiXmlElement *XMLRoot)
00106 {
00107     // Process the scene parameters
00108     String version = getAttrib(XMLRoot, "formatVersion", "unknown");
00109 
00110     String message = "[DotSceneLoader] Parsing dotScene file with version " + version;
00111     if(XMLRoot->Attribute("ID"))
00112         message += ", id " + String(XMLRoot->Attribute("ID"));
00113     if(XMLRoot->Attribute("sceneManager"))
00114         message += ", scene manager " + String(XMLRoot->Attribute("sceneManager"));
00115     if(XMLRoot->Attribute("minOgreVersion"))
00116         message += ", min. Ogre version " + String(XMLRoot->Attribute("minOgreVersion"));
00117     if(XMLRoot->Attribute("author"))
00118         message += ", author " + String(XMLRoot->Attribute("author"));
00119 
00120     LogManager::getSingleton().logMessage(message);
00121 
00122     if(!mSceneMgr)
00123     {
00124         if (XMLRoot->Attribute("sceneManager"))
00125             mSceneMgr = Root::getSingleton().createSceneManager(getAttrib(XMLRoot,"sceneManager"));
00126         else
00127             mSceneMgr = Root::getSingleton().createSceneManager(ST_GENERIC);
00128         if(!mAttachNode)
00129             mAttachNode = mSceneMgr->getRootSceneNode();
00130     }
00131         
00132         // do this first so we generate edge lists
00133         //mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE);
00134 
00135     TiXmlElement *pElement;
00136 
00137     // Process nodes (?)
00138     pElement = XMLRoot->FirstChildElement("nodes");
00139     if(pElement)
00140         processNodes(pElement);
00141 
00142     // Process externals (?)
00143     pElement = XMLRoot->FirstChildElement("externals");
00144     if(pElement)
00145         processExternals(pElement);
00146 
00147     // Process environment (?)
00148     pElement = XMLRoot->FirstChildElement("environment");
00149     if(pElement)
00150         processEnvironment(pElement);
00151 
00152     // Process terrain (?)
00153     pElement = XMLRoot->FirstChildElement("terrain");
00154     if(pElement)
00155         processTerrain(pElement);
00156 
00157     // Process userDataReference (?)
00158     pElement = XMLRoot->FirstChildElement("userDataReference");
00159     if(pElement)
00160         processUserDataReference(pElement);
00161 
00162     // Process octree (?)
00163     pElement = XMLRoot->FirstChildElement("octree");
00164     if(pElement)
00165         processOctree(pElement);
00166 
00167     // Process light (?)
00168     pElement = XMLRoot->FirstChildElement("light");
00169     if(pElement)
00170         processLight(pElement);
00171 
00172     // Process camera (?)
00173     pElement = XMLRoot->FirstChildElement("camera");
00174     if(pElement)
00175         processCamera(pElement);
00176 }
00177 
00178 void DotSceneLoader::processNodes(TiXmlElement *XMLNode)
00179 {
00180     TiXmlElement *pElement;
00181 
00182     // Process node (*)
00183     pElement = XMLNode->FirstChildElement("node");
00184     while(pElement)
00185     {
00186         processNode(pElement);
00187         pElement = pElement->NextSiblingElement("node");
00188     }
00189 
00190     // Process position (?)
00191     pElement = XMLNode->FirstChildElement("position");
00192     if(pElement)
00193     {
00194         mAttachNode->setPosition(parseVector3(pElement));
00195         mAttachNode->setInitialState();
00196     }
00197 
00198     // Process rotation (?)
00199     pElement = XMLNode->FirstChildElement("rotation");
00200     if(pElement)
00201     {
00202         mAttachNode->setOrientation(parseQuaternion(pElement));
00203         mAttachNode->setInitialState();
00204     }
00205 
00206     // Process scale (?)
00207     pElement = XMLNode->FirstChildElement("scale");
00208     if(pElement)
00209     {
00210         mAttachNode->setScale(parseVector3(pElement));
00211         mAttachNode->setInitialState();
00212     }
00213 }
00214 
00215 void DotSceneLoader::processExternals(TiXmlElement *XMLNode)
00216 {
00218 }
00219 
00220 void DotSceneLoader::processEnvironment(TiXmlElement *XMLNode)
00221 {
00222     TiXmlElement *pElement;
00223 
00224     // Process fog (?)
00225     pElement = XMLNode->FirstChildElement("fog");
00226     if(pElement)
00227         processFog(pElement);
00228 
00229     // Process skyBox (?)
00230     pElement = XMLNode->FirstChildElement("skyBox");
00231     if(pElement)
00232         processSkyBox(pElement);
00233 
00234     // Process skyDome (?)
00235     pElement = XMLNode->FirstChildElement("skyDome");
00236     if(pElement)
00237         processSkyDome(pElement);
00238 
00239     // Process skyPlane (?)
00240     pElement = XMLNode->FirstChildElement("skyPlane");
00241     if(pElement)
00242         processSkyPlane(pElement);
00243 
00244     // Process clipping (?)
00245     pElement = XMLNode->FirstChildElement("clipping");
00246     if(pElement)
00247         processClipping(pElement);
00248 
00249     // Process colourAmbient (?)
00250     pElement = XMLNode->FirstChildElement("colourAmbient");
00251     if(pElement)
00252     {
00253         environment.ambientColour = parseColour(pElement);
00254         mSceneMgr->setAmbientLight(environment.ambientColour);
00255     }
00256 
00257     // Process colourBackground (?)
00259     pElement = XMLNode->FirstChildElement("colourBackground");
00260     if(pElement)    
00261         environment.backgroundColour = parseColour(pElement);       
00262     
00263     // Process userDataReference (?)
00264     pElement = XMLNode->FirstChildElement("userDataReference");
00265     if(pElement)
00266         processUserDataReference(pElement);
00267 }
00268 
00269 void DotSceneLoader::processTerrain(TiXmlElement *XMLNode)
00270 {
00272 }
00273 
00274 void DotSceneLoader::processUserDataReference(TiXmlElement *XMLNode, SceneNode *pParent)
00275 {
00277 }
00278 
00279 void DotSceneLoader::processOctree(TiXmlElement *XMLNode)
00280 {
00282 }
00283 
00284 void DotSceneLoader::processLight(TiXmlElement *XMLNode, SceneNode *pParent)
00285 {
00286     // Process attributes
00287     String name = getAttrib(XMLNode, "name");
00288     String id = getAttrib(XMLNode, "id");
00289 
00290     // Create the light
00291     Light *pLight = mSceneMgr->createLight(name);
00292     if(pParent)
00293         pParent->attachObject(pLight);
00294 
00295     String sValue = getAttrib(XMLNode, "type");
00296     if(sValue == "point")
00297         pLight->setType(Light::LT_POINT);
00298     else if(sValue == "directional")
00299         pLight->setType(Light::LT_DIRECTIONAL);
00300     else if(sValue == "spot")
00301         pLight->setType(Light::LT_SPOTLIGHT);
00302     else if(sValue == "radPoint")
00303         pLight->setType(Light::LT_POINT);
00304 
00305     pLight->setVisible(getAttribBool(XMLNode, "visible", true));
00306     pLight->setCastShadows(getAttribBool(XMLNode, "castShadows", true));
00307 
00308     TiXmlElement *pElement;
00309 
00310     // Process position (?)
00311     pElement = XMLNode->FirstChildElement("position");
00312     if(pElement)
00313         pLight->setPosition(parseVector3(pElement));
00314 
00315     // Process normal (?)
00316     pElement = XMLNode->FirstChildElement("normal");
00317     if(pElement)
00318         pLight->setDirection(parseVector3(pElement));
00319 
00320     // Process colourDiffuse (?)
00321     pElement = XMLNode->FirstChildElement("colourDiffuse");
00322     if(pElement)
00323         pLight->setDiffuseColour(parseColour(pElement));
00324 
00325     // Process colourSpecular (?)
00326     pElement = XMLNode->FirstChildElement("colourSpecular");
00327     if(pElement)
00328         pLight->setSpecularColour(parseColour(pElement));
00329 
00330     // Process lightRange (?)
00331     pElement = XMLNode->FirstChildElement("lightRange");
00332     if(pElement)
00333         processLightRange(pElement, pLight);
00334 
00335     // Process lightAttenuation (?)
00336     pElement = XMLNode->FirstChildElement("lightAttenuation");
00337     if(pElement)
00338         processLightAttenuation(pElement, pLight);
00339 
00340     // Process userDataReference (?)
00341     pElement = XMLNode->FirstChildElement("userDataReference");
00342 //  if(pElement)
00343 //      ;//processUserDataReference(pElement, pLight);
00344 }
00345 
00346 void DotSceneLoader::processCamera(TiXmlElement *XMLNode, SceneNode *pParent)
00347 {
00348     // Process attributes
00349     String name = getAttrib(XMLNode, "name");
00350     String id = getAttrib(XMLNode, "id");
00351 
00352     // Create the camera
00353     Camera *pCamera = mSceneMgr->createCamera(name);
00354     if(pParent)
00355         pParent->attachObject(pCamera);
00356 
00357     if (XMLNode->Attribute("fov"))
00358         pCamera->setFOVy(Ogre::Angle(getAttribReal(XMLNode, "fov")));
00359     if (XMLNode->Attribute("aspectRatio"))
00360         pCamera->setAspectRatio(getAttribReal(XMLNode, "aspectRatio"));
00361 
00362     String sValue = getAttrib(XMLNode, "projectionType");
00363     if(sValue == "perspective")
00364         pCamera->setProjectionType(Ogre::PT_PERSPECTIVE);
00365     else if(sValue == "orthographic")
00366         pCamera->setProjectionType(Ogre::PT_ORTHOGRAPHIC);
00367 
00368     TiXmlElement *pElement;
00369 
00370     // Process clipping (?)
00371     pElement = XMLNode->FirstChildElement("clipping");
00372     if(pElement)
00373         processClipping(pElement, pCamera);
00374 
00375     // Process position (?)
00376     pElement = XMLNode->FirstChildElement("position");
00377     if(pElement)
00378         pCamera->setPosition(parseVector3(pElement));
00379 
00380     // Process rotation (?)
00381     pElement = XMLNode->FirstChildElement("rotation");
00382     if(pElement)
00383         pCamera->setOrientation(parseQuaternion(pElement));
00384 
00385     // Process normal (?)
00386     pElement = XMLNode->FirstChildElement("normal");
00387     if(pElement)
00388         pCamera->setDirection(parseVector3(pElement));
00389 
00390     // Process lookTarget (?)
00391     pElement = XMLNode->FirstChildElement("lookTarget");
00392     if(pElement)
00393         processLookTarget(pElement, pCamera);
00394 
00395     // Process trackTarget (?)
00396     pElement = XMLNode->FirstChildElement("trackTarget");
00397     if(pElement)
00398         processTrackTarget(pElement, pCamera);
00399 
00400     // Process userDataReference (?)
00401     pElement = XMLNode->FirstChildElement("userDataReference");
00402 //  if(pElement)
00403 //      ;//processUserDataReference(pElement, pCamera);
00404 }
00405 
00406 void DotSceneLoader::processNode(TiXmlElement *XMLNode, SceneNode *pParent)
00407 {
00408     // Construct the node's name
00409     String name = m_sPrependNode + getAttrib(XMLNode, "name");
00410 
00411     // Create the scene node
00412     SceneNode *pNode;
00413     if(name.empty())
00414     {
00415         // Let Ogre choose the name
00416         if(pParent)
00417             pNode = pParent->createChildSceneNode();
00418         else
00419             pNode = mAttachNode->createChildSceneNode();
00420     }
00421     else
00422     {
00423         // Provide the name
00424         if(pParent)
00425             pNode = pParent->createChildSceneNode(name);
00426         else
00427             pNode = mAttachNode->createChildSceneNode(name);
00428     }
00429 
00430     // Process other attributes
00431     String id = getAttrib(XMLNode, "id");
00432     bool isTarget = getAttribBool(XMLNode, "isTarget");
00433 
00434     TiXmlElement *pElement;
00435 
00436     // Process position (?)
00437     pElement = XMLNode->FirstChildElement("position");
00438     if(pElement)
00439     {
00440         pNode->setPosition(parseVector3(pElement));
00441         pNode->setInitialState();
00442     }
00443 
00444     // Process rotation (?)
00445     pElement = XMLNode->FirstChildElement("rotation");
00446     if(pElement)
00447     {
00448         pNode->setOrientation(parseQuaternion(pElement));
00449         pNode->setInitialState();
00450     }
00451 
00452     // Process scale (?)
00453     pElement = XMLNode->FirstChildElement("scale");
00454     if(pElement)
00455     {
00456         pNode->setScale(parseVector3(pElement));
00457         pNode->setInitialState();
00458     }
00459 
00460     // Process lookTarget (?)
00461     pElement = XMLNode->FirstChildElement("lookTarget");
00462     if(pElement)
00463         processLookTarget(pElement, pNode);
00464 
00465     // Process trackTarget (?)
00466     pElement = XMLNode->FirstChildElement("trackTarget");
00467     if(pElement)
00468         processTrackTarget(pElement, pNode);
00469 
00470     // Process node (*)
00471     pElement = XMLNode->FirstChildElement("node");
00472     while(pElement)
00473     {
00474         processNode(pElement, pNode);
00475         pElement = pElement->NextSiblingElement("node");
00476     }
00477 
00478     // Process entity (*)
00479     pElement = XMLNode->FirstChildElement("entity");
00480     while(pElement)
00481     {
00482         processEntity(pElement, pNode);
00483         pElement = pElement->NextSiblingElement("entity");
00484     }
00485 
00486     // Process light (*)
00487     pElement = XMLNode->FirstChildElement("light");
00488     while(pElement)
00489     {
00490         processLight(pElement, pNode);
00491         pElement = pElement->NextSiblingElement("light");
00492     }
00493 
00494     // Process camera (*)
00495     pElement = XMLNode->FirstChildElement("camera");
00496     while(pElement)
00497     {
00498         processCamera(pElement, pNode);
00499         pElement = pElement->NextSiblingElement("camera");
00500     }
00501 
00502     // Process particleSystem (*)
00503     pElement = XMLNode->FirstChildElement("particleSystem");
00504     while(pElement)
00505     {
00506         processParticleSystem(pElement, pNode);
00507         pElement = pElement->NextSiblingElement("particleSystem");
00508     }
00509 
00510     // Process billboardSet (*)
00511     pElement = XMLNode->FirstChildElement("billboardSet");
00512     while(pElement)
00513     {
00514         processBillboardSet(pElement, pNode);
00515         pElement = pElement->NextSiblingElement("billboardSet");
00516     }
00517 
00518     // Process plane (*)
00519     pElement = XMLNode->FirstChildElement("plane");
00520     while(pElement)
00521     {
00522         processPlane(pElement, pNode);
00523         pElement = pElement->NextSiblingElement("plane");
00524     }
00525 
00526     // Process userDataReference (?)
00527     pElement = XMLNode->FirstChildElement("userDataReference");
00528     if(pElement)
00529         processUserDataReference(pElement, pNode);
00530 }
00531 
00532 void DotSceneLoader::processLookTarget(TiXmlElement *XMLNode, SceneNode *pParent)
00533 {
00535 
00536     // Process attributes
00537     String nodeName = getAttrib(XMLNode, "nodeName");
00538 
00539     Node::TransformSpace relativeTo = Node::TS_PARENT;
00540     String sValue = getAttrib(XMLNode, "relativeTo");
00541     if(sValue == "local")
00542         relativeTo = Node::TS_LOCAL;
00543     else if(sValue == "parent")
00544         relativeTo = Node::TS_PARENT;
00545     else if(sValue == "world")
00546         relativeTo = Node::TS_WORLD;
00547 
00548     TiXmlElement *pElement;
00549 
00550     // Process position (?)
00551     Vector3 position;
00552     pElement = XMLNode->FirstChildElement("position");
00553     if(pElement)
00554         position = parseVector3(pElement);
00555 
00556     // Process localDirection (?)
00557     Vector3 localDirection = Vector3::NEGATIVE_UNIT_Z;
00558     pElement = XMLNode->FirstChildElement("localDirection");
00559     if(pElement)
00560         localDirection = parseVector3(pElement);
00561 
00562     // Setup the look target
00563     try
00564     {
00565         if(!nodeName.empty())
00566         {
00567             SceneNode *pLookNode = mSceneMgr->getSceneNode(nodeName);
00568             position = pLookNode->_getDerivedPosition();
00569         }
00570 
00571         pParent->lookAt(position, relativeTo, localDirection);
00572     }
00573     catch(Ogre::Exception &/*e*/)
00574     {
00575         LogManager::getSingleton().logMessage("[DotSceneLoader] Error processing a look target!");
00576     }
00577 }
00578 
00579 void DotSceneLoader::processTrackTarget(TiXmlElement *XMLNode, SceneNode *pParent)
00580 {
00581     // Process attributes
00582     String nodeName = getAttrib(XMLNode, "nodeName");
00583 
00584     TiXmlElement *pElement;
00585 
00586     // Process localDirection (?)
00587     Vector3 localDirection = Vector3::NEGATIVE_UNIT_Z;
00588     pElement = XMLNode->FirstChildElement("localDirection");
00589     if(pElement)
00590         localDirection = parseVector3(pElement);
00591 
00592     // Process offset (?)
00593     Vector3 offset = Vector3::ZERO;
00594     pElement = XMLNode->FirstChildElement("offset");
00595     if(pElement)
00596         offset = parseVector3(pElement);
00597 
00598     // Setup the track target
00599     try
00600     {
00601         SceneNode *pTrackNode = mSceneMgr->getSceneNode(nodeName);
00602         pParent->setAutoTracking(true, pTrackNode, localDirection, offset);
00603     }
00604     catch(Ogre::Exception &/*e*/)
00605     {
00606         LogManager::getSingleton().logMessage("[DotSceneLoader] Error processing a track target!");
00607     }
00608 }
00609 
00610 void DotSceneLoader::processLookTarget(TiXmlElement *XMLNode, Camera *pParent)
00611 {
00614 
00615     // Process attributes
00616     String nodeName = getAttrib(XMLNode, "nodeName");
00617 
00618     Node::TransformSpace relativeTo = Node::TS_PARENT;
00619     String sValue = getAttrib(XMLNode, "relativeTo");
00620     if(sValue == "local")
00621         relativeTo = Node::TS_LOCAL;
00622     else if(sValue == "parent")
00623         relativeTo = Node::TS_PARENT;
00624     else if(sValue == "world")
00625         relativeTo = Node::TS_WORLD;
00626 
00627     TiXmlElement *pElement;
00628 
00629     // Process position (?)
00630     Vector3 position;
00631     pElement = XMLNode->FirstChildElement("position");
00632     if(pElement)
00633         position = parseVector3(pElement);
00634 
00635     // Process localDirection (?)
00636     Vector3 localDirection = Vector3::NEGATIVE_UNIT_Z;
00637     pElement = XMLNode->FirstChildElement("localDirection");
00638     if(pElement)
00639         localDirection = parseVector3(pElement);
00640 
00641     // Setup the look target
00642     try
00643     {
00644         if(!nodeName.empty())
00645         {
00646             SceneNode *pLookNode = mSceneMgr->getSceneNode(nodeName);
00647             position = pLookNode->_getDerivedPosition();
00648         }
00649 
00650         pParent->lookAt(position /*, relativeTo, localDirection*/);
00651     }
00652     catch(Ogre::Exception &/*e*/)
00653     {
00654         LogManager::getSingleton().logMessage("[DotSceneLoader] Error processing a look target!");
00655     }
00656 }
00657 
00658 void DotSceneLoader::processTrackTarget(TiXmlElement *XMLNode, Camera *pParent)
00659 {
00661     // Process attributes
00662     String nodeName = getAttrib(XMLNode, "nodeName");
00663 
00664     TiXmlElement *pElement;
00665 
00666     // Process localDirection (?)
00667     Vector3 localDirection = Vector3::NEGATIVE_UNIT_Z;
00668     pElement = XMLNode->FirstChildElement("localDirection");
00669     if(pElement)
00670         localDirection = parseVector3(pElement);
00671 
00672     // Process offset (?)
00673     Vector3 offset = Vector3::ZERO;
00674     pElement = XMLNode->FirstChildElement("offset");
00675     if(pElement)
00676         offset = parseVector3(pElement);
00677 
00678     // Setup the track target
00679     try
00680     {
00681         SceneNode *pTrackNode = mSceneMgr->getSceneNode(nodeName);
00682         pParent->setAutoTracking(true, pTrackNode, /*localDirection,*/ offset);
00683     }
00684     catch(Ogre::Exception &/*e*/)
00685     {
00686         LogManager::getSingleton().logMessage("[DotSceneLoader] Error processing a track target!");
00687     }
00688 }
00689 
00690 void DotSceneLoader::processEntity(TiXmlElement *XMLNode, SceneNode *pParent)
00691 {
00692 
00693   
00694     // Process attributes
00695     String name = getAttrib(XMLNode, "name");
00696 
00697     String id = getAttrib(XMLNode, "id");
00698     String meshFile = getAttrib(XMLNode, "meshFile");
00699     String materialFile = getAttrib(XMLNode, "materialFile");
00700     bool isStatic = getAttribBool(XMLNode, "static", false);;
00701     bool castShadows = getAttribBool(XMLNode, "castShadows", true);
00702 
00703     // TEMP: Maintain a list of static and dynamic objects
00704     if(isStatic)
00705         staticObjects.push_back(name);
00706     else
00707         dynamicObjects.push_back(name);
00708 
00709     TiXmlElement *pElement;
00710 
00711     // Process vertexBuffer (?)
00712     pElement = XMLNode->FirstChildElement("vertexBuffer");
00713 //  if(pElement)
00714 //      ;//processVertexBuffer(pElement);
00715 
00716     // Process indexBuffer (?)
00717     pElement = XMLNode->FirstChildElement("indexBuffer");
00718 //  if(pElement)
00719 //      ;//processIndexBuffer(pElement);
00720 
00721     // Create the entity
00722     Entity *pEntity = 0;
00723     try
00724     {
00725         MeshManager::getSingleton().load(meshFile, m_sGroupName);
00726         pEntity = mSceneMgr->createEntity(name, meshFile);
00727         pEntity->setCastShadows(castShadows);
00728         pParent->attachObject(pEntity);
00729         
00730         if(!materialFile.empty())
00731             pEntity->setMaterialName(materialFile);
00732     }
00733     catch(Ogre::Exception &/*e*/)
00734     {
00735         LogManager::getSingleton().logMessage("[DotSceneLoader] Error loading an entity!");
00736     }
00737 
00738     // Process userDataReference (?)
00739     pElement = XMLNode->FirstChildElement("userDataReference");
00740     if(pElement)
00741         processUserDataReference(pElement, pEntity);
00742 
00743     
00744 }
00745 
00746 void DotSceneLoader::processParticleSystem(TiXmlElement *XMLNode, SceneNode *pParent)
00747 {
00748     // Process attributes
00749     String name = getAttrib(XMLNode, "name");
00750     String id = getAttrib(XMLNode, "id");
00751     String file = getAttrib(XMLNode, "file");
00752 
00753     // Create the particle system
00754     try
00755     {
00756         ParticleSystem *pParticles = mSceneMgr->createParticleSystem(name, file);
00757         pParent->attachObject(pParticles);
00758     }
00759     catch(Ogre::Exception &/*e*/)
00760     {
00761         LogManager::getSingleton().logMessage("[DotSceneLoader] Error creating a particle system!");
00762     }
00763 }
00764 
00765 void DotSceneLoader::processBillboardSet(TiXmlElement *XMLNode, SceneNode *pParent)
00766 {
00768 }
00769 
00770 void DotSceneLoader::processPlane(TiXmlElement *XMLNode, SceneNode *pParent)
00771 {
00772     String name = getAttrib(XMLNode, "name");
00773     String id = getAttrib(XMLNode, "id");
00774     Real distance = getAttribReal(XMLNode, "distance");
00775     Real width = getAttribReal(XMLNode, "width");
00776     Real height = getAttribReal(XMLNode, "height");
00777     int xSegments = getAttribInt(XMLNode, "xSegments",1);
00778     int ySegments = getAttribInt(XMLNode, "ySegments",1);
00779     int numTexCoordSets = getAttribInt(XMLNode, "numTexCoordSets",1);
00780     Real uTile = getAttribReal(XMLNode, "uTile", 1);
00781     Real vTile = getAttribReal(XMLNode, "vTile", 1);
00782     String material = getAttrib(XMLNode, "material", "");
00783     bool normals = getAttribBool(XMLNode, "normals", true);
00784 
00785     TiXmlElement *pElement;
00786 
00787     Plane plane;
00788     pElement = XMLNode->FirstChildElement("normal");
00789     if(pElement)
00790         plane.normal = parseVector3(pElement);
00791     plane.d = distance;
00792     
00793     Vector3 upVector = Vector3::UNIT_Y;
00794     pElement = XMLNode->FirstChildElement("upVector");
00795     if(pElement)
00796         upVector = parseVector3(pElement);
00797     
00798     Entity *pEntity = 0;
00799     try
00800     {
00801         MeshPtr pPlaneMesh = MeshManager::getSingleton().createPlane(name, m_sGroupName, plane, width, height, xSegments, ySegments, normals, numTexCoordSets, uTile, vTile, upVector);
00802         pEntity = mSceneMgr->createEntity(name, name);
00803         pParent->attachObject(pEntity);
00804         
00805         if(!material.empty())
00806             pEntity->setMaterialName(material);
00807     }
00808     catch(Ogre::Exception &e)
00809     {
00810         LogManager::getSingleton().logMessage("[DotSceneLoader] Error creating a plane!");
00811         LogManager::getSingleton().logMessage(e.getFullDescription());
00812     }
00813 }
00814 
00815 void DotSceneLoader::processFog(TiXmlElement *XMLNode)
00816 {
00817     // Process attributes
00818     Real expDensity = getAttribReal(XMLNode, "expDensity", 0.001);
00819     Real linearStart = getAttribReal(XMLNode, "linearStart", 0.0);
00820     Real linearEnd = getAttribReal(XMLNode, "linearEnd", 1.0);
00821 
00822     FogMode mode = FOG_NONE;
00823     String sMode = getAttrib(XMLNode, "mode");
00824     if(sMode == "none")
00825         mode = FOG_NONE;
00826     else if(sMode == "exp")
00827         mode = FOG_EXP;
00828     else if(sMode == "exp2")
00829         mode = FOG_EXP2;
00830     else if(sMode == "linear")
00831         mode = FOG_LINEAR;
00832 
00833     TiXmlElement *pElement;
00834 
00835     // Process colourDiffuse (?)
00836     ColourValue colourDiffuse = ColourValue::White;
00837     pElement = XMLNode->FirstChildElement("colourDiffuse");
00838     if(pElement)
00839         colourDiffuse = parseColour(pElement);
00840 
00841     // Setup the fog
00842     mSceneMgr->setFog(mode, colourDiffuse, expDensity, linearStart, linearEnd);
00843 }
00844 
00845 void DotSceneLoader::processSkyBox(TiXmlElement *XMLNode)
00846 {
00847     // Process attributes
00848     String material = getAttrib(XMLNode, "material");
00849     Real distance = getAttribReal(XMLNode, "distance", 5000);
00850     bool drawFirst = getAttribBool(XMLNode, "drawFirst", true);
00851 
00852     TiXmlElement *pElement;
00853 
00854     // Process rotation (?)
00855     Quaternion rotation = Quaternion::IDENTITY;
00856     pElement = XMLNode->FirstChildElement("rotation");
00857     if(pElement)
00858         rotation = parseQuaternion(pElement);
00859 
00860     // Setup the sky box
00861     mSceneMgr->setSkyBox(true, material, distance, drawFirst, rotation, m_sGroupName);
00862 }
00863 
00864 void DotSceneLoader::processSkyDome(TiXmlElement *XMLNode)
00865 {
00866     // Process attributes
00867     String material = XMLNode->Attribute("material");
00868     Real curvature = getAttribReal(XMLNode, "curvature", 10);
00869     Real tiling = getAttribReal(XMLNode, "tiling", 8);
00870     Real distance = getAttribReal(XMLNode, "distance", 4000);
00871     bool drawFirst = getAttribBool(XMLNode, "drawFirst", true);
00872 
00873     TiXmlElement *pElement;
00874 
00875     // Process rotation (?)
00876     Quaternion rotation = Quaternion::IDENTITY;
00877     pElement = XMLNode->FirstChildElement("rotation");
00878     if(pElement)
00879         rotation = parseQuaternion(pElement);
00880 
00881     // Setup the sky dome
00882     mSceneMgr->setSkyDome(true, material, curvature, tiling, distance, drawFirst, rotation, 16, 16, -1, m_sGroupName);
00883 }
00884 
00885 void DotSceneLoader::processSkyPlane(TiXmlElement *XMLNode)
00886 {
00887     // Process attributes
00888     String material = getAttrib(XMLNode, "material");
00889     Real planeX = getAttribReal(XMLNode, "planeX", 0);
00890     Real planeY = getAttribReal(XMLNode, "planeY", -1);
00891     Real planeZ = getAttribReal(XMLNode, "planeX", 0);
00892     Real planeD = getAttribReal(XMLNode, "planeD", 5000);
00893     Real scale = getAttribReal(XMLNode, "scale", 1000);
00894     Real bow = getAttribReal(XMLNode, "bow", 0);
00895     Real tiling = getAttribReal(XMLNode, "tiling", 10);
00896     bool drawFirst = getAttribBool(XMLNode, "drawFirst", true);
00897 
00898     // Setup the sky plane
00899     Plane plane;
00900     plane.normal = Vector3(planeX, planeY, planeZ);
00901     plane.d = planeD;
00902     mSceneMgr->setSkyPlane(true, plane, material, scale, tiling, drawFirst, bow, 1, 1, m_sGroupName);
00903 }
00904 
00905 void DotSceneLoader::processClipping(TiXmlElement *XMLNode, Camera* pCamera)
00906 {
00908 
00909     // Process attributes
00910     Real fNear = getAttribReal(XMLNode, "near", 0);
00911     Real fFar = getAttribReal(XMLNode, "far", 1);
00912     
00913     if (pCamera)
00914     {
00915         pCamera->setNearClipDistance(fNear);
00916         pCamera->setFarClipDistance(fFar);
00917     }
00918     else
00919     {
00920         environment.nearClipDistance = fNear;
00921         environment.farClipDistance = fFar;
00922     }
00923 }
00924 
00925 void DotSceneLoader::processLightRange(TiXmlElement *XMLNode, Light *pLight)
00926 {
00927     // Process attributes
00928     Real inner = getAttribReal(XMLNode, "inner");
00929     Real outer = getAttribReal(XMLNode, "outer");
00930     Real falloff = getAttribReal(XMLNode, "falloff", 1.0);
00931 
00932     // Setup the light range
00933     pLight->setSpotlightRange(Angle(inner), Angle(outer), falloff);
00934 }
00935 
00936 void DotSceneLoader::processLightAttenuation(TiXmlElement *XMLNode, Light *pLight)
00937 {
00938     // Process attributes
00939     Real range = getAttribReal(XMLNode, "range");
00940     Real constant = getAttribReal(XMLNode, "constant");
00941     Real linear = getAttribReal(XMLNode, "linear");
00942     Real quadratic = getAttribReal(XMLNode, "quadratic");
00943 
00944     // Setup the light attenuation
00945     pLight->setAttenuation(range, constant, linear, quadratic);
00946 }
00947 
00948 
00949 String DotSceneLoader::getAttrib(TiXmlElement *XMLNode, const String &attrib, const String &defaultValue)
00950 {
00951     if(XMLNode->Attribute(attrib))
00952         return XMLNode->Attribute(attrib);
00953     else
00954         return defaultValue;
00955 }
00956 
00957 Real DotSceneLoader::getAttribReal(TiXmlElement *XMLNode, const String &attrib, Real defaultValue)
00958 {
00959     if(XMLNode->Attribute(attrib))
00960         return StringConverter::parseReal(XMLNode->Attribute(attrib));
00961     else
00962         return defaultValue;
00963 }
00964 
00965 int DotSceneLoader::getAttribInt(TiXmlElement *XMLNode, const String &attrib, int defaultValue)
00966 {
00967     if(XMLNode->Attribute(attrib))
00968         return StringConverter::parseInt(XMLNode->Attribute(attrib));
00969     else
00970         return defaultValue;
00971 }
00972 
00973 bool DotSceneLoader::getAttribBool(TiXmlElement *XMLNode, const String &attrib, bool defaultValue)
00974 {
00975     if(!XMLNode->Attribute(attrib))
00976         return defaultValue;
00977 
00978     if(String(XMLNode->Attribute(attrib)) == "true")
00979         return true;
00980 
00981     return false;
00982 }
00983 
00984 
00985 Vector3 DotSceneLoader::parseVector3(TiXmlElement *XMLNode)
00986 {
00987     return Vector3(
00988         StringConverter::parseReal(XMLNode->Attribute("x")),
00989         StringConverter::parseReal(XMLNode->Attribute("y")),
00990         StringConverter::parseReal(XMLNode->Attribute("z"))
00991     );
00992 }
00993 
00994 Quaternion DotSceneLoader::parseQuaternion(TiXmlElement *XMLNode)
00995 {
00996     Quaternion orientation;
00997 
00998     if(XMLNode->Attribute("qx"))
00999     {
01000         orientation.x = StringConverter::parseReal(XMLNode->Attribute("qx"));
01001         orientation.y = StringConverter::parseReal(XMLNode->Attribute("qy"));
01002         orientation.z = StringConverter::parseReal(XMLNode->Attribute("qz"));
01003         orientation.w = StringConverter::parseReal(XMLNode->Attribute("qw"));
01004     }
01005     else if(XMLNode->Attribute("axisX"))
01006     {
01007         Vector3 axis;
01008         axis.x = StringConverter::parseReal(XMLNode->Attribute("axisX"));
01009         axis.y = StringConverter::parseReal(XMLNode->Attribute("axisY"));
01010         axis.z = StringConverter::parseReal(XMLNode->Attribute("axisZ"));
01011         Real angle = StringConverter::parseReal(XMLNode->Attribute("angle"));;
01012         orientation.FromAngleAxis(Ogre::Angle(angle), axis);
01013     }
01014     else if(XMLNode->Attribute("angleX"))
01015     {
01016         Vector3 angle;
01017         angle.x = StringConverter::parseReal(XMLNode->Attribute("angleX"));
01018         angle.y = StringConverter::parseReal(XMLNode->Attribute("angleY"));
01019         angle.z = StringConverter::parseReal(XMLNode->Attribute("angleZ"));
01020         Quaternion qx,qy,qz;
01021         qx.FromAngleAxis(Ogre::Angle(angle.x), Vector3::UNIT_X);
01022         qy.FromAngleAxis(Ogre::Angle(angle.y), Vector3::UNIT_Y);
01023         qz.FromAngleAxis(Ogre::Angle(angle.z), Vector3::UNIT_Z);
01025         orientation = qx * qy * qz;
01026     }
01027 
01028     return orientation;
01029 }
01030 
01031 ColourValue DotSceneLoader::parseColour(TiXmlElement *XMLNode)
01032 {
01033     return ColourValue(
01034         StringConverter::parseReal(XMLNode->Attribute("r")),
01035         StringConverter::parseReal(XMLNode->Attribute("g")),
01036         StringConverter::parseReal(XMLNode->Attribute("b")),
01037         XMLNode->Attribute("a") != NULL ? StringConverter::parseReal(XMLNode->Attribute("a")) : 1
01038     );
01039 }
01040 
01041 String DotSceneLoader::getProperty(const String &ndNm, const String &prop)
01042 {
01043     for ( unsigned int i = 0 ; i < nodeProperties.size(); i++ )
01044     {
01045         if ( nodeProperties[i].nodeName == ndNm && nodeProperties[i].propertyNm == prop )
01046         {
01047             return nodeProperties[i].valueName;
01048         }
01049     }
01050 
01051     return "";
01052 }
01053 
01054 void DotSceneLoader::processUserDataReference(TiXmlElement *XMLNode, Entity *pEntity)
01055 {
01056     String str = XMLNode->Attribute("id");
01057     pEntity->setUserAny(Any(str));
01058 }

Generated on Sun Nov 23 02:28:37 2008 for Sofa by  doxygen 1.5.7.1