(1)源程序
#include <osgDB/ReadFile>
#include <osgProducer/Viewer>
#include <osgUtil/Optimizer>
#include <osg/TexGen>
#include <osg/Texture2D>
int main(int argc,char *argv[])
{
std::string filename("cow.osg");
//load the scene.
osg::ref_ptr<osg::Node> loadedModel= osgDB::readNodeFile(filename);
if(!loadedModel)
{
std::cout << argv[0] <<": No data loaded." << std::endl;
return 1;
}
// construct the viewer.
osgProducer::Viewer viewer;
// set up the value with sensible default event handlers.
viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
// optimize the scene graph,remove redundant nodes and state etc.
osgUtil::Optimizer optimizer;
optimizer.optimize(loadedModel.get());
// add a reflection map to the teapot.
osg::Image* image = osgDB::readImageFile("Images/lz.rgb");
if (image)
{
osg::Texture2D* texture = new osg::Texture2D;
texture->setImage(image);
osg::TexGen* texgen = new osg::TexGen;
texgen->setMode(osg::TexGen::SPHERE_MAP);
osg::StateSet* stateset = new osg::StateSet;
stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
stateset->setTextureAttributeAndModes(0,texgen,osg::StateAttribute::ON);
loadedModel.get()->setStateSet(stateset);
}
// add model to viewer.
viewer.setSceneData(loadedModel.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 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 the clean up frame to complete.
viewer.sync();
return 0;
}
(2)编译
Project/Setting->c/c++,在project options的最后一个/c后加上/Zm200
(3)运行错误提示:
Could not find plugin to read objects from file "cow.osg"
这个是要编译osgdb_rgbd.dll
附录:
编译osgteapot时出现runtime errors,解决方法:
(1)Enable Run-Time Type Information(RTTI)
(2)Debug Multithreaded DLL