| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/Geode> |
|---|
| 14 | #include <osg/Notify> |
|---|
| 15 | |
|---|
| 16 | #include <stdio.h> |
|---|
| 17 | #include <math.h> |
|---|
| 18 | |
|---|
| 19 | #define square(x) ((x)*(x)) |
|---|
| 20 | |
|---|
| 21 | using namespace osg; |
|---|
| 22 | |
|---|
| 23 | Geode::Geode() |
|---|
| 24 | { |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | Geode::Geode(const Geode& geode,const CopyOp& copyop): |
|---|
| 28 | Node(geode,copyop) |
|---|
| 29 | { |
|---|
| 30 | for(DrawableList::const_iterator itr=geode._drawables.begin(); |
|---|
| 31 | itr!=geode._drawables.end(); |
|---|
| 32 | ++itr) |
|---|
| 33 | { |
|---|
| 34 | Drawable* drawable = copyop(itr->get()); |
|---|
| 35 | if (drawable) addDrawable(drawable); |
|---|
| 36 | } |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | Geode::~Geode() |
|---|
| 40 | { |
|---|
| 41 | |
|---|
| 42 | for(DrawableList::iterator itr=_drawables.begin(); |
|---|
| 43 | itr!=_drawables.end(); |
|---|
| 44 | ++itr) |
|---|
| 45 | { |
|---|
| 46 | (*itr)->removeParent(this); |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | bool Geode::addDrawable( Drawable *drawable ) |
|---|
| 51 | { |
|---|
| 52 | if (drawable ) |
|---|
| 53 | { |
|---|
| 54 | |
|---|
| 55 | _drawables.push_back(drawable); |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | drawable->addParent(this); |
|---|
| 59 | |
|---|
| 60 | if (drawable->requiresUpdateTraversal()) |
|---|
| 61 | { |
|---|
| 62 | setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()+1); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | if (drawable->requiresEventTraversal()) |
|---|
| 66 | { |
|---|
| 67 | setNumChildrenRequiringEventTraversal(getNumChildrenRequiringEventTraversal()+1); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | dirtyBound(); |
|---|
| 71 | |
|---|
| 72 | return true; |
|---|
| 73 | } |
|---|
| 74 | else return false; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | bool Geode::removeDrawable( Drawable *drawable ) |
|---|
| 79 | { |
|---|
| 80 | return removeDrawables(getDrawableIndex(drawable),1); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | bool Geode::removeDrawables(unsigned int pos,unsigned int numDrawablesToRemove) |
|---|
| 84 | { |
|---|
| 85 | if (pos<_drawables.size() && numDrawablesToRemove>0) |
|---|
| 86 | { |
|---|
| 87 | unsigned int endOfRemoveRange = pos+numDrawablesToRemove; |
|---|
| 88 | if (endOfRemoveRange>_drawables.size()) |
|---|
| 89 | { |
|---|
| 90 | OSG_DEBUG<<"Warning: Geode::removeDrawable(i,numDrawablesToRemove) has been passed an excessive number"<<std::endl; |
|---|
| 91 | OSG_DEBUG<<" of drawables to remove, trimming just to end of drawable list."<<std::endl; |
|---|
| 92 | endOfRemoveRange=_drawables.size(); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | unsigned int updateCallbackRemoved = 0; |
|---|
| 96 | unsigned int eventCallbackRemoved = 0; |
|---|
| 97 | for(unsigned i=pos;i<endOfRemoveRange;++i) |
|---|
| 98 | { |
|---|
| 99 | |
|---|
| 100 | _drawables[i]->removeParent(this); |
|---|
| 101 | |
|---|
| 102 | if (_drawables[i]->requiresUpdateTraversal()) ++updateCallbackRemoved; |
|---|
| 103 | if (_drawables[i]->requiresEventTraversal()) ++eventCallbackRemoved; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | _drawables.erase(_drawables.begin()+pos,_drawables.begin()+endOfRemoveRange); |
|---|
| 107 | |
|---|
| 108 | if (updateCallbackRemoved) |
|---|
| 109 | { |
|---|
| 110 | setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()-updateCallbackRemoved); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | if (eventCallbackRemoved) |
|---|
| 114 | { |
|---|
| 115 | setNumChildrenRequiringEventTraversal(getNumChildrenRequiringEventTraversal()-eventCallbackRemoved); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | dirtyBound(); |
|---|
| 119 | |
|---|
| 120 | return true; |
|---|
| 121 | } |
|---|
| 122 | else return false; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | bool Geode::replaceDrawable( Drawable *origDrawable, Drawable *newDrawable ) |
|---|
| 126 | { |
|---|
| 127 | if (newDrawable==NULL || origDrawable==newDrawable) return false; |
|---|
| 128 | |
|---|
| 129 | unsigned int pos = getDrawableIndex(origDrawable); |
|---|
| 130 | if (pos<_drawables.size()) |
|---|
| 131 | { |
|---|
| 132 | return setDrawable(pos,newDrawable); |
|---|
| 133 | } |
|---|
| 134 | return false; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | bool Geode::setDrawable( unsigned int i, Drawable* newDrawable ) |
|---|
| 138 | { |
|---|
| 139 | if (i<_drawables.size() && newDrawable) |
|---|
| 140 | { |
|---|
| 141 | |
|---|
| 142 | Drawable* origDrawable = _drawables[i].get(); |
|---|
| 143 | |
|---|
| 144 | int deltaUpdate = 0; |
|---|
| 145 | if (origDrawable->requiresUpdateTraversal()) --deltaUpdate; |
|---|
| 146 | if (newDrawable->requiresUpdateTraversal()) ++deltaUpdate; |
|---|
| 147 | if (deltaUpdate!=0) |
|---|
| 148 | { |
|---|
| 149 | setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()+deltaUpdate); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | int deltaEvent = 0; |
|---|
| 153 | if (origDrawable->requiresEventTraversal()) --deltaEvent; |
|---|
| 154 | if (newDrawable->requiresEventTraversal()) ++deltaEvent; |
|---|
| 155 | if (deltaEvent!=0) |
|---|
| 156 | { |
|---|
| 157 | setNumChildrenRequiringEventTraversal(getNumChildrenRequiringEventTraversal()+deltaEvent); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | origDrawable->removeParent(this); |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | _drawables[i] = newDrawable; |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | newDrawable->addParent(this); |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | dirtyBound(); |
|---|
| 173 | |
|---|
| 174 | return true; |
|---|
| 175 | } |
|---|
| 176 | else return false; |
|---|
| 177 | |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | BoundingSphere Geode::computeBound() const |
|---|
| 182 | { |
|---|
| 183 | BoundingSphere bsphere; |
|---|
| 184 | |
|---|
| 185 | _bbox.init(); |
|---|
| 186 | |
|---|
| 187 | DrawableList::const_iterator itr; |
|---|
| 188 | for(itr=_drawables.begin(); |
|---|
| 189 | itr!=_drawables.end(); |
|---|
| 190 | ++itr) |
|---|
| 191 | { |
|---|
| 192 | _bbox.expandBy((*itr)->getBound()); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | if (_bbox.valid()) |
|---|
| 196 | { |
|---|
| 197 | bsphere.expandBy(_bbox); |
|---|
| 198 | } |
|---|
| 199 | return bsphere; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | void Geode::compileDrawables(RenderInfo& renderInfo) |
|---|
| 203 | { |
|---|
| 204 | for(DrawableList::iterator itr = _drawables.begin(); |
|---|
| 205 | itr!=_drawables.end(); |
|---|
| 206 | ++itr) |
|---|
| 207 | { |
|---|
| 208 | (*itr)->compileGLObjects(renderInfo); |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | void Geode::setThreadSafeRefUnref(bool threadSafe) |
|---|
| 213 | { |
|---|
| 214 | Node::setThreadSafeRefUnref(threadSafe); |
|---|
| 215 | |
|---|
| 216 | for(DrawableList::const_iterator itr=_drawables.begin(); |
|---|
| 217 | itr!=_drawables.end(); |
|---|
| 218 | ++itr) |
|---|
| 219 | { |
|---|
| 220 | (*itr)->setThreadSafeRefUnref(threadSafe); |
|---|
| 221 | } |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | void Geode::resizeGLObjectBuffers(unsigned int maxSize) |
|---|
| 225 | { |
|---|
| 226 | Node::resizeGLObjectBuffers(maxSize); |
|---|
| 227 | |
|---|
| 228 | for(DrawableList::const_iterator itr=_drawables.begin(); |
|---|
| 229 | itr!=_drawables.end(); |
|---|
| 230 | ++itr) |
|---|
| 231 | { |
|---|
| 232 | (*itr)->resizeGLObjectBuffers(maxSize); |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | void Geode::releaseGLObjects(osg::State* state) const |
|---|
| 237 | { |
|---|
| 238 | Node::releaseGLObjects(state); |
|---|
| 239 | |
|---|
| 240 | for(DrawableList::const_iterator itr=_drawables.begin(); |
|---|
| 241 | itr!=_drawables.end(); |
|---|
| 242 | ++itr) |
|---|
| 243 | { |
|---|
| 244 | (*itr)->releaseGLObjects(state); |
|---|
| 245 | } |
|---|
| 246 | } |
|---|