[1595] | 1 | #ifndef _PlatFormBoundingBoxConverter_H__ |
---|
| 2 | #define _PlatFormBoundingBoxConverter_H__ |
---|
[933] | 3 | |
---|
| 4 | #include "OgreAxisAlignedBox.h" |
---|
| 5 | #include "AxisAlignedBox3.h" |
---|
| 6 | #include "Vector3.h" |
---|
| 7 | #include "BoundingBoxConverter.h" |
---|
| 8 | #include "Containers.h" |
---|
[1616] | 9 | #include "IntersectableWrapper.h" |
---|
| 10 | //#include "KdTree.h" |
---|
[933] | 11 | |
---|
[1616] | 12 | |
---|
[933] | 13 | namespace Ogre {
|
---|
| 14 |
|
---|
[1616] | 15 | #define USE_KD_PVS 1
|
---|
[933] | 16 | class Entity; |
---|
| 17 | class OctreeSceneManager;
|
---|
[1296] | 18 | class KdTreeSceneManager;
|
---|
[1595] | 19 | class BvHierarchySceneManager;
|
---|
[933] | 20 |
|
---|
[1616] | 21 | typedef vector<Entity *> EntityContainer;
|
---|
[1595] | 22 |
|
---|
[1616] | 23 | class ObjectsIntersectable: public GtpVisibilityPreprocessor::IntersectableWrapper<EntityContainer *>
|
---|
| 24 | {
|
---|
| 25 | public:
|
---|
| 26 | ObjectsIntersectable(EntityContainer *item):
|
---|
| 27 | GtpVisibilityPreprocessor::IntersectableWrapper<EntityContainer *>(item) {}
|
---|
| 28 |
|
---|
| 29 | // hack
|
---|
| 30 | ObjectsIntersectable::~ObjectsIntersectable()
|
---|
| 31 | {
|
---|
| 32 | //CLEAR_CONTAINER(*mItem);
|
---|
| 33 | delete mItem;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | int Type() const
|
---|
| 37 | {
|
---|
| 38 | return Intersectable::OBJECTS_INTERSECTABLE;
|
---|
| 39 | }
|
---|
| 40 | };
|
---|
| 41 |
|
---|
[933] | 42 | /** Class which converts preprocessor types to OGRE types
|
---|
| 43 | */
|
---|
[1595] | 44 | template<typename T>
|
---|
| 45 | class __declspec(dllexport) PlatFormBoundingBoxConverter: public GtpVisibilityPreprocessor::BoundingBoxConverter
|
---|
[933] | 46 | {
|
---|
| 47 | public:
|
---|
[1595] | 48 | PlatFormBoundingBoxConverter(T *sm);
|
---|
| 49 |
|
---|
[933] | 50 | bool IdentifyObjects(const GtpVisibilityPreprocessor::IndexedBoundingBoxContainer &iboxes,
|
---|
| 51 | GtpVisibilityPreprocessor::ObjectContainer &objects) const;
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | protected:
|
---|
| 55 |
|
---|
[1595] | 56 | //-------------------------------------------------------------------------
|
---|
| 57 | inline static AxisAlignedBox EnlargeBox(const AxisAlignedBox &box)
|
---|
| 58 | {
|
---|
| 59 | const float eps = 1e-3f;
|
---|
| 60 | const Vector3 veps(eps, eps, eps);
|
---|
| 61 |
|
---|
| 62 | Vector3 max = box.getMaximum();
|
---|
| 63 | Vector3 min = box.getMinimum();
|
---|
[933] | 64 |
|
---|
[1595] | 65 | return AxisAlignedBox(min - veps, max + veps);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[1623] | 68 | //-------------------------------------------------------------------------
|
---|
| 69 | inline static AxisAlignedBox ScaleBox(const AxisAlignedBox &box)
|
---|
| 70 | {
|
---|
| 71 | const float scale = 1.5f;
|
---|
| 72 | Vector3 max = box.getMaximum();
|
---|
| 73 | Vector3 min = box.getMinimum();
|
---|
| 74 | Vector3 size = max - min;
|
---|
| 75 | Vector3 newSize = size*(scale*0.5f);
|
---|
| 76 | Vector3 center = 0.5f * (min + max);
|
---|
| 77 |
|
---|
| 78 | Vector3 min = center - newSize;
|
---|
| 79 | Vector3 max = center + newSize;
|
---|
| 80 |
|
---|
| 81 | return AxisAlignedBox(min, max);
|
---|
| 82 | }
|
---|
[1616] | 83 | /** find object which fits best to this bounding box
|
---|
| 84 | */
|
---|
[1595] | 85 | Entity *FindBestFittingObject(const AxisAlignedBox &box) const;
|
---|
[1616] | 86 |
|
---|
| 87 | /** find objects which are intersected by this box
|
---|
| 88 | */
|
---|
| 89 | void FindIntersectingObjects(const AxisAlignedBox &box,
|
---|
| 90 | vector<Entity *> &objects) const;
|
---|
[1595] | 91 |
|
---|
| 92 | T *mSceneMgr;
|
---|
[933] | 93 | };
|
---|
| 94 |
|
---|
[1595] | 95 | //-------------------------------------------------------------------------
|
---|
| 96 | template<typename T>
|
---|
| 97 | PlatFormBoundingBoxConverter<T>::PlatFormBoundingBoxConverter(T *sm):
|
---|
| 98 | mSceneMgr(sm)
|
---|
| 99 | {
|
---|
| 100 | }
|
---|
| 101 | //-----------------------------------------------------------------------
|
---|
| 102 | template<typename T>
|
---|
| 103 | Entity *PlatFormBoundingBoxConverter<T>::FindBestFittingObject(const AxisAlignedBox &box) const
|
---|
| 104 | {
|
---|
| 105 | list<SceneNode *> sceneNodeList;
|
---|
[1623] | 106 | AxisAlignedBox3 enlargedBox =
|
---|
[1595] | 107 | AxisAlignedBox mybox = EnlargeBox(box);
|
---|
| 108 |
|
---|
| 109 | // get intersecting scene nodes
|
---|
| 110 | mSceneMgr->findNodesIn(mybox, sceneNodeList, NULL);
|
---|
| 111 |
|
---|
| 112 | // minimal overlap
|
---|
| 113 | float overlap = 0;//1e-6;
|
---|
| 114 |
|
---|
| 115 | Entity *bestFittingObj = NULL;
|
---|
| 116 | float bestFit = overlap;
|
---|
| 117 |
|
---|
| 118 | // perfect fit threshold
|
---|
| 119 | const float thresh = 1.0 - GtpVisibilityPreprocessor::Limits::Small;
|
---|
| 120 |
|
---|
| 121 | list<SceneNode *>::const_iterator sit, sit_end = sceneNodeList.end();
|
---|
| 122 | // find the bbox which is closest to the current bbox
|
---|
| 123 | for (sit = sceneNodeList.begin(); sit != sceneNodeList.end(); ++ sit)
|
---|
| 124 | {
|
---|
| 125 | SceneNode *sn = *sit;
|
---|
| 126 | SceneNode::ObjectIterator oit = sn->getAttachedObjectIterator();
|
---|
| 127 |
|
---|
| 128 | while (oit.hasMoreElements())
|
---|
| 129 | {
|
---|
| 130 | MovableObject *mo = oit.getNext();
|
---|
| 131 |
|
---|
| 132 | // we are only interested in scene entities
|
---|
| 133 | if (mo->getMovableType() != "Entity")
|
---|
| 134 | {
|
---|
| 135 | continue;
|
---|
| 136 | }
|
---|
[1676] | 137 |
|
---|
[1595] | 138 | const AxisAlignedBox bbox = EnlargeBox(mo->getWorldBoundingBox());
|
---|
| 139 |
|
---|
| 140 | // compute measure how much aabbs overlap
|
---|
| 141 | overlap = RatioOfOverlap(OgreTypeConverter::ConvertFromOgre(mybox),
|
---|
| 142 | OgreTypeConverter::ConvertFromOgre(bbox));
|
---|
| 143 |
|
---|
| 144 | if (overlap > bestFit)
|
---|
| 145 | {
|
---|
| 146 | bestFit = overlap;
|
---|
| 147 | bestFittingObj = static_cast<Entity *>(mo);
|
---|
| 148 |
|
---|
| 149 | // perfect fit => object found, early exit
|
---|
| 150 | if (overlap >= thresh)
|
---|
| 151 | {
|
---|
| 152 | return bestFittingObj;
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | if (0)
|
---|
| 159 | {
|
---|
| 160 | std::stringstream d;
|
---|
| 161 | if (bestFittingObj)
|
---|
| 162 | d << "best fit: " << bestFit;
|
---|
| 163 | else
|
---|
| 164 | d << "warning, no best fitting object\n" << box;
|
---|
| 165 |
|
---|
| 166 | Ogre::LogManager::getSingleton().logMessage(d.str());
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | return bestFittingObj;
|
---|
| 170 | }
|
---|
[1616] | 171 | //-----------------------------------------------------------------------
|
---|
[1623] | 172 | /*template<typename T>
|
---|
| 173 | void PlatFormBoundingBoxConverter<T>::FindIntersectingObjects(const AxisAlignedBox &box,
|
---|
| 174 | EntityContainer &objects) const
|
---|
| 175 | {//return;
|
---|
| 176 | list<SceneNode *> sceneNodeList;
|
---|
| 177 |
|
---|
| 178 | // get intersecting scene nodes (= candidates)
|
---|
| 179 | AxisAlignedBox mybox = EnlargeBox(box);
|
---|
| 180 | mSceneMgr->findNodesIn(mybox, sceneNodeList, NULL);
|
---|
| 181 | //mSceneMgr->findNodesIn(box, sceneNodeList, NULL);
|
---|
| 182 |
|
---|
| 183 | list<SceneNode *>::const_iterator sit, sit_end = sceneNodeList.end();
|
---|
| 184 |
|
---|
| 185 | GtpVisibilityPreprocessor::AxisAlignedBox3 nodeBox = OgreTypeConverter::ConvertFromOgre(mybox);
|
---|
| 186 | //GtpVisibilityPreprocessor::AxisAlignedBox3 nodeBox = OgreTypeConverter::ConvertFromOgre(box);
|
---|
| 187 |
|
---|
| 188 | // find really intersecting objects
|
---|
| 189 | for (sit = sceneNodeList.begin(); sit != sceneNodeList.end(); ++ sit)
|
---|
| 190 | {
|
---|
| 191 | SceneNode *sn = *sit;
|
---|
| 192 | SceneNode::ObjectIterator oit = sn->getAttachedObjectIterator();
|
---|
| 193 |
|
---|
| 194 | while (oit.hasMoreElements())
|
---|
| 195 | {
|
---|
| 196 | MovableObject *mo = oit.getNext();
|
---|
| 197 |
|
---|
| 198 | // we are only interested in scene entities
|
---|
| 199 | if (mo->getMovableType() != "Entity")
|
---|
| 200 | {
|
---|
| 201 | continue;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | //const AxisAlignedBox bbox = EnlargeBox(mo->getWorldBoundingBox());
|
---|
| 205 | const AxisAlignedBox bbox = mo->getWorldBoundingBox();
|
---|
| 206 |
|
---|
| 207 | const bool overlaps = Overlap(nodeBox,
|
---|
| 208 | OgreTypeConverter::ConvertFromOgre(bbox)
|
---|
| 209 | );
|
---|
| 210 | //,0.00001);
|
---|
| 211 |
|
---|
| 212 | if (overlaps)
|
---|
| 213 | {
|
---|
| 214 | objects.push_back(static_cast<Entity *>(mo));
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | */
|
---|
[1616] | 220 | template<typename T>
|
---|
| 221 | void PlatFormBoundingBoxConverter<T>::FindIntersectingObjects(const AxisAlignedBox &box,
|
---|
| 222 | EntityContainer &objects) const
|
---|
[1621] | 223 | {//return;
|
---|
[1616] | 224 | list<SceneNode *> sceneNodeList;
|
---|
| 225 |
|
---|
| 226 | // get intersecting scene nodes (= candidates)
|
---|
[1676] | 227 | //AxisAlignedBox mybox = EnlargeBox(box);
|
---|
[1616] | 228 | //mSceneMgr->findNodesIn(mybox, sceneNodeList, NULL);
|
---|
| 229 | mSceneMgr->findNodesIn(box, sceneNodeList, NULL);
|
---|
| 230 |
|
---|
| 231 | list<SceneNode *>::const_iterator sit, sit_end = sceneNodeList.end();
|
---|
| 232 |
|
---|
[1623] | 233 | //GtpVisibilityPreprocessor::AxisAlignedBox3 nodeBox = OgreTypeConverter::ConvertFromOgre(mybox);
|
---|
[1616] | 234 | GtpVisibilityPreprocessor::AxisAlignedBox3 nodeBox = OgreTypeConverter::ConvertFromOgre(box);
|
---|
| 235 |
|
---|
| 236 | // find really intersecting objects
|
---|
| 237 | for (sit = sceneNodeList.begin(); sit != sceneNodeList.end(); ++ sit)
|
---|
| 238 | {
|
---|
| 239 | SceneNode *sn = *sit;
|
---|
| 240 | SceneNode::ObjectIterator oit = sn->getAttachedObjectIterator();
|
---|
| 241 |
|
---|
| 242 | while (oit.hasMoreElements())
|
---|
| 243 | {
|
---|
| 244 | MovableObject *mo = oit.getNext();
|
---|
| 245 |
|
---|
| 246 | // we are only interested in scene entities
|
---|
| 247 | if (mo->getMovableType() != "Entity")
|
---|
| 248 | {
|
---|
| 249 | continue;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | //const AxisAlignedBox bbox = EnlargeBox(mo->getWorldBoundingBox());
|
---|
| 253 | const AxisAlignedBox bbox = mo->getWorldBoundingBox();
|
---|
| 254 |
|
---|
[1676] | 255 | const bool overlaps =
|
---|
| 256 | Overlap(nodeBox, OgreTypeConverter::ConvertFromOgre(bbox));
|
---|
| 257 |
|
---|
[1616] | 258 |
|
---|
| 259 | if (overlaps)
|
---|
| 260 | {
|
---|
| 261 | objects.push_back(static_cast<Entity *>(mo));
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 | #if USE_KD_PVS
|
---|
[1595] | 267 | //-------------------------------------------------------------------------
|
---|
| 268 | template<typename T>
|
---|
| 269 | bool PlatFormBoundingBoxConverter<T>::IdentifyObjects(
|
---|
| 270 | const GtpVisibilityPreprocessor::IndexedBoundingBoxContainer &iboxes,
|
---|
| 271 | GtpVisibilityPreprocessor::ObjectContainer &objects) const
|
---|
| 272 | {
|
---|
[1618] | 273 | //Ogre::LogManager().logMessage("pvs: intersecting objects");
|
---|
[1616] | 274 | const long startTime = GtpVisibilityPreprocessor::GetTime();
|
---|
| 275 |
|
---|
[1595] | 276 | GtpVisibilityPreprocessor::IndexedBoundingBoxContainer::
|
---|
| 277 | const_iterator iit, iit_end = iboxes.end();
|
---|
| 278 |
|
---|
[1616] | 279 | int i = 0;
|
---|
| 280 |
|
---|
| 281 | for (iit = iboxes.begin(); iit != iit_end; ++ iit, ++ i)
|
---|
| 282 | {
|
---|
[1618] | 283 | if (0 && ((i % 1000) == 0))
|
---|
[1616] | 284 | {
|
---|
| 285 | std::stringstream d; d << "found " << i << " objects";
|
---|
| 286 | Ogre::LogManager().getSingleton().logMessage(d.str());
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | const AxisAlignedBox box = OgreTypeConverter::ConvertToOgre((*iit).second);
|
---|
| 290 |
|
---|
| 291 | //GtpVisibilityPreprocessor::ObjectContainer *entryObjects = new EntityContainer();
|
---|
| 292 | EntityContainer *entryObjects = new EntityContainer();
|
---|
| 293 |
|
---|
| 294 | // find all objects that intersect the bounding box
|
---|
| 295 | FindIntersectingObjects(box, *entryObjects);
|
---|
| 296 |
|
---|
| 297 | /*vector<Entity *>::const_iterator mit, mit_end = sceneObjects.end();
|
---|
| 298 |
|
---|
| 299 | for (mit = sceneObjects.begin(); mit != mit_end; ++ mit)
|
---|
| 300 | {
|
---|
| 301 | Entity *ent = *mit;
|
---|
| 302 | // create new mesh instance
|
---|
| 303 | OgreMeshInstance *omi = new OgreGetOrCreateOgreMeshInstance(ent);
|
---|
| 304 | //omi->SetId((*iit).first);
|
---|
| 305 | entryObjects->push_back(omi);
|
---|
| 306 | }*/
|
---|
| 307 |
|
---|
| 308 | ObjectsIntersectable *entry =
|
---|
| 309 | new ObjectsIntersectable(entryObjects);
|
---|
| 310 | entry->SetId((*iit).first);
|
---|
| 311 | //kdObj->mBbox = (*iit).second;
|
---|
| 312 | objects.push_back(entry);
|
---|
| 313 | }
|
---|
| 314 |
|
---|
[1618] | 315 | //std::stringstream d; d << "finished object intersection in " << GtpVisibilityPreprocessor::TimeDiff(startTime, GtpVisibilityPreprocessor::GetTime()) * 1e-3 << "secs";
|
---|
| 316 | //Ogre::LogManager().logMessage(d.str());
|
---|
[1616] | 317 |
|
---|
| 318 | return true;
|
---|
| 319 | }
|
---|
| 320 | #else
|
---|
| 321 | //-------------------------------------------------------------------------
|
---|
| 322 | template<typename T>
|
---|
| 323 | bool PlatFormBoundingBoxConverter<T>::IdentifyObjects(
|
---|
| 324 | const GtpVisibilityPreprocessor::IndexedBoundingBoxContainer &iboxes,
|
---|
| 325 | GtpVisibilityPreprocessor::ObjectContainer &objects) const
|
---|
| 326 | {
|
---|
| 327 | GtpVisibilityPreprocessor::IndexedBoundingBoxContainer::
|
---|
| 328 | const_iterator iit, iit_end = iboxes.end();
|
---|
| 329 |
|
---|
[1595] | 330 | for (iit = iboxes.begin(); iit != iit_end; ++ iit)
|
---|
| 331 | {
|
---|
| 332 | const GtpVisibilityPreprocessor::AxisAlignedBox3 box = (*iit).second;
|
---|
| 333 | const AxisAlignedBox currentBox = OgreTypeConverter::ConvertToOgre(box);
|
---|
| 334 |
|
---|
| 335 | Entity *ent = FindBestFittingObject(currentBox);
|
---|
| 336 |
|
---|
| 337 | if (ent)
|
---|
| 338 | {
|
---|
[1616] | 339 | // create new mesh instance
|
---|
[1595] | 340 | OgreMeshInstance *omi = new OgreMeshInstance(ent);
|
---|
| 341 | omi->SetId((*iit).first);
|
---|
| 342 | objects.push_back(omi);
|
---|
| 343 | }
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | return true;
|
---|
| 347 | }
|
---|
[1616] | 348 | #endif
|
---|
[1595] | 349 |
|
---|
| 350 | typedef PlatFormBoundingBoxConverter<OctreeSceneManager> OctreeBoundingBoxConverter;
|
---|
| 351 | typedef PlatFormBoundingBoxConverter<BvHierarchySceneManager> BvhBoundingBoxConverter;
|
---|
| 352 | typedef PlatFormBoundingBoxConverter<KdTreeSceneManager> KdTreeBoundingBoxConverter;
|
---|
| 353 |
|
---|
[933] | 354 | } // namespace Ogre
|
---|
| 355 |
|
---|
[1595] | 356 | #endif // PlatFormBoundingBoxConverter
|
---|