From 8e9ffd09af113b0a2b833258481fb29488b37fa7 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 4 Oct 2005 13:41:20 +0000 Subject: [PATCH] Added osg::CameraView to help application/modellers position their cameras in scenes. Note, CameraView is *not* a camera, it isn't an active object, but a passive one that camera must track each frame to following the path of the CameraView. --- VisualStudio/osg/osg.dsp | 8 +++ include/osg/CameraView | 105 +++++++++++++++++++++++++++++++++++++++ src/osg/CameraView.cpp | 53 ++++++++++++++++++++ src/osg/GNUmakefile | 1 + 4 files changed, 167 insertions(+) create mode 100644 include/osg/CameraView create mode 100644 src/osg/CameraView.cpp diff --git a/VisualStudio/osg/osg.dsp b/VisualStudio/osg/osg.dsp index 5bb0f8996..8827cd670 100755 --- a/VisualStudio/osg/osg.dsp +++ b/VisualStudio/osg/osg.dsp @@ -152,6 +152,10 @@ SOURCE=..\..\src\osg\CameraNode.cpp # End Source File # Begin Source File +SOURCE=..\..\src\osg\CameraView.cpp +# End Source File +# Begin Source File + SOURCE=..\..\src\osg\ClearNode.cpp # End Source File # Begin Source File @@ -604,6 +608,10 @@ SOURCE=..\..\include\osg\CameraNode # End Source File # Begin Source File +SOURCE=..\..\include\osg\CameraView +# End Source File +# Begin Source File + SOURCE=..\..\include\osg\ClearNode # End Source File # Begin Source File diff --git a/include/osg/CameraView b/include/osg/CameraView new file mode 100644 index 000000000..ece9a1a40 --- /dev/null +++ b/include/osg/CameraView @@ -0,0 +1,105 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef OSG_CAMERAVIEW +#define OSG_CAMERAVIEW 1 + +#include +#include +#include +#include +#include + +namespace osg { + +/** CameraView - is a Transform that is used to specify camera views from within the scene graph. + * The application must attach a camera to a CameraView via the NodePath from the top of the scene graph + * to the CameraView node itself, and accimulate the view matrix from this NodePath. +*/ +class OSG_EXPORT CameraView : public Transform +{ + public : + CameraView(); + + CameraView(const CameraView& pat,const CopyOp& copyop=CopyOp::SHALLOW_COPY): + Transform(pat,copyop), + _position(pat._position), + _attitude(pat._attitude), + _fieldOfView(pat._fieldOfView), + _fieldOfViewMode(pat._fieldOfViewMode), + _focalLength(pat._focalLength) {} + + + META_Node(osg, CameraView); + + /** Set the position of the camera view.*/ + inline void setPosition(const Vec3d& pos) { _position = pos; dirtyBound(); } + + /** Get the position of the camera view.*/ + inline const Vec3d& getPosition() const { return _position; } + + /** Set the attitide of the camera view.*/ + inline void setAttitude(const Quat& quat) { _attitude = quat; dirtyBound(); } + + /** Get the attitide of the camera view.*/ + inline const Quat& getAttitude() const { return _attitude; } + + /** Set the field of view. + * The cameras field of view can be contrained to either the horizontal or vertex axis of the camera, or unconstrained + * in which case the camera/application are left to choose an appropriate field of view. + * The default value if 60 degrres. */ + inline void setFieldOfView(double fieldOfView) { _fieldOfView; } + + /** Get the field of view.*/ + inline double getFieldOfView() const { return _fieldOfView; } + + enum FieldOfViewMode + { + UNCONSTRAINED, + HORIZONTAL, + VERTICAL + }; + + /** Set the field of view mode - controlling how the field of view of the camera is contrained by the CameaView settings.*/ + inline void setFieldOfViewMode(FieldOfViewMode mode) { _fieldOfViewMode = mode; } + + /** Get the field of view mode.*/ + inline FieldOfViewMode getFieldOfViewMode() const { return _fieldOfViewMode; } + + /** Set the focal length of the camera. + * A focal length of 0.0 indicates that the camera/application should determine the focal length. + * The default value is 0.0. */ + inline void setFocalLength(double FocalLength) { _focalLength; } + + /** Get the focal length of the camera.*/ + inline double getFocalLength() const { return _focalLength; } + + + virtual bool computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor* nv) const; + virtual bool computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor* nv) const; + + + protected : + + virtual ~CameraView() {} + + Vec3d _position; + Quat _attitude; + double _fieldOfView; + FieldOfViewMode _fieldOfViewMode; + double _focalLength; +}; + +} + +#endif diff --git a/src/osg/CameraView.cpp b/src/osg/CameraView.cpp new file mode 100644 index 000000000..b8c959242 --- /dev/null +++ b/src/osg/CameraView.cpp @@ -0,0 +1,53 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ +#include + +using namespace osg; + +CameraView::CameraView(): + _fieldOfView(60.0), + _fieldOfViewMode(VERTICAL), + _focalLength(0.0) +{ +} + +bool CameraView::computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const +{ + if (_referenceFrame==RELATIVE_RF) + { + matrix.preMult(osg::Matrix::rotate(_attitude)* + osg::Matrix::translate(_position)); + } + else // absolute + { + matrix = osg::Matrix::rotate(_attitude)* + osg::Matrix::translate(_position); + } + return true; +} + + +bool CameraView::computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const +{ + if (_referenceFrame==RELATIVE_RF) + { + matrix.postMult(osg::Matrix::translate(-_position)* + osg::Matrix::rotate(_attitude.inverse())); + } + else // absolute + { + matrix = osg::Matrix::translate(-_position)* + osg::Matrix::rotate(_attitude.inverse()); + } + return true; +} diff --git a/src/osg/GNUmakefile b/src/osg/GNUmakefile index 733640772..1b9f381ea 100644 --- a/src/osg/GNUmakefile +++ b/src/osg/GNUmakefile @@ -16,6 +16,7 @@ CXXFILES =\ BoundingSphere.cpp\ BufferObject.cpp\ CameraNode.cpp\ + CameraView.cpp\ ClearNode.cpp\ ClipNode.cpp\ ClipPlane.cpp\