1 | |
---|
2 | |
---|
3 | #include "BBCBillboardCloudUVMapper.h" |
---|
4 | #include "BBCBillboardCloud.h" |
---|
5 | #include "BBCEntityCluster.h" |
---|
6 | #include "BBCBillboardClusterData.h" |
---|
7 | |
---|
8 | namespace BBC { |
---|
9 | |
---|
10 | BillboardCloudUVMapper::BillboardCloudUVMapper() |
---|
11 | { |
---|
12 | } |
---|
13 | |
---|
14 | BillboardCloudUVMapper::~BillboardCloudUVMapper() |
---|
15 | { |
---|
16 | shutdown(); |
---|
17 | } |
---|
18 | |
---|
19 | void BillboardCloudUVMapper::initialize() |
---|
20 | { |
---|
21 | } |
---|
22 | |
---|
23 | void BillboardCloudUVMapper::shutdown() |
---|
24 | { |
---|
25 | while (getNumBsp() > 0) |
---|
26 | { |
---|
27 | removeBsp(getNumBsp()-1); |
---|
28 | } |
---|
29 | |
---|
30 | while (getNumClusterLists() > 0) |
---|
31 | { |
---|
32 | removeClusterList(getNumClusterLists()-1); |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | unsigned int BillboardCloudUVMapper::getNumClusterLists() |
---|
37 | { |
---|
38 | return mClusterLists.size(); |
---|
39 | } |
---|
40 | |
---|
41 | unsigned int BillboardCloudUVMapper::getNumBsp() |
---|
42 | { |
---|
43 | return mBspList.size(); |
---|
44 | } |
---|
45 | |
---|
46 | void BillboardCloudUVMapper::addBsp(IMG::Bsp *bsp) |
---|
47 | { |
---|
48 | mBspList.push_back(bsp); |
---|
49 | } |
---|
50 | |
---|
51 | IMG::Bsp* BillboardCloudUVMapper::getBsp(unsigned int iBsp) |
---|
52 | { |
---|
53 | return mBspList[iBsp]; |
---|
54 | } |
---|
55 | |
---|
56 | void BillboardCloudUVMapper::removeBsp(unsigned int iBsp) |
---|
57 | { |
---|
58 | IMG::Bsp *bsp = mBspList[iBsp]; |
---|
59 | mBspList.erase(mBspList.begin() + iBsp); |
---|
60 | delete bsp; |
---|
61 | } |
---|
62 | |
---|
63 | void BillboardCloudUVMapper::addClusterList(IMG::ClusterList *clusterList) |
---|
64 | { |
---|
65 | mClusterLists.push_back(clusterList); |
---|
66 | } |
---|
67 | |
---|
68 | IMG::ClusterList* BillboardCloudUVMapper::getClusterList(unsigned int iClusterList) |
---|
69 | { |
---|
70 | return mClusterLists[iClusterList]; |
---|
71 | } |
---|
72 | |
---|
73 | void BillboardCloudUVMapper::removeClusterList(unsigned int iClusterList) |
---|
74 | { |
---|
75 | IMG::ClusterList *clusterList = mClusterLists[iClusterList]; |
---|
76 | mClusterLists.erase(mClusterLists.begin() + iClusterList); |
---|
77 | delete clusterList; |
---|
78 | } |
---|
79 | |
---|
80 | void BillboardCloudUVMapper::setTextureWidth(unsigned short int width) |
---|
81 | { |
---|
82 | mTextureWidth = width; |
---|
83 | } |
---|
84 | |
---|
85 | unsigned short int BillboardCloudUVMapper::getTextureWidth() |
---|
86 | { |
---|
87 | return mTextureWidth; |
---|
88 | } |
---|
89 | |
---|
90 | void BillboardCloudUVMapper::setTextureHeight(unsigned short int height) |
---|
91 | { |
---|
92 | mTextureHeight = height; |
---|
93 | } |
---|
94 | |
---|
95 | unsigned short BillboardCloudUVMapper::getTextureHeight() |
---|
96 | { |
---|
97 | return mTextureHeight; |
---|
98 | } |
---|
99 | |
---|
100 | void BillboardCloudUVMapper::setTextureAtlasWidth(unsigned short int width) |
---|
101 | { |
---|
102 | mTextureAtlasWidth = width; |
---|
103 | } |
---|
104 | |
---|
105 | unsigned short int BillboardCloudUVMapper::getTextureAtlasWidth() |
---|
106 | { |
---|
107 | return mTextureAtlasWidth; |
---|
108 | } |
---|
109 | |
---|
110 | void BillboardCloudUVMapper::setTextureAtlasHeight(unsigned short int height) |
---|
111 | { |
---|
112 | mTextureAtlasHeight = height; |
---|
113 | } |
---|
114 | |
---|
115 | unsigned short BillboardCloudUVMapper::getTextureAtlasHeight() |
---|
116 | { |
---|
117 | return mTextureAtlasHeight; |
---|
118 | } |
---|
119 | |
---|
120 | void BillboardCloudUVMapper::addTextureAtlasTextureCoords(bool mergeBillboards, bool groupedBillboards, unsigned int numGeneratedBillboards, unsigned int iBillboardGroup, unsigned int iBillboard, unsigned int iBillboardInGroup) |
---|
121 | { |
---|
122 | IMG::Cluster *cluster = getClusterList(iBillboardGroup)->get(getClusterList(iBillboardGroup)->getSize()-1); |
---|
123 | IMG::Box2d *box = cluster->getBspNodeBound(); |
---|
124 | Ogre::Vector2 max = box->getMaximum(); |
---|
125 | Ogre::Vector2 min = box->getMinimum(); |
---|
126 | |
---|
127 | Ogre::Vector2 umax; |
---|
128 | umax[0] = (max[0] / mTextureAtlasWidth); |
---|
129 | umax[1] = (max[1] / mTextureAtlasHeight); |
---|
130 | |
---|
131 | Ogre::Vector2 umin; |
---|
132 | umin[0] = (min[0] / mTextureAtlasWidth); |
---|
133 | umin[1] = (min[1] / mTextureAtlasHeight); |
---|
134 | |
---|
135 | umin = umin * 2.0 - 1.0; |
---|
136 | umax = umax * 2.0 - 1.0; |
---|
137 | |
---|
138 | mBillboardCloud->getBillboard(iBillboard)->getBillboardClusterData()->addBillboardUVMapMax(umax); |
---|
139 | mBillboardCloud->getBillboard(iBillboard)->getBillboardClusterData()->addBillboardUVMapMin(umin); |
---|
140 | |
---|
141 | //Ogre::LogManager::getSingleton().logMessage("Max:" + Ogre::StringConverter::toString(Ogre::Vector3(max[0],max[1],0)) + |
---|
142 | // "\tMin:" + Ogre::StringConverter::toString(Ogre::Vector3(min[0],min[1],0)) + |
---|
143 | // "\tSize(" + Ogre::StringConverter::toString(max[0]-min[0]) + |
---|
144 | // "," + Ogre::StringConverter::toString(max[1]-min[1]) + ")"); |
---|
145 | |
---|
146 | //Ogre::LogManager::getSingleton().logMessage( |
---|
147 | // "UMax:" + Ogre::StringConverter::toString(Ogre::Vector3(umax[0], umax[1],0)) + |
---|
148 | // "\tUMin:" + Ogre::StringConverter::toString(Ogre::Vector3(umin[0], umin[1],0)) + |
---|
149 | // "\tUSize(" + Ogre::StringConverter::toString(umax[0]-umin[0]) + |
---|
150 | // "," + Ogre::StringConverter::toString(umax[1]-umin[1]) + ")"); |
---|
151 | |
---|
152 | Ogre::Vector2 texCoords01,texCoords02,texCoords03,texCoords04; |
---|
153 | |
---|
154 | // DirectX working version... |
---|
155 | texCoords03[0] = min[0] / mTextureAtlasWidth; |
---|
156 | texCoords03[1] = 1.0 - (min[1] / mTextureAtlasHeight); |
---|
157 | |
---|
158 | texCoords04[0] = max[0] / mTextureAtlasWidth; |
---|
159 | texCoords04[1] = 1.0 - (min[1] / mTextureAtlasHeight); |
---|
160 | |
---|
161 | texCoords01[0] = max[0] / mTextureAtlasWidth; |
---|
162 | texCoords01[1] = 1.0 - (max[1] / mTextureAtlasHeight); |
---|
163 | |
---|
164 | texCoords02[0] = min[0] / mTextureAtlasWidth; |
---|
165 | texCoords02[1] = 1.0 - (max[1] / mTextureAtlasHeight); |
---|
166 | |
---|
167 | //texCoords02[0] = min[0] / mTextureAtlasWidth; |
---|
168 | //texCoords02[1] = 1.0 - (min[1] / mTextureAtlasHeight); |
---|
169 | |
---|
170 | //texCoords01[0] = max[0] / mTextureAtlasWidth; |
---|
171 | //texCoords01[1] = 1.0 - (min[1] / mTextureAtlasHeight); |
---|
172 | |
---|
173 | //texCoords04[0] = max[0] / mTextureAtlasWidth; |
---|
174 | //texCoords04[1] = 1.0 - (max[1] / mTextureAtlasHeight); |
---|
175 | |
---|
176 | //texCoords03[0] = min[0] / mTextureAtlasWidth; |
---|
177 | //texCoords03[1] = 1.0 - (max[1] / mTextureAtlasHeight); |
---|
178 | |
---|
179 | if (groupedBillboards) |
---|
180 | { |
---|
181 | SubEntityPtr subEntity; |
---|
182 | |
---|
183 | unsigned int iUniqueVertex = 0; |
---|
184 | |
---|
185 | subEntity = mBillboardCloud->getEntity()->getSubEntity(numGeneratedBillboards); |
---|
186 | |
---|
187 | subEntity->addTextureCoordSet(2); |
---|
188 | subEntity->enableVertexColours(true); |
---|
189 | |
---|
190 | // Face A |
---|
191 | //(0,0,0); |
---|
192 | //(1,0,0); |
---|
193 | //(1,1,0); |
---|
194 | subEntity->setTexCoord(iUniqueVertex, subEntity->getNumTexCoordSets()-1, Ogre::Vector3(texCoords01[0],texCoords01[1],0)); |
---|
195 | subEntity->setTexCoord(iUniqueVertex+1, subEntity->getNumTexCoordSets()-1, Ogre::Vector3(texCoords02[0],texCoords02[1],0)); |
---|
196 | subEntity->setTexCoord(iUniqueVertex+2, subEntity->getNumTexCoordSets()-1, Ogre::Vector3(texCoords03[0],texCoords03[1],0)); |
---|
197 | |
---|
198 | //Ogre::ColourValue subEntityColour01 = Ogre::ColourValue(0, texCoords01[0],texCoords01[1]); |
---|
199 | //Ogre::ColourValue subEntityColour02 = Ogre::ColourValue(0, texCoords02[0],texCoords02[1]); |
---|
200 | //Ogre::ColourValue subEntityColour03 = Ogre::ColourValue(0, texCoords03[0],texCoords03[1]); |
---|
201 | |
---|
202 | //subEntity->setVertexColour(iUniqueVertex, subEntityColour01.getAsRGBA()); |
---|
203 | //subEntity->setVertexColour(iUniqueVertex+1, subEntityColour02.getAsRGBA()); |
---|
204 | //subEntity->setVertexColour(iUniqueVertex+2, subEntityColour03.getAsRGBA()); |
---|
205 | |
---|
206 | // Face B |
---|
207 | //(1,1,0); |
---|
208 | //(0,1,0); |
---|
209 | //(0,0,0); |
---|
210 | subEntity->setTexCoord(iUniqueVertex+3, subEntity->getNumTexCoordSets()-1, Ogre::Vector3(texCoords03[0],texCoords03[1],0)); |
---|
211 | subEntity->setTexCoord(iUniqueVertex+4, subEntity->getNumTexCoordSets()-1, Ogre::Vector3(texCoords04[0],texCoords04[1],0)); |
---|
212 | subEntity->setTexCoord(iUniqueVertex+5, subEntity->getNumTexCoordSets()-1, Ogre::Vector3(texCoords01[0],texCoords01[1],0)); |
---|
213 | |
---|
214 | //Ogre::ColourValue subEntityColour04 = Ogre::ColourValue(0, texCoords03[0],texCoords03[1]); |
---|
215 | //Ogre::ColourValue subEntityColour05 = Ogre::ColourValue(0, texCoords04[0],texCoords04[1]); |
---|
216 | //Ogre::ColourValue subEntityColour06 = Ogre::ColourValue(0, texCoords01[0],texCoords01[1]); |
---|
217 | |
---|
218 | //subEntity->setVertexColour(iUniqueVertex+3, subEntityColour04.getAsRGBA()); |
---|
219 | //subEntity->setVertexColour(iUniqueVertex+4, subEntityColour05.getAsRGBA()); |
---|
220 | //subEntity->setVertexColour(iUniqueVertex+5, subEntityColour06.getAsRGBA()); |
---|
221 | } |
---|
222 | else |
---|
223 | { |
---|
224 | if (mergeBillboards) |
---|
225 | { |
---|
226 | SubEntityPtr subEntity; |
---|
227 | |
---|
228 | unsigned int iUniqueVertex = numGeneratedBillboards * 6; |
---|
229 | |
---|
230 | subEntity = mBillboardCloud->getEntity()->getSubEntity(0); |
---|
231 | |
---|
232 | // Face A |
---|
233 | //(0,0,0); |
---|
234 | //(1,0,0); |
---|
235 | //(1,1,0); |
---|
236 | subEntity->setTexCoord(iUniqueVertex, subEntity->getNumTexCoordSets()-1, Ogre::Vector3(texCoords01[0],texCoords01[1],0)); |
---|
237 | subEntity->setTexCoord(iUniqueVertex+1, subEntity->getNumTexCoordSets()-1, Ogre::Vector3(texCoords02[0],texCoords02[1],0)); |
---|
238 | subEntity->setTexCoord(iUniqueVertex+2, subEntity->getNumTexCoordSets()-1, Ogre::Vector3(texCoords03[0],texCoords03[1],0)); |
---|
239 | |
---|
240 | //Ogre::ColourValue subEntityColour01 = Ogre::ColourValue(0, texCoords01[0],texCoords01[1]); |
---|
241 | //Ogre::ColourValue subEntityColour02 = Ogre::ColourValue(0, texCoords02[0],texCoords02[1]); |
---|
242 | //Ogre::ColourValue subEntityColour03 = Ogre::ColourValue(0, texCoords03[0],texCoords03[1]); |
---|
243 | |
---|
244 | //subEntity->setVertexColour(iUniqueVertex, subEntityColour01.getAsRGBA()); |
---|
245 | //subEntity->setVertexColour(iUniqueVertex+1, subEntityColour02.getAsRGBA()); |
---|
246 | //subEntity->setVertexColour(iUniqueVertex+2, subEntityColour03.getAsRGBA()); |
---|
247 | |
---|
248 | // Face B |
---|
249 | //(1,1,0); |
---|
250 | //(0,1,0); |
---|
251 | //(0,0,0); |
---|
252 | subEntity->setTexCoord(iUniqueVertex+3, subEntity->getNumTexCoordSets()-1, Ogre::Vector3(texCoords03[0],texCoords03[1],0)); |
---|
253 | subEntity->setTexCoord(iUniqueVertex+4, subEntity->getNumTexCoordSets()-1, Ogre::Vector3(texCoords04[0],texCoords04[1],0)); |
---|
254 | subEntity->setTexCoord(iUniqueVertex+5, subEntity->getNumTexCoordSets()-1, Ogre::Vector3(texCoords01[0],texCoords01[1],0)); |
---|
255 | |
---|
256 | //Ogre::ColourValue subEntityColour04 = Ogre::ColourValue(0, texCoords03[0],texCoords03[1]); |
---|
257 | //Ogre::ColourValue subEntityColour05 = Ogre::ColourValue(0, texCoords04[0],texCoords04[1]); |
---|
258 | //Ogre::ColourValue subEntityColour06 = Ogre::ColourValue(0, texCoords01[0],texCoords01[1]); |
---|
259 | |
---|
260 | //subEntity->setVertexColour(iUniqueVertex+3, subEntityColour04.getAsRGBA()); |
---|
261 | //subEntity->setVertexColour(iUniqueVertex+4, subEntityColour05.getAsRGBA()); |
---|
262 | //subEntity->setVertexColour(iUniqueVertex+5, subEntityColour06.getAsRGBA()); |
---|
263 | |
---|
264 | } |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | void BillboardCloudUVMapper::addTextureInBsp(unsigned int iBillboardGroup, unsigned int iBillboard) |
---|
269 | { |
---|
270 | IMG::NodeBspPtr nodeDest; |
---|
271 | IMG::Cluster cluster; |
---|
272 | |
---|
273 | nodeDest = getBsp(iBillboardGroup)->insert(mTextureWidth,mTextureHeight,iBillboard); |
---|
274 | |
---|
275 | cluster.setBspNodeBound(nodeDest->getBound()); |
---|
276 | getClusterList(iBillboardGroup)->insert(&cluster); |
---|
277 | } |
---|
278 | |
---|
279 | void BillboardCloudUVMapper::generateTextureAtlasTextureCoords(bool mergeBillboards, bool groupedBillboards, unsigned int numGroups) |
---|
280 | { |
---|
281 | unsigned int numBillboardGroups; |
---|
282 | bool enabledTextureCoordSet = false; |
---|
283 | unsigned int numGeneratedBillboards = 0; |
---|
284 | |
---|
285 | for (unsigned int iBillboard = 0; iBillboard < mBillboardCloud->getNumBillboards(); iBillboard++) |
---|
286 | { |
---|
287 | BillboardPtr billboard = mBillboardCloud->getBillboard(iBillboard); |
---|
288 | EntityClusterPtr entityCluster = billboard->getBillboardClusterData()->getEntityCluster(); |
---|
289 | |
---|
290 | if (entityCluster->getNumEntitiesClusterData() > 0) |
---|
291 | { |
---|
292 | if (mergeBillboards) |
---|
293 | { |
---|
294 | if (!enabledTextureCoordSet) |
---|
295 | { |
---|
296 | SubEntityPtr subEntity = mBillboardCloud->getEntity()->getSubEntity(0); |
---|
297 | subEntity->addTextureCoordSet(2); |
---|
298 | subEntity->enableVertexColours(true); |
---|
299 | enabledTextureCoordSet = true; |
---|
300 | } |
---|
301 | } |
---|
302 | numGeneratedBillboards++; |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | if (groupedBillboards) |
---|
307 | { |
---|
308 | numBillboardGroups = numGroups; |
---|
309 | } |
---|
310 | else |
---|
311 | { |
---|
312 | if (mergeBillboards) |
---|
313 | { |
---|
314 | numBillboardGroups = 1; |
---|
315 | } |
---|
316 | else // (!mergeBillboards) |
---|
317 | { |
---|
318 | numBillboardGroups = 1; |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | unsigned int iBillboardsInGroup = 0; |
---|
323 | unsigned int numberBillboardsEachGroup = numGeneratedBillboards / numGroups; |
---|
324 | unsigned int numberBillboardsAddedFirstGroup = numGeneratedBillboards % numGroups; |
---|
325 | numGeneratedBillboards = 0; |
---|
326 | unsigned int iBillboardGroup; |
---|
327 | for (iBillboardGroup = 0; iBillboardGroup < numBillboardGroups; iBillboardGroup++) |
---|
328 | { |
---|
329 | IMG::Bsp *bsp = new IMG::Bsp(); |
---|
330 | IMG::ClusterList *clusterList = new IMG::ClusterList(); |
---|
331 | IMG::NodeBspPtr node = IMG::NodeBspPtr(new IMG::NodeBsp); |
---|
332 | |
---|
333 | node->getBound()->setBoundBox(0, 0, mTextureAtlasWidth, mTextureAtlasHeight); |
---|
334 | bsp->setRoot(node); |
---|
335 | |
---|
336 | addBsp(bsp); |
---|
337 | addClusterList(clusterList); |
---|
338 | } |
---|
339 | |
---|
340 | iBillboardGroup = 0; |
---|
341 | for (unsigned int iBillboard = 0; iBillboard < mBillboardCloud->getNumBillboards(); iBillboard++) |
---|
342 | { |
---|
343 | //Ogre::LogManager::getSingleton().logMessage("id:" + Ogre::StringConverter::toString(mBillboardCloud->getBillboard(iBillboard)->getBillboardHandle())); |
---|
344 | |
---|
345 | BillboardClusterData *billboardClusterData = mBillboardCloud->getBillboard(iBillboard)->getBillboardClusterData().get(); |
---|
346 | |
---|
347 | if (billboardClusterData != NULL) |
---|
348 | { |
---|
349 | BillboardPtr billboard = mBillboardCloud->getBillboard(iBillboard); |
---|
350 | EntityClusterPtr entityCluster = billboardClusterData->getEntityCluster(); |
---|
351 | |
---|
352 | if (entityCluster->getNumEntitiesClusterData() > 0) |
---|
353 | { |
---|
354 | if ((iBillboardGroup == 0) && (iBillboardsInGroup < (numberBillboardsEachGroup + numberBillboardsAddedFirstGroup))) |
---|
355 | { |
---|
356 | addTextureInBsp(iBillboardGroup, iBillboard); |
---|
357 | addTextureAtlasTextureCoords(mergeBillboards, groupedBillboards, numGeneratedBillboards, iBillboardGroup, iBillboard, iBillboardsInGroup); |
---|
358 | iBillboardsInGroup++; |
---|
359 | } |
---|
360 | else |
---|
361 | { |
---|
362 | if ((iBillboardGroup > 0) && (iBillboardsInGroup < numberBillboardsEachGroup)) |
---|
363 | { |
---|
364 | addTextureInBsp(iBillboardGroup, iBillboard); |
---|
365 | addTextureAtlasTextureCoords(mergeBillboards, groupedBillboards, numGeneratedBillboards, iBillboardGroup, iBillboard, iBillboardsInGroup); |
---|
366 | iBillboardsInGroup++; |
---|
367 | } |
---|
368 | else |
---|
369 | { |
---|
370 | iBillboardGroup++; |
---|
371 | iBillboardsInGroup = 0; |
---|
372 | |
---|
373 | addTextureInBsp(iBillboardGroup, iBillboard); |
---|
374 | addTextureAtlasTextureCoords(mergeBillboards, groupedBillboards, numGeneratedBillboards, iBillboardGroup, iBillboard, iBillboardsInGroup); |
---|
375 | iBillboardsInGroup++; |
---|
376 | } |
---|
377 | } |
---|
378 | |
---|
379 | numGeneratedBillboards++; |
---|
380 | } |
---|
381 | } |
---|
382 | } |
---|
383 | } |
---|
384 | |
---|
385 | void BillboardCloudUVMapper::setBillboardCloud(BillboardCloud *billboardCloud) |
---|
386 | { |
---|
387 | mBillboardCloud = billboardCloud; |
---|
388 | } |
---|
389 | |
---|
390 | BillboardCloud * BillboardCloudUVMapper::getBillboardCloud() |
---|
391 | { |
---|
392 | return mBillboardCloud; |
---|
393 | } |
---|
394 | |
---|
395 | } |
---|