Added a ShadowTechnique::computeOrthogonalVector(const osg::Vec3& direction) const method for helping compute an appropriate

up vector to setViewMatrixAsLookAt(..) codes in osgShadow.  This will addresses previous issues that occured when look vectors
co-incided with the hard coded up vectors.
This commit is contained in:
Robert Osfield
2010-03-01 13:00:04 +00:00
parent 42a050e87c
commit ba3971bb45
4 changed files with 19 additions and 5 deletions

View File

@@ -71,6 +71,7 @@ class OSGSHADOW_EXPORT ShadowTechnique : public osg::Object
ShadowTechnique* _shadowTechnique;
};
osg::Vec3 computeOrthogonalVector(const osg::Vec3& direction) const;
virtual ~ShadowTechnique();

View File

@@ -408,7 +408,7 @@ void ShadowMap::cull(osgUtil::CullVisitor& cv)
{
osg::Vec3 position(lightpos.x(), lightpos.y(), lightpos.z());
_camera->setProjectionMatrixAsPerspective(fov, 1.0, 0.1, 1000.0);
_camera->setViewMatrixAsLookAt(position,position+lightDir,osg::Vec3(0.0f,1.0f,0.0f));
_camera->setViewMatrixAsLookAt(position,position+lightDir,computeOrthogonalVector(lightDir));
}
else
{
@@ -435,7 +435,7 @@ void ShadowMap::cull(osgUtil::CullVisitor& cv)
float right = top;
_camera->setProjectionMatrixAsFrustum(-right,right,-top,top,znear,zfar);
_camera->setViewMatrixAsLookAt(position,bb.center(),osg::Vec3(0.0f,1.0f,0.0f));
_camera->setViewMatrixAsLookAt(position,bb.center(),computeOrthogonalVector(bb.center()-position));
}
else // directional light
{
@@ -457,7 +457,7 @@ void ShadowMap::cull(osgUtil::CullVisitor& cv)
float right = top;
_camera->setProjectionMatrixAsOrtho(-right, right, -top, top, znear, zfar);
_camera->setViewMatrixAsLookAt(position,bb.center(),osg::Vec3(0.0f,1.0f,0.0f));
_camera->setViewMatrixAsLookAt(position,bb.center(),computeOrthogonalVector(lightDir));
}

View File

@@ -14,6 +14,7 @@
#include <osgShadow/ShadowTechnique>
#include <osgShadow/ShadowedScene>
#include <osg/Notify>
#include <osg/io_utils>
using namespace osgShadow;
@@ -92,3 +93,15 @@ void ShadowTechnique::traverse(osg::NodeVisitor& nv)
_shadowedScene->osg::Group::traverse(nv);
}
}
osg::Vec3 ShadowTechnique::computeOrthogonalVector(const osg::Vec3& direction) const
{
float length = direction.length();
osg::Vec3 orthogonalVector = direction ^ osg::Vec3(0.0f, 1.0f, 0.0f);
if (orthogonalVector.normalize()<length*0.5f)
{
orthogonalVector = direction ^ osg::Vec3(0.0f, 0.0f, 1.0f);
orthogonalVector.normalize();
}
return orthogonalVector;
}

View File

@@ -187,7 +187,7 @@ void ShadowTexture::cull(osgUtil::CullVisitor& cv)
_camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
_camera->setProjectionMatrixAsFrustum(-right,right,-top,top,znear,zfar);
_camera->setViewMatrixAsLookAt(position,bb.center(),osg::Vec3(0.0f,1.0f,0.0f));
_camera->setViewMatrixAsLookAt(position,bb.center(),computeOrthogonalVector(bb.center()-position));
// compute the matrix which takes a vertex from local coords into tex coords
@@ -221,7 +221,7 @@ void ShadowTexture::cull(osgUtil::CullVisitor& cv)
_camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
_camera->setProjectionMatrixAsOrtho(-right, right, -top, top, znear, zfar);
_camera->setViewMatrixAsLookAt(position,bb.center(),osg::Vec3(0.0f,1.0f,0.0f));
_camera->setViewMatrixAsLookAt(position,bb.center(),computeOrthogonalVector(lightDir));
// compute the matrix which takes a vertex from local coords into tex coords