Hi Art, thanks for your attention. I've constructed an example for you by
minimally modifying Example viewer. Below is the modified view.cpp. It has an
additional createScene function that sets up a shadowed scene. Also note the
#define ENABLE_SHADOWS. Commenting that out will use the default scene instead
of the shadowed one for comparison.
Michael
PS You'll have to link to osgShadow as well of course
#include <osg/GLExtensions>
#include <osgViewer/Renderer>
#include <osgGA/TrackballManipulator>
#include <osgDB/WriteFile>
#include <osgPPU/Processor.h>
#include "osgteapot.h"
#define ENABLE_SHADOWS
#if defined (ENABLE_SHADOWS)
# include <osgShadow/ShadowedScene>
# include <osgShadow/ShadowMap>
# include <osg/LightSource>
#endif
//--------------------------------------------------------------------------
// Create camera resulting texture
//--------------------------------------------------------------------------
osg::Texture* createRenderTexture(int tex_width, int tex_height, bool depth)
{
// create simple 2D texture
osg::Texture2D* texture2D = new osg::Texture2D;
texture2D->setTextureSize(tex_width, tex_height);
texture2D->setResizeNonPowerOfTwoHint(false);
texture2D->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
texture2D->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
texture2D->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_BORDER);
texture2D->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_BORDER);
texture2D->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
// setup float format
if (!depth)
{
texture2D->setInternalFormat(GL_RGBA16F_ARB);
texture2D->setSourceFormat(GL_RGBA);
texture2D->setSourceType(GL_FLOAT);
}else{
texture2D->setInternalFormat(GL_DEPTH_COMPONENT);
}
return texture2D;
}
//--------------------------------------------------------------------------
// Create scene
//--------------------------------------------------------------------------
osg::Group* createScene(const std::string& filename)
{
osg::Node* loadedModel = NULL;
// Load in the model to use in our scene
if (!filename.empty()) loadedModel = osgDB::readNodeFile(filename);
if (!loadedModel) loadedModel = createTeapot();
if (!loadedModel) return NULL;
#if defined (ENABLE_SHADOWS)
const int ReceivesShadowTraversalMask = 0x1;
const int CastsShadowTraversalMask = 0x2;
osgShadow::ShadowedScene* shadowedScene = new osgShadow::ShadowedScene;
shadowedScene->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask);
shadowedScene->setCastsShadowTraversalMask(CastsShadowTraversalMask);
osg::LightSource* lightSource = new osg::LightSource;
lightSource->getLight()->setPosition(
osg::Vec4(osg::Vec3(100.0, 0.0, 100.0f), 0.0f));
// Setup the scene with shadows via shadow mapping
osgShadow::ShadowMap* shadowMap = new osgShadow::ShadowMap;
shadowMap->setTextureSize(osg::Vec2s(1024, 1024));
shadowMap->setLight(lightSource);
shadowedScene->setShadowTechnique(shadowMap);
shadowedScene->addChild(lightSource);
return shadowedScene;
#else
osg::Group* scene = new osg::Group;
scene->addChild(loadedModel);
return scene;
#endif
}
//--------------------------------------------------------------------------
// Setup the camera to do the render to texture
//--------------------------------------------------------------------------
void setupCamera(osg::Camera* camera)
{
osg::Viewport* vp = camera->getViewport();
// create texture to render to
osg::Texture* texture = createRenderTexture((int)vp->width(),
(int)vp->height(), false);
osg::Texture* depthTexture = createRenderTexture((int)vp->width(),
(int)vp->height(), true);
// set up the background color and clear mask.
camera->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,0.0f));
camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set viewport
camera->setViewport(vp);
camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
camera->setProjectionMatrixAsPerspective(35.0, vp->width()/vp->height(),
1.0, 256.0);
// tell the camera to use OpenGL frame buffer object where supported.
camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
// attach the texture and use it as the color buffer.
camera->attach(osg::Camera::COLOR_BUFFER, texture);
camera->attach(osg::Camera::DEPTH_BUFFER, depthTexture);
}
//--------------------------------------------------------------------------
int main(int argc, char **argv)
{
// parse arguments
osg::ArgumentParser arguments(&argc,argv);
// give some info in the console
printf("view ppufile [osgfile]\n");
if (argc <= 1) return 0;
// construct the viewer.
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
unsigned int screenWidth;
unsigned int screenHeight;
osg::GraphicsContext::getWindowingSystemInterface()->getScreenResolution(
osg::GraphicsContext::ScreenIdentifier(0), screenWidth, screenHeight);
unsigned int windowWidth = 640;
unsigned int windowHeight = 480;
viewer->setUpViewInWindow((screenWidth-windowWidth)/2,
(screenHeight-windowHeight)/2, windowWidth, windowHeight);
// setup scene
osg::Group* node = createScene((argc > 2) ? arguments[2]: "");
// disable color clamping, because we want to work on real hdr values
osg::ClampColor* clamp = new osg::ClampColor();
clamp->setClampVertexColor(GL_FALSE);
clamp->setClampFragmentColor(GL_FALSE);
clamp->setClampReadColor(GL_FALSE);
// make it protected and override, so that it is done for the whole
rendering pipeline
node->getOrCreateStateSet()->setAttribute(clamp, osg::StateAttribute::ON |
osg::StateAttribute::OVERRIDE | osg::StateAttribute::PROTECTED);
// load the processor from a file
osg::ref_ptr<osgPPU::Processor> processor =
dynamic_cast<osgPPU::Processor*>(osgDB::readObjectFile(arguments[1]));
if (!processor)
{
osg::notify(osg::FATAL) << "File does not contain a valid pipeline" <<
std::endl;
return 0;
}
// setup viewers camera
setupCamera(viewer->getCamera());
processor->setCamera(viewer->getCamera());
// add processor to the scene
node->addChild(processor.get());
// add model to viewer.
viewer->setSceneData( node );
// run viewer
return viewer->run();
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org