From 868d381528cde80e01bf46e9ac6d8d81bd4c3d15 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 14 Jun 2005 13:16:58 +0000 Subject: [PATCH] Added osg::CameraNode. --- VisualStudio/osg/osg.dsp | 8 ++ include/osg/CameraNode | 177 ++++++++++++++++++++++++++++++++++++ include/osg/MatrixTransform | 1 - include/osg/NodeVisitor | 2 + src/osg/CameraNode.cpp | 139 ++++++++++++++++++++++++++++ src/osg/GNUmakefile | 1 + 6 files changed, 327 insertions(+), 1 deletion(-) create mode 100644 include/osg/CameraNode create mode 100644 src/osg/CameraNode.cpp diff --git a/VisualStudio/osg/osg.dsp b/VisualStudio/osg/osg.dsp index 850c776b3..ee7d4f042 100755 --- a/VisualStudio/osg/osg.dsp +++ b/VisualStudio/osg/osg.dsp @@ -148,6 +148,10 @@ SOURCE=..\..\src\osg\BufferObject.cpp # End Source File # Begin Source File +SOURCE=..\..\src\osg\CameraNode.cpp +# End Source File +# Begin Source File + SOURCE=..\..\src\osg\ClearNode.cpp # End Source File # Begin Source File @@ -580,6 +584,10 @@ SOURCE=..\..\include\osg\BufferObject # End Source File # Begin Source File +SOURCE=..\..\include\osg\CameraNode +# End Source File +# Begin Source File + SOURCE=..\..\include\osg\ClearNode # End Source File # Begin Source File diff --git a/include/osg/CameraNode b/include/osg/CameraNode new file mode 100644 index 000000000..a985c1728 --- /dev/null +++ b/include/osg/CameraNode @@ -0,0 +1,177 @@ +/* -*-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_CAMERANODE +#define OSG_CAMERANODE 1 + +#include +#include + +namespace osg { + +/** CameraNode - is a subclass of Transform which represents encapsulates the settings of a Camera. +*/ +class OSG_EXPORT CameraNode : public Transform +{ + public : + + + CameraNode(); + + /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ + CameraNode(const CameraNode&,const CopyOp& copyop=CopyOp::SHALLOW_COPY); + + META_Node(osg, CameraNode); + + + /** Sets the clear color. */ + inline void setClearColor(const Vec4& color) { _clearColor = color; } + + /** Returns the clear color. */ + inline const Vec4& getClearColor() const { return _clearColor; } + + /** Set the clear mask used in glClear(..). + * Defaults to GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT. */ + inline void setClearMask(GLbitfield mask) { _clearMask = mask; } + + /** Get the clear mask.*/ + inline GLbitfield getClearMask() const { return _clearMask; } + + + + /** Set the viewport of the scene view to use specified osg::Viewport. */ + void setViewport(osg::Viewport* viewport) + { + _viewport = viewport; + } + + /** Set the viewport of the scene view to specified dimensions. */ + void setViewport(int x,int y,int width,int height) + { + if (!_viewport) _viewport = new osg::Viewport; + _viewport->setViewport(x,y,width,height); + } + + /** Get the const viewport. */ + const Viewport* getViewport() const { return _viewport.get(); } + + /** Get the viewport. */ + Viewport* getViewport() { return _viewport.get(); } + + /** Get the viewport of the scene view. */ + void getViewport(int& x,int& y,int& width,int& height) const + { + if (_viewport.valid()) _viewport->getViewport(x,y,width,height); + } + + + /** Set the projection matrix. Can be thought of as setting the lens of a camera. */ + inline void setProjectionMatrix(const osg::Matrixf& matrix) { _projectionMatrix.set(matrix); } + + /** Set the projection matrix. Can be thought of as setting the lens of a camera. */ + inline void setProjectionMatrix(const osg::Matrixd& matrix) { _projectionMatrix.set(matrix); } + + /** Set to an orthographic projection. See OpenGL glOrtho for documentation further details.*/ + void setProjectionMatrixAsOrtho(double left, double right, + double bottom, double top, + double zNear, double zFar); + + /** Set to a 2D orthographic projection. See OpenGL glOrtho2D documentation for further details.*/ + void setProjectionMatrixAsOrtho2D(double left, double right, + double bottom, double top); + + /** Set to a perspective projection. See OpenGL glFrustum documentation for further details.*/ + void setProjectionMatrixAsFrustum(double left, double right, + double bottom, double top, + double zNear, double zFar); + + /** Create a symmetrical perspective projection, See OpenGL gluPerspective documentation for further details. + * Aspect ratio is defined as width/height.*/ + void setProjectionMatrixAsPerspective(double fovy,double aspectRatio, + double zNear, double zFar); + + /** Get the projection matrix.*/ + osg::Matrixd& getProjectionMatrix() { return _projectionMatrix; } + + /** Get the const projection matrix.*/ + const osg::Matrixd& getProjectionMatrix() const { return _projectionMatrix; } + + /** Get the othographic settings of the orthographic projection matrix. + * Returns false if matrix is not an orthographic matrix, where parameter values are undefined.*/ + bool getProjectionMatrixAsOrtho(double& left, double& right, + double& bottom, double& top, + double& zNear, double& zFar); + + /** Get the frustum setting of a perspective projection matrix. + * Returns false if matrix is not a perspective matrix, where parameter values are undefined.*/ + bool getProjectionMatrixAsFrustum(double& left, double& right, + double& bottom, double& top, + double& zNear, double& zFar); + + /** Get the frustum setting of a symmetric perspective projection matrix. + * Returns false if matrix is not a perspective matrix, where parameter values are undefined. + * Note, if matrix is not a symmetric perspective matrix then the shear will be lost. + * Asymmetric matrices occur when stereo, power walls, caves and reality center display are used. + * In these configurations one should use the 'getProjectionMatrixAsFrustum' method instead.*/ + bool getProjectionMatrixAsPerspective(double& fovy,double& aspectRatio, + double& zNear, double& zFar); + + + + /** Set the view matrix. Can be thought of as setting the position of the world relative to the camera in camera coordinates. */ + inline void setViewMatrix(const osg::Matrixf& matrix) { _viewMatrix.set(matrix); dirtyBound();} + + /** Set the view matrix. Can be thought of as setting the position of the world relative to the camera in camera coordinates. */ + inline void setViewMatrix(const osg::Matrixd& matrix) { _viewMatrix.set(matrix); dirtyBound();} + + /** Set to the position and orientation of view matrix, using the same convention as gluLookAt. */ + void setViewMatrixAsLookAt(const osg::Vec3& eye,const osg::Vec3& center,const osg::Vec3& up); + + /** Get the view matrix. */ + osg::Matrixd& getViewMatrix() { return _viewMatrix; } + + /** Get the const view matrix. */ + const osg::Matrixd& getViewMatrix() const { return _viewMatrix; } + + /** Get to the position and orientation of a modelview matrix, using the same convention as gluLookAt. */ + void getViewMatrixAsLookAt(osg::Vec3& eye,osg::Vec3& center,osg::Vec3& up,float lookDistance=1.0f); + + /** Get the inverse view matrix.*/ + Matrixd getInverseViewMatrix() const; + + + + + + + /** Transform method that must be defined to provide generic interface for scene graph traversals.*/ + virtual bool computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const; + + /** Transform method that must be defined to provide generic interface for scene graph traversals.*/ + virtual bool computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const; + + protected : + + virtual ~CameraNode(); + + Vec4 _clearColor; + GLbitfield _clearMask; + ref_ptr _viewport; + Matrixd _projectionMatrix; + Matrixd _viewMatrix; + +}; + +} + +#endif diff --git a/include/osg/MatrixTransform b/include/osg/MatrixTransform index edee53e20..926e4653d 100644 --- a/include/osg/MatrixTransform +++ b/include/osg/MatrixTransform @@ -15,7 +15,6 @@ #define OSG_MATRIXTRANSFORM 1 #include -#include namespace osg { diff --git a/include/osg/NodeVisitor b/include/osg/NodeVisitor index 738cbcde4..a7c0b8c69 100644 --- a/include/osg/NodeVisitor +++ b/include/osg/NodeVisitor @@ -38,6 +38,7 @@ class Sequence; class Switch; class TexGenNode; class Transform; +class CameraNode; /** Visitor for type safe operations on osg::Nodes. Based on GOF's Visitor pattern. The NodeVisitor @@ -233,6 +234,7 @@ class OSG_EXPORT NodeVisitor : public virtual Referenced virtual void apply(LightSource& node) { apply((Group&)node); } virtual void apply(Transform& node) { apply((Group&)node); } + virtual void apply(CameraNode& node) { apply((Group&)node); } virtual void apply(MatrixTransform& node) { apply((Transform&)node); } virtual void apply(PositionAttitudeTransform& node) { apply((Transform&)node); } diff --git a/src/osg/CameraNode.cpp b/src/osg/CameraNode.cpp new file mode 100644 index 000000000..4d027a0cd --- /dev/null +++ b/src/osg/CameraNode.cpp @@ -0,0 +1,139 @@ +/* -*-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; + + + +CameraNode::CameraNode(): + _clearColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f)), + _clearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) +{ +} + +CameraNode::CameraNode(const CameraNode& camera,const CopyOp& copyop): + Transform(camera,copyop), + _clearColor(camera._clearColor), + _clearMask(camera._clearMask), + _viewport(camera._viewport), + _projectionMatrix(camera._projectionMatrix), + _viewMatrix(camera._viewMatrix) +{ +} + + +CameraNode::~CameraNode() +{ +} + +Matrixd CameraNode::getInverseViewMatrix() const +{ + Matrixd inverse; + inverse.invert(_viewMatrix); + return inverse; +} +void CameraNode::setProjectionMatrixAsOrtho(double left, double right, + double bottom, double top, + double zNear, double zFar) +{ + setProjectionMatrix(osg::Matrixd::ortho(left, right, + bottom, top, + zNear, zFar)); +} + +void CameraNode::setProjectionMatrixAsOrtho2D(double left, double right, + double bottom, double top) +{ + setProjectionMatrix(osg::Matrixd::ortho2D(left, right, + bottom, top)); +} + +void CameraNode::setProjectionMatrixAsFrustum(double left, double right, + double bottom, double top, + double zNear, double zFar) +{ + setProjectionMatrix(osg::Matrixd::frustum(left, right, + bottom, top, + zNear, zFar)); +} + +void CameraNode::setProjectionMatrixAsPerspective(double fovy,double aspectRatio, + double zNear, double zFar) +{ + setProjectionMatrix(osg::Matrixd::perspective(fovy,aspectRatio, + zNear, zFar)); +} + +bool CameraNode::getProjectionMatrixAsOrtho(double& left, double& right, + double& bottom, double& top, + double& zNear, double& zFar) +{ + return _projectionMatrix.getOrtho(left, right, + bottom, top, + zNear, zFar); +} + +bool CameraNode::getProjectionMatrixAsFrustum(double& left, double& right, + double& bottom, double& top, + double& zNear, double& zFar) +{ + return _projectionMatrix.getFrustum(left, right, + bottom, top, + zNear, zFar); +} + +bool CameraNode::getProjectionMatrixAsPerspective(double& fovy,double& aspectRatio, + double& zNear, double& zFar) +{ + return _projectionMatrix.getPerspective(fovy, aspectRatio, zNear, zFar); +} + +void CameraNode::setViewMatrixAsLookAt(const Vec3& eye,const Vec3& center,const Vec3& up) +{ + setViewMatrix(osg::Matrixd::lookAt(eye,center,up)); +} + +void CameraNode::getViewMatrixAsLookAt(Vec3& eye,Vec3& center,Vec3& up,float lookDistance) +{ + _viewMatrix.getLookAt(eye,center,up,lookDistance); +} + +bool CameraNode::computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const +{ + if (_referenceFrame==RELATIVE_RF) + { + matrix.preMult(_viewMatrix); + } + else // absolute + { + matrix = _viewMatrix; + } + return true; +} + +bool CameraNode::computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const +{ + const Matrixd& inverse = getInverseViewMatrix(); + + if (_referenceFrame==RELATIVE_RF) + { + matrix.postMult(inverse); + } + else // absolute + { + matrix = inverse; + } + return true; +} + diff --git a/src/osg/GNUmakefile b/src/osg/GNUmakefile index da7ab5bfa..48fd333c3 100644 --- a/src/osg/GNUmakefile +++ b/src/osg/GNUmakefile @@ -15,6 +15,7 @@ CXXFILES =\ BoundingBox.cpp\ BoundingSphere.cpp\ BufferObject.cpp\ + CameraNode.cpp\ ClearNode.cpp\ ClipNode.cpp\ ClipPlane.cpp\