2003-03-15 04:35:45 +08:00
|
|
|
#include <osg/Texture2D>
|
|
|
|
#include <osg/Material>
|
|
|
|
#include <osg/LightSource>
|
|
|
|
#include <osg/Geode>
|
2005-07-10 22:50:52 +08:00
|
|
|
#include <osg/Geometry>
|
2003-03-15 04:35:45 +08:00
|
|
|
#include <osg/ShapeDrawable>
|
2006-11-27 22:52:07 +08:00
|
|
|
#include <osg/Camera>
|
2005-07-10 22:50:52 +08:00
|
|
|
#include <osg/TexGenNode>
|
|
|
|
#include <osg/Notify>
|
|
|
|
#include <osg/io_utils>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
#include "CreateShadowedScene.h"
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
class UpdateCameraAndTexGenCallback : public osg::NodeCallback
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
UpdateCameraAndTexGenCallback(const osg::Vec3& position, osg::Camera* Camera, osg::TexGenNode* texgenNode):
|
2003-03-15 04:35:45 +08:00
|
|
|
_position(position),
|
2006-11-27 22:52:07 +08:00
|
|
|
_Camera(Camera),
|
2005-07-10 22:50:52 +08:00
|
|
|
_texgenNode(texgenNode)
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
|
|
|
|
{
|
2005-07-10 22:50:52 +08:00
|
|
|
// first update subgraph to make sure objects are all moved into postion
|
|
|
|
traverse(node,nv);
|
|
|
|
|
|
|
|
// now compute the camera's view and projection matrix to point at the shadower (the camera's children)
|
|
|
|
osg::BoundingSphere bs;
|
2006-11-27 22:52:07 +08:00
|
|
|
for(unsigned int i=0; i<_Camera->getNumChildren(); ++i)
|
2005-07-10 22:50:52 +08:00
|
|
|
{
|
2006-11-27 22:52:07 +08:00
|
|
|
bs.expandBy(_Camera->getChild(i)->getBound());
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
2005-07-10 22:50:52 +08:00
|
|
|
|
|
|
|
if (!bs.valid())
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
2006-11-27 22:52:07 +08:00
|
|
|
osg::notify(osg::WARN) << "bb invalid"<<_Camera.get()<<std::endl;
|
2005-07-10 22:50:52 +08:00
|
|
|
return;
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
float centerDistance = (_position-bs.center()).length();
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
float znear = centerDistance-bs.radius();
|
|
|
|
float zfar = centerDistance+bs.radius();
|
|
|
|
float zNearRatio = 0.001f;
|
|
|
|
if (znear<zfar*zNearRatio) znear = zfar*zNearRatio;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
float top = (bs.radius()/centerDistance)*znear;
|
|
|
|
float right = top;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
_Camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
|
|
|
|
_Camera->setProjectionMatrixAsFrustum(-right,right,-top,top,znear,zfar);
|
|
|
|
_Camera->setViewMatrixAsLookAt(_position,bs.center(),osg::Vec3(0.0f,1.0f,0.0f));
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// compute the matrix which takes a vertex from local coords into tex coords
|
|
|
|
// will use this later to specify osg::TexGen..
|
2006-11-27 22:52:07 +08:00
|
|
|
osg::Matrix MVPT = _Camera->getViewMatrix() *
|
|
|
|
_Camera->getProjectionMatrix() *
|
2005-07-10 22:50:52 +08:00
|
|
|
osg::Matrix::translate(1.0,1.0,1.0) *
|
|
|
|
osg::Matrix::scale(0.5f,0.5f,0.5f);
|
|
|
|
|
|
|
|
_texgenNode->getTexGen()->setMode(osg::TexGen::EYE_LINEAR);
|
|
|
|
_texgenNode->getTexGen()->setPlanesFromMatrix(MVPT);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
virtual ~UpdateCameraAndTexGenCallback() {}
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
osg::Vec3 _position;
|
2006-11-27 22:52:07 +08:00
|
|
|
osg::ref_ptr<osg::Camera> _Camera;
|
2005-07-10 22:50:52 +08:00
|
|
|
osg::ref_ptr<osg::TexGenNode> _texgenNode;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
};
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
osg::Group* createShadowedScene(osg::Node* shadower,osg::Node* shadowed,const osg::Vec3& lightPosition,float radius,unsigned int unit)
|
|
|
|
{
|
|
|
|
osg::Group* group = new osg::Group;
|
|
|
|
|
|
|
|
// add light source
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
2005-07-10 22:50:52 +08:00
|
|
|
osg::LightSource* lightSource = new osg::LightSource;
|
|
|
|
lightSource->getLight()->setPosition(osg::Vec4(lightPosition,1.0f));
|
|
|
|
lightSource->getLight()->setLightNum(0);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
group->addChild(lightSource);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
osg::Geode* lightgeode = new osg::Geode;
|
|
|
|
lightgeode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
|
|
|
|
lightgeode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(lightPosition,radius)));
|
|
|
|
group->addChild(lightgeode);
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
2005-07-10 22:50:52 +08:00
|
|
|
|
|
|
|
osg::Vec4 ambientLightColor(0.2,0.2f,0.2f,1.0f);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// add the shadower and shadowed.
|
|
|
|
group->addChild(shadower);
|
|
|
|
group->addChild(shadowed);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
|
|
|
|
unsigned int tex_width = 512;
|
|
|
|
unsigned int tex_height = 512;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
osg::Texture2D* texture = new osg::Texture2D;
|
|
|
|
texture->setTextureSize(tex_width, tex_height);
|
|
|
|
texture->setInternalFormat(GL_RGB);
|
|
|
|
texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
|
|
|
|
texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
|
|
|
|
texture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_BORDER);
|
|
|
|
texture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_BORDER);
|
|
|
|
texture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
|
|
|
|
|
|
|
|
// set up the render to texture camera.
|
|
|
|
{
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// create the camera
|
2006-11-27 22:52:07 +08:00
|
|
|
osg::Camera* camera = new osg::Camera;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
camera->setClearColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// set viewport
|
|
|
|
camera->setViewport(0,0,tex_width,tex_height);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// set the camera to render before the main camera.
|
2006-11-27 22:52:07 +08:00
|
|
|
camera->setRenderOrder(osg::Camera::PRE_RENDER);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// tell the camera to use OpenGL frame buffer object where supported.
|
2006-11-27 22:52:07 +08:00
|
|
|
camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
|
2004-07-09 06:12:49 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// attach the texture and use it as the color buffer.
|
2006-11-27 22:52:07 +08:00
|
|
|
camera->attach(osg::Camera::COLOR_BUFFER, texture);
|
2004-07-09 06:12:49 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// add subgraph to render
|
|
|
|
camera->addChild(shadower);
|
|
|
|
|
|
|
|
osg::StateSet* stateset = camera->getOrCreateStateSet();
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// make the material black for a shadow.
|
|
|
|
osg::Material* material = new osg::Material;
|
|
|
|
material->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(0.0f,0.0f,0.0f,1.0f));
|
|
|
|
material->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(0.0f,0.0f,0.0f,1.0f));
|
|
|
|
material->setEmission(osg::Material::FRONT_AND_BACK,ambientLightColor);
|
|
|
|
material->setShininess(osg::Material::FRONT_AND_BACK,0.0f);
|
|
|
|
stateset->setAttribute(material,osg::StateAttribute::OVERRIDE);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
group->addChild(camera);
|
|
|
|
|
|
|
|
// create the texgen node to project the tex coords onto the subgraph
|
|
|
|
osg::TexGenNode* texgenNode = new osg::TexGenNode;
|
|
|
|
texgenNode->setTextureUnit(unit);
|
|
|
|
group->addChild(texgenNode);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// set an update callback to keep moving the camera and tex gen in the right direction.
|
|
|
|
group->setUpdateCallback(new UpdateCameraAndTexGenCallback(lightPosition, camera, texgenNode));
|
|
|
|
}
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// set the shadowed subgraph so that it uses the texture and tex gen settings.
|
|
|
|
{
|
|
|
|
osg::StateSet* stateset = shadowed->getOrCreateStateSet();
|
|
|
|
stateset->setTextureAttributeAndModes(unit,texture,osg::StateAttribute::ON);
|
|
|
|
stateset->setTextureMode(unit,GL_TEXTURE_GEN_S,osg::StateAttribute::ON);
|
|
|
|
stateset->setTextureMode(unit,GL_TEXTURE_GEN_T,osg::StateAttribute::ON);
|
|
|
|
stateset->setTextureMode(unit,GL_TEXTURE_GEN_R,osg::StateAttribute::ON);
|
|
|
|
stateset->setTextureMode(unit,GL_TEXTURE_GEN_Q,osg::StateAttribute::ON);
|
|
|
|
}
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// set hud to render shadow texture, just for interest
|
|
|
|
{
|
|
|
|
osg::Geode* geode = new osg::Geode;
|
|
|
|
osg::Geometry* geom = osg::createTexturedQuadGeometry(osg::Vec3(0,0,0),osg::Vec3(100.0,0.0,0.0),osg::Vec3(0.0,100.0,0.0));
|
|
|
|
geom->getOrCreateStateSet()->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
|
|
|
|
geom->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
|
|
|
|
geode->addDrawable(geom);
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
osg::Camera* camera = new osg::Camera;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// set the projection matrix
|
|
|
|
camera->setProjectionMatrix(osg::Matrix::ortho2D(0,100,0,100));
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// set the view matrix
|
|
|
|
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
|
|
|
|
camera->setViewMatrix(osg::Matrix::identity());
|
|
|
|
|
|
|
|
camera->setViewport(50,50,100,100);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// only clear the depth buffer
|
|
|
|
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
// draw subgraph after main camera view.
|
2006-11-27 22:52:07 +08:00
|
|
|
camera->setRenderOrder(osg::Camera::POST_RENDER);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
camera->addChild(geode);
|
|
|
|
|
|
|
|
group->addChild(camera);
|
|
|
|
|
|
|
|
}
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-07-10 22:50:52 +08:00
|
|
|
return group;
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|