source: GTP/trunk/Lib/Illum/IBRBillboardCloudTrees/OGRE/src/LBBCLeaf.cpp @ 721

Revision 721, 6.2 KB checked in by igarcia, 19 years ago (diff)
Line 
1
2#include "LBBCLeaf.h"
3
4namespace LBBC {
5
6Leaf::Leaf()
7{
8}
9
10Leaf::~Leaf()
11{
12
13}
14
15void Leaf::setPlaneD(float value) {
16        mPlaneD = value;
17}
18
19float Leaf::getPlaneD() {
20        return mPlaneD;
21}
22
23void Leaf::setPlaneNormal(Ogre::Vector3 value) {
24}
25
26Ogre::Vector3 Leaf::getPlaneNormal() {
27        return mPlaneNormal;
28}
29
30void Leaf::setPosition(Ogre::Vector3 value) {
31        mPosition = value;
32}
33
34Ogre::Vector3 Leaf::getPosition() {
35        return mPosition;
36}
37
38Ogre::Quaternion Leaf::getOrientation() {
39        return mOrientation;
40}
41
42void Leaf::setOrientation(const Ogre::Quaternion & orientation) {
43        mOrientation = orientation;
44}
45
46void Leaf::addFaceNormal(Ogre::Vector3 value)
47{
48        mFacesNormals.push_back(value);
49}
50
51void Leaf::removeFaceNormal(unsigned int value)
52{
53        mFacesNormals.erase(mFacesNormals.begin()+value);
54}
55
56Ogre::Vector3 Leaf::getFaceNormal(unsigned int value)
57{
58        return mFacesNormals[value];
59}
60
61bool Leaf::hasFace(unsigned int value)
62{
63        bool hasFace = false;
64
65        if (mFacesEntitySet.size() > 0)
66        {
67                hasFace = mFacesEntitySet.find(value) != mFacesEntitySet.end();
68        }
69        return hasFace;
70}
71
72void Leaf::addFace(unsigned int value)
73{
74        mFacesEntitySet.insert(value);
75}
76
77void Leaf::removeFace(unsigned int value)
78{
79        mFacesEntitySet.erase(value);
80}
81
82unsigned int Leaf::getFace(unsigned int value)
83{
84        ID_FACESet::iterator itFacesEntitySet;
85        itFacesEntitySet = mFacesEntitySet.begin();
86
87        unsigned int iElem = 0;
88        while ((iElem < value) && (value < mFacesEntitySet.size()))
89        {
90                itFacesEntitySet++;
91                iElem++;
92        }       
93        return static_cast<unsigned int>(* itFacesEntitySet);
94}
95
96unsigned int Leaf::getNumFaces()
97{
98        return static_cast<unsigned int>(mFacesEntitySet.size());       
99}
100
101unsigned int Leaf::getNumLeafNormals()
102{
103        return static_cast<unsigned int>(mFacesNormals.size());
104}
105
106void Leaf::setLeafNormal(Ogre::Vector3 normal)
107{
108        mLeafNormal = normal;
109}
110
111Ogre::Vector3 Leaf::getLeafNormal()
112{
113        return mLeafNormal;
114}
115
116void Leaf::setLeafD(float leafD)
117{
118        mLeafD = leafD;
119}
120
121float Leaf::getLeafD()
122{
123        return mLeafD;
124}
125
126void Leaf::addFaceInfo(BBC::UniqueVertex v0, BBC::UniqueVertex v1, BBC::UniqueVertex v2)
127{
128        // Add vertex 0 from the first face     
129        getSubEntity(0)->addUniqueVertex(v0);                                   
130       
131        // Add vertex 1 from the first face
132        getSubEntity(0)->addUniqueVertex(v1);
133
134        // Add vertex 2 from the first face
135        getSubEntity(0)->addUniqueVertex(v2);
136
137        unsigned int numIndices = getSubEntity(0)->getNumFaces() * 3;
138        Ogre::Vector3 indices = Ogre::Vector3(numIndices, numIndices + 1, numIndices + 2);
139        getSubEntity(0)->addFaceVerticesIDs(indices);
140
141        Ogre::Vector3 p1 = v0.position;
142        Ogre::Vector3 p2 = v1.position;
143        Ogre::Vector3 p3 = v2.position;
144        Ogre::Vector3 v21 = p2 - p1;
145        Ogre::Vector3 v31 = p3 - p1;
146        Ogre::Vector3 normal = v21.crossProduct(v31);
147
148        //if (dotProd < 0)
149        if (normal[2] < 0)
150        {
151                normal = v31.crossProduct(v21);
152                normal.normalise();
153        }
154
155        addFaceNormal(normal);
156}
157
158void Leaf::readLeaf(TiXmlNode *leafNode)
159{
160        unsigned int id = Ogre::StringConverter::parseInt(leafNode->ToElement()->Attribute("id"));
161        setEntityHandle(id);
162
163        Ogre::Vector3 normal;
164        TiXmlNode *coord4dNode = leafNode->FirstChild("coord4d");
165        normal.x = Ogre::StringConverter::parseReal(coord4dNode->ToElement()->Attribute("nx"));
166        normal.y = Ogre::StringConverter::parseReal(coord4dNode->ToElement()->Attribute("ny"));
167        normal.z = Ogre::StringConverter::parseReal(coord4dNode->ToElement()->Attribute("nz"));
168        float d = Ogre::StringConverter::parseReal(coord4dNode->ToElement()->Attribute("d"));   
169
170        Ogre::Vector3 position;
171        TiXmlNode *positionNode = leafNode->FirstChild("position");
172        position.x = Ogre::StringConverter::parseReal(positionNode->ToElement()->Attribute("x"));
173        position.y = Ogre::StringConverter::parseReal(positionNode->ToElement()->Attribute("y"));
174        position.z = Ogre::StringConverter::parseReal(positionNode->ToElement()->Attribute("z"));       
175
176        setLeafNormal(normal);
177        setPosition(position);
178        setLeafD(d);
179
180        TiXmlNode *facesNode = leafNode->FirstChild("faces");
181        unsigned int count = Ogre::StringConverter::parseInt(facesNode->ToElement()->Attribute("count"));
182
183        for (TiXmlNode *faceNode = facesNode->FirstChild("face"); faceNode; faceNode = faceNode->NextSibling() )
184        {
185                unsigned int id = Ogre::StringConverter::parseInt(faceNode->ToElement()->Attribute("id"));
186                addFace(id);
187        }
188}
189
190void Leaf::writeLeaf(TiXmlNode *leafNode)
191{       
192        leafNode->ToElement()->SetAttribute(Ogre::String("id"),Ogre::StringConverter::toString(getEntityHandle()));
193       
194        TiXmlNode *coord4dNode = leafNode->ToElement()->InsertEndChild(TiXmlElement(Ogre::String("coord4d").c_str()))->ToElement();
195
196        Ogre::String sNx = Ogre::StringConverter::toString(getLeafNormal().x);
197        Ogre::String sNy = Ogre::StringConverter::toString(getLeafNormal().y);
198        Ogre::String sNz = Ogre::StringConverter::toString(getLeafNormal().z);
199        Ogre::String sD = Ogre::StringConverter::toString(getLeafD());
200
201        coord4dNode->ToElement()->SetAttribute("nx", sNx.c_str());
202        coord4dNode->ToElement()->SetAttribute("ny", sNy.c_str());
203        coord4dNode->ToElement()->SetAttribute("nz", sNz.c_str());
204        coord4dNode->ToElement()->SetAttribute("d", sD.c_str());
205
206        TiXmlNode *positionNode = leafNode->ToElement()->InsertEndChild(TiXmlElement(Ogre::String("position").c_str()))->ToElement();           
207
208        Ogre::String sCoordX = Ogre::StringConverter::toString(getPosition().x);
209        Ogre::String sCoordY = Ogre::StringConverter::toString(getPosition().y);
210        Ogre::String sCoordZ = Ogre::StringConverter::toString(getPosition().z);
211
212        positionNode->ToElement()->SetAttribute(Ogre::String("x").c_str(), sCoordX.c_str());
213        positionNode->ToElement()->SetAttribute(Ogre::String("y").c_str(), sCoordY.c_str());
214        positionNode->ToElement()->SetAttribute(Ogre::String("z").c_str(), sCoordZ.c_str());
215        TiXmlNode *facesNode;
216        for (unsigned int i = 0; i < getNumFaces(); i++)
217        {
218                if (i == 0)
219                {
220                        facesNode = leafNode->ToElement()->InsertEndChild(TiXmlElement(Ogre::String("faces").c_str()))->ToElement();
221
222                        Ogre::String sFace = Ogre::StringConverter::toString(getNumFaces());
223                        facesNode->ToElement()->SetAttribute(Ogre::String("count").c_str(), sFace.c_str());
224                }
225               
226                TiXmlNode *faceNode = facesNode->ToElement()->InsertEndChild(TiXmlElement(Ogre::String("face").c_str()))->ToElement();
227                Ogre::String sId = Ogre::StringConverter::toString(getFace(i));
228                faceNode->ToElement()->SetAttribute("id", sId.c_str());         
229        }
230}
231
232}
Note: See TracBrowser for help on using the repository browser.