source: GTP/trunk/Lib/Illum/IBRBillboardCloudTrees/OGRE/src/LBBCLeafDistributionGenerator.cpp @ 751

Revision 751, 10.5 KB checked in by igarcia, 18 years ago (diff)
Line 
1
2#include "LBBCLeafDistributionGenerator.h"
3
4#include <BBC.h>
5
6namespace LBBC {
7
8LeafDistributionGenerator::LeafDistributionGenerator()
9{
10        mEntity = BBC::EntityPtr(new BBC::Entity());
11}
12
13LeafDistributionGenerator::~LeafDistributionGenerator()
14{
15}
16
17void LeafDistributionGenerator::generate()
18{
19        BBC::Timer leafDistributionTimer;
20        leafDistributionTimer.start();
21        leafDistributionTimer.printStartTime();
22
23        mEntity->loadMesh(true);
24
25        unsigned int posNorm = 0;
26        unsigned int negNorm = 0;
27       
28        std::vector<Ogre::Vector3>::iterator iFaces01;
29        std::vector<Ogre::Vector3>::iterator iFaces02;
30        std::vector<Ogre::Vector2>::iterator iVertexs;
31
32        std::vector<float> vD,vNx,vNy,vNz;
33       
34        unsigned int iV101,iV201,iV301;
35        unsigned int iV102,iV202,iV302;
36        Ogre::Vector3 v101,v201,v301;
37        Ogre::Vector3 v102,v202,v302;
38               
39        for (unsigned int iF01 = 0; iF01 < mEntity->getSubEntity(0)->getNumFaces(); iF01++)
40        {
41                Ogre::Vector3 vFace01 = mEntity->getSubEntity(0)->getFaceVerticesIDs(iF01);
42
43                iV101 = vFace01[0];
44                iV201 = vFace01[1];
45                iV301 = vFace01[2];
46
47                v101 = mEntity->getSubEntity(0)->getUniqueVertex(iV101).position;
48                v201 = mEntity->getSubEntity(0)->getUniqueVertex(iV201).position;
49                v301 = mEntity->getSubEntity(0)->getUniqueVertex(iV301).position;
50
51                for (unsigned int iF02 = 0; iF02 < mEntity->getSubEntity(0)->getNumFaces(); iF02++)
52                {
53                        Ogre::Vector3 vFace02 = mEntity->getSubEntity(0)->getFaceVerticesIDs(iF02);
54                        iV102 = vFace02[0];
55                        iV202 = vFace02[1];
56                        iV302 = vFace02[2];
57
58                        v102 = mEntity->getSubEntity(0)->getUniqueVertex(iV102).position;
59                        v202 = mEntity->getSubEntity(0)->getUniqueVertex(iV202).position;
60                        v302 = mEntity->getSubEntity(0)->getUniqueVertex(iV302).position;
61
62                       
63                       
64                        //Ogre::LogManager::getSingleton().logMessage("face A: " + Ogre::StringConverter::toString(iF01));                                     
65                        //Ogre::LogManager::getSingleton().logMessage("indices A:" + Ogre::StringConverter::toString(vFace01));
66                        //Ogre::LogManager::getSingleton().logMessage("face B: " + Ogre::StringConverter::toString(iF02));                                     
67                        //Ogre::LogManager::getSingleton().logMessage("indices B:" + Ogre::StringConverter::toString(vFace02));
68
69                        if ((iF01 != iF02) &&
70                                (
71                                ((v101 == v102) && (v201 == v202)) ||
72                                ((v101 == v202) && (v201 == v102)) ||
73                                ((v101 == v102) && (v201 == v302)) ||
74                                ((v101 == v302) && (v201 == v102)) ||
75                                ((v101 == v202) && (v201 == v302)) ||
76                                ((v101 == v302) && (v201 == v202)) ||
77                                //-----------------------------------
78                                ((v201 == v102) && (v301 == v202)) ||
79                                ((v201 == v202) && (v301 == v102)) ||
80                                ((v201 == v102) && (v301 == v302)) ||
81                                ((v201 == v302) && (v301 == v102)) ||
82                                ((v201 == v202) && (v301 == v302)) ||
83                                ((v201 == v302) && (v301 == v202)) ||
84                                //-----------------------------------
85                                ((v101 == v102) && (v301 == v202)) ||
86                                ((v101 == v202) && (v301 == v102)) ||
87                                ((v101 == v102) && (v301 == v302)) ||
88                                ((v101 == v302) && (v301 == v102)) ||
89                                ((v101 == v202) && (v301 == v302)) ||
90                                ((v101 == v302) && (v301 == v202))
91                                )
92                                )
93                        {                       
94                                if (mEntityDistribution->getNumEntities() == 0)
95                                {       
96                                        // Instance new leaf
97                                        Leaf *leaf = new Leaf();
98                                       
99                                        leaf->setEntityHandle(mEntityDistribution->getNumEntities());
100                                        leaf->getSubEntity(0)->addTextureCoordSet(2);
101
102                                        leaf->addFace(iF01);
103                                        leaf->addFaceInfo(
104                                                        mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(iF01).x),
105                                                        mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(iF01).y),
106                                                        mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(iF01).z));
107                                       
108                                        mEntityDistribution->addEntity(leaf);
109
110                                        //Ogre::LogManager::getSingleton().logMessage("leaf:" + Ogre::StringConverter::toString(leaf->getEntityHandle()));
111                                        //Ogre::LogManager::getSingleton().logMessage("indices:" + Ogre::StringConverter::toString(vFace01));
112                                        //Ogre::LogManager::getSingleton().logMessage("face stored: " + Ogre::StringConverter::toString(iF01));                                 
113                                        //Ogre::LogManager::getSingleton().logMessage("face stored: " + Ogre::StringConverter::toString(leaf->getFace(0)));
114                                }
115                                else
116                                {
117                                        unsigned int j = 0;
118                                        bool leaveFound = false;
119
120                                        // Check if the face is in one of the Leaf sets...
121                                        while ((j < mEntityDistribution->getNumEntities()) && (!leaveFound))
122                                        {                                               
123                                                Leaf *leaf = (Leaf *)mEntityDistribution->getEntity(j).get();
124                                                if (leaf->hasFace(iF01))
125                                                {
126                                                        leaveFound = true;
127                                                        leaf->addFace(iF02);
128                                                        leaf->addFaceInfo(
129                                                                        mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(iF02).x),
130                                                                        mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(iF02).y),
131                                                                        mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(iF02).z));
132
133                                                        //Ogre::LogManager::getSingleton().logMessage("leaf:" + Ogre::StringConverter::toString(leaf->getEntityHandle()));
134                                                        //Ogre::LogManager::getSingleton().logMessage("indices:" + Ogre::StringConverter::toString(vFace02));
135                                                        //Ogre::LogManager::getSingleton().logMessage("face stored: " + Ogre::StringConverter::toString(iF02));                                 
136                                                        //Ogre::LogManager::getSingleton().logMessage("face stored: " + Ogre::StringConverter::toString(leaf->getFace(0)));
137                                                }
138                                                else
139                                                {                                                       
140                                                        if (leaf->hasFace(iF02))
141                                                        {
142                                                                leaveFound = true;
143                                                                leaf->addFace(iF01);
144                                                                leaf->addFaceInfo(
145                                                                                mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(iF01).x),
146                                                                                mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(iF01).y),
147                                                                                mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(iF01).z));
148
149                                                                //Ogre::LogManager::getSingleton().logMessage("leaf:" + Ogre::StringConverter::toString(leaf->getEntityHandle()));
150                                                                //Ogre::LogManager::getSingleton().logMessage("indices:" + Ogre::StringConverter::toString(vFace01));
151                                                                //Ogre::LogManager::getSingleton().logMessage("face stored: " + Ogre::StringConverter::toString(iF01));                                 
152                                                                //Ogre::LogManager::getSingleton().logMessage("face stored: " + Ogre::StringConverter::toString(leaf->getFace(0)));
153                                                        }
154                                                }
155                                                j++;
156                                        }
157
158                                        if (!leaveFound)
159                                        {
160                                                // Instance new leaf
161                                                Leaf *leaf = new Leaf();
162                                               
163                                                leaf->setEntityHandle(mEntityDistribution->getNumEntities());
164                                                leaf->getSubEntity(0)->addTextureCoordSet(2);
165                                                leaf->addFace(iF01);
166                                                leaf->addFaceInfo(
167                                                                mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(iF01).x),
168                                                                mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(iF01).y),
169                                                                mEntity->getSubEntity(0)->getUniqueVertex(mEntity->getSubEntity(0)->getFaceVerticesIDs(iF01).z));
170                                               
171                                                mEntityDistribution->addEntity(leaf);
172
173                                                //Ogre::LogManager::getSingleton().logMessage("leaf:" + Ogre::StringConverter::toString(leaf->getEntityHandle()));
174                                                //Ogre::LogManager::getSingleton().logMessage("indices:" + Ogre::StringConverter::toString(vFace01));
175                                                //Ogre::LogManager::getSingleton().logMessage("face stored: " + Ogre::StringConverter::toString(iF01));                                 
176                                                //Ogre::LogManager::getSingleton().logMessage("face stored: " + Ogre::StringConverter::toString(leaf->getFace(0)));                                     
177                                                //cin.get();
178                                        }
179                                }
180                        }                                               
181                }
182        }       
183
184        // Generate the average normal, leaf position, and d leaf plane parameter for each leaf...
185        for (unsigned int ileaf = 0; ileaf < mEntityDistribution->getNumEntities(); ileaf++)
186        {
187                Leaf *leaf = (Leaf*) mEntityDistribution->getEntity(ileaf).get();
188                generateAverageLeafPosition(leaf);
189        generateAverageLeafNormal(leaf);
190                leaf->setLeafD(-leaf->getLeafNormal().dotProduct(leaf->getPosition()));
191        }
192
193        // Compute the max and min d leaf plane parameter, and the max and min normal value...
194        float minD;
195        float maxD;
196        Ogre::Vector3 minNormal;
197        Ogre::Vector3 maxNormal;
198
199        generateNormalDThreshold(minD, maxD, minNormal, maxNormal);
200
201        ((LeafDistribution*)mEntityDistribution)->setMaxD(maxD);
202        ((LeafDistribution*)mEntityDistribution)->setMinD(minD);
203        ((LeafDistribution*)mEntityDistribution)->setMaxNormal(maxNormal);
204        ((LeafDistribution*)mEntityDistribution)->setMinNormal(minNormal);
205
206        leafDistributionTimer.stop();
207        leafDistributionTimer.printStopTime();
208}
209
210void LeafDistributionGenerator::generateNormalDThreshold(float &minD, float &maxD, Ogre::Vector3 &minNormal, Ogre::Vector3 &maxNormal)
211{
212        float dMin = FLT_MAX;
213        float dMax = -FLT_MAX;
214
215        float nXMax = -1;
216        float nYMax = -1;
217        float nZMax = -1;
218        float nYMin = 1;
219        float nXMin = 1;                 
220        float nZMin = 1;
221
222        for (unsigned int iD = 0; iD < mEntityDistribution->getNumEntities(); iD++)
223        {               
224                Leaf *leaf = (Leaf *)mEntityDistribution->getEntity(iD).get();         
225                if (leaf->getLeafD() < dMin)
226                {
227                        dMin = leaf->getLeafD();
228                }
229                       
230                if (leaf->getLeafD() > dMax)
231                {
232                        dMax = leaf->getLeafD();
233                }
234                //-------------------- 
235                if (leaf->getLeafNormal().x < nXMin)
236                {
237                        nXMin = leaf->getLeafNormal().x;
238                }
239               
240                if (leaf->getLeafNormal().x > nXMax)
241                {
242                        nXMax = leaf->getLeafNormal().x;
243                }
244                //--------------------
245                if (leaf->getLeafNormal().y < nYMin)
246                {
247                        nYMin = leaf->getLeafNormal().y;
248                }
249               
250                if (leaf->getLeafNormal().y > nYMax)
251                {
252                        nYMax = leaf->getLeafNormal().y;
253                }
254                //--------------------
255                if (leaf->getLeafNormal().z < nZMin)
256                {
257                        nZMin = leaf->getLeafNormal().z;
258                }
259               
260                if (leaf->getLeafNormal().z > nZMax)
261                {
262                        nZMax = leaf->getLeafNormal().z;
263                }               
264        }
265        minNormal = Ogre::Vector3(nXMin, nYMin, nZMin);
266        maxNormal = Ogre::Vector3(nXMax, nYMax, nZMax);
267       
268        minD = dMin;
269        maxD = dMax;
270}
271
272void LeafDistributionGenerator::generateAverageLeafNormal(Leaf *leaf)
273{
274        Ogre::Vector3 aVnormal(0,0,0);
275        for (unsigned int iN = 0; iN < leaf->getNumLeafNormals(); iN++)
276        {
277                aVnormal = aVnormal + leaf->getFaceNormal(iN);
278        }
279        aVnormal = aVnormal / leaf->getNumLeafNormals();
280        aVnormal.normalise();
281
282        leaf->setLeafNormal(aVnormal);
283}
284
285void LeafDistributionGenerator::generateAverageLeafPosition(Leaf *leaf)
286{
287        Ogre::Vector3 aVPosition(0,0,0);
288        for (unsigned int iN = 0; iN < leaf->getSubEntity(0)->getNumVertices(); iN++)
289        {
290                aVPosition = aVPosition + leaf->getSubEntity(0)->getPosition(iN);
291        }
292        aVPosition = aVPosition / leaf->getSubEntity(0)->getNumVertices();
293
294        leaf->setPosition(aVPosition);
295}
296
297}
Note: See TracBrowser for help on using the repository browser.