2005-10-10 18:10:44 +08:00
|
|
|
#include "DepthPartitionNode.h"
|
|
|
|
#include <osgUtil/CullVisitor>
|
|
|
|
|
2005-12-05 18:24:02 +08:00
|
|
|
using namespace osg;
|
|
|
|
|
2005-10-10 18:10:44 +08:00
|
|
|
#define CURRENT_CLASS DepthPartitionNode
|
2005-12-05 18:24:02 +08:00
|
|
|
|
2005-10-10 18:10:44 +08:00
|
|
|
CURRENT_CLASS::CURRENT_CLASS()
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
_distAccumulator = new DistanceAccumulator;
|
|
|
|
init();
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CURRENT_CLASS::CURRENT_CLASS(const CURRENT_CLASS& dpn, const osg::CopyOp& copyop)
|
2005-12-05 18:24:02 +08:00
|
|
|
: osg::Group(dpn, copyop),
|
2005-11-09 18:37:10 +08:00
|
|
|
_active(dpn._active),
|
2005-11-18 04:22:55 +08:00
|
|
|
_renderOrder(dpn._renderOrder),
|
|
|
|
_clearColorBuffer(dpn._clearColorBuffer)
|
2005-10-10 18:10:44 +08:00
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
_distAccumulator = new DistanceAccumulator;
|
|
|
|
_numCameras = 0;
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CURRENT_CLASS::~CURRENT_CLASS() {}
|
|
|
|
|
|
|
|
void CURRENT_CLASS::init()
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
_active = true;
|
|
|
|
_numCameras = 0;
|
|
|
|
setCullingActive(false);
|
2006-11-27 22:52:07 +08:00
|
|
|
_renderOrder = osg::Camera::POST_RENDER;
|
2005-11-18 04:22:55 +08:00
|
|
|
_clearColorBuffer = true;
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CURRENT_CLASS::setActive(bool active)
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
if(_active == active) return;
|
|
|
|
_active = active;
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CURRENT_CLASS::setClearColorBuffer(bool clear)
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
_clearColorBuffer = clear;
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
// Update the render order for the first Camera if it exists
|
2005-11-18 04:22:55 +08:00
|
|
|
if(!_cameraList.empty())
|
|
|
|
{
|
|
|
|
if(clear)
|
|
|
|
_cameraList[0]->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
else
|
|
|
|
_cameraList[0]->setClearMask(GL_DEPTH_BUFFER_BIT);
|
|
|
|
}
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
void CURRENT_CLASS::setRenderOrder(osg::Camera::RenderOrder order)
|
2005-10-10 18:10:44 +08:00
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
_renderOrder = order;
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
// Update the render order for existing Cameras
|
2005-11-18 04:22:55 +08:00
|
|
|
unsigned int numCameras = _cameraList.size();
|
|
|
|
for(unsigned int i = 0; i < numCameras; i++)
|
|
|
|
{
|
|
|
|
_cameraList[i]->setRenderOrder(_renderOrder);
|
|
|
|
}
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CURRENT_CLASS::traverse(osg::NodeVisitor &nv)
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
// If the scene hasn't been defined then don't do anything
|
|
|
|
unsigned int numChildren = _children.size();
|
|
|
|
if(numChildren == 0) return;
|
|
|
|
|
|
|
|
// If the node is not active then don't analyze it
|
|
|
|
if(!_active)
|
|
|
|
{
|
|
|
|
// Traverse the graph as usual
|
|
|
|
Group::traverse(nv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the visitor is not a cull visitor, pass it directly onto the scene.
|
|
|
|
osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(&nv);
|
|
|
|
if(!cv)
|
|
|
|
{
|
|
|
|
Group::traverse(nv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are in the cull traversal, so first collect information on the
|
|
|
|
// current modelview and projection matrices and viewport.
|
2007-02-21 21:48:01 +08:00
|
|
|
osg::RefMatrix& modelview = *(cv->getModelViewMatrix());
|
|
|
|
osg::RefMatrix& projection = *(cv->getProjectionMatrix());
|
2005-11-18 04:22:55 +08:00
|
|
|
osg::Viewport* viewport = cv->getViewport();
|
|
|
|
|
|
|
|
// Prepare for scene traversal.
|
|
|
|
_distAccumulator->setMatrices(modelview, projection);
|
|
|
|
_distAccumulator->setNearFarRatio(cv->getNearFarRatio());
|
|
|
|
_distAccumulator->reset();
|
|
|
|
|
|
|
|
// Step 1: Traverse the children, collecting the near/far distances.
|
|
|
|
unsigned int i;
|
|
|
|
for(i = 0; i < numChildren; i++)
|
|
|
|
{
|
|
|
|
_children[i]->accept(*(_distAccumulator.get()));
|
|
|
|
}
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
// Step 2: Compute the near and far distances for every Camera that
|
2005-11-18 04:22:55 +08:00
|
|
|
// should be used to render the scene.
|
|
|
|
_distAccumulator->computeCameraPairs();
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
// Step 3: Create the Cameras, and add them as children.
|
2005-11-18 04:22:55 +08:00
|
|
|
DistanceAccumulator::PairList& camPairs = _distAccumulator->getCameraPairs();
|
|
|
|
_numCameras = camPairs.size(); // Get the number of cameras
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
// Create the Cameras, and add them as children.
|
2005-11-18 04:22:55 +08:00
|
|
|
if(_numCameras > 0)
|
|
|
|
{
|
2006-11-27 22:52:07 +08:00
|
|
|
osg::Camera *currCam;
|
2005-11-18 04:22:55 +08:00
|
|
|
DistanceAccumulator::DistancePair currPair;
|
|
|
|
|
|
|
|
for(i = 0; i < _numCameras; i++)
|
|
|
|
{
|
|
|
|
// Create the camera, and clamp it's projection matrix
|
|
|
|
currPair = camPairs[i]; // (near,far) pair for current camera
|
|
|
|
currCam = createOrReuseCamera(projection, currPair.first,
|
|
|
|
currPair.second, i);
|
|
|
|
|
|
|
|
// Set the modelview matrix and viewport of the camera
|
|
|
|
currCam->setViewMatrix(modelview);
|
|
|
|
currCam->setViewport(viewport);
|
|
|
|
|
|
|
|
// Redirect the CullVisitor to the current camera
|
|
|
|
currCam->accept(nv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the clear color for the first camera
|
|
|
|
_cameraList[0]->setClearColor(cv->getRenderStage()->getClearColor());
|
|
|
|
}
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CURRENT_CLASS::addChild(osg::Node *child)
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
return insertChild(_children.size(), child);
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CURRENT_CLASS::insertChild(unsigned int index, osg::Node *child)
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
if(!Group::insertChild(index, child)) return false; // Insert child
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
// Insert child into each Camera
|
2005-11-18 04:22:55 +08:00
|
|
|
unsigned int totalCameras = _cameraList.size();
|
|
|
|
for(unsigned int i = 0; i < totalCameras; i++)
|
|
|
|
{
|
|
|
|
_cameraList[i]->insertChild(index, child);
|
|
|
|
}
|
|
|
|
return true;
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-02 17:45:31 +08:00
|
|
|
bool CURRENT_CLASS::removeChildren(unsigned int pos, unsigned int numRemove)
|
2005-10-10 18:10:44 +08:00
|
|
|
{
|
2006-05-02 17:45:31 +08:00
|
|
|
if(!Group::removeChildren(pos, numRemove)) return false; // Remove child
|
2005-11-18 04:22:55 +08:00
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
// Remove child from each Camera
|
2005-11-18 04:22:55 +08:00
|
|
|
unsigned int totalCameras = _cameraList.size();
|
|
|
|
for(unsigned int i = 0; i < totalCameras; i++)
|
|
|
|
{
|
2006-05-02 17:45:31 +08:00
|
|
|
_cameraList[i]->removeChildren(pos, numRemove);
|
2005-11-18 04:22:55 +08:00
|
|
|
}
|
|
|
|
return true;
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CURRENT_CLASS::setChild(unsigned int i, osg::Node *node)
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
if(!Group::setChild(i, node)) return false; // Set child
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
// Set child for each Camera
|
2005-11-18 04:22:55 +08:00
|
|
|
unsigned int totalCameras = _cameraList.size();
|
|
|
|
for(unsigned int j = 0; j < totalCameras; j++)
|
|
|
|
{
|
|
|
|
_cameraList[j]->setChild(i, node);
|
|
|
|
}
|
|
|
|
return true;
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
osg::Camera* CURRENT_CLASS::createOrReuseCamera(const osg::Matrix& proj,
|
2005-11-11 03:16:01 +08:00
|
|
|
double znear, double zfar,
|
|
|
|
const unsigned int &camNum)
|
2005-10-10 18:10:44 +08:00
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
if(_cameraList.size() <= camNum) _cameraList.resize(camNum+1);
|
2006-11-27 22:52:07 +08:00
|
|
|
osg::Camera *camera = _cameraList[camNum].get();
|
2005-11-18 04:22:55 +08:00
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
if(!camera) // Create a new Camera
|
2005-11-18 04:22:55 +08:00
|
|
|
{
|
2006-11-27 22:52:07 +08:00
|
|
|
camera = new osg::Camera;
|
2005-11-18 04:22:55 +08:00
|
|
|
camera->setCullingActive(false);
|
|
|
|
camera->setRenderOrder(_renderOrder);
|
|
|
|
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
|
|
|
|
|
|
|
|
// We will compute the near/far planes ourselves
|
|
|
|
camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
|
|
|
|
camera->setCullingMode(osg::CullSettings::ENABLE_ALL_CULLING);
|
|
|
|
|
|
|
|
if(camNum == 0 && _clearColorBuffer)
|
|
|
|
camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
else
|
|
|
|
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
// Add our children to the new Camera's children
|
2005-11-18 04:22:55 +08:00
|
|
|
unsigned int numChildren = _children.size();
|
|
|
|
for(unsigned int i = 0; i < numChildren; i++)
|
|
|
|
{
|
|
|
|
camera->addChild(_children[i].get());
|
|
|
|
}
|
|
|
|
|
|
|
|
_cameraList[camNum] = camera;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Matrixd &projection = camera->getProjectionMatrix();
|
|
|
|
projection = proj;
|
|
|
|
|
|
|
|
// Slightly inflate the near & far planes to avoid objects at the
|
|
|
|
// extremes being clipped out.
|
|
|
|
znear *= 0.999;
|
|
|
|
zfar *= 1.001;
|
|
|
|
|
|
|
|
// Clamp the projection matrix z values to the range (near, far)
|
|
|
|
double epsilon = 1.0e-6;
|
|
|
|
if(fabs(projection(0,3)) < epsilon &&
|
|
|
|
fabs(projection(1,3)) < epsilon &&
|
|
|
|
fabs(projection(2,3)) < epsilon ) // Projection is Orthographic
|
|
|
|
{
|
|
|
|
epsilon = -1.0/(zfar - znear); // Used as a temp variable
|
|
|
|
projection(2,2) = 2.0*epsilon;
|
|
|
|
projection(3,2) = (zfar + znear)*epsilon;
|
|
|
|
}
|
|
|
|
else // Projection is Perspective
|
|
|
|
{
|
|
|
|
double trans_near = (-znear*projection(2,2) + projection(3,2)) /
|
|
|
|
(-znear*projection(2,3) + projection(3,3));
|
|
|
|
double trans_far = (-zfar*projection(2,2) + projection(3,2)) /
|
|
|
|
(-zfar*projection(2,3) + projection(3,3));
|
|
|
|
double ratio = fabs(2.0/(trans_near - trans_far));
|
|
|
|
double center = -0.5*(trans_near + trans_far);
|
|
|
|
|
|
|
|
projection.postMult(osg::Matrixd(1.0, 0.0, 0.0, 0.0,
|
|
|
|
0.0, 1.0, 0.0, 0.0,
|
|
|
|
0.0, 0.0, ratio, 0.0,
|
|
|
|
0.0, 0.0, center*ratio, 1.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
return camera;
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
|
|
|
#undef CURRENT_CLASS
|