Advertisement
Guest User

Untitled

a guest
Mar 17th, 2014
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 37.22 KB | None | 0 0
  1. #include "DotSceneLoader.hpp"
  2. #include <Ogre.h>
  3. #include <Terrain/OgreTerrain.h>
  4. #include <Terrain/OgreTerrainGroup.h>
  5. #include <Terrain/OgreTerrainMaterialGeneratorA.h>
  6.  
  7. #pragma warning(disable:4390)
  8. #pragma warning(disable:4305)
  9.  
  10.  
  11. DotSceneLoader::DotSceneLoader() : mSceneMgr(0), mTerrainGroup(0)
  12. {
  13.     mTerrainGlobalOptions = OGRE_NEW Ogre::TerrainGlobalOptions();
  14. }
  15.  
  16.  
  17. DotSceneLoader::~DotSceneLoader()
  18. {
  19.     if(mTerrainGroup)
  20.     {
  21.         OGRE_DELETE mTerrainGroup;
  22.     }
  23.  
  24.     OGRE_DELETE mTerrainGlobalOptions;
  25. }
  26.  
  27. void DotSceneLoader::parseDotScene(const Ogre::String &SceneName, const Ogre::String &groupName, Ogre::SceneManager *yourSceneMgr, Ogre::SceneNode *pAttachNode, const Ogre::String &sPrependNode)
  28. {
  29.     // set up shared object values
  30.     m_sGroupName = groupName;
  31.     mSceneMgr = yourSceneMgr;
  32.     m_sPrependNode = sPrependNode;
  33.     staticObjects.clear();
  34.     dynamicObjects.clear();
  35.  
  36.     rapidxml::xml_document<> XMLDoc;    // character type defaults to char
  37.  
  38.     rapidxml::xml_node<>* XMLRoot;
  39.  
  40.     Ogre::DataStreamPtr stream = Ogre::ResourceGroupManager::getSingleton().openResource(SceneName, groupName );
  41.     char* scene = strdup(stream->getAsString().c_str());
  42.     XMLDoc.parse<0>(scene);
  43.  
  44.     // Grab the scene node
  45.     XMLRoot = XMLDoc.first_node("scene");
  46.  
  47.     // Validate the File
  48.     if( getAttrib(XMLRoot, "formatVersion", "") == "")
  49.     {
  50.         Ogre::LogManager::getSingleton().logMessage( "[DotSceneLoader] Error: Invalid .scene File. Missing <scene>" );
  51.         return;
  52.     }
  53.  
  54.     // figure out where to attach any nodes we create
  55.     mAttachNode = pAttachNode;
  56.     if(!mAttachNode)
  57.         mAttachNode = mSceneMgr->getRootSceneNode();
  58.  
  59.     // Process the scene
  60.     processScene(XMLRoot);
  61. }
  62.  
  63. void DotSceneLoader::processScene(rapidxml::xml_node<>* XMLRoot)
  64. {
  65.     // Process the scene parameters
  66.     Ogre::String version = getAttrib(XMLRoot, "formatVersion", "unknown");
  67.  
  68.     Ogre::String message = "[DotSceneLoader] Parsing dotScene file with version " + version;
  69.     if(XMLRoot->first_attribute("ID"))
  70.         message += ", id " + Ogre::String(XMLRoot->first_attribute("ID")->value());
  71.     if(XMLRoot->first_attribute("sceneManager"))
  72.         message += ", scene manager " + Ogre::String(XMLRoot->first_attribute("sceneManager")->value());
  73.     if(XMLRoot->first_attribute("minOgreVersion"))
  74.         message += ", min. Ogre version " + Ogre::String(XMLRoot->first_attribute("minOgreVersion")->value());
  75.     if(XMLRoot->first_attribute("author"))
  76.         message += ", author " + Ogre::String(XMLRoot->first_attribute("author")->value());
  77.  
  78.     Ogre::LogManager::getSingleton().logMessage(message);
  79.  
  80.     rapidxml::xml_node<>* pElement;
  81.  
  82.     // Process environment (?)
  83.     pElement = XMLRoot->first_node("environment");
  84.     if(pElement)
  85.         processEnvironment(pElement);
  86.  
  87.     // Process nodes (?)
  88.     pElement = XMLRoot->first_node("nodes");
  89.     if(pElement)
  90.         processNodes(pElement);
  91.  
  92.     // Process externals (?)
  93.     pElement = XMLRoot->first_node("externals");
  94.     if(pElement)
  95.         processExternals(pElement);
  96.  
  97.     // Process userDataReference (?)
  98.     pElement = XMLRoot->first_node("userDataReference");
  99.     if(pElement)
  100.         processUserDataReference(pElement);
  101.  
  102.     // Process octree (?)
  103.     pElement = XMLRoot->first_node("octree");
  104.     if(pElement)
  105.         processOctree(pElement);
  106.  
  107.     // Process light (?)
  108.     //pElement = XMLRoot->first_node("light");
  109.     //if(pElement)
  110.     //   processLight(pElement);
  111.  
  112.     // Process camera (?)
  113.     pElement = XMLRoot->first_node("camera");
  114.     if(pElement)
  115.         processCamera(pElement);
  116.  
  117.     // Process terrain (?)
  118.     pElement = XMLRoot->first_node("terrain");
  119.     if(pElement)
  120.         processTerrain(pElement);
  121. }
  122.  
  123. void DotSceneLoader::processNodes(rapidxml::xml_node<>* XMLNode)
  124. {
  125.     rapidxml::xml_node<>* pElement;
  126.  
  127.     // Process node (*)
  128.     pElement = XMLNode->first_node("node");
  129.     while(pElement)
  130.     {
  131.         processNode(pElement);
  132.         pElement = pElement->next_sibling("node");
  133.     }
  134.  
  135.     // Process position (?)
  136.     pElement = XMLNode->first_node("position");
  137.     if(pElement)
  138.     {
  139.         mAttachNode->setPosition(parseVector3(pElement));
  140.         mAttachNode->setInitialState();
  141.     }
  142.  
  143.     // Process rotation (?)
  144.     pElement = XMLNode->first_node("rotation");
  145.     if(pElement)
  146.     {
  147.         mAttachNode->setOrientation(parseQuaternion(pElement));
  148.         mAttachNode->setInitialState();
  149.     }
  150.  
  151.     // Process scale (?)
  152.     pElement = XMLNode->first_node("scale");
  153.     if(pElement)
  154.     {
  155.         mAttachNode->setScale(parseVector3(pElement));
  156.         mAttachNode->setInitialState();
  157.     }
  158. }
  159.  
  160. void DotSceneLoader::processExternals(rapidxml::xml_node<>* XMLNode)
  161. {
  162.     //! @todo Implement this
  163. }
  164.  
  165. void DotSceneLoader::processEnvironment(rapidxml::xml_node<>* XMLNode)
  166. {
  167.     rapidxml::xml_node<>* pElement;
  168.  
  169.     // Process camera (?)
  170.     pElement = XMLNode->first_node("camera");
  171.     if(pElement)
  172.         processCamera(pElement);
  173.  
  174.     // Process fog (?)
  175.     pElement = XMLNode->first_node("fog");
  176.     if(pElement)
  177.         processFog(pElement);
  178.  
  179.     // Process skyBox (?)
  180.     pElement = XMLNode->first_node("skyBox");
  181.     if(pElement)
  182.         processSkyBox(pElement);
  183.  
  184.     // Process skyDome (?)
  185.     pElement = XMLNode->first_node("skyDome");
  186.     if(pElement)
  187.         processSkyDome(pElement);
  188.  
  189.     // Process skyPlane (?)
  190.     pElement = XMLNode->first_node("skyPlane");
  191.     if(pElement)
  192.         processSkyPlane(pElement);
  193.  
  194.     // Process clipping (?)
  195.     pElement = XMLNode->first_node("clipping");
  196.     if(pElement)
  197.         processClipping(pElement);
  198.  
  199.     // Process colourAmbient (?)
  200.     pElement = XMLNode->first_node("colourAmbient");
  201.     if(pElement)
  202.         mSceneMgr->setAmbientLight(parseColour(pElement));
  203.  
  204.     // Process colourBackground (?)
  205.     //! @todo Set the background colour of all viewports (RenderWindow has to be provided then)
  206.     pElement = XMLNode->first_node("colourBackground");
  207.     if(pElement)
  208.         ;//mSceneMgr->set(parseColour(pElement));
  209.  
  210.     // Process userDataReference (?)
  211.     pElement = XMLNode->first_node("userDataReference");
  212.     if(pElement)
  213.         processUserDataReference(pElement);
  214. }
  215.  
  216. void DotSceneLoader::processTerrain(rapidxml::xml_node<>* XMLNode)
  217. {
  218.     Ogre::Real worldSize = getAttribReal(XMLNode, "worldSize");
  219.     int mapSize = Ogre::StringConverter::parseInt(XMLNode->first_attribute("mapSize")->value());
  220.     int rows = Ogre::StringConverter::parseInt(XMLNode->first_attribute("rows")->value());
  221.     int columns = Ogre::StringConverter::parseInt(XMLNode->first_attribute("columns")->value());
  222.     bool colourmapEnabled = getAttribBool(XMLNode, "colourmapEnabled");
  223.     int colourMapTextureSize = Ogre::StringConverter::parseInt(XMLNode->first_attribute("colourMapTextureSize")->value());
  224.  
  225.     Ogre::Vector3 lightdir(0, -0.3, 0.75);
  226.     lightdir.normalise();
  227.     Ogre::Light* l = mSceneMgr->createLight("tstLight");
  228.     l->setType(Ogre::Light::LT_DIRECTIONAL);
  229.     l->setDirection(lightdir);
  230.     l->setDiffuseColour(Ogre::ColourValue(1.0, 1.0, 1.0));
  231.     l->setSpecularColour(Ogre::ColourValue(0.4, 0.4, 0.4));
  232.     mSceneMgr->setAmbientLight(Ogre::ColourValue(0.6, 0.6, 0.6));
  233.  
  234.     mTerrainGlobalOptions->setMaxPixelError(1);
  235.     mTerrainGlobalOptions->setCompositeMapDistance(2000);
  236.     mTerrainGlobalOptions->setLightMapDirection(lightdir);
  237.     mTerrainGlobalOptions->setCompositeMapAmbient(mSceneMgr->getAmbientLight());
  238.     mTerrainGlobalOptions->setCompositeMapDiffuse(l->getDiffuseColour());
  239.  
  240.     mTerrainGroup = OGRE_NEW Ogre::TerrainGroup(mSceneMgr, Ogre::Terrain::ALIGN_X_Z, mapSize, worldSize);
  241.     mTerrainGroup->setOrigin(Ogre::Vector3::ZERO);
  242.  
  243.     rapidxml::xml_node<>* pElement;
  244.     rapidxml::xml_node<>* pPageElement;
  245.  
  246.     // Process terrain pages (*)
  247.     pElement = XMLNode->first_node("terrainPages");
  248.     while(pElement)
  249.     {
  250.         pPageElement = pElement->first_node("terrainPage");
  251.         while(pPageElement)
  252.         {
  253.             processTerrainPage(pPageElement);
  254.             pPageElement = pPageElement->next_sibling("terrainPage");
  255.         }
  256.         pElement = pElement->next_sibling("terrainPages");
  257.     }
  258.     mTerrainGroup->loadAllTerrains(true);
  259.  
  260.     // process blendmaps
  261.     pElement = XMLNode->first_node("terrainPages");
  262.     while(pElement)
  263.     {
  264.         pPageElement = pElement->first_node("terrainPage");
  265.         while(pPageElement)
  266.         {
  267.             processBlendmaps(pPageElement);
  268.             pPageElement = pPageElement->next_sibling("terrainPage");
  269.         }
  270.         pElement = pElement->next_sibling("terrainPages");
  271.     }
  272.     mTerrainGroup->freeTemporaryResources();
  273.     //mTerrain->setPosition(mTerrainPosition);
  274. }
  275.  
  276. void DotSceneLoader::processTerrainPage(rapidxml::xml_node<>* XMLNode)
  277. {
  278.     Ogre::String name = getAttrib(XMLNode, "name");
  279.     int pageX = Ogre::StringConverter::parseInt(XMLNode->first_attribute("pageX")->value());
  280.     int pageY = Ogre::StringConverter::parseInt(XMLNode->first_attribute("pageY")->value());
  281.     Ogre::Real worldSize = getAttribReal(XMLNode, "worldSize");
  282.     int mapSize = Ogre::StringConverter::parseInt(XMLNode->first_attribute("mapSize")->value());
  283.     bool colourmapEnabled = getAttribBool(XMLNode, "colourmapEnabled");
  284.     int colourmapTexturesize = Ogre::StringConverter::parseInt(XMLNode->first_attribute("colourmapTexturesize")->value());
  285.     int layerCount = Ogre::StringConverter::parseInt(XMLNode->first_attribute("layerCount")->value());
  286.  
  287.     Ogre::String filename = mTerrainGroup->generateFilename(pageX, pageY);
  288.     if (Ogre::ResourceGroupManager::getSingleton().resourceExists(mTerrainGroup->getResourceGroup(), filename))
  289.     {
  290.         mTerrainGroup->defineTerrain(pageX, pageY);
  291.     }
  292.     else
  293.     {
  294.         rapidxml::xml_node<>* pElement;
  295.  
  296.         pElement = XMLNode->first_node("position");
  297.         if(pElement)
  298.             mTerrainPosition = parseVector3(pElement);
  299.  
  300.         Ogre::DataStreamPtr stream = Ogre::ResourceGroupManager::getSingleton().openResource(name + Ogre::String(".ohm"), "General" );
  301.         size_t size = stream.get()->size();
  302.         if(size != mapSize * mapSize * 4)
  303.         {
  304.             OGRE_EXCEPT( Ogre::Exception::ERR_INTERNAL_ERROR, "Size of stream does not match terrainsize!", "TerrainPage" );
  305.         }
  306.         float* buffer = OGRE_ALLOC_T(float, size, Ogre::MEMCATEGORY_GEOMETRY);
  307.         stream->read(buffer, size);
  308.  
  309.         Ogre::Terrain::ImportData& imp = mTerrainGroup->getDefaultImportSettings();
  310.         imp.terrainSize = mapSize;
  311.         imp.worldSize = worldSize;
  312.         imp.inputFloat = buffer;
  313.         imp.inputImage = 0;
  314.         imp.deleteInputData = true;
  315.         imp.minBatchSize = 33;
  316.         imp.maxBatchSize = 65;
  317.  
  318.         imp.layerList.resize(layerCount);
  319.  
  320.         int count = 0;
  321.  
  322.         // Process layers (*)
  323.         rapidxml::xml_node<>* pTerrainLayer;
  324.         rapidxml::xml_node<>* pTerrainTextures;
  325.         rapidxml::xml_node<>* pTerrainTexture;
  326.         pElement = XMLNode->first_node("layers");
  327.         while(pElement)
  328.         {
  329.             pTerrainLayer = pElement->first_node("layer");
  330.             while(pTerrainLayer)
  331.             {
  332.                 int worldSize = Ogre::StringConverter::parseInt(pTerrainLayer->first_attribute("worldSize")->value());
  333.                 pTerrainTextures = pTerrainLayer->first_node("textures");
  334.                 pTerrainTexture = pTerrainTextures->first_node("texture");
  335.                 while(pTerrainTexture)
  336.                 {
  337.                     imp.layerList[count].textureNames.push_back(getAttrib(pTerrainTexture,"name",""));
  338.                     imp.layerList[count].worldSize = (Ogre::Real)worldSize;
  339.                     pTerrainTexture = pTerrainTexture->next_sibling("texture");
  340.                 }
  341.                 count++;
  342.                 // do stuff
  343.                 pTerrainLayer = pTerrainLayer->next_sibling("layer");
  344.             }
  345.             pElement = pElement->next_sibling("layers");
  346.         }
  347.  
  348.         mTerrainGroup->defineTerrain(pageX, pageY, &imp);
  349.     }
  350. }
  351.  
  352. void DotSceneLoader::processBlendmaps(rapidxml::xml_node<>* XMLNode)
  353. {
  354.     int pageX = Ogre::StringConverter::parseInt(XMLNode->first_attribute("pageX")->value());
  355.     int pageY = Ogre::StringConverter::parseInt(XMLNode->first_attribute("pageY")->value());
  356.  
  357.     Ogre::String filename = mTerrainGroup->generateFilename(pageX, pageY);
  358.     // skip this is terrain page has been saved already
  359.     if (!Ogre::ResourceGroupManager::getSingleton().resourceExists(mTerrainGroup->getResourceGroup(), filename))
  360.     {
  361.         rapidxml::xml_node<>* pElement;
  362.  
  363.         // Process blendmaps (*)
  364.         std::vector<Ogre::String> blendMaps;
  365.         rapidxml::xml_node<>* pBlendmap;
  366.         pElement = XMLNode->first_node("blendMaps");
  367.         pBlendmap = pElement->first_node("blendMap");
  368.         while(pBlendmap)
  369.         {
  370.             blendMaps.push_back(getAttrib(pBlendmap, "texture",""));
  371.             pBlendmap = pBlendmap->next_sibling("blendMap");
  372.         }
  373.  
  374.         for(int j = 1;j < mTerrainGroup->getTerrain(pageX, pageY)->getLayerCount();j++)
  375.         {
  376.             Ogre::TerrainLayerBlendMap *blendmap = mTerrainGroup->getTerrain(pageX, pageY)->getLayerBlendMap(j);
  377.             Ogre::Image img;
  378.             img.load(blendMaps[j-1],"General");
  379.             int blendmapsize = mTerrainGroup->getTerrain(pageX, pageY)->getLayerBlendMapSize();
  380.             if(img.getWidth() != blendmapsize)
  381.                 img.resize(blendmapsize, blendmapsize);
  382.  
  383.             float *ptr = blendmap->getBlendPointer();
  384.             Ogre::uint8 *data = static_cast<Ogre::uint8*>(img.getPixelBox().data);
  385.  
  386.             for(int bp = 0;bp < blendmapsize * blendmapsize;bp++)
  387.                 ptr[bp] = static_cast<float>(data[bp]) / 255.0f;
  388.  
  389.             blendmap->dirty();
  390.             blendmap->update();
  391.         }
  392.     }
  393. }
  394.  
  395. void DotSceneLoader::processUserDataReference(rapidxml::xml_node<>* XMLNode, Ogre::SceneNode *pParent)
  396. {
  397.     //! @todo Implement this
  398. }
  399.  
  400. void DotSceneLoader::processOctree(rapidxml::xml_node<>* XMLNode)
  401. {
  402.     //! @todo Implement this
  403. }
  404.  
  405. void DotSceneLoader::processLight(rapidxml::xml_node<>* XMLNode, Ogre::SceneNode *pParent)
  406. {
  407.     // Process attributes
  408.     Ogre::String name = getAttrib(XMLNode, "name");
  409.     Ogre::String id = getAttrib(XMLNode, "id");
  410.  
  411.     // Create the light
  412.     Ogre::Light *pLight = mSceneMgr->createLight(name);
  413.     if(pParent)
  414.         pParent->attachObject(pLight);
  415.  
  416.     Ogre::String sValue = getAttrib(XMLNode, "type");
  417.     if(sValue == "point")
  418.         pLight->setType(Ogre::Light::LT_POINT);
  419.     else if(sValue == "directional")
  420.         pLight->setType(Ogre::Light::LT_DIRECTIONAL);
  421.     else if(sValue == "spot")
  422.         pLight->setType(Ogre::Light::LT_SPOTLIGHT);
  423.     else if(sValue == "radPoint")
  424.         pLight->setType(Ogre::Light::LT_POINT);
  425.  
  426.     pLight->setVisible(getAttribBool(XMLNode, "visible", true));
  427.     pLight->setCastShadows(getAttribBool(XMLNode, "castShadows", true));
  428.  
  429.     rapidxml::xml_node<>* pElement;
  430.  
  431.     // Process position (?)
  432.     pElement = XMLNode->first_node("position");
  433.     if(pElement)
  434.         pLight->setPosition(parseVector3(pElement));
  435.  
  436.     // Process normal (?)
  437.     pElement = XMLNode->first_node("normal");
  438.     if(pElement)
  439.         pLight->setDirection(parseVector3(pElement));
  440.  
  441.     pElement = XMLNode->first_node("directionVector");
  442.     if(pElement)
  443.     {
  444.         pLight->setDirection(parseVector3(pElement));
  445.         mLightDirection = parseVector3(pElement);
  446.     }
  447.  
  448.     // Process colourDiffuse (?)
  449.     pElement = XMLNode->first_node("colourDiffuse");
  450.     if(pElement)
  451.         pLight->setDiffuseColour(parseColour(pElement));
  452.  
  453.     // Process colourSpecular (?)
  454.     pElement = XMLNode->first_node("colourSpecular");
  455.     if(pElement)
  456.         pLight->setSpecularColour(parseColour(pElement));
  457.  
  458.     if(sValue != "directional")
  459.     {
  460.         // Process lightRange (?)
  461.         pElement = XMLNode->first_node("lightRange");
  462.         if(pElement)
  463.             processLightRange(pElement, pLight);
  464.  
  465.         // Process lightAttenuation (?)
  466.         pElement = XMLNode->first_node("lightAttenuation");
  467.         if(pElement)
  468.             processLightAttenuation(pElement, pLight);
  469.     }
  470.     // Process userDataReference (?)
  471.     pElement = XMLNode->first_node("userDataReference");
  472.     if(pElement)
  473.         ;//processUserDataReference(pElement, pLight);
  474. }
  475.  
  476. void DotSceneLoader::processCamera(rapidxml::xml_node<>* XMLNode, Ogre::SceneNode *pParent)
  477. {
  478.     // Process attributes
  479.     Ogre::String name = getAttrib(XMLNode, "name");
  480.     Ogre::String id = getAttrib(XMLNode, "id");
  481.     Ogre::Real fov = getAttribReal(XMLNode, "fov", 45);
  482.     Ogre::Real aspectRatio = getAttribReal(XMLNode, "aspectRatio", 1.3333);
  483.     Ogre::String projectionType = getAttrib(XMLNode, "projectionType", "perspective");
  484.  
  485.     // Create the camera
  486.     Ogre::Camera *pCamera = mSceneMgr->createCamera(name);
  487.  
  488.     //TODO: make a flag or attribute indicating whether or not the camera should be attached to any parent node.
  489.     //if(pParent)
  490.     //   pParent->attachObject(pCamera);
  491.  
  492.     // Set the field-of-view
  493.     //! @todo Is this always in degrees?
  494.     //pCamera->setFOVy(Ogre::Degree(fov));
  495.  
  496.     // Set the aspect ratio
  497.     //pCamera->setAspectRatio(aspectRatio);
  498.  
  499.     // Set the projection type
  500.     if(projectionType == "perspective")
  501.         pCamera->setProjectionType(Ogre::PT_PERSPECTIVE);
  502.     else if(projectionType == "orthographic")
  503.         pCamera->setProjectionType(Ogre::PT_ORTHOGRAPHIC);
  504.  
  505.     rapidxml::xml_node<>* pElement;
  506.  
  507.     // Process clipping (?)
  508.     pElement = XMLNode->first_node("clipping");
  509.     if(pElement)
  510.     {
  511.         Ogre::Real nearDist = getAttribReal(pElement, "near");
  512.         pCamera->setNearClipDistance(nearDist);
  513.  
  514.         Ogre::Real farDist =  getAttribReal(pElement, "far");
  515.         pCamera->setFarClipDistance(farDist);
  516.     }
  517.  
  518.     // Process position (?)
  519.     pElement = XMLNode->first_node("position");
  520.     if(pElement)
  521.         pCamera->setPosition(parseVector3(pElement));
  522.  
  523.     // Process rotation (?)
  524.     pElement = XMLNode->first_node("rotation");
  525.     if(pElement)
  526.         pCamera->setOrientation(parseQuaternion(pElement));
  527.  
  528.     // Process normal (?)
  529.     pElement = XMLNode->first_node("normal");
  530.     if(pElement)
  531.         ;//!< @todo What to do with this element?
  532.  
  533.     // Process lookTarget (?)
  534.     pElement = XMLNode->first_node("lookTarget");
  535.     if(pElement)
  536.         ;//!< @todo Implement the camera look target
  537.  
  538.     // Process trackTarget (?)
  539.     pElement = XMLNode->first_node("trackTarget");
  540.     if(pElement)
  541.         ;//!< @todo Implement the camera track target
  542.  
  543.     // Process userDataReference (?)
  544.     pElement = XMLNode->first_node("userDataReference");
  545.     if(pElement)
  546.         ;//!< @todo Implement the camera user data reference
  547.  
  548.     // construct a scenenode is no parent
  549.     if(!pParent)
  550.     {
  551.         Ogre::SceneNode* pNode = mAttachNode->createChildSceneNode(name);
  552.         pNode->setPosition(pCamera->getPosition());
  553.         pNode->setOrientation(pCamera->getOrientation());
  554.         pNode->scale(1,1,1);
  555.     }
  556. }
  557.  
  558. void DotSceneLoader::processNode(rapidxml::xml_node<>* XMLNode, Ogre::SceneNode *pParent)
  559. {
  560.     // Construct the node's name
  561.     Ogre::String name = m_sPrependNode + getAttrib(XMLNode, "name");
  562.  
  563.     // Create the scene node
  564.     Ogre::SceneNode *pNode;
  565.     if(name.empty())
  566.     {
  567.         // Let Ogre choose the name
  568.         if(pParent)
  569.             pNode = pParent->createChildSceneNode();
  570.         else
  571.             pNode = mAttachNode->createChildSceneNode();
  572.     }
  573.     else
  574.     {
  575.         // Provide the name
  576.         if(pParent)
  577.             pNode = pParent->createChildSceneNode(name);
  578.         else
  579.             pNode = mAttachNode->createChildSceneNode(name);
  580.     }
  581.  
  582.     // Process other attributes
  583.     Ogre::String id = getAttrib(XMLNode, "id");
  584.     bool isTarget = getAttribBool(XMLNode, "isTarget");
  585.  
  586.     rapidxml::xml_node<>* pElement;
  587.  
  588.     // Process position (?)
  589.     pElement = XMLNode->first_node("position");
  590.     if(pElement)
  591.     {
  592.         pNode->setPosition(parseVector3(pElement));
  593.         pNode->setInitialState();
  594.     }
  595.  
  596.     // Process rotation (?)
  597.     pElement = XMLNode->first_node("rotation");
  598.     if(pElement)
  599.     {
  600.         pNode->setOrientation(parseQuaternion(pElement));
  601.         pNode->setInitialState();
  602.     }
  603.  
  604.     // Process scale (?)
  605.     pElement = XMLNode->first_node("scale");
  606.     if(pElement)
  607.     {
  608.         pNode->setScale(parseVector3(pElement));
  609.         pNode->setInitialState();
  610.     }
  611.  
  612.     // Process lookTarget (?)
  613.     pElement = XMLNode->first_node("lookTarget");
  614.     if(pElement)
  615.         processLookTarget(pElement, pNode);
  616.  
  617.     // Process trackTarget (?)
  618.     pElement = XMLNode->first_node("trackTarget");
  619.     if(pElement)
  620.         processTrackTarget(pElement, pNode);
  621.  
  622.     // Process node (*)
  623.     pElement = XMLNode->first_node("node");
  624.     while(pElement)
  625.     {
  626.         processNode(pElement, pNode);
  627.         pElement = pElement->next_sibling("node");
  628.     }
  629.  
  630.     // Process entity (*)
  631.     pElement = XMLNode->first_node("entity");
  632.     while(pElement)
  633.     {
  634.         processEntity(pElement, pNode);
  635.         pElement = pElement->next_sibling("entity");
  636.     }
  637.  
  638.     // Process light (*)
  639.     //pElement = XMLNode->first_node("light");
  640.     //while(pElement)
  641.     //{
  642.     //   processLight(pElement, pNode);
  643.     //   pElement = pElement->next_sibling("light");
  644.     //}
  645.  
  646.     // Process camera (*)
  647.     pElement = XMLNode->first_node("camera");
  648.     while(pElement)
  649.     {
  650.         processCamera(pElement, pNode);
  651.         pElement = pElement->next_sibling("camera");
  652.     }
  653.  
  654.     // Process particleSystem (*)
  655.     pElement = XMLNode->first_node("particleSystem");
  656.     while(pElement)
  657.     {
  658.         processParticleSystem(pElement, pNode);
  659.         pElement = pElement->next_sibling("particleSystem");
  660.     }
  661.  
  662.     // Process billboardSet (*)
  663.     pElement = XMLNode->first_node("billboardSet");
  664.     while(pElement)
  665.     {
  666.         processBillboardSet(pElement, pNode);
  667.         pElement = pElement->next_sibling("billboardSet");
  668.     }
  669.  
  670.     // Process plane (*)
  671.     pElement = XMLNode->first_node("plane");
  672.     while(pElement)
  673.     {
  674.         processPlane(pElement, pNode);
  675.         pElement = pElement->next_sibling("plane");
  676.     }
  677.  
  678.     // Process userDataReference (?)
  679.     pElement = XMLNode->first_node("userDataReference");
  680.     if(pElement)
  681.         processUserDataReference(pElement, pNode);
  682. }
  683.  
  684. void DotSceneLoader::processLookTarget(rapidxml::xml_node<>* XMLNode, Ogre::SceneNode *pParent)
  685. {
  686.     //! @todo Is this correct? Cause I don't have a clue actually
  687.  
  688.     // Process attributes
  689.     Ogre::String nodeName = getAttrib(XMLNode, "nodeName");
  690.  
  691.     Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_PARENT;
  692.     Ogre::String sValue = getAttrib(XMLNode, "relativeTo");
  693.     if(sValue == "local")
  694.         relativeTo = Ogre::Node::TS_LOCAL;
  695.     else if(sValue == "parent")
  696.         relativeTo = Ogre::Node::TS_PARENT;
  697.     else if(sValue == "world")
  698.         relativeTo = Ogre::Node::TS_WORLD;
  699.  
  700.     rapidxml::xml_node<>* pElement;
  701.  
  702.     // Process position (?)
  703.     Ogre::Vector3 position;
  704.     pElement = XMLNode->first_node("position");
  705.     if(pElement)
  706.         position = parseVector3(pElement);
  707.  
  708.     // Process localDirection (?)
  709.     Ogre::Vector3 localDirection = Ogre::Vector3::NEGATIVE_UNIT_Z;
  710.     pElement = XMLNode->first_node("localDirection");
  711.     if(pElement)
  712.         localDirection = parseVector3(pElement);
  713.  
  714.     // Setup the look target
  715.     try
  716.     {
  717.         if(!nodeName.empty())
  718.         {
  719.             Ogre::SceneNode *pLookNode = mSceneMgr->getSceneNode(nodeName);
  720.             position = pLookNode->_getDerivedPosition();
  721.         }
  722.  
  723.         pParent->lookAt(position, relativeTo, localDirection);
  724.     }
  725.     catch(Ogre::Exception &/*e*/)
  726.     {
  727.         Ogre::LogManager::getSingleton().logMessage("[DotSceneLoader] Error processing a look target!");
  728.     }
  729. }
  730.  
  731. void DotSceneLoader::processTrackTarget(rapidxml::xml_node<>* XMLNode, Ogre::SceneNode *pParent)
  732. {
  733.     // Process attributes
  734.     Ogre::String nodeName = getAttrib(XMLNode, "nodeName");
  735.  
  736.     rapidxml::xml_node<>* pElement;
  737.  
  738.     // Process localDirection (?)
  739.     Ogre::Vector3 localDirection = Ogre::Vector3::NEGATIVE_UNIT_Z;
  740.     pElement = XMLNode->first_node("localDirection");
  741.     if(pElement)
  742.         localDirection = parseVector3(pElement);
  743.  
  744.     // Process offset (?)
  745.     Ogre::Vector3 offset = Ogre::Vector3::ZERO;
  746.     pElement = XMLNode->first_node("offset");
  747.     if(pElement)
  748.         offset = parseVector3(pElement);
  749.  
  750.     // Setup the track target
  751.     try
  752.     {
  753.         Ogre::SceneNode *pTrackNode = mSceneMgr->getSceneNode(nodeName);
  754.         pParent->setAutoTracking(true, pTrackNode, localDirection, offset);
  755.     }
  756.     catch(Ogre::Exception &/*e*/)
  757.     {
  758.         Ogre::LogManager::getSingleton().logMessage("[DotSceneLoader] Error processing a track target!");
  759.     }
  760. }
  761.  
  762. void DotSceneLoader::processEntity(rapidxml::xml_node<>* XMLNode, Ogre::SceneNode *pParent)
  763. {
  764.     // Process attributes
  765.     Ogre::String name = getAttrib(XMLNode, "name");
  766.     Ogre::String id = getAttrib(XMLNode, "id");
  767.     Ogre::String meshFile = getAttrib(XMLNode, "meshFile");
  768.     Ogre::String materialFile = getAttrib(XMLNode, "materialFile");
  769.     bool isStatic = getAttribBool(XMLNode, "static", false);;
  770.     bool castShadows = getAttribBool(XMLNode, "castShadows", true);
  771.  
  772.     // TEMP: Maintain a list of static and dynamic objects
  773.     if(isStatic)
  774.         staticObjects.push_back(name);
  775.     else
  776.         dynamicObjects.push_back(name);
  777.  
  778.     rapidxml::xml_node<>* pElement;
  779.  
  780.     // Process vertexBuffer (?)
  781.     pElement = XMLNode->first_node("vertexBuffer");
  782.     if(pElement)
  783.         ;//processVertexBuffer(pElement);
  784.  
  785.     // Process indexBuffer (?)
  786.     pElement = XMLNode->first_node("indexBuffer");
  787.     if(pElement)
  788.         ;//processIndexBuffer(pElement);
  789.  
  790.     // Create the entity
  791.     Ogre::Entity *pEntity = 0;
  792.     try
  793.     {
  794.         Ogre::MeshManager::getSingleton().load(meshFile, m_sGroupName);
  795.         pEntity = mSceneMgr->createEntity(name, meshFile);
  796.         pEntity->setCastShadows(castShadows);
  797.         pParent->attachObject(pEntity);
  798.  
  799.         if(!materialFile.empty())
  800.             pEntity->setMaterialName(materialFile);
  801.     }
  802.     catch(Ogre::Exception &/*e*/)
  803.     {
  804.         Ogre::LogManager::getSingleton().logMessage("[DotSceneLoader] Error loading an entity!");
  805.     }
  806.  
  807.     // Process userDataReference (?)
  808.     pElement = XMLNode->first_node("userDataReference");
  809.     if(pElement)
  810.         processUserDataReference(pElement, pEntity);
  811.  
  812.  
  813. }
  814.  
  815. void DotSceneLoader::processParticleSystem(rapidxml::xml_node<>* XMLNode, Ogre::SceneNode *pParent)
  816. {
  817.     // Process attributes
  818.     Ogre::String name = getAttrib(XMLNode, "name");
  819.     Ogre::String id = getAttrib(XMLNode, "id");
  820.     Ogre::String file = getAttrib(XMLNode, "file");
  821.  
  822.     // Create the particle system
  823.     try
  824.     {
  825.         Ogre::ParticleSystem *pParticles = mSceneMgr->createParticleSystem(name, file);
  826.         pParent->attachObject(pParticles);
  827.     }
  828.     catch(Ogre::Exception &/*e*/)
  829.     {
  830.         Ogre::LogManager::getSingleton().logMessage("[DotSceneLoader] Error creating a particle system!");
  831.     }
  832. }
  833.  
  834. void DotSceneLoader::processBillboardSet(rapidxml::xml_node<>* XMLNode, Ogre::SceneNode *pParent)
  835. {
  836.     //! @todo Implement this
  837. }
  838.  
  839. void DotSceneLoader::processPlane(rapidxml::xml_node<>* XMLNode, Ogre::SceneNode *pParent)
  840. {
  841.     //! @todo Implement this
  842. }
  843.  
  844. void DotSceneLoader::processFog(rapidxml::xml_node<>* XMLNode)
  845. {
  846.     // Process attributes
  847.     Ogre::Real expDensity = getAttribReal(XMLNode, "density", 0.001);
  848.     Ogre::Real linearStart = getAttribReal(XMLNode, "start", 0.0);
  849.     Ogre::Real linearEnd = getAttribReal(XMLNode, "end", 1.0);
  850.  
  851.     Ogre::FogMode mode = Ogre::FOG_NONE;
  852.     Ogre::String sMode = getAttrib(XMLNode, "mode");
  853.     if(sMode == "none")
  854.         mode = Ogre::FOG_NONE;
  855.     else if(sMode == "exp")
  856.         mode = Ogre::FOG_EXP;
  857.     else if(sMode == "exp2")
  858.         mode = Ogre::FOG_EXP2;
  859.     else if(sMode == "linear")
  860.         mode = Ogre::FOG_LINEAR;
  861.  
  862.     rapidxml::xml_node<>* pElement;
  863.  
  864.     // Process colourDiffuse (?)
  865.     Ogre::ColourValue colourDiffuse = Ogre::ColourValue::White;
  866.     pElement = XMLNode->first_node("colour");
  867.     if(pElement)
  868.         colourDiffuse = parseColour(pElement);
  869.  
  870.     // Setup the fog
  871.     mSceneMgr->setFog(mode, colourDiffuse, expDensity, linearStart, linearEnd);
  872. }
  873.  
  874. void DotSceneLoader::processSkyBox(rapidxml::xml_node<>* XMLNode)
  875. {
  876.     // Process attributes
  877.     Ogre::String material = getAttrib(XMLNode, "material", "BaseWhite");
  878.     Ogre::Real distance = getAttribReal(XMLNode, "distance", 5000);
  879.     bool drawFirst = getAttribBool(XMLNode, "drawFirst", true);
  880.     bool active = getAttribBool(XMLNode, "active", false);
  881.     if(!active)
  882.         return;
  883.  
  884.     rapidxml::xml_node<>* pElement;
  885.  
  886.     // Process rotation (?)
  887.     Ogre::Quaternion rotation = Ogre::Quaternion::IDENTITY;
  888.     pElement = XMLNode->first_node("rotation");
  889.     if(pElement)
  890.         rotation = parseQuaternion(pElement);
  891.  
  892.     // Setup the sky box
  893.     mSceneMgr->setSkyBox(true, material, distance, drawFirst, rotation, m_sGroupName);
  894. }
  895.  
  896. void DotSceneLoader::processSkyDome(rapidxml::xml_node<>* XMLNode)
  897. {
  898.     // Process attributes
  899.     Ogre::String material = XMLNode->first_attribute("material")->value();
  900.     Ogre::Real curvature = getAttribReal(XMLNode, "curvature", 10);
  901.     Ogre::Real tiling = getAttribReal(XMLNode, "tiling", 8);
  902.     Ogre::Real distance = getAttribReal(XMLNode, "distance", 4000);
  903.     bool drawFirst = getAttribBool(XMLNode, "drawFirst", true);
  904.  
  905.     rapidxml::xml_node<>* pElement;
  906.  
  907.     // Process rotation (?)
  908.     Ogre::Quaternion rotation = Ogre::Quaternion::IDENTITY;
  909.     pElement = XMLNode->first_node("rotation");
  910.     if(pElement)
  911.         rotation = parseQuaternion(pElement);
  912.  
  913.     // Setup the sky dome
  914.     mSceneMgr->setSkyDome(true, material, curvature, tiling, distance, drawFirst, rotation, 16, 16, -1, m_sGroupName);
  915. }
  916.  
  917. void DotSceneLoader::processSkyPlane(rapidxml::xml_node<>* XMLNode)
  918. {
  919.     // Process attributes
  920.     Ogre::String material = getAttrib(XMLNode, "material");
  921.     Ogre::Real planeX = getAttribReal(XMLNode, "planeX", 0);
  922.     Ogre::Real planeY = getAttribReal(XMLNode, "planeY", -1);
  923.     Ogre::Real planeZ = getAttribReal(XMLNode, "planeX", 0);
  924.     Ogre::Real planeD = getAttribReal(XMLNode, "planeD", 5000);
  925.     Ogre::Real scale = getAttribReal(XMLNode, "scale", 1000);
  926.     Ogre::Real bow = getAttribReal(XMLNode, "bow", 0);
  927.     Ogre::Real tiling = getAttribReal(XMLNode, "tiling", 10);
  928.     bool drawFirst = getAttribBool(XMLNode, "drawFirst", true);
  929.  
  930.     // Setup the sky plane
  931.     Ogre::Plane plane;
  932.     plane.normal = Ogre::Vector3(planeX, planeY, planeZ);
  933.     plane.d = planeD;
  934.     mSceneMgr->setSkyPlane(true, plane, material, scale, tiling, drawFirst, bow, 1, 1, m_sGroupName);
  935. }
  936.  
  937. void DotSceneLoader::processClipping(rapidxml::xml_node<>* XMLNode)
  938. {
  939.     //! @todo Implement this
  940.  
  941.     // Process attributes
  942.     Ogre::Real fNear = getAttribReal(XMLNode, "near", 0);
  943.     Ogre::Real fFar = getAttribReal(XMLNode, "far", 1);
  944. }
  945.  
  946. void DotSceneLoader::processLightRange(rapidxml::xml_node<>* XMLNode, Ogre::Light *pLight)
  947. {
  948.     // Process attributes
  949.     Ogre::Real inner = getAttribReal(XMLNode, "inner");
  950.     Ogre::Real outer = getAttribReal(XMLNode, "outer");
  951.     Ogre::Real falloff = getAttribReal(XMLNode, "falloff", 1.0);
  952.  
  953.     // Setup the light range
  954.     pLight->setSpotlightRange(Ogre::Angle(inner), Ogre::Angle(outer), falloff);
  955. }
  956.  
  957. void DotSceneLoader::processLightAttenuation(rapidxml::xml_node<>* XMLNode, Ogre::Light *pLight)
  958. {
  959.     // Process attributes
  960.     Ogre::Real range = getAttribReal(XMLNode, "range");
  961.     Ogre::Real constant = getAttribReal(XMLNode, "constant");
  962.     Ogre::Real linear = getAttribReal(XMLNode, "linear");
  963.     Ogre::Real quadratic = getAttribReal(XMLNode, "quadratic");
  964.  
  965.     // Setup the light attenuation
  966.     pLight->setAttenuation(range, constant, linear, quadratic);
  967. }
  968.  
  969.  
  970. Ogre::String DotSceneLoader::getAttrib(rapidxml::xml_node<>* XMLNode, const Ogre::String &attrib, const Ogre::String &defaultValue)
  971. {
  972.     if(XMLNode->first_attribute(attrib.c_str()))
  973.         return XMLNode->first_attribute(attrib.c_str())->value();
  974.     else
  975.         return defaultValue;
  976. }
  977.  
  978. Ogre::Real DotSceneLoader::getAttribReal(rapidxml::xml_node<>* XMLNode, const Ogre::String &attrib, Ogre::Real defaultValue)
  979. {
  980.     if(XMLNode->first_attribute(attrib.c_str()))
  981.         return Ogre::StringConverter::parseReal(XMLNode->first_attribute(attrib.c_str())->value());
  982.     else
  983.         return defaultValue;
  984. }
  985.  
  986. bool DotSceneLoader::getAttribBool(rapidxml::xml_node<>* XMLNode, const Ogre::String &attrib, bool defaultValue)
  987. {
  988.     if(!XMLNode->first_attribute(attrib.c_str()))
  989.         return defaultValue;
  990.  
  991.     if(Ogre::String(XMLNode->first_attribute(attrib.c_str())->value()) == "true")
  992.         return true;
  993.  
  994.     return false;
  995. }
  996.  
  997. Ogre::Vector3 DotSceneLoader::parseVector3(rapidxml::xml_node<>* XMLNode)
  998. {
  999.     return Ogre::Vector3(
  1000.         Ogre::StringConverter::parseReal(XMLNode->first_attribute("x")->value()),
  1001.         Ogre::StringConverter::parseReal(XMLNode->first_attribute("y")->value()),
  1002.         Ogre::StringConverter::parseReal(XMLNode->first_attribute("z")->value())
  1003.         );
  1004. }
  1005.  
  1006. Ogre::Quaternion DotSceneLoader::parseQuaternion(rapidxml::xml_node<>* XMLNode)
  1007. {
  1008.     //! @todo Fix this crap!
  1009.  
  1010.     Ogre::Quaternion orientation;
  1011.  
  1012.     if(XMLNode->first_attribute("qx"))
  1013.     {
  1014.         orientation.x = Ogre::StringConverter::parseReal(XMLNode->first_attribute("qx")->value());
  1015.         orientation.y = Ogre::StringConverter::parseReal(XMLNode->first_attribute("qy")->value());
  1016.         orientation.z = Ogre::StringConverter::parseReal(XMLNode->first_attribute("qz")->value());
  1017.         orientation.w = Ogre::StringConverter::parseReal(XMLNode->first_attribute("qw")->value());
  1018.     }
  1019.     if(XMLNode->first_attribute("qw"))
  1020.     {
  1021.         orientation.w = Ogre::StringConverter::parseReal(XMLNode->first_attribute("qw")->value());
  1022.         orientation.x = Ogre::StringConverter::parseReal(XMLNode->first_attribute("qx")->value());
  1023.         orientation.y = Ogre::StringConverter::parseReal(XMLNode->first_attribute("qy")->value());
  1024.         orientation.z = Ogre::StringConverter::parseReal(XMLNode->first_attribute("qz")->value());
  1025.     }
  1026.     else if(XMLNode->first_attribute("axisX"))
  1027.     {
  1028.         Ogre::Vector3 axis;
  1029.         axis.x = Ogre::StringConverter::parseReal(XMLNode->first_attribute("axisX")->value());
  1030.         axis.y = Ogre::StringConverter::parseReal(XMLNode->first_attribute("axisY")->value());
  1031.         axis.z = Ogre::StringConverter::parseReal(XMLNode->first_attribute("axisZ")->value());
  1032.         Ogre::Real angle = Ogre::StringConverter::parseReal(XMLNode->first_attribute("angle")->value());;
  1033.         orientation.FromAngleAxis(Ogre::Angle(angle), axis);
  1034.     }
  1035.     else if(XMLNode->first_attribute("angleX"))
  1036.     {
  1037.         Ogre::Vector3 axis;
  1038.         axis.x = Ogre::StringConverter::parseReal(XMLNode->first_attribute("angleX")->value());
  1039.         axis.y = Ogre::StringConverter::parseReal(XMLNode->first_attribute("angleY")->value());
  1040.         axis.z = Ogre::StringConverter::parseReal(XMLNode->first_attribute("angleZ")->value());
  1041.         //orientation.FromAxes(&axis);
  1042.         //orientation.F
  1043.     }
  1044.     else if(XMLNode->first_attribute("x"))
  1045.     {
  1046.         orientation.x = Ogre::StringConverter::parseReal(XMLNode->first_attribute("x")->value());
  1047.         orientation.y = Ogre::StringConverter::parseReal(XMLNode->first_attribute("y")->value());
  1048.         orientation.z = Ogre::StringConverter::parseReal(XMLNode->first_attribute("z")->value());
  1049.         orientation.w = Ogre::StringConverter::parseReal(XMLNode->first_attribute("w")->value());
  1050.     }
  1051.     else if(XMLNode->first_attribute("w"))
  1052.     {
  1053.         orientation.w = Ogre::StringConverter::parseReal(XMLNode->first_attribute("w")->value());
  1054.         orientation.x = Ogre::StringConverter::parseReal(XMLNode->first_attribute("x")->value());
  1055.         orientation.y = Ogre::StringConverter::parseReal(XMLNode->first_attribute("y")->value());
  1056.         orientation.z = Ogre::StringConverter::parseReal(XMLNode->first_attribute("z")->value());
  1057.     }
  1058.  
  1059.     return orientation;
  1060. }
  1061.  
  1062. Ogre::ColourValue DotSceneLoader::parseColour(rapidxml::xml_node<>* XMLNode)
  1063. {
  1064.     return Ogre::ColourValue(
  1065.         Ogre::StringConverter::parseReal(XMLNode->first_attribute("r")->value()),
  1066.         Ogre::StringConverter::parseReal(XMLNode->first_attribute("g")->value()),
  1067.         Ogre::StringConverter::parseReal(XMLNode->first_attribute("b")->value()),
  1068.         XMLNode->first_attribute("a") != NULL ? Ogre::StringConverter::parseReal(XMLNode->first_attribute("a")->value()) : 1
  1069.         );
  1070. }
  1071.  
  1072. Ogre::String DotSceneLoader::getProperty(const Ogre::String &ndNm, const Ogre::String &prop)
  1073. {
  1074.     for ( unsigned int i = 0 ; i < nodeProperties.size(); i++ )
  1075.     {
  1076.         if ( nodeProperties[i].nodeName == ndNm && nodeProperties[i].propertyNm == prop )
  1077.         {
  1078.             return nodeProperties[i].valueName;
  1079.         }
  1080.     }
  1081.  
  1082.     return "";
  1083. }
  1084.  
  1085. void DotSceneLoader::processUserDataReference(rapidxml::xml_node<>* XMLNode, Ogre::Entity *pEntity)
  1086. {
  1087.     Ogre::String str = XMLNode->first_attribute("id")->value();
  1088.     pEntity->setUserAny(Ogre::Any(str));
  1089. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement