They say a problem shared is a problem solved. Here's my hackish code
for dome correction, based on osgprerendercubemap. I don't know C++ nor
OSG very well. I'd like to be able to get the camera frustum to take
over from the viewing camera, in terms of being able to fly/steer around
models:
*********************
#include <osgProducer/Viewer>
#include <osgDB/ReadFile>
#include <osg/Projection>
#include <osg/Geometry>
#include <osg/Texture>
#include <osg/TexGen>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/PolygonOffset>
#include <osg/CullFace>
#include <osg/TextureCubeMap>
#include <osg/TexMat>
#include <osg/MatrixTransform>
#include <osg/Light>
#include <osg/LightSource>
#include <osg/PolygonOffset>
#include <osg/CullFace>
#include <osg/Material>
//#include <osg/NodePath>
#include <osg/PositionAttitudeTransform>
#include <osg/CameraNode>
#include <osg/TexGenNode>
using namespace osg;
ref_ptr<Group> _create_scene()
{
ref_ptr<Group> scene = new Group;
ref_ptr<Geode> geode_1 = new Geode;
scene->addChild(geode_1.get());
ref_ptr<Geode> geode_2 = new Geode;
ref_ptr<MatrixTransform> transform_2 = new MatrixTransform;
transform_2->addChild(geode_2.get());
transform_2->setUpdateCallback(new
osg::AnimationPathCallback(Vec3(25, 25, 20), Z_AXIS, inDegrees(-15.0f)));
scene->addChild(transform_2.get());
/*ref_ptr<Geode> geode_3 = new Geode;
ref_ptr<MatrixTransform> transform_3 = new MatrixTransform;
transform_3->addChild(geode_3.get());
transform_3->setUpdateCallback(new osg::AnimationPathCallback(Vec3(0,
0, 0), Y_AXIS, inDegrees(-22.5f)));
scene->addChild(transform_3.get());
*/
/*
const float radius = 0.8f;
const float height = 1.0f;
*/
ref_ptr<TessellationHints> hints = new TessellationHints;
hints->setDetailRatio(2.0f);
ref_ptr<ShapeDrawable> shape;
//load model
osg::Node* modelNode = osgDB::readNodeFile("test003.osg");
//scene->addChild(modelNode);
transform_2->addChild(modelNode);
// material
ref_ptr<Material> matirial = new Material;
matirial->setColorMode(Material::DIFFUSE);
matirial->setAmbient(Material::BACK, Vec4(1, 1, 1, 1));
matirial->setSpecular(Material::BACK, Vec4(1, 1, 1, 1));
matirial->setShininess(Material::BACK, 128.0f);
scene->getOrCreateStateSet()->setAttributeAndModes(matirial.get(),
StateAttribute::ON);
return scene;
}
osg::NodePath createReflector()
{
osg::PositionAttitudeTransform* pat = new
osg::PositionAttitudeTransform;
//pete
pat->setPosition(osg::Vec3(0.0f,0.0f,0.0f));
pat->setAttitude(osg::Quat(osg::inDegrees(0.0f),osg::Vec3(0.0f,0.0f,1.0f
)));
Geode* geode_1 = new Geode;
pat->addChild(geode_1);
const float radius = 200.0f;
ref_ptr<TessellationHints> hints = new TessellationHints;
hints->setDetailRatio(2.0f);
ShapeDrawable* shape = new ShapeDrawable(new Sphere(Vec3(0.0f, 0.0f,
0.0f), radius * 10.0f), hints.get());
shape->setColor(Vec4(4.0f, 4.0f, 4.0f, 0.8f));
//cull front faces
osg::StateSet* stateset = shape->getOrCreateStateSet();
osg::CullFace* cullFace = new osg::CullFace();
cullFace->setMode(osg::CullFace::FRONT);
stateset->setAttribute(cullFace);
stateset->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
stateset->setAttributeAndModes(cullFace, osg::StateAttribute::ON);
shape->setStateSet(stateset);
geode_1->addDrawable(shape);
osg::NodePath NodeList;
NodeList.push_back(pat);
NodeList.push_back(geode_1);
return NodeList;
}
class UpdateCameraAndTexGenCallback : public osg::NodeCallback
{
public:
typedef std::vector< osg::ref_ptr<osg::CameraNode> > CameraList;
UpdateCameraAndTexGenCallback(osg::NodePath& reflectorNodePath,
CameraList& cameraNodes):
_reflectorNodePath(reflectorNodePath),
_cameraNodes(cameraNodes)
{
}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
// first update subgraph to make sure objects are all moved
into postion
traverse(node,nv);
// compute the position of the center of the reflector
subgraph
osg::Matrixd worldToLocal =
osg::computeWorldToLocal(_reflectorNodePath);
osg::BoundingSphere bs =
_reflectorNodePath.back()->getBound();
//osg::Vec3 position = bs.center();
osg::Vec3 position = Vec3(0,0,25);
typedef std::pair<osg::Vec3, osg::Vec3> ImageData;
const ImageData id[] =
{
ImageData( osg::Vec3( 1, 0, 0), osg::Vec3( 0, -1, 0)
), // +X
ImageData( osg::Vec3(-1, 0, 0), osg::Vec3( 0, -1, 0)
), // -X
ImageData( osg::Vec3( 0, 1, 0), osg::Vec3( 0, 0, 1)
), // +Y
ImageData( osg::Vec3( 0, -1, 0), osg::Vec3( 0, 0, -1)
), // -Y
ImageData( osg::Vec3( 0, 0, 1), osg::Vec3( 0, -1, 0)
), // +Z
ImageData( osg::Vec3( 0, 0, -1), osg::Vec3( 0, -1, 0)
) // -Z
};
for(unsigned int i=0;
i<6 && i<_cameraNodes.size();
++i)
{
osg::Matrix localOffset;
localOffset.makeLookAt(position,position+id[i].first,id[i].second);
osg::Matrix viewMatrix = worldToLocal*localOffset;
_cameraNodes[i]->setReferenceFrame(osg::CameraNode::ABSOLUTE_RF);
_cameraNodes[i]->setProjectionMatrixAsFrustum(-1.0,1.0,-1.0,1.0,1.0,
10000.0);
_cameraNodes[i]->setViewMatrix(viewMatrix);
}
}
protected:
virtual ~UpdateCameraAndTexGenCallback() {}
osg::NodePath _reflectorNodePath;
CameraList _cameraNodes;
};
class TexMatCullCallback : public osg::NodeCallback
{
public:
TexMatCullCallback(osg::TexMat* texmat):
_texmat(texmat)
{
}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
// first update subgraph to make sure objects are all moved
into postion
traverse(node,nv);
osgUtil::CullVisitor* cv =
dynamic_cast<osgUtil::CullVisitor*>(nv);
if (cv)
{
osg::Quat quat;
cv->getModelViewMatrix().get(quat);
_texmat->setMatrix(osg::Matrix::rotate(quat.inverse()));
}
}
protected:
osg::ref_ptr<TexMat> _texmat;
};
osg::Group* createShadowedScene(osg::Node* reflectedSubgraph,
osg::NodePath reflectorNodePath, unsigned int unit, const osg::Vec4&
clearColor, unsigned tex_width, unsigned tex_height,
osg::CameraNode::RenderTargetImplementation renderImplementation)
{
osg::Group* group = new osg::Group;
osg::TextureCubeMap* texture = new osg::TextureCubeMap;
texture->setTextureSize(tex_width, tex_height);
texture->setInternalFormat(GL_RGB);
texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
texture->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
texture->setFilter(osg::TextureCubeMap::MIN_FILTER,osg::TextureCubeMap::LINEAR);
texture->setFilter(osg::TextureCubeMap::MAG_FILTER,osg::TextureCubeMap::LINEAR);
// set up the render to texture cameras.
UpdateCameraAndTexGenCallback::CameraList cameraNodes;
//pete's
//osg::PositionAttitudeTransform* driverOffsetPAT = new
osg::PositionAttitudeTransform();
//driverOffsetPAT->setPosition(osg::Vec3(0,-15,4));
//driverOffsetPAT->setAttitude( osg::Quat(
osg::DegreesToRadians(-5.0f), osg::Vec3(1,0,0) ) );
for(unsigned int i=0; i<6; ++i)
{
// create the camera
osg::CameraNode* camera = new osg::CameraNode;
//clearColor=Vec4(0, 0, 0, 0);
camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera->setClearColor(clearColor); // clearColor of inner model
// set viewport
camera->setViewport(0,0,tex_width,tex_height);
// set the camera to render before the main camera.
camera->setRenderOrder(osg::CameraNode::PRE_RENDER);
// tell the camera to use OpenGL frame buffer object where
supported.
camera->setRenderTargetImplementation(renderImplementation);
// attach the texture and use it as the color buffer.
camera->attach(osg::CameraNode::COLOR_BUFFER, texture, 0, i);
// add subgraph to render
camera->addChild(reflectedSubgraph);
//driverOffsetPAT->addChild(camera);
//camera->addChild(driverOffsetPAT);
group->addChild(camera);
cameraNodes.push_back(camera);
}
// Declare a transform accumulator to retrieve world coordinates
// of the driver transform.
//transformAccumulator* driverWorldCoords = new transformAccumulator();
//driverWorldCoords->attachToGroup(driverOffsetPAT);
// create the texgen node to project the tex coords onto the subgraph
osg::TexGenNode* texgenNode = new osg::TexGenNode;
//texgenNode->getTexGen()->setMode(osg::TexGen::REFLECTION_MAP);
texgenNode->getTexGen()->setMode(osg::TexGen::NORMAL_MAP);
texgenNode->setTextureUnit(unit);
group->addChild(texgenNode);
// set the reflected subgraph so that it uses the texture and tex
gen settings.
{
osg::Node* reflectorNode = reflectorNodePath.front();
group->addChild(reflectorNode);
osg::StateSet* stateset = reflectorNode->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);
osg::TexMat* texmat = new osg::TexMat;
stateset->setTextureAttributeAndModes(unit,texmat,osg::StateAttribute::ON);
reflectorNode->setCullCallback(new TexMatCullCallback(texmat));
}
// add the reflector scene to draw just as normal
//group->addChild(reflectedSubgraph);
// set an update callback to keep moving the camera and tex gen in
the right direction.
group->setUpdateCallback(new
UpdateCameraAndTexGenCallback(reflectorNodePath, cameraNodes));
return group;
}
int main(int argc, char** argv)
{
// use an ArgumentParser object to manage the program arguments.
ArgumentParser arguments(&argc, argv);
// set up the usage document, in case we need to print out how to
use this program.
arguments.getApplicationUsage()->setDescription(
arguments.getApplicationName()
+ " is the example which demonstrates using of GL_ARB_shadow extension
implemented in osg::Texture class");
arguments.getApplicationUsage()->setCommandLineUsage(
arguments.getApplicationName());
arguments.getApplicationUsage()->addCommandLineOption("-h or
--help", "Display this information");
arguments.getApplicationUsage()->addCommandLineOption("--fbo","Use
Frame Buffer Object for render to texture, where supported.");
arguments.getApplicationUsage()->addCommandLineOption("--fb","Use
FrameBuffer for render to texture.");
arguments.getApplicationUsage()->addCommandLineOption("--pbuffer","Use
Pixel Buffer for render to texture, where supported.");
arguments.getApplicationUsage()->addCommandLineOption("--window","Use a
seperate Window for render to texture.");
arguments.getApplicationUsage()->addCommandLineOption("--width","Set the
width of the render to texture");
arguments.getApplicationUsage()->addCommandLineOption("--height","Set
the height of the render to texture");
osgProducer::Viewer viewer(arguments);
// set up the value with sensible default event handlers.
viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
//viewer.setUpViewer(osgProducer::Viewer::VIEWER_MANIPULATOR);
// get details on keyboard and mouse bindings used by the viewer.
viewer.getUsage(*arguments. getApplicationUsage());
//viewer.setClearColor(Vec4(0, 0, 0, 0));
// if user request help write it out to cout.
if (arguments.read("-h") || arguments.read("--help"))
{
arguments.getApplicationUsage()->write(std::cout);
return 1;
}
unsigned tex_width = 512;
unsigned tex_height = 512;
while (arguments.read("--width", tex_width)) {}
while (arguments.read("--height", tex_height)) {}
osg::CameraNode::RenderTargetImplementation renderImplementation =
osg::CameraNode::FRAME_BUFFER_OBJECT;
while (arguments.read("--fbo")) { renderImplementation =
osg::CameraNode::FRAME_BUFFER_OBJECT; }
while (arguments.read("--pbuffer")) { renderImplementation =
osg::CameraNode::PIXEL_BUFFER; }
while (arguments.read("--fb")) { renderImplementation =
osg::CameraNode::FRAME_BUFFER; }
while (arguments.read("--window")) { renderImplementation =
osg::CameraNode::SEPERATE_WINDOW; }
// any option left unread are converted into errors to write out
later.
arguments.reportRemainingOptionsAsUnrecognized();
// report any errors if they have occured when parsing the program
aguments.
if (arguments.errors())
{
arguments.writeErrorMessages(std::cout);
return 1;
}
ref_ptr<MatrixTransform> scene = new MatrixTransform;
//scene->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(125.0),1.0,
0.0,0.0));
ref_ptr<Group> reflectedSubgraph = _create_scene();
if (!reflectedSubgraph.valid()) return 1;
ref_ptr<Group> reflectedScene =
createShadowedScene(reflectedSubgraph.get(), createReflector(), 0,
viewer.getClearColor(),
tex_width,
tex_height, renderImplementation);
scene->addChild(reflectedScene.get());
viewer.setClearColor(Vec4(0, 0, 0, 1));
viewer.setSceneData(scene.get());
// create the windows and run the threads.
viewer.realize();
while (!viewer.done())
{
// wait for all cull and draw threads to complete.
viewer.sync();
// update the scene by traversing it with the the update visitor
which will
// call all node update callbacks and animations.
viewer.update();
// fire off the cull and draw traversals of the scene.
viewer.frame();
}
// wait for all cull and draw threads to complete before exit.
viewer.sync();
return 0;
}
*********************
Pete
Robert Osfield wrote:
> Hi Akhtar,
>
> For future reference, I will be undertaking some work early next year
> to add support for dome correction to the OSG.
>
> This doesn't solve your task right now, but it will provide a standard
> OSG solution in the not too distant future.
>
> Robert.
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://openscenegraph.net/mailman/listinfo/osg-users
> http://www.openscenegraph.org/
--
Pete Carss - Research Assistant, Immersive Vision
[ i ][>][...] - Institute of Digital Art and Technology
CETL (Centre for Excellence in Teaching & Learning)
Experiential Learning in Environmental & Natural Sciences
University of Plymouth, Drake Circus, Plymouth, PL4 8AA, Devon, UK
_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/