Various work on osgViewer library, including warp point and graphics window resize support

This commit is contained in:
Robert Osfield
2007-01-01 18:20:10 +00:00
parent 88fc4ee986
commit 7155f7d1b0
36 changed files with 1624 additions and 1030 deletions

View File

@@ -102,8 +102,8 @@ void AutoTransform::accept(NodeVisitor& nv)
if (cs)
{
int width = _previousWidth;
int height = _previousHeight;
Viewport::value_type width = _previousWidth;
Viewport::value_type height = _previousHeight;
osg::Viewport* viewport = cs->getViewport();
if (viewport)

View File

@@ -50,13 +50,27 @@ Camera::Camera(const Camera& camera,const CopyOp& copyop):
_bufferAttachmentMap(camera._bufferAttachmentMap),
_postDrawCallback(camera._postDrawCallback)
{
// need to copy/share graphics context?
}
Camera::~Camera()
{
if (_graphicsContext.valid()) _graphicsContext->removeCamera(this);
}
void Camera::setGraphicsContext(GraphicsContext* context)
{
if (_graphicsContext == context) return;
if (_graphicsContext.valid()) _graphicsContext->removeCamera(this);
_graphicsContext = context;
if (_graphicsContext.valid()) _graphicsContext->addCamera(this);
}
bool Camera::isRenderToTextureCamera() const
{
return (!_bufferAttachmentMap.empty());

View File

@@ -13,7 +13,11 @@
#include <osg/GraphicsContext>
#include <osg/Camera>
#include <osg/View>
#include <osg/Notify>
#include <map>
#include <sstream>
@@ -362,3 +366,105 @@ void GraphicsContext::runOperations()
}
}
}
void GraphicsContext::addCamera(osg::Camera* camera)
{
_cameras.push_back(camera);
}
void GraphicsContext::removeCamera(osg::Camera* camera)
{
for(Cameras::iterator itr = _cameras.begin();
itr != _cameras.end();
++itr)
{
if (*itr == camera)
{
_cameras.erase(itr);
return;
}
}
}
void GraphicsContext::resized(int x, int y, int width, int height)
{
if (!_traits) return;
double widthChangeRatio = double(width) / double(_traits->width);
double heigtChangeRatio = double(height) / double(_traits->height);
double aspectRatioChange = widthChangeRatio / heigtChangeRatio;
for(Cameras::iterator itr = _cameras.begin();
itr != _cameras.end();
++itr)
{
Camera* camera = (*itr);
Viewport* viewport = camera->getViewport();
if (viewport)
{
if (viewport->x()==0 && viewport->y()==0 &&
viewport->width()>=_traits->width && viewport->height()>=_traits->height)
{
viewport->setViewport(0,0,width,height);
}
else
{
viewport->x() = static_cast<osg::Viewport::value_type>(double(viewport->x())*widthChangeRatio);
viewport->y() = static_cast<osg::Viewport::value_type>(double(viewport->y())*heigtChangeRatio);
viewport->width() = static_cast<osg::Viewport::value_type>(double(viewport->width())*widthChangeRatio);
viewport->height() = static_cast<osg::Viewport::value_type>(double(viewport->height())*heigtChangeRatio);
}
}
// if aspect ratio adjusted change the project matrix to suit.
if (aspectRatioChange != 1.0)
{
osg::View* view = camera->getView();
osg::View::Slave* slave = view ? view->findSlaveForCamera(camera) : 0;
if (slave && camera->getReferenceFrame()==osg::Transform::RELATIVE_RF)
{
slave->_projectionOffset *= osg::Matrix::scale(1.0/aspectRatioChange,1.0,1.0);
}
else
{
#if 0
osg::Matrixd& pm = camera->getProjectionMatrix();
bool orthographicCamera = (pm(0,3)==0.0) && (pm(1,3)==0.0) && (pm(2,3)==0.0) && (pm(3,3)==1.0);
if (orthographicCamera)
{
double left, right, bottom, top, zNear, zFar;
camera->getProjectionMatrixAsOrtho(left, right, bottom, top, zNear, zFar);
double mid = (right+left)*0.5;
double halfWidth = (right-left)*0.5;
left = mid - halfWidth * aspectRatioChange;
right = mid + halfWidth * aspectRatioChange;
camera->setProjectionMatrixAsOrtho(left, right, bottom, top, zNear, zFar);
}
else
{
double left, right, bottom, top, zNear, zFar;
camera->getProjectionMatrixAsFrustum(left, right, bottom, top, zNear, zFar);
double mid = (right+left)*0.5;
double halfWidth = (right-left)*0.5;
left = mid - halfWidth * aspectRatioChange;
right = mid + halfWidth * aspectRatioChange;
camera->setProjectionMatrixAsFrustum(left, right, bottom, top, zNear, zFar);
}
#else
camera->getProjectionMatrix() *= osg::Matrix::scale(1.0/aspectRatioChange,1.0,1.0);
#endif
}
}
}
_traits->x = x;
_traits->y = y;
_traits->width = width;
_traits->height = height;
}

View File

@@ -43,20 +43,26 @@ View::~View()
void View::updateSlaves()
{
if (!_camera) return;
for(Slaves::iterator itr = _slaves.begin();
itr != _slaves.end();
++itr)
for(unsigned int i=0; i<_slaves.size(); ++i)
{
updateSlave(i);
}
}
void View::updateSlave(unsigned int i)
{
if (i >= _slaves.size() || !_camera) return;
Slave& slave = _slaves[i];
if (slave._camera->getReferenceFrame()==osg::Transform::RELATIVE_RF)
{
Slave& slave = *itr;
slave._camera->setProjectionMatrix(_camera->getProjectionMatrix() * slave._projectionOffset);
slave._camera->setViewMatrix(_camera->getViewMatrix() * slave._viewOffset);
slave._camera->inheritCullSettings(*_camera);
if (_camera->getInheritanceMask() & osg::CullSettings::CLEAR_COLOR) slave._camera->setClearColor(_camera->getClearColor());
}
slave._camera->inheritCullSettings(*_camera);
if (_camera->getInheritanceMask() & osg::CullSettings::CLEAR_COLOR) slave._camera->setClearColor(_camera->getClearColor());
}
bool View::addSlave(osg::Camera* camera, const osg::Matrix& projectionOffset, const osg::Matrix& viewOffset)
@@ -64,20 +70,12 @@ bool View::addSlave(osg::Camera* camera, const osg::Matrix& projectionOffset, co
if (!camera) return false;
camera->setView(this);
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
if (_camera.valid())
{
camera->setProjectionMatrix(projectionOffset * _camera->getProjectionMatrix());
camera->setViewMatrix(viewOffset * _camera->getViewMatrix());
camera->inheritCullSettings(*_camera);
if (_camera->getInheritanceMask() & osg::CullSettings::CLEAR_COLOR) camera->setClearColor(_camera->getClearColor());
}
unsigned int i = _slaves.size();
_slaves.push_back(Slave(camera, projectionOffset, viewOffset));
// osg::notify(osg::NOTICE)<<"Added camera"<<std::endl;
updateSlave(i);
return true;
}
@@ -90,10 +88,19 @@ bool View::removeSlave(unsigned int pos)
_slaves[pos]._camera->setCullCallback(0);
_slaves.erase(_slaves.begin()+pos);
osg::notify(osg::NOTICE)<<"Removed camera"<<std::endl;
return true;
}
View::Slave* View::findSlaveForCamera(osg::Camera* camera)
{
if (_camera == camera) return 0;
for(unsigned int i=0; i<_slaves.size(); ++i)
{
if (_slaves[i]._camera == camera) return &(_slaves[i]);
}
return 0;
}

View File

@@ -29,6 +29,7 @@ Viewport::~Viewport()
void Viewport::apply(State&) const
{
glViewport(_x,_y,_width,_height);
glViewport( static_cast<GLint>(_x),static_cast<GLint>(_y),
static_cast<GLsizei>(_width),static_cast<GLsizei>(_height) );
}