[1163] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of the GameTools Project
|
---|
| 4 | http://www.gametools.org
|
---|
| 5 |
|
---|
| 6 | Author: Martin Szydlowski
|
---|
| 7 | -----------------------------------------------------------------------------
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include "OgreKdTreeSceneNode.h"
|
---|
| 11 | #include "OgreKdTreeSceneManager.h"
|
---|
| 12 |
|
---|
| 13 | #include <OgreStringConverter.h>
|
---|
[1183] | 14 | #include <OgreLogManager.h>
|
---|
[1163] | 15 |
|
---|
| 16 | namespace Ogre
|
---|
| 17 | {
|
---|
| 18 | // TRIVIA: this function is _not_ called for nodes which did not change since the last frame
|
---|
| 19 | // only exception: the root node ... it's aabb should be null however, so we don't care
|
---|
| 20 | void KdTreeSceneNode::_updateBounds(void)
|
---|
| 21 | {
|
---|
| 22 | // Reset bounds first
|
---|
| 23 | mWorldAABB.setNull();
|
---|
| 24 |
|
---|
| 25 | // Update bounds from own attached objects
|
---|
| 26 | ObjectMap::iterator i;
|
---|
| 27 | for (i = mObjectsByName.begin(); i != mObjectsByName.end(); ++i)
|
---|
| 28 | {
|
---|
| 29 | // Merge world bounds of each object
|
---|
| 30 | mWorldAABB.merge(i->second->getWorldBoundingBox(true));
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | if (!mWorldAABB.isNull())
|
---|
| 34 | {
|
---|
| 35 | static_cast<KdTreeSceneManager *>(mCreator)->_updateNode(this);
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | void KdTreeSceneNode::computeScene(PlaneEventList& events, AxisAlignedBox& aabb, int& nObjects, bool includeChildren)
|
---|
| 40 | {
|
---|
[1190] | 41 | /**************************************************************************************/
|
---|
| 42 | /* HACK: do not include camera node, FUCKS UP world AABB really BAD, dunno why */
|
---|
| 43 | /* TODO: find a generic solution where MOVABLE nodes are NOT INSERTED into the KDTREE */
|
---|
| 44 | /**************************************************************************************/
|
---|
[1203] | 45 | //if (mName == "PlayerCamNode")
|
---|
| 46 | // return;
|
---|
[1190] | 47 |
|
---|
[1163] | 48 | // first compute nodes AABB
|
---|
| 49 | mWorldAABB.setNull();
|
---|
| 50 | // Update bounds from own attached objects
|
---|
| 51 | ObjectMap::iterator i;
|
---|
| 52 | for (i = mObjectsByName.begin(); i != mObjectsByName.end(); ++i)
|
---|
| 53 | {
|
---|
[1203] | 54 | // ignore objects with no actual geometry like frustums & lights
|
---|
| 55 | if (getExcludedMovables().find(i->second->getMovableType()) == getExcludedMovables().end())
|
---|
| 56 | {
|
---|
| 57 | //LogManager::getSingleton().getLog(KDTREE_LOGNAME)->logMessage("Adding movable type " + i->second->getMovableType() + " to KdTree");
|
---|
| 58 | // Merge world bounds of each object
|
---|
| 59 | mWorldAABB.merge(i->second->getWorldBoundingBox(true));
|
---|
| 60 | }
|
---|
[1163] | 61 | }
|
---|
| 62 |
|
---|
[1203] | 63 | // create information for KdTree/SAH
|
---|
[1163] | 64 | if (!mWorldAABB.isNull())
|
---|
| 65 | {
|
---|
| 66 | // merge aabb with global AABB
|
---|
| 67 | aabb.merge(mWorldAABB);
|
---|
[1203] | 68 |
|
---|
[1163] | 69 | // increase object count
|
---|
| 70 | nObjects++;
|
---|
| 71 |
|
---|
| 72 | // generate events
|
---|
| 73 | PlaneEvent::Dimension axes[] =
|
---|
| 74 | {PlaneEvent::PED_X, PlaneEvent::PED_Y, PlaneEvent::PED_Z};
|
---|
| 75 | Vector3 min = mWorldAABB.getMinimum();
|
---|
| 76 | Vector3 max = mWorldAABB.getMaximum();
|
---|
| 77 | for (int i = 0; i < 3; i++)
|
---|
| 78 | {
|
---|
| 79 | // 1-D and 2-D objects
|
---|
| 80 | if (min[i] == max[i])
|
---|
| 81 | {
|
---|
| 82 | //e = new PlaneEvent(this, min, axes[i], PlaneEvent::PET_ON);
|
---|
| 83 | //sceneinfo.events.push_back(*e);
|
---|
| 84 | events.push_back(PlaneEvent(this, min, axes[i], PlaneEvent::PET_ON));
|
---|
| 85 | }
|
---|
| 86 | else
|
---|
| 87 | {
|
---|
| 88 | //e = new PlaneEvent(this, min, axes[i], PlaneEvent::PET_START);
|
---|
| 89 | //sceneinfo.events.push_back(*e);
|
---|
| 90 | //e = new PlaneEvent(this, max, axes[i], PlaneEvent::PET_END);
|
---|
| 91 | //sceneinfo.events.push_back(*e);
|
---|
| 92 | events.push_back(PlaneEvent(this, min, axes[i], PlaneEvent::PET_START));
|
---|
| 93 | events.push_back(PlaneEvent(this, max, axes[i], PlaneEvent::PET_END));
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | // Merge with children
|
---|
| 99 | if (includeChildren)
|
---|
| 100 | {
|
---|
| 101 | ChildNodeMap::iterator child;
|
---|
| 102 | for (child = mChildren.begin(); child != mChildren.end(); ++child)
|
---|
| 103 | {
|
---|
| 104 | KdTreeSceneNode* sceneChild = static_cast<KdTreeSceneNode*>(child->second);
|
---|
| 105 | sceneChild->computeScene(events, aabb, nObjects);
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | //void KdTreeSceneNode::_findVisibleObjects(Camera* cam, RenderQueue* queue,
|
---|
| 111 | // bool includeChildren, bool displayNodes, bool onlyShadowCasters)
|
---|
| 112 | void KdTreeSceneNode::queueObjects(Camera* cam, RenderQueue* queue, bool onlyShadowCasters)
|
---|
| 113 | {
|
---|
| 114 | //SceneNode::_findVisibleObjects(cam, queue, includeChildren, displayNodes, onlyShadowCasters);
|
---|
| 115 |
|
---|
| 116 | // Add all entities
|
---|
| 117 | ObjectMap::iterator iobj;
|
---|
| 118 | ObjectMap::iterator iobjend = mObjectsByName.end();
|
---|
| 119 | for (iobj = mObjectsByName.begin(); iobj != iobjend; ++iobj)
|
---|
| 120 | {
|
---|
| 121 | // Tell attached objects about camera position (incase any extra processing they want to do)
|
---|
| 122 | iobj->second->_notifyCurrentCamera(cam);
|
---|
[1187] | 123 | if (iobj->second->isVisible() &&
|
---|
| 124 | (!onlyShadowCasters || iobj->second->getCastShadows()))
|
---|
[1163] | 125 | {
|
---|
| 126 | iobj->second->_updateRenderQueue(queue);
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | // Check if the bounding box should be shown.
|
---|
| 131 | // See if our flag is set or if the scene manager flag is set.
|
---|
| 132 | if (mShowBoundingBox || (mCreator && mCreator->getShowBoundingBoxes()))
|
---|
| 133 | {
|
---|
| 134 | _addBoundingBoxToQueue(queue);
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[1173] | 138 | void KdTreeSceneNode::getGeometryList(GtpVisibility::GeometryVector *geometryList)
|
---|
| 139 | {
|
---|
| 140 | SceneNode::ObjectIterator objIt = getAttachedObjectIterator();
|
---|
| 141 |
|
---|
| 142 | while (objIt.hasMoreElements())
|
---|
| 143 | {
|
---|
| 144 | MovableObject *movable = objIt.getNext();
|
---|
| 145 |
|
---|
| 146 | // we are interested only in the entities, i.e., instances of geometry
|
---|
| 147 | if (movable->getMovableType() == "Entity")
|
---|
| 148 | {
|
---|
| 149 | Entity *ent = static_cast<Entity *>(movable);
|
---|
| 150 | geometryList->push_back(ent);
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[1183] | 155 | // recalculate the world aabb
|
---|
[1203] | 156 | AxisAlignedBox KdTreeSceneNode::getBoundingBox(void) const
|
---|
[1163] | 157 | {
|
---|
[1203] | 158 | return mWorldAABB;
|
---|
| 159 | }
|
---|
[1183] | 160 |
|
---|
[1203] | 161 | KdTreeSceneNode::StringSet& KdTreeSceneNode::getExcludedMovables(void)
|
---|
| 162 | {
|
---|
| 163 | static StringSet sExluded;
|
---|
[1183] | 164 |
|
---|
[1203] | 165 | // initialise once
|
---|
| 166 | if (sExluded.empty())
|
---|
[1183] | 167 | {
|
---|
[1203] | 168 | sExluded.insert("Camera");
|
---|
| 169 | sExluded.insert("Frustum");
|
---|
| 170 | sExluded.insert("Light");
|
---|
[1183] | 171 | }
|
---|
| 172 |
|
---|
[1203] | 173 | return sExluded;
|
---|
[1163] | 174 | }
|
---|
| 175 |
|
---|
| 176 | // DEBUG
|
---|
[1203] | 177 | String KdTreeSceneNode::dumpToString(void)
|
---|
[1163] | 178 | {
|
---|
| 179 | String objects;
|
---|
| 180 | // Add all entities
|
---|
| 181 | ObjectMap::iterator iobj = mObjectsByName.begin();
|
---|
| 182 | ObjectMap::iterator iobjend = mObjectsByName.end();
|
---|
| 183 | while (iobj != iobjend)
|
---|
| 184 | {
|
---|
| 185 | objects += " " + iobj->second->getName();
|
---|
| 186 | iobj++;
|
---|
| 187 | }
|
---|
| 188 | objects += " Attached to " + StringConverter::toString(mLeaves.size()) + " KdLeaves.";
|
---|
| 189 | return objects;
|
---|
| 190 | }
|
---|
| 191 | } |
---|