source: GTP/trunk/Lib/Illum/IBRBillboardCloudTrees/OGRE/src/LBBCLeafDistributionSerializer.cpp @ 745

Revision 745, 4.4 KB checked in by igarcia, 19 years ago (diff)
Line 
1
2#include "LBBCLeafDistributionSerializer.h"
3
4namespace LBBC {
5
6LeafDistributionSerializer::LeafDistributionSerializer()
7                                                   : BBC::EntityDistributionSerializer()
8{
9
10}
11
12LeafDistributionSerializer::~LeafDistributionSerializer()
13{
14
15}
16
17void LeafDistributionSerializer::setLeafDistribution(LeafDistribution *value)
18{
19        mLeafDistribution = value;
20}
21
22LeafDistribution* LeafDistributionSerializer::getLeafDistribution()
23{
24        return mLeafDistribution;
25}
26
27void LeafDistributionSerializer::readLeafDistribution(TiXmlDocument *document)
28{
29        mEntity->loadMesh(true);
30       
31        TiXmlNode *parentNode = document->FirstChild("tree");
32        TiXmlNode *leavesNode = parentNode->FirstChild("leaves");
33
34        unsigned numEntities = Ogre::StringConverter::parseInt(leavesNode->ToElement()->Attribute("count"));
35        float dMin = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("dMin"));
36        float dMax = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("dMax"));
37        float nXMin = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("nXMin"));
38        float nXMax = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("nXMax"));
39        float nYMin = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("nYMin"));
40        float nYMax = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("nYMax"));
41        float nZMin = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("nZMin"));
42        float nZMax = Ogre::StringConverter::parseReal(leavesNode->ToElement()->Attribute("nZMax"));
43
44        mLeafDistribution->setMinD(dMin);
45        mLeafDistribution->setMaxD(dMax);
46        mLeafDistribution->setMinNormal(Ogre::Vector3(nXMin,nYMin,nZMin));
47        mLeafDistribution->setMaxNormal(Ogre::Vector3(nXMax,nYMax,nZMax));
48
49        for (TiXmlNode *leafNode = leavesNode->FirstChild("leaf"); leafNode; leafNode = leafNode->NextSibling() )
50        {
51                Leaf *leaf = new Leaf();               
52                leaf->readLeaf(leafNode);                               
53                addFacesInfo(leaf);
54                mLeafDistribution->addEntity(leaf);                             
55        }
56}
57
58void LeafDistributionSerializer::addFacesInfo(Leaf *leaf)
59{
60        for (unsigned int iFace = 0; iFace < leaf->getNumFaces(); iFace++)
61        {               
62                leaf->addFaceInfo(
63                        mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(leaf->getFace(iFace)).x),
64                        mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(leaf->getFace(iFace)).y),
65                        mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(leaf->getFace(iFace)).z));                                                       
66        }
67}
68
69void LeafDistributionSerializer::writeLeafDistribution(TiXmlDocument *document)
70{
71        TiXmlNode *parentNode = document->InsertEndChild(TiXmlElement("tree"))->ToElement();
72
73        TiXmlNode *leavesNode = parentNode->ToElement()->InsertEndChild(TiXmlElement(Ogre::String("leaves").c_str()))->ToElement();
74        Ogre::String sLeaves = Ogre::StringConverter::toString(mLeafDistribution->getNumEntities());   
75        Ogre::String sDmin = Ogre::StringConverter::toString(mLeafDistribution->getMinD());
76        Ogre::String sDmax = Ogre::StringConverter::toString(mLeafDistribution->getMaxD());
77        Ogre::String sNxMin = Ogre::StringConverter::toString(mLeafDistribution->getMinNormal().x);
78        Ogre::String sNxMax = Ogre::StringConverter::toString(mLeafDistribution->getMaxNormal().x);
79        Ogre::String sNyMin = Ogre::StringConverter::toString(mLeafDistribution->getMinNormal().y);
80        Ogre::String sNyMax = Ogre::StringConverter::toString(mLeafDistribution->getMaxNormal().y);
81        Ogre::String sNzMin = Ogre::StringConverter::toString(mLeafDistribution->getMinNormal().z);
82        Ogre::String sNzMax = Ogre::StringConverter::toString(mLeafDistribution->getMaxNormal().z);
83
84        leavesNode->ToElement()->SetAttribute("count",sLeaves.c_str());
85        leavesNode->ToElement()->SetAttribute("dMin",sDmin.c_str());
86        leavesNode->ToElement()->SetAttribute("dMax",sDmax.c_str());
87        leavesNode->ToElement()->SetAttribute("nXMin",sNxMin.c_str());
88        leavesNode->ToElement()->SetAttribute("nXMax",sNxMax.c_str());
89        leavesNode->ToElement()->SetAttribute("nYMin",sNyMin.c_str());
90        leavesNode->ToElement()->SetAttribute("nYMax",sNyMax.c_str());
91        leavesNode->ToElement()->SetAttribute("nZMin",sNzMin.c_str());
92        leavesNode->ToElement()->SetAttribute("nZMax",sNzMax.c_str());
93
94        for (unsigned int k=0; k < mLeafDistribution->getNumEntities(); k++)
95        {
96                Leaf *leaf = (Leaf *)mLeafDistribution->getEntity(k).get();
97
98                TiXmlNode *leafNode = leavesNode->ToElement()->InsertEndChild(TiXmlElement(Ogre::String("leaf").c_str()))->ToElement();
99                leaf->writeLeaf(leafNode);     
100        }
101}
102
103}
Note: See TracBrowser for help on using the repository browser.