| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include "DepthPartitionNode.h" |
|---|
| 20 | #include <osgUtil/CullVisitor> |
|---|
| 21 | |
|---|
| 22 | using namespace osg; |
|---|
| 23 | |
|---|
| 24 | #define CURRENT_CLASS DepthPartitionNode |
|---|
| 25 | |
|---|
| 26 | CURRENT_CLASS::CURRENT_CLASS() |
|---|
| 27 | { |
|---|
| 28 | _distAccumulator = new DistanceAccumulator; |
|---|
| 29 | init(); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | CURRENT_CLASS::CURRENT_CLASS(const CURRENT_CLASS& dpn, const osg::CopyOp& copyop) |
|---|
| 33 | : osg::Group(dpn, copyop), |
|---|
| 34 | _active(dpn._active), |
|---|
| 35 | _renderOrder(dpn._renderOrder), |
|---|
| 36 | _clearColorBuffer(dpn._clearColorBuffer) |
|---|
| 37 | { |
|---|
| 38 | _distAccumulator = new DistanceAccumulator; |
|---|
| 39 | _numCameras = 0; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | CURRENT_CLASS::~CURRENT_CLASS() {} |
|---|
| 43 | |
|---|
| 44 | void CURRENT_CLASS::init() |
|---|
| 45 | { |
|---|
| 46 | _active = true; |
|---|
| 47 | _numCameras = 0; |
|---|
| 48 | setCullingActive(false); |
|---|
| 49 | _renderOrder = osg::Camera::POST_RENDER; |
|---|
| 50 | _clearColorBuffer = true; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | void CURRENT_CLASS::setActive(bool active) |
|---|
| 54 | { |
|---|
| 55 | if(_active == active) return; |
|---|
| 56 | _active = active; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | void CURRENT_CLASS::setClearColorBuffer(bool clear) |
|---|
| 60 | { |
|---|
| 61 | _clearColorBuffer = clear; |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | if(!_cameraList.empty()) |
|---|
| 65 | { |
|---|
| 66 | if(clear) |
|---|
| 67 | _cameraList[0]->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|---|
| 68 | else |
|---|
| 69 | _cameraList[0]->setClearMask(GL_DEPTH_BUFFER_BIT); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | void CURRENT_CLASS::setRenderOrder(osg::Camera::RenderOrder order) |
|---|
| 74 | { |
|---|
| 75 | _renderOrder = order; |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | unsigned int numCameras = _cameraList.size(); |
|---|
| 79 | for(unsigned int i = 0; i < numCameras; i++) |
|---|
| 80 | { |
|---|
| 81 | _cameraList[i]->setRenderOrder(_renderOrder); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | void CURRENT_CLASS::traverse(osg::NodeVisitor &nv) |
|---|
| 86 | { |
|---|
| 87 | |
|---|
| 88 | unsigned int numChildren = _children.size(); |
|---|
| 89 | if(numChildren == 0) return; |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | if(!_active) |
|---|
| 93 | { |
|---|
| 94 | |
|---|
| 95 | Group::traverse(nv); |
|---|
| 96 | return; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(&nv); |
|---|
| 101 | if(!cv) |
|---|
| 102 | { |
|---|
| 103 | Group::traverse(nv); |
|---|
| 104 | return; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | osg::RefMatrix& modelview = *(cv->getModelViewMatrix()); |
|---|
| 110 | osg::RefMatrix& projection = *(cv->getProjectionMatrix()); |
|---|
| 111 | osg::Viewport* viewport = cv->getViewport(); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | _distAccumulator->setMatrices(modelview, projection); |
|---|
| 115 | _distAccumulator->setNearFarRatio(cv->getNearFarRatio()); |
|---|
| 116 | _distAccumulator->reset(); |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | unsigned int i; |
|---|
| 120 | for(i = 0; i < numChildren; i++) |
|---|
| 121 | { |
|---|
| 122 | _children[i]->accept(*(_distAccumulator.get())); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | _distAccumulator->computeCameraPairs(); |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | DistanceAccumulator::PairList& camPairs = _distAccumulator->getCameraPairs(); |
|---|
| 131 | _numCameras = camPairs.size(); |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | if(_numCameras > 0) |
|---|
| 135 | { |
|---|
| 136 | osg::Camera *currCam; |
|---|
| 137 | DistanceAccumulator::DistancePair currPair; |
|---|
| 138 | |
|---|
| 139 | for(i = 0; i < _numCameras; i++) |
|---|
| 140 | { |
|---|
| 141 | |
|---|
| 142 | currPair = camPairs[i]; |
|---|
| 143 | currCam = createOrReuseCamera(projection, currPair.first, |
|---|
| 144 | currPair.second, i); |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | currCam->setViewMatrix(modelview); |
|---|
| 148 | currCam->setViewport(viewport); |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | currCam->accept(nv); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | _cameraList[0]->setClearColor(cv->getRenderStage()->getClearColor()); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | bool CURRENT_CLASS::addChild(osg::Node *child) |
|---|
| 160 | { |
|---|
| 161 | return insertChild(_children.size(), child); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | bool CURRENT_CLASS::insertChild(unsigned int index, osg::Node *child) |
|---|
| 165 | { |
|---|
| 166 | if(!Group::insertChild(index, child)) return false; |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | unsigned int totalCameras = _cameraList.size(); |
|---|
| 170 | for(unsigned int i = 0; i < totalCameras; i++) |
|---|
| 171 | { |
|---|
| 172 | _cameraList[i]->insertChild(index, child); |
|---|
| 173 | } |
|---|
| 174 | return true; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | bool CURRENT_CLASS::removeChildren(unsigned int pos, unsigned int numRemove) |
|---|
| 179 | { |
|---|
| 180 | if(!Group::removeChildren(pos, numRemove)) return false; |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | unsigned int totalCameras = _cameraList.size(); |
|---|
| 184 | for(unsigned int i = 0; i < totalCameras; i++) |
|---|
| 185 | { |
|---|
| 186 | _cameraList[i]->removeChildren(pos, numRemove); |
|---|
| 187 | } |
|---|
| 188 | return true; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | bool CURRENT_CLASS::setChild(unsigned int i, osg::Node *node) |
|---|
| 192 | { |
|---|
| 193 | if(!Group::setChild(i, node)) return false; |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | unsigned int totalCameras = _cameraList.size(); |
|---|
| 197 | for(unsigned int j = 0; j < totalCameras; j++) |
|---|
| 198 | { |
|---|
| 199 | _cameraList[j]->setChild(i, node); |
|---|
| 200 | } |
|---|
| 201 | return true; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | osg::Camera* CURRENT_CLASS::createOrReuseCamera(const osg::Matrix& proj, |
|---|
| 205 | double znear, double zfar, |
|---|
| 206 | const unsigned int &camNum) |
|---|
| 207 | { |
|---|
| 208 | if(_cameraList.size() <= camNum) _cameraList.resize(camNum+1); |
|---|
| 209 | osg::Camera *camera = _cameraList[camNum].get(); |
|---|
| 210 | |
|---|
| 211 | if(!camera) |
|---|
| 212 | { |
|---|
| 213 | camera = new osg::Camera; |
|---|
| 214 | camera->setCullingActive(false); |
|---|
| 215 | camera->setRenderOrder(_renderOrder); |
|---|
| 216 | camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); |
|---|
| 220 | camera->setCullingMode(osg::CullSettings::ENABLE_ALL_CULLING); |
|---|
| 221 | |
|---|
| 222 | if(camNum == 0 && _clearColorBuffer) |
|---|
| 223 | camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|---|
| 224 | else |
|---|
| 225 | camera->setClearMask(GL_DEPTH_BUFFER_BIT); |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | unsigned int numChildren = _children.size(); |
|---|
| 229 | for(unsigned int i = 0; i < numChildren; i++) |
|---|
| 230 | { |
|---|
| 231 | camera->addChild(_children[i].get()); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | _cameraList[camNum] = camera; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | osg::Matrixd &projection = camera->getProjectionMatrix(); |
|---|
| 238 | projection = proj; |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | znear *= 0.999; |
|---|
| 243 | zfar *= 1.001; |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | double epsilon = 1.0e-6; |
|---|
| 247 | if(fabs(projection(0,3)) < epsilon && |
|---|
| 248 | fabs(projection(1,3)) < epsilon && |
|---|
| 249 | fabs(projection(2,3)) < epsilon ) |
|---|
| 250 | { |
|---|
| 251 | epsilon = -1.0/(zfar - znear); |
|---|
| 252 | projection(2,2) = 2.0*epsilon; |
|---|
| 253 | projection(3,2) = (zfar + znear)*epsilon; |
|---|
| 254 | } |
|---|
| 255 | else |
|---|
| 256 | { |
|---|
| 257 | double trans_near = (-znear*projection(2,2) + projection(3,2)) / |
|---|
| 258 | (-znear*projection(2,3) + projection(3,3)); |
|---|
| 259 | double trans_far = (-zfar*projection(2,2) + projection(3,2)) / |
|---|
| 260 | (-zfar*projection(2,3) + projection(3,3)); |
|---|
| 261 | double ratio = fabs(2.0/(trans_near - trans_far)); |
|---|
| 262 | double center = -0.5*(trans_near + trans_far); |
|---|
| 263 | |
|---|
| 264 | projection.postMult(osg::Matrixd(1.0, 0.0, 0.0, 0.0, |
|---|
| 265 | 0.0, 1.0, 0.0, 0.0, |
|---|
| 266 | 0.0, 0.0, ratio, 0.0, |
|---|
| 267 | 0.0, 0.0, center*ratio, 1.0)); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | return camera; |
|---|
| 271 | } |
|---|
| 272 | #undef CURRENT_CLASS |
|---|