Added CameraProperty and associated command line parameters for controlling camera position and animation.

--center x y z
  --eye x y z
  --up x y z 
  --rotation-center x y z
  --rotation-axis x y z
  --rotation-speed degreesPerSecond
This commit is contained in:
Robert Osfield
2012-12-04 12:47:46 +00:00
parent cbf6745da5
commit b6d19d1052
5 changed files with 201 additions and 19 deletions

View File

@@ -1,5 +1,6 @@
SET(TARGET_SRC
CameraPathProperty.cpp
CameraProperty.cpp
CaptureSettings.cpp
EventProperty.cpp
osgframerenderer.cpp

View File

@@ -0,0 +1,59 @@
#include "CameraProperty.h"
using namespace gsc;
void CameraProperty::setToModel(const osg::Node* node)
{
double distanceRatio = 3.5;
osg::BoundingSphere bs = node->getBound();
_center = bs.center();
_eye = _center - osg::Vec3d(0.0, -bs.radius()*distanceRatio, 0.0);
_up = osg::Vec3d(0.0, 0.0, 1.0);
_rotationCenter = _center;
_rotationAxis = osg::Vec3d(0.0, 0.0, 1.0);
_rotationSpeed = 0.0;
}
void CameraProperty::update(osgViewer::View* view)
{
osg::Camera* camera = view->getCamera();
osg::FrameStamp* fs = view->getFrameStamp();
osg::Matrixd matrix;
matrix.makeLookAt(_eye, _center, _up);
if (_rotationSpeed!=0.0)
{
matrix.preMult(osg::Matrixd::translate(-_rotationCenter) *
osg::Matrix::rotate(osg::DegreesToRadians(_rotationSpeed*fs->getSimulationTime()), _rotationAxis) *
osg::Matrixd::translate(_rotationCenter));
}
camera->setViewMatrix( matrix );
}
/////////////////////////////////////////////////////////////////////////////////////////
//
// Serialization support
//
REGISTER_OBJECT_WRAPPER( gsc_CameraProperty,
new gsc::CameraProperty,
gsc::CameraProperty,
"osg::Object gsc::CameraProperty" )
{
ADD_VEC3D_SERIALIZER( Center, osg::Vec3d(0.0,0.0,0.0) );
ADD_VEC3D_SERIALIZER( EyePoint, osg::Vec3d(0.0,-1.0,0.0) );
ADD_VEC3D_SERIALIZER( UpVector, osg::Vec3d(0.0,0.0,1.0) );
ADD_VEC3D_SERIALIZER( RotationCenter, osg::Vec3d(0.0,0.0,0.0) );
ADD_VEC3D_SERIALIZER( RotationAxis, osg::Vec3d(0.0,0.0,1.0) );
ADD_DOUBLE_SERIALIZER( RotationSpeed, 0.0 );
}

View File

@@ -0,0 +1,72 @@
#ifndef CAMERAPROPERTY_H
#define CAMERAPROPERTY_H
#include <osg/AnimationPath>
#include "UpdateProperty.h"
namespace gsc
{
class CameraProperty : public gsc::UpdateProperty
{
public:
CameraProperty():
_center(0.0,0.0,0.0),
_eye(0.0,-1.0,0.0),
_up(0.0,0.0,1.0),
_rotationCenter(0.0,0.0,0.0),
_rotationAxis(0.0,0.0,1.0),
_rotationSpeed(0.0) {}
CameraProperty(const CameraProperty& cp, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
_center(cp._center),
_eye(cp._eye),
_up(cp._up),
_rotationCenter(cp._rotationCenter),
_rotationAxis(cp._rotationAxis),
_rotationSpeed(cp._rotationSpeed)
{}
META_Object(gsc, CameraProperty);
void setToModel(const osg::Node* node);
void setCenter(const osg::Vec3d& center) { _center = center; }
const osg::Vec3d& getCenter() const { return _center; }
void setEyePoint(const osg::Vec3d& eye) { _eye = eye; }
const osg::Vec3d& getEyePoint() const { return _eye; }
void setUpVector(const osg::Vec3d& up) { _up = up; }
const osg::Vec3d& getUpVector() const { return _up; }
void setRotationCenter(const osg::Vec3d& center) { _rotationCenter = center; }
const osg::Vec3d& getRotationCenter() const { return _rotationCenter; }
void setRotationAxis(const osg::Vec3d& axis) { _rotationAxis = axis; }
const osg::Vec3d& getRotationAxis() const { return _rotationAxis; }
void setRotationSpeed(double speed) { _rotationSpeed = speed; }
double getRotationSpeed() const { return _rotationSpeed; }
virtual void update(osgViewer::View* view);
protected:
virtual ~CameraProperty() {}
osg::Vec3d _center;
osg::Vec3d _eye;
osg::Vec3d _up;
osg::Vec3d _rotationCenter;
osg::Vec3d _rotationAxis;
double _rotationSpeed;
};
}
#endif

View File

@@ -66,6 +66,19 @@ public:
Properties& getProperties() { return _properties; }
const Properties& getProperties() const { return _properties; }
template<typename T>
T* getPropertyOfType()
{
for(Properties::iterator itr = _properties.begin();
itr != _properties.end();
++itr)
{
T* p = dynamic_cast<T*>(itr->get());
if (p) return p;
}
return 0;
}
bool valid() const;
protected:

View File

@@ -5,6 +5,7 @@
#include <osg/AnimationPath>
#include "UpdateProperty.h"
#include "CameraProperty.h"
#include "CameraPathProperty.h"
#include "EventProperty.h"
@@ -65,6 +66,12 @@ int main( int argc, char **argv )
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of 3D textures.");
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
arguments.getApplicationUsage()->addCommandLineOption("--center x y z","View center");
arguments.getApplicationUsage()->addCommandLineOption("--eye x y z","Camera eye point");
arguments.getApplicationUsage()->addCommandLineOption("--up x y z","Camera up vector");
arguments.getApplicationUsage()->addCommandLineOption("--rotation-center x y z","Position to rotatate around");
arguments.getApplicationUsage()->addCommandLineOption("--rotation-axis x y z","Axis to rotate around");
arguments.getApplicationUsage()->addCommandLineOption("--rotation-speed v","Degrees per second");
osgViewer::Viewer viewer;
@@ -77,12 +84,13 @@ int main( int argc, char **argv )
double fps = 0.0f;
unsigned int nframes = 0;
bool readCaptureSettings = false;
std::string filename;
if (arguments.read("--cs",filename))
{
osg::ref_ptr<osg::Object> object = osgDB::readObjectFile(filename);
gsc::CaptureSettings* input_cs = dynamic_cast<gsc::CaptureSettings*>(object.get());
if (input_cs) fc = input_cs;
if (input_cs) { fc = input_cs; readCaptureSettings = true; }
else OSG_NOTICE<<"Unable to read CaptureSettings from file: "<<filename<<std::endl;
}
if (arguments.read("-i",filename)) fc->setInputFileName(filename);
@@ -117,8 +125,34 @@ int main( int argc, char **argv )
fc->addUpdateProperty(cpp.get());
}
else
{
osg::ref_ptr<gsc::CameraProperty> cp = fc->getPropertyOfType<gsc::CameraProperty>();
if (!cp)
{
cp = new gsc::CameraProperty;
osg::ref_ptr<osg::Node> node = fc->getInputFileName().empty() ? 0 : osgDB::readNodeFile(fc->getInputFileName());
if (node.valid()) cp->setToModel(node.get());
fc->addUpdateProperty(cp.get());
}
osg::Vec3d vec;
while (arguments.read("--center",vec.x(), vec.y(), vec.z())) { cp->setCenter(vec); }
while (arguments.read("--eye",vec.x(), vec.y(), vec.z())) { cp->setEyePoint(vec); }
while (arguments.read("--up",vec.x(), vec.y(), vec.z())) { cp->setUpVector(vec); }
while (arguments.read("--rotation-center",vec.x(), vec.y(), vec.z())) { cp->setRotationCenter(vec); }
while (arguments.read("--rotation-axis",vec.x(), vec.y(), vec.z())) { cp->setRotationAxis(vec); }
double speed;
while (arguments.read("--rotation-speed",speed)) { cp->setRotationSpeed(speed); }
}
if (arguments.read("--offscreen")) fc->setOffscreen(true);
if (arguments.read("--screen")) fc->setOffscreen(false);
unsigned int width = 1024;
if (arguments.read("-w",width)) fc->setWidth(width);
@@ -196,28 +230,31 @@ int main( int argc, char **argv )
}
if (duration!=0.0)
if (!readCaptureSettings)
{
if (fps!=0.0) nframes = static_cast<unsigned int>(ceil(duration*fps));
else if (nframes!=0) fps = duration/static_cast<double>(nframes);
else
if (duration!=0.0)
{
fps = 60.0;
nframes = static_cast<unsigned int>(ceil(duration/fps));
if (fps!=0.0) nframes = static_cast<unsigned int>(ceil(duration*fps));
else if (nframes!=0) fps = duration/static_cast<double>(nframes);
else
{
fps = 60.0;
nframes = static_cast<unsigned int>(ceil(duration/fps));
}
}
else // duration == 0.0
{
if (fps==0.0) fps=60.0;
if (nframes==0) nframes=1;
duration = static_cast<double>(nframes)/fps;
}
fc->setNumberOfFrames(nframes);
fc->setFrameRate(fps);
OSG_NOTICE<<"Duration="<<duration<<", FPS="<<fps<<", Number of Frames="<<nframes<<std::endl;
}
else // duration == 0.0
{
if (fps==0.0) fps=60.0;
if (nframes==0) nframes=1;
duration = static_cast<double>(nframes)/fps;
}
fc->setNumberOfFrames(nframes);
fc->setFrameRate(fps);
OSG_NOTICE<<"Duration="<<duration<<", FPS="<<fps<<", Number of Frames="<<nframes<<std::endl;