1 | |
---|
2 | #include "LBBCManager.h" |
---|
3 | |
---|
4 | namespace LBBC { |
---|
5 | |
---|
6 | Manager::Manager() |
---|
7 | : BBC::Manager() |
---|
8 | { |
---|
9 | //mCf.load("lbbc.cfg"); |
---|
10 | |
---|
11 | mBillboardCloudSerializer = new KdTreeClusterSerializer(); |
---|
12 | mLeafDistributionSerializer = new LeafDistributionSerializer(); |
---|
13 | mLeafDistributionGenerator = new LeafDistributionGenerator(); |
---|
14 | mBillboardCloudGenerator = new KdTreeClusterGenerator(); |
---|
15 | mBillboardCloudUVMapper = new BBC::BillboardCloudUVMapper(); |
---|
16 | mOgreMeshSerializer = new BBC::OgreMeshSerializer(); |
---|
17 | } |
---|
18 | |
---|
19 | Manager::~Manager() |
---|
20 | { |
---|
21 | // How to manage the destruction of BBC::Manager? |
---|
22 | delete mBillboardCloudSerializer; |
---|
23 | delete mLeafDistributionSerializer; |
---|
24 | delete mLeafDistributionGenerator; |
---|
25 | delete mBillboardCloudGenerator; |
---|
26 | delete mBillboardCloudUVMapper; |
---|
27 | delete mOgreMeshSerializer; |
---|
28 | delete mLeaves; |
---|
29 | } |
---|
30 | |
---|
31 | SampleConfigFile* Manager::getSampleConfigFile() |
---|
32 | { |
---|
33 | return &mSampleCf; |
---|
34 | } |
---|
35 | void Manager::runSample(Ogre::String sampleConfigFile) |
---|
36 | { |
---|
37 | Ogre::LogManager::getSingleton().logMessage("Sample Config Filename:" + sampleConfigFile); |
---|
38 | |
---|
39 | mSampleCf.load(sampleConfigFile); |
---|
40 | mLeaves = new Leaves(); |
---|
41 | |
---|
42 | Ogre::Mesh* leavesMesh = BBC::Manager::getSingleton().loadMesh(
|
---|
43 | mSampleCf.getEntitiesMeshPrefixFilename() + ".mesh"); |
---|
44 | |
---|
45 | mLeaves->setMesh(leavesMesh); |
---|
46 | |
---|
47 | if (mSampleCf.getEntityDistributionGeneration()) |
---|
48 | { |
---|
49 | generateEntityDistribution(); |
---|
50 | } |
---|
51 | else |
---|
52 | { |
---|
53 | loadEntityDistribution(); |
---|
54 | } |
---|
55 | |
---|
56 | if (mSampleCf.getEntityClustersGeneration()) |
---|
57 | { |
---|
58 | generateEntityClusters(); |
---|
59 | } |
---|
60 | if (mSampleCf.getBillboardCloudGeneration())
|
---|
61 | {
|
---|
62 | generateBillboardCloud();
|
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | void Manager::createEntityDistribution() |
---|
67 | { |
---|
68 | mLeafDistributionGenerator->setDistribution(mLeaves->getDistribution()); |
---|
69 | mLeafDistributionGenerator->getEntity()->setMesh(mLeaves->getMesh()); |
---|
70 | mLeafDistributionGenerator->generate(); |
---|
71 | } |
---|
72 | |
---|
73 | void Manager::saveEntityDistributionSplitted() |
---|
74 | { |
---|
75 | BBC::Entity *entity; |
---|
76 | entity = new BBC::Entity(); |
---|
77 | |
---|
78 | if (mSampleCf.getEntityDistributionSplitted() || mSampleCf.getEntityDistributionMerged()) |
---|
79 | { |
---|
80 | BBC::EntityDistribution *entityDistribution; |
---|
81 | unsigned int numSerializedEntities = 0; |
---|
82 | |
---|
83 | |
---|
84 | entityDistribution = mLeaves->getDistribution(); |
---|
85 | for (unsigned int iEntity = 0; iEntity < entityDistribution->getNumEntities(); iEntity++) |
---|
86 | { |
---|
87 | BBC::SubEntity *subEntity = entityDistribution->getEntity(iEntity)->getSubEntity(0); |
---|
88 | |
---|
89 | if (iEntity == 0) |
---|
90 | { |
---|
91 | // Delete the default constructed subentity... |
---|
92 | entity->removeSubEntity(0); |
---|
93 | |
---|
94 | // Add our first subentity... |
---|
95 | entity->addSubEntity(subEntity); |
---|
96 | } |
---|
97 | else |
---|
98 | { |
---|
99 | entity->addSubEntity(subEntity); |
---|
100 | } |
---|
101 | |
---|
102 | entity->getSubEntity(numSerializedEntities)->setMaterialName(mSampleCf.getEntityDistributionMergedMaterialPrefixName()); |
---|
103 | numSerializedEntities++; |
---|
104 | } |
---|
105 | |
---|
106 | entity->setSubEntitiesDistinctVertexColours(); |
---|
107 | |
---|
108 | if (mSampleCf.getEntityDistributionSplitted()) |
---|
109 | { |
---|
110 | mOgreMeshSerializer->setEntity(entity); |
---|
111 | mOgreMeshSerializer->exportMesh(mSampleCf.getEntityClustersFolder() + |
---|
112 | mSampleCf.getEntityClustersMeshPrefixFilename() + "Splitted.mesh", false, false); |
---|
113 | |
---|
114 | mMeshSerializer->exportMesh(entity->getMesh(), mSampleCf.getEntityDistributionFolder() + mSampleCf.getEntitiesMeshPrefixFilename() + "Splitted.mesh"); |
---|
115 | //mXMLMeshSerializer->exportMesh(entity->getMesh(), mSampleCf.getEntityClustersFolder() + mSampleCf.getEntitiesMeshPrefixFilename() + "Splitted.mesh" + ".xml"); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | if (mSampleCf.getEntityDistributionMerged()) |
---|
120 | { |
---|
121 | entity->mergeSubEntities(); |
---|
122 | entity->getSubEntity(0)->setMaterialName(mSampleCf.getEntityDistributionMergedMaterialPrefixName());
|
---|
123 | |
---|
124 | mOgreMeshSerializer->setEntity(entity); |
---|
125 | mOgreMeshSerializer->exportMesh(mSampleCf.getEntityClustersFolder() + |
---|
126 | mSampleCf.getEntityClustersMeshPrefixFilename() + "Merged.mesh", false, false); |
---|
127 | |
---|
128 | mMeshSerializer->exportMesh(entity->getMesh(), mSampleCf.getEntityDistributionFolder() + mSampleCf.getEntitiesMeshPrefixFilename() + "Merged.mesh"); |
---|
129 | //mXMLMeshSerializer->exportMesh(entity->getMesh(), mSampleCf.getEntityClustersFolder() + mSampleCf.getEntitiesMeshPrefixFilename() + "Merged.mesh" + ".xml"); |
---|
130 | |
---|
131 | delete entity; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | void Manager::saveEntityDistributionXML() |
---|
136 | { |
---|
137 | TiXmlDocument *document = mLeafDistributionSerializer->create( |
---|
138 | mSampleCf.getEntityDistributionFolder() + |
---|
139 | mSampleCf.getEntityDistributionXMLPrefixFilename() + ".xml"); |
---|
140 | mLeafDistributionSerializer->setLeafDistribution(mLeaves->getDistribution()); |
---|
141 | mLeafDistributionSerializer->writeLeafDistribution(document); |
---|
142 | mLeafDistributionSerializer->save(); |
---|
143 | } |
---|
144 | |
---|
145 | void Manager::loadEntityDistribution() |
---|
146 | { |
---|
147 | TiXmlDocument *document = mLeafDistributionSerializer->load( |
---|
148 | mSampleCf.getEntityDistributionFolder() + |
---|
149 | mSampleCf.getEntityDistributionXMLPrefixFilename() + ".xml"); |
---|
150 | mLeafDistributionSerializer->setLeafDistribution(mLeaves->getDistribution()); |
---|
151 | mLeafDistributionSerializer->getEntity()->setMesh(mLeaves->getMesh()); |
---|
152 | mLeafDistributionSerializer->readLeafDistribution(document); |
---|
153 | |
---|
154 | BBC::Entity *entity; |
---|
155 | BBC::EntityDistribution *entityDistribution; |
---|
156 | |
---|
157 | Ogre::Mesh* leavesSplittedMesh = BBC::Manager::getSingleton().loadMesh(mSampleCf.getEntitiesMeshPrefixFilename() + "Splitted.mesh"); |
---|
158 | entity = new BBC::Entity(); |
---|
159 | entity->setMesh(leavesSplittedMesh); |
---|
160 | |
---|
161 | entity->loadMesh(false); |
---|
162 | entityDistribution = mLeaves->getDistribution(); |
---|
163 | |
---|
164 | for (unsigned int iSubEntity = 0; iSubEntity < entity->getNumSubEntities(); iSubEntity++) |
---|
165 | { |
---|
166 | BBC::Entity *entitySample; |
---|
167 | BBC::SubEntity *subEntity; |
---|
168 | BBC::SubEntity *subEntitySample; |
---|
169 | |
---|
170 | subEntity = entity->getSubEntity(iSubEntity); |
---|
171 | entitySample = entityDistribution->getEntity(iSubEntity); |
---|
172 | subEntitySample = entitySample->getSubEntity(0); |
---|
173 | entitySample->removeSubEntity(0); |
---|
174 | |
---|
175 | // Add our first subentity... |
---|
176 | entitySample->addSubEntity(subEntity); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | void Manager::generateEntityDistribution() |
---|
181 | { |
---|
182 | if (mSampleCf.getEntityDistributionGeneration()) |
---|
183 | { |
---|
184 | createEntityDistribution(); |
---|
185 | saveEntityDistributionSplitted(); |
---|
186 | saveEntityDistributionXML(); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | void Manager::saveBillboardCloudSplitted() |
---|
191 | { |
---|
192 | //exportBillboardCloudMesh() |
---|
193 | mOgreMeshSerializer->setEntity(mLeaves->getBillboardCloud()->getEntity()); |
---|
194 | mOgreMeshSerializer->exportMesh(mSampleCf.getBillboardCloudFolder() + |
---|
195 | mSampleCf.getBillboardCloudMeshPrefixFilename() + "Splitted.mesh",false, false); |
---|
196 | mMeshSerializer->exportMesh(mLeaves->getBillboardCloud()->getEntity()->getMesh(), mSampleCf.getBillboardCloudFolder() + mSampleCf.getBillboardCloudMeshPrefixFilename() + "Splitted.mesh"); |
---|
197 | } |
---|
198 | |
---|
199 | void Manager::saveBillboardCloudMerged() |
---|
200 | { |
---|
201 | mLeaves->getBillboardCloud()->getEntity()->getSubEntity(0)->setMaterialName(mSampleCf.getBillboardCloudMaterialPrefixFilename()); |
---|
202 | mOgreMeshSerializer->setEntity(mLeaves->getBillboardCloud()->getEntity()); |
---|
203 | mOgreMeshSerializer->exportMesh(mSampleCf.getBillboardCloudFolder() + |
---|
204 | mSampleCf.getBillboardCloudMeshPrefixFilename() + "Merged.mesh", mSampleCf.getBillboardCloudMergedBillboards(), false); |
---|
205 | mMeshSerializer->exportMesh(mLeaves->getBillboardCloud()->getEntity()->getMesh(), |
---|
206 | mSampleCf.getBillboardCloudFolder() + mSampleCf.getBillboardCloudMeshPrefixFilename() |
---|
207 | |
---|
208 | + "Merged.mesh"); |
---|
209 | } |
---|
210 | |
---|
211 | void Manager::saveBillboardCloudXML() |
---|
212 | { |
---|
213 | //saveBillboardCloud() |
---|
214 | TiXmlDocument *document2 = mBillboardCloudSerializer->create( |
---|
215 | mSampleCf.getBillboardCloudFolder() + |
---|
216 | mSampleCf.getBillboardCloudXMLPrefixFilename() + ".xml"); |
---|
217 | mBillboardCloudSerializer->setBillboardCloud(mLeaves->getBillboardCloud()); |
---|
218 | mBillboardCloudSerializer->writeBillboardCloud(document2); |
---|
219 | mBillboardCloudSerializer->save(); |
---|
220 | } |
---|
221 | |
---|
222 | void Manager::saveEntityClusters() |
---|
223 | { |
---|
224 | BBC::BillboardClusterData *billboardClusterData; |
---|
225 | BBC::EntityCluster *entityCluster; |
---|
226 | unsigned int numSerializedBillboards = 0; |
---|
227 | |
---|
228 | if (mSampleCf.getEntityClustersSplittedClusters()) |
---|
229 | { |
---|
230 | |
---|
231 | for (unsigned int iBillboard = 0; iBillboard < mLeaves->getBillboardCloud()->getNumBillboards(); iBillboard++) |
---|
232 | { |
---|
233 | billboardClusterData = mLeaves->getBillboardCloud()->getBillboard(iBillboard)->getBillboardClusterData(); |
---|
234 | entityCluster = billboardClusterData->getEntityCluster(); |
---|
235 | Ogre::LogManager::getSingleton().logMessage("iBillboard: " + Ogre::StringConverter::toString(iBillboard)); |
---|
236 | if (entityCluster->getNumEntitiesClusterData() > 0) |
---|
237 | { |
---|
238 | BBC::Entity *entityClusterized = entityCluster->getEntity(); |
---|
239 | entityClusterized->getSubEntity(0)->setMaterialName(mSampleCf.getEntityClustersMaterialPrefixFilename()); |
---|
240 | mOgreMeshSerializer->setEntity(entityClusterized); |
---|
241 | mOgreMeshSerializer->exportMesh(mSampleCf.getEntityDistributionFolder() + |
---|
242 | mSampleCf.getEntityClustersMeshPrefixFilename() + |
---|
243 | Ogre::StringConverter::toString(numSerializedBillboards) + ".mesh", !(mSampleCf.getEntityClustersSplittedClusters()), false); |
---|
244 | mMeshSerializer->exportMesh(entityClusterized->getMesh(), |
---|
245 | mSampleCf.getEntityClustersFolder() + |
---|
246 | mSampleCf.getEntityClustersMeshPrefixFilename() + |
---|
247 | Ogre::StringConverter::toString(numSerializedBillboards) + ".mesh"); |
---|
248 | //mXMLMeshSerializer->exportMesh(entityClusterized->getMesh(), entityClusterized->getMesh()->getName() + ".xml"); |
---|
249 | |
---|
250 | numSerializedBillboards++; |
---|
251 | } |
---|
252 | } |
---|
253 | } |
---|
254 | numSerializedBillboards = 0; |
---|
255 | |
---|
256 | if (mSampleCf.getEntityClustersMergedClusters()) |
---|
257 | { |
---|
258 | BBC::Entity *entity; |
---|
259 | entity = new BBC::Entity(); |
---|
260 | |
---|
261 | for (unsigned int iBillboard = 0; iBillboard < mLeaves->getBillboardCloud()->getNumBillboards(); iBillboard++) |
---|
262 | { |
---|
263 | billboardClusterData = mLeaves->getBillboardCloud()->getBillboard(iBillboard)->getBillboardClusterData(); |
---|
264 | entityCluster = billboardClusterData->getEntityCluster(); |
---|
265 | |
---|
266 | if (entityCluster->getNumEntitiesClusterData() > 0) |
---|
267 | { |
---|
268 | BBC::Entity *entityClusterized = entityCluster->getEntity(); |
---|
269 | entityClusterized->getSubEntity(0)->setMaterialName(mSampleCf.getEntityClustersMaterialPrefixFilename()); |
---|
270 | BBC::SubEntity *subEntity = entityClusterized->getSubEntity(0); |
---|
271 | |
---|
272 | if (numSerializedBillboards == 0) |
---|
273 | { |
---|
274 | // Delete the default constructed subentity... |
---|
275 | entity->removeSubEntity(0); |
---|
276 | |
---|
277 | // Add our first subentity... |
---|
278 | entity->addSubEntity(subEntity); |
---|
279 | } |
---|
280 | else |
---|
281 | { |
---|
282 | entity->addSubEntity(subEntity); |
---|
283 | } |
---|
284 | |
---|
285 | numSerializedBillboards++; |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | Ogre::LogManager::getSingleton().logMessage(" Mesh Generation --" + Ogre::StringConverter::toString(numSerializedBillboards)); |
---|
290 | |
---|
291 | mOgreMeshSerializer->setEntity(entity); |
---|
292 | mOgreMeshSerializer->exportMesh(mSampleCf.getEntityClustersFolder() + |
---|
293 | mSampleCf.getEntityClustersMeshPrefixFilename() + "Merged.mesh", false, false); |
---|
294 | |
---|
295 | mMeshSerializer->exportMesh(entity->getMesh(), mSampleCf.getEntityClustersFolder() + mSampleCf.getEntityClustersMeshPrefixFilename() + "Merged.mesh"); |
---|
296 | //mXMLMeshSerializer->exportMesh(entity->getMesh(), mSampleCf.getEntityClustersFolder() + mSampleCf.getEntityClustersMeshPrefixFilename() + "Merged.mesh" + ".xml"); |
---|
297 | |
---|
298 | delete entity; |
---|
299 | } |
---|
300 | |
---|
301 | |
---|
302 | |
---|
303 | } |
---|
304 | |
---|
305 | void Manager::createEntityClusters() |
---|
306 | { |
---|
307 | mBillboardCloudGenerator->setDistribution(mLeaves->getDistribution()); |
---|
308 | mBillboardCloudGenerator->setBillboardCloud(mLeaves->getBillboardCloud()); |
---|
309 | mBillboardCloudGenerator->setMaxNumBillboards(mSampleCf.getEntityClustersMaxClusters()); |
---|
310 | ((KdTreeClusterGenerator*)mBillboardCloudGenerator)->setAlpha(mSampleCf.getEntityClustersAlpha()); |
---|
311 | ((KdTreeClusterGenerator*)mBillboardCloudGenerator)->setNumIterations(mSampleCf.getEntityClustersNumIterations()); |
---|
312 | ((KdTreeClusterGenerator*)mBillboardCloudGenerator)->generate(); |
---|
313 | } |
---|
314 | |
---|
315 | void Manager::saveSingleEntity() |
---|
316 | { |
---|
317 | if (mSampleCf.getEntitySampleMeshGeneration()) |
---|
318 | { |
---|
319 | BBC::Entity *entityLeaf = mLeaves->getDistribution()->getEntity(2); |
---|
320 | entityLeaf->getSubEntity(0)->setMaterialName(mSampleCf.getEntityClustersMaterialPrefixFilename()); |
---|
321 | mOgreMeshSerializer->setEntity(entityLeaf); |
---|
322 | mOgreMeshSerializer->exportMesh(mSampleCf.getEntityDistributionFolder() + |
---|
323 | mSampleCf.getEntitySampleMeshPrefixFilename(), mSampleCf.getEntityClustersMergedClusters(), false); |
---|
324 | //mXMLMeshSerializer->exportMesh(entityLeaf->getMesh(), entityLeaf->getMesh()->getName() + ".xml"); |
---|
325 | } |
---|
326 | } |
---|
327 | |
---|
328 | void Manager::generateEntityClusters() |
---|
329 | { |
---|
330 | Ogre::LogManager::getSingleton().logMessage("EntityClustersMeshPrefixName:" + mSampleCf.getEntityClustersMeshPrefixFilename()); |
---|
331 | createEntityClusters(); |
---|
332 | saveEntityClusters(); |
---|
333 | saveSingleEntity(); |
---|
334 | } |
---|
335 | |
---|
336 | void Manager::generateBillboardCloud() |
---|
337 | { |
---|
338 | if (mSampleCf.getBillboardCloudMergedBillboards()) |
---|
339 | { |
---|
340 | mLeaves->getBillboardCloud()->generateBillboardCloud(mSampleCf.getBillboardCloudMergedBillboards()); |
---|
341 | if (mSampleCf.getBillboardCloudDiffuseColorTextureAtlasGeneration()) |
---|
342 | { |
---|
343 | mBillboardCloudUVMapper->setBillboardCloud(mLeaves->getBillboardCloud()); |
---|
344 | mBillboardCloudUVMapper->setTextureWidth(mSampleCf.getBillboardCloudDiffuseColorTextureSize()); |
---|
345 | mBillboardCloudUVMapper->setTextureHeight(mSampleCf.getBillboardCloudDiffuseColorTextureSize()); |
---|
346 | mBillboardCloudUVMapper->setTextureAtlasWidth(mSampleCf.getBillboardCloudDiffuseColorTextureAtlasSize()); |
---|
347 | mBillboardCloudUVMapper->setTextureAtlasHeight(mSampleCf.getBillboardCloudDiffuseColorTextureAtlasSize()); |
---|
348 | mBillboardCloudUVMapper->initialize(); |
---|
349 | mBillboardCloudUVMapper->generateTextureAtlasTextureCoords(mSampleCf.getBillboardCloudMergedBillboards()); |
---|
350 | } |
---|
351 | saveBillboardCloudMerged(); |
---|
352 | } |
---|
353 | |
---|
354 | |
---|
355 | if (mSampleCf.getBillboardCloudSplittedBillboards()) |
---|
356 | { |
---|
357 | mLeaves->getBillboardCloud()->generateBillboardCloud(!mSampleCf.getBillboardCloudSplittedBillboards()); |
---|
358 | if (mSampleCf.getBillboardCloudDiffuseColorTextureAtlasGeneration()) |
---|
359 | { |
---|
360 | mBillboardCloudUVMapper->setBillboardCloud(mLeaves->getBillboardCloud()); |
---|
361 | mBillboardCloudUVMapper->setTextureWidth(mSampleCf.getBillboardCloudDiffuseColorTextureSize()); |
---|
362 | mBillboardCloudUVMapper->setTextureHeight(mSampleCf.getBillboardCloudDiffuseColorTextureSize()); |
---|
363 | mBillboardCloudUVMapper->setTextureAtlasWidth(mSampleCf.getBillboardCloudDiffuseColorTextureAtlasSize()); |
---|
364 | mBillboardCloudUVMapper->setTextureAtlasHeight(mSampleCf.getBillboardCloudDiffuseColorTextureAtlasSize()); |
---|
365 | mBillboardCloudUVMapper->initialize(); |
---|
366 | mBillboardCloudUVMapper->generateTextureAtlasTextureCoords(!mSampleCf.getBillboardCloudSplittedBillboards()); |
---|
367 | } |
---|
368 | saveBillboardCloudSplitted(); |
---|
369 | } |
---|
370 | |
---|
371 | saveBillboardCloudXML(); |
---|
372 | } |
---|
373 | |
---|
374 | Leaves* Manager::getLeaves() |
---|
375 | { |
---|
376 | return mLeaves; |
---|
377 | } |
---|
378 | |
---|
379 | //exportEntities() |
---|
380 | /* |
---|
381 | BBC::Entity *entityLeaves = new BBC::Entity(); |
---|
382 | entityLeaves->setMesh(leavesMesh); |
---|
383 | entityLeaves->loadMesh(true); |
---|
384 | entityLeaves->getSubEntity(0)->setMaterialName(mSampleCf.getEntityClustersMaterialPrefixFilename() + ".material"); |
---|
385 | mOgreMeshSerializer->setEntity(entityLeaves); |
---|
386 | mOgreMeshSerializer->exportMesh(mSampleCf.getEntityDistributionFolder() + |
---|
387 | mSampleCf.getEntitySampleMeshPrefixFilename() + "New.mesh", false, false); |
---|
388 | mXMLMeshSerializer->exportMesh(entityLeaves->getMesh(), mSampleCf.getEntityDistributionFolder() + |
---|
389 | mSampleCf.getEntitySampleMeshPrefixFilename() + "New.mesh.xml"); |
---|
390 | */ |
---|
391 | |
---|
392 | } |
---|