Fixed handling of window size/projection matrix

This commit is contained in:
Robert Osfield
2012-12-07 12:47:42 +00:00
parent aa3634979a
commit ccdca3d9bf
2 changed files with 59 additions and 16 deletions

View File

@@ -4,11 +4,22 @@ using namespace gsc;
void CameraProperty::setToModel(const osg::Node* node)
{
double distanceRatio = 3.5;
osg::BoundingSphere bs = node->getBound();
double screenWidth = osg::DisplaySettings::instance()->getScreenWidth();
double screenHeight = osg::DisplaySettings::instance()->getScreenHeight();
double screenDistance = osg::DisplaySettings::instance()->getScreenDistance();
double vfov = atan2(screenHeight/2.0,screenDistance)*2.0;
double hfov = atan2(screenWidth/2.0,screenDistance)*2.0;
double viewAngle = vfov<hfov ? vfov : hfov;
double dist = bs.radius() / sin(viewAngle*0.5);
// dist = osg::DisplaySettings::instance()->getScreenDistance();
_center = bs.center();
_eye = _center - osg::Vec3d(0.0, bs.radius()*distanceRatio, 0.0);
_eye = _center - osg::Vec3d(0.0, dist, 0.0);
_up = osg::Vec3d(0.0, 0.0, 1.0);
_rotationCenter = _center;

View File

@@ -125,6 +125,27 @@ int main( int argc, char **argv )
return 1;
}
}
float screenWidth = fc->getScreenWidth()!=0.0 ? fc->getScreenWidth() : osg::DisplaySettings::instance()->getScreenWidth();
if (arguments.read("--screen-width",screenWidth)) {}
float screenHeight = fc->getScreenHeight()!=0.0 ? fc->getScreenHeight() : osg::DisplaySettings::instance()->getScreenHeight();
if (arguments.read("--screen-height",screenHeight)) {}
float screenDistance = fc->getScreenDistance()!=0.0 ? fc->getScreenDistance() : osg::DisplaySettings::instance()->getScreenDistance();
if (arguments.read("--screen-distance",screenDistance)) {}
fc->setScreenWidth(screenWidth);
osg::DisplaySettings::instance()->setScreenWidth(screenWidth);
fc->setScreenHeight(screenHeight);
osg::DisplaySettings::instance()->setScreenHeight(screenHeight);
fc->setScreenDistance(screenDistance);
osg::DisplaySettings::instance()->setScreenDistance(screenDistance);
bool useScreenSizeForProjectionMatrix = true;
if (arguments.read("-i",filename)) fc->setInputFileName(filename);
if (arguments.read("-o",filename)) fc->setOutputFileName(filename);
if (arguments.read("-p",filename))
@@ -211,14 +232,8 @@ int main( int argc, char **argv )
unsigned int height = 512;
if (arguments.read("--height",height)) fc->setHeight(height);
float value;
if (arguments.read("--screen-width",value)) fc->setScreenWidth(value);
if (arguments.read("--screen-height",value)) fc->setScreenHeight(value);
if (arguments.read("--screen-distance",value)) fc->setScreenDistance(value);
unsigned int samples = 0;
if (arguments.read("--samples",samples)) fc->setSamples(samples);
@@ -390,15 +405,32 @@ int main( int argc, char **argv )
OSG_NOTICE<<"PixelBuffer has been created succseffully "<<traits->width<<", "<<traits->height<<std::endl;
}
double fovy, aspectRatio, zNear, zFar;
viewer.getCamera()->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar);
double newAspectRatio = double(traits->width) / double(traits->height);
double aspectRatioChange = newAspectRatio / aspectRatio;
if (aspectRatioChange != 1.0)
if (useScreenSizeForProjectionMatrix)
{
viewer.getCamera()->getProjectionMatrix() *= osg::Matrix::scale(1.0/aspectRatioChange,1.0,1.0);
OSG_NOTICE<<"Setting projection matrix"<<std::endl;
double vfov = osg::RadiansToDegrees(atan2(screenHeight/2.0f,screenDistance)*2.0);
// double hfov = osg::RadiansToDegrees(atan2(width/2.0f,distance)*2.0);
viewer.getCamera()->setProjectionMatrixAsPerspective( vfov, screenWidth/screenHeight, 0.1, 1000.0);
}
else
{
double fovy, aspectRatio, zNear, zFar;
viewer.getCamera()->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar);
double newAspectRatio = double(traits->width) / double(traits->height);
double aspectRatioChange = newAspectRatio / aspectRatio;
if (aspectRatioChange != 1.0)
{
viewer.getCamera()->getProjectionMatrix() *= osg::Matrix::scale(1.0/aspectRatioChange,1.0,1.0);
}
}
// set up stereo masks
viewer.getCamera()->setCullMask(0xffffffff);
viewer.getCamera()->setCullMaskLeft(0x00000001);
viewer.getCamera()->setCullMaskRight(0x00000002);
viewer.getCamera()->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));