|
楼主 |
发表于 2010-8-12 16:45:38
|
显示全部楼层
#include "stdafx.h"
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osg/CoordinateSystemNode>
#include <osg/Switch>
#include <osgText/Text>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include "planeManipulator.h"
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Quat>
#include <osg/Matrix>
#include <osg/ShapeDrawable>
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/Transform>
#include <osg/Material>
#include <osg/NodeCallback>
#include <osg/Depth>
#include <osg/CullFace>
#include <osg/TexMat>
#include <osg/TexGen>
#include <osg/TexEnv>
#include <osg/TexEnvCombine>
#include <osg/TextureCubeMap>
#include <osg/VertexProgram>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/SmoothingVisitor>
#include "StereoTerrainManipulator.h"
#include <iostream>
bool g_bSave=false;
void createPreRenderSubGraph(osg::Node* subgraph,
unsigned tex_width, unsigned tex_height,
unsigned int samples, unsigned int colorSamples,osg::Camera *LCamera,osg::Image *LImage)
{
// set up the background color and clear mask.
LCamera->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
LCamera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set view
LCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
// set viewport
LCamera->setViewport(0,0,tex_width,tex_height);
// set the camera to render before the main camera.
LCamera->setRenderOrder(osg::Camera:RE_RENDER);
// tell the camera to use OpenGL frame buffer object where supported.
LCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
//osg::Image* LImage = new osg::Image;
LImage->allocateImage(tex_width, tex_height, 1, GL_RGBA, GL_UNSIGNED_BYTE);
// attach the image so its copied on each frame.
LCamera->attach(osg::Camera::COLOR_BUFFER, LImage,
samples, colorSamples);
// LCamera->setPostDrawCallback(new LMyCameraPostDrawCallback(LImage,"Left.bmp"));
// add subgraph to render
LCamera->addChild(subgraph);
}
class KeyboardEventHandler : public osgGA::GUIEventHandler
{
public:
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::KEYDOWN):
{
switch(ea.getKey())
{
case('n'):
{
g_bSave=true;
return true;
}
default:
return false;
}
}
}
};
int main(int argc, char** argv)
{
setlocale(LC_ALL,"chinese-simplified" );
osg::ArgumentParser arguments(&argc,argv);
arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
arguments.getApplicationUsage()->addCommandLineOption("--image <filename>","Load an image and render it on a quad");
arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>","Load an image/DEM and render it on a HeightField");
arguments.getApplicationUsage()->addCommandLineOption("--login <url> <username> <password>","rovide authentication information for http file access.");
osgViewer::Viewer viewer(arguments);
unsigned int helpType = 0;
if ((helpType = arguments.readHelpType()))
{
arguments.getApplicationUsage()->write(std::cout, helpType);
return 1;
}
// report any errors if they have occurred when parsing the program arguments.
if (arguments.errors())
{
arguments.writeErrorMessages(std::cout);
return 1;
}
/*if (arguments.argc()<=1)
{
arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
return 1;
}*/
std::string url, username, password;
while(arguments.read("--login",url, username, password))
{
if (!osgDB::Registry::instance()->getAuthenticationMap())
{
osgDB::Registry::instance()->setAuthenticationMap(new osgDB::AuthenticationMap);
osgDB::Registry::instance()->getAuthenticationMap()->addAuthenticationDetails(
url,
new osgDB::AuthenticationDetails(username, password)
);
}
}
// set up the camera manipulators.
osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA:riveManipulator() );
keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
std::string pathfile;
char keyForAnimationPath = '5';
while (arguments.read("-p",pathfile))
{
//osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator("KeyPoint.path");
osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
if (apm || !apm->valid())
{
unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
keyswitchManipulator->selectMatrixManipulator(num);
++keyForAnimationPath;
}
}
keyswitchManipulator->selectMatrixManipulator(3);
viewer.setCameraManipulator( keyswitchManipulator.get() );
osg::ref_ptr <osg::Camera>RTTCamera=new osg::Camera;//虚拟相机
osg::Image *RTTImage=new osg::Image;//虚拟图像
viewer.getCamera()->setClearColor(osg::Vec4(0.0,0.0,0.0,1));
// add the state manipulator
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
// add the thread model handler
viewer.addEventHandler(new osgViewer::ThreadingHandler);
// add the window size toggle handler
viewer.addEventHandler(new osgViewer::WindowSizeHandler);
// add the stats handler
viewer.addEventHandler(new osgViewer::StatsHandler);
// add the help handler
viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
// add the record camera path handler
viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
// add the LOD Scale handler
viewer.addEventHandler(new osgViewer:ODScaleHandler);
// add the screen capture handler
viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
viewer.addEventHandler(new KeyboardEventHandler());
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("cow.osg");
if (!loadedModel)
{
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
return 1;
}
// any option left unread are converted into errors to write out later.
arguments.reportRemainingOptionsAsUnrecognized();
// report any errors if they have occurred when parsing the program arguments.
if (arguments.errors())
{
arguments.writeErrorMessages(std::cout);
return 1;
}
osg::ref_ptr<osg::Group> root=new osg::Group();
osg::ref_ptr<osg::Group>subGraph=new osg::Group();
subGraph->addChild(loadedModel.get());
createPreRenderSubGraph(subGraph,1920,1200,16,8,RTTCamera,RTTImage);
root->addChild(RTTCamera);
root->addChild(loadedModel.get());
// optimize the scene graph, remove redundant nodes and state etc.
osgUtil::Optimizer optimizer;
optimizer.optimize(root.get());
viewer.setSceneData( root.get() );
Matrixd mProjection=viewer.getCamera()->getProjectionMatrix();
osg::DisplaySettings::instance()->setStereo(1);
osg::DisplaySettings::instance()->setStereoMode(osg::DisplaySettings::LEFT_EYE);//左眼立体
viewer.realize();
while( !viewer.done() )
{
RTTCamera->setViewMatrix(keyswitchManipulator.get()->getInverseMatrix());
RTTCamera->setProjectionMatrix(viewer.getCamera()->getProjectionMatrix());
if(g_bSave==true)
{
osgDB::writeImageFile(*RTTImage,"Copy.bmp");
g_bSave=false;
}
viewer.frame();
}
return 0;
} |
|