1. 程式人生 > >用vc6.0編譯OpenSceneGraph第一個程式

用vc6.0編譯OpenSceneGraph第一個程式

(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