Hello,robert!
 
  I use the code to load the nodes from the commandline arguments. Just like 
these(a example of osgWater today I receive):
 
#include "WaterPlane.h"
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osgProducer/Viewer>
#include <osg/CoordinateSystemNode>
#include <osg/Group>
#include <osg/Node>
#include <osg/CameraNode>
#include <osg/Texture>
#include <osg/TexGenNode>
#include <osg/ShapeDrawable>
#include <osg/State>
#include <osg/StateSet>
#include <osg/Material>
#include <osgUtil/SceneView>
#include <osg/Texture2D>
#include <osg/CameraNode>
#include <osg/Shader>
#include <osg/Uniform>
#include <osg/Program>

class Ocean : public osg::Drawable {
 public:
  Ocean(osgUtil::SceneView* );
  /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  Ocean(const Ocean& teapot,const osg::CopyOp& 
copyop=osg::CopyOp::SHALLOW_COPY):
  osg::Drawable(teapot,copyop) {}
  META_Object(myOcean,Ocean)
  /** Compute the bounding box around Drawables's geometry.*/
  virtual osg::BoundingBox computeBound() const;
  /** Deprecated. */
  virtual void drawImplementation(osg::State&) const;

  class OceanNodeUpdateCallback : public osg::NodeCallback {
   public:
    OceanNodeUpdateCallback(Ocean* ocean){
     m_Ocean = ocean;
    }
    virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) {
     // first update subgraph to make sure objects are all moved into position
     traverse(node,nv);
     // update scene 
     osg::Vec3 eye,center,up;
     m_Ocean->m_SceneView->getCamera()->getViewMatrixAsLookAt(eye,center,up);
     m_Ocean->m_camObserving.position(eye, center);
     //aim the projecting camera
     m_Ocean->m_WaterPlane->aimProjector(&(m_Ocean->m_camProjecting), 
&(m_Ocean->m_camObserving));
     m_Ocean->m_camProjecting.saveFrustum(m_Ocean->m_SceneView);
     m_Ocean->m_camObserving.position(eye, center);
     m_Ocean->m_camObserving.saveFrustum(m_Ocean->m_SceneView);
     double time = .1;
     //update and render water plane
     m_Ocean->m_WaterPlane->update(m_Ocean->m_SceneView,time, 
&(m_Ocean->m_camProjecting), &(m_Ocean->m_camObserving));
     //m_Ocean->dirtyBound();
     //osg::BoundingBox bb;
     //osg::Vec3d minBB,maxBB;
     //m_Ocean->m_WaterPlane->getBoundBox(minBB,maxBB);
     //bb.set(minBB,maxBB);
     //m_Ocean->setBound(bb);
    }
   Ocean* m_Ocean;
  };
  

  void updateBoundingBox(osg::BoundingBox);
  WaterPlane* m_WaterPlane;
  Ocean() {};
  virtual ~Ocean() {
   delete m_WaterPlane;
  }
  osgUtil::SceneView* m_SceneView;
  Camera    m_camProjecting;  //the "extra camera" used to project the water, 
based on m_camObserving but modified if necessary
  Camera    m_camObserving;   //the camera in the scene
  osg::BoundingBox m_boundingBox;
  osg::CameraNode* createReflectionsMap(osg::Group* waterScene, osg::Node* 
reflectedScene, unsigned int textureID);
  osg::Texture2D* m_textureReflections;
  osg::CameraNode* m_cameraNodeReflections;
  osg::TexGenNode* m_texgenNodeReflections;
  osg::CameraNode* createRefractionsMap(osg::Group* waterScene, osg::Node* 
refractionsScene, unsigned int textureID);
  osg::Texture2D* m_textureRefractions;
  osg::CameraNode* m_cameraNodeRefractions;
  osg::TexGenNode* m_texgenNodeRefractions;
};
osg::CameraNode* Ocean::createReflectionsMap(osg::Group* waterScene, osg::Node* 
reflectedScene, unsigned int textureID){
 m_textureReflections = new osg::Texture2D;
 m_textureReflections->setTextureSize(1024, 1024);
 m_textureReflections->setInternalFormat(GL_RGBA);
 
m_textureReflections->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
 
m_textureReflections->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
 
m_textureReflections->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_BORDER);
 
m_textureReflections->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_BORDER);
 m_textureReflections->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
 // create the camera
 m_cameraNodeReflections = new osg::CameraNode;
 m_cameraNodeReflections->setClearColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
 m_cameraNodeReflections->setViewport(0,0,512,512);
 m_cameraNodeReflections->setRenderOrder(osg::CameraNode::PRE_RENDER);
 m_cameraNodeReflections->attach(osg::CameraNode::COLOR_BUFFER , 
m_textureReflections);
 m_cameraNodeReflections->addChild(reflectedScene);
 
 // create the texgen node to project the tex coords onto the subgraph
 m_texgenNodeReflections = new osg::TexGenNode;
 m_texgenNodeReflections->setTextureUnit(textureID);
 waterScene->addChild(m_texgenNodeReflections);
 
 m_WaterPlane->setTexGenNodeReflections(m_texgenNodeReflections);
 osg::StateSet* waterSceneStateSet = waterScene->getOrCreateStateSet();
 
waterSceneStateSet->setTextureAttributeAndModes(textureID,m_textureReflections,osg::StateAttribute::ON);
 
waterSceneStateSet->setTextureMode(textureID,GL_TEXTURE_GEN_S,osg::StateAttribute::ON);
 
waterSceneStateSet->setTextureMode(textureID,GL_TEXTURE_GEN_T,osg::StateAttribute::ON);
 
waterSceneStateSet->setTextureMode(textureID,GL_TEXTURE_GEN_R,osg::StateAttribute::ON);
 
waterSceneStateSet->setTextureMode(textureID,GL_TEXTURE_GEN_Q,osg::StateAttribute::ON);
 return m_cameraNodeReflections;
}

osg::CameraNode* Ocean::createRefractionsMap(osg::Group* waterScene, osg::Node* 
refractionsScene, unsigned int textureID){
 m_textureRefractions = new osg::Texture2D;
 m_textureRefractions->setTextureSize(1024, 1024);
 m_textureRefractions->setInternalFormat(GL_RGBA);
 
m_textureRefractions->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
 
m_textureRefractions->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
 
m_textureRefractions->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_BORDER);
 
m_textureRefractions->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_BORDER);
 m_textureRefractions->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
 // create the camera
 m_cameraNodeRefractions = new osg::CameraNode;
 m_cameraNodeRefractions->setClearColor(osg::Vec4(0.1f, 0.4f, 0.6f, 0.0f));
 m_cameraNodeRefractions->setViewport(0,0,512,512);
 m_cameraNodeRefractions->setRenderOrder(osg::CameraNode::PRE_RENDER);
 m_cameraNodeRefractions->attach(osg::CameraNode::COLOR_BUFFER, 
m_textureRefractions);
 m_cameraNodeRefractions->addChild(refractionsScene);

 // create the texgen node to project the tex coords onto the subgraph
 m_texgenNodeRefractions = new osg::TexGenNode;
 m_texgenNodeRefractions->setTextureUnit(textureID);
 waterScene->addChild(m_texgenNodeRefractions);
 m_WaterPlane->setTexGenNodeRefractions(m_texgenNodeRefractions);
 osg::StateSet* waterSceneStateSet = waterScene->getOrCreateStateSet();
 
waterSceneStateSet->setTextureAttributeAndModes(textureID,m_textureRefractions,osg::StateAttribute::ON);
 
waterSceneStateSet->setTextureMode(textureID,GL_TEXTURE_GEN_S,osg::StateAttribute::ON);
 
waterSceneStateSet->setTextureMode(textureID,GL_TEXTURE_GEN_T,osg::StateAttribute::ON);
 
waterSceneStateSet->setTextureMode(textureID,GL_TEXTURE_GEN_R,osg::StateAttribute::ON);
 
waterSceneStateSet->setTextureMode(textureID,GL_TEXTURE_GEN_Q,osg::StateAttribute::ON);

 return m_cameraNodeRefractions;
}

Ocean::Ocean(osgUtil::SceneView* sv){
 m_SceneView = sv;
 m_WaterPlane = new WaterPlane;
 m_WaterPlane->init();
 m_WaterPlane->setPreset(10, 0.0075, 180.0f, 200.0f, true);
 m_WaterPlane->setWaterColor(0.1f, 0.4f, 0.6f, 0.0f);
 dirtyDisplayList();
 setUseDisplayList(false);
 setUseVertexBufferObjects(false);
}
osg::BoundingBox Ocean::computeBound() const {
 return m_boundingBox;
}
void Ocean::updateBoundingBox(osg::BoundingBox bb){ 
 m_boundingBox = bb;
 setBound(bb);
}

void Ocean::drawImplementation(osg::State& state) const {
 

 // render it
 m_WaterPlane->render(&m_camObserving);//, &m_skybox[m_currentSkybox], 
m_modelList);
 

}

int main( int argc, char **argv )
{
    // use an ArgumentParser object to manage the program arguments.
    osg::ArgumentParser arguments(&argc,argv);
     
    // construct the viewer.
    osgProducer::Viewer viewer(arguments);
    // set up the value with sensible default event handlers.
    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
    // get details on keyboard and mouse bindings used by the viewer.
    viewer.getUsage(*arguments.getApplicationUsage());

    osg::Timer_t start_tick = osg::Timer::instance()->tick();

    // read the scene from the list of file specified command line args.
    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
 
 

    osg::Timer_t end_tick = osg::Timer::instance()->tick();
    std::cout << "Time to load = 
"<<osg::Timer::instance()->delta_s(start_tick,end_tick)<<std::endl;

 
    // create the windows and run the threads.
    viewer.realize();

 // load the nodes from the commandline arguments.
 osgProducer::Viewer::SceneHandlerList::iterator sh_it = 
viewer.getSceneHandlerList().begin();
 osgProducer::OsgSceneHandler* sh =sh_it->get();
 

 // not used for fix, beacause is a very bad solution 
m_sceneView->getCullVisitor()->setComputeNearFarMode(osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR);
 osg::Group* waterGroup = new osg::Group;
 osg::Geode* waterRoot = new osg::Geode();
 Ocean* ocean =  new Ocean(sh->getSceneView());
 osg::StateSet* oceanSS = ocean->getOrCreateStateSet(); 
 osg::Material* waterMaterial = new osg::Material;
 waterMaterial->setAlpha(osg::Material::Face::FRONT_AND_BACK,0.5f);
 waterMaterial->setShininess(osg::Material::Face::FRONT_AND_BACK,10);
 waterMaterial->setAmbient(osg::Material::Face::FRONT_AND_BACK,osg::Vec4(0.1f, 
0.4f, 0.6f, 0.0f));
 waterMaterial->setDiffuse(osg::Material::Face::FRONT_AND_BACK,osg::Vec4(0.1f, 
0.4f, 0.6f, 0.0f));
 waterMaterial->setEmission(osg::Material::Face::FRONT_AND_BACK,osg::Vec4(0.1f, 
0.4f, 0.6f, 0.0f));
 waterMaterial->setTransparency(osg::Material::Face::FRONT_AND_BACK,0.5f);
 oceanSS->setAttributeAndModes(waterMaterial);
 oceanSS->setMode(GL_BLEND, osg::StateAttribute::ON); 
 oceanSS->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
 waterRoot->addDrawable( ocean );
 osg::Group* waterSurfaceGroup = new osg::Group;
 waterSurfaceGroup->addChild(waterRoot);

 osg::CameraNode* relectionsCam = 
ocean->createReflectionsMap(waterSurfaceGroup,loadedModel.get(),0);
 osg::CameraNode* refractionsCam = 
ocean->createRefractionsMap(waterSurfaceGroup,loadedModel.get(),1);
 ocean->m_WaterPlane->setCameraNodes(relectionsCam,refractionsCam);
 
 waterGroup->addChild(waterSurfaceGroup);
 waterGroup->addChild(relectionsCam);
 waterGroup->addChild(refractionsCam);
 
 
 

 //Shader
 osg::ref_ptr<osg::Program> program = new osg::Program;
 osg::ref_ptr<osg::Shader> vertShader = new osg::Shader(osg::Shader::VERTEX);
 osg::ref_ptr<osg::Shader> fragShader= new osg::Shader(osg::Shader::FRAGMENT);
 vertShader->loadShaderSourceFromFile("envmap.vert");
 fragShader->loadShaderSourceFromFile("envmap.frag");
 program->addShader( vertShader.get() );
 program->addShader( fragShader.get() );
 waterSurfaceGroup->getOrCreateStateSet()->setAttributeAndModes(program.get(), 
osg::StateAttribute::ON);
 waterSurfaceGroup->getOrCreateStateSet()->addUniform(new 
osg::Uniform("Reflection", 0));
 waterSurfaceGroup->getOrCreateStateSet()->addUniform(new 
osg::Uniform("Refraction", 1));
 ocean->m_WaterPlane->addUniforms(waterSurfaceGroup);

 ocean->dirtyBound();
 osg::BoundingBox bb;
 osg::Vec3d minBB,maxBB;
 minBB = loadedModel.get()->getBound().center() - 
osg::Vec3d(1.,1.,0)*loadedModel.get()->getBound().radius();
 maxBB = loadedModel.get()->getBound().center() + 
osg::Vec3d(1.,1.,0)*loadedModel.get()->getBound().radius();
 bb.set(minBB,maxBB);
 ocean->updateBoundingBox(bb);
 
 waterGroup->setUpdateCallback(new Ocean::OceanNodeUpdateCallback(ocean));
 
 osg::Group* root = new osg::Group;
 root->addChild(waterGroup);
 // model
 root->addChild(loadedModel.get());
 

 {
  osg::Geode* geode = new osg::Geode;
  osg::Geometry* geom = 
osg::createTexturedQuadGeometry(osg::Vec3(0,0,0),osg::Vec3(256.0,0.0,0.0),osg::Vec3(0.0,256.0,0.0));
  
geom->getOrCreateStateSet()->setTextureAttributeAndModes(0,ocean->m_textureReflections,osg::StateAttribute::ON);
  geode->addDrawable(geom);
  osg::CameraNode* camera = new osg::CameraNode;
  camera->setProjectionMatrix(osg::Matrix::ortho2D(0,512,0,512));
  camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
  camera->setViewMatrix(osg::Matrix::identity());
  camera->setViewport(50,600-1*102,512,512);
  camera->setClearMask(GL_DEPTH_BUFFER_BIT);
  camera->setRenderOrder(osg::CameraNode::POST_RENDER);
  camera->addChild(geode);
  root->addChild(camera);
 }
 {
  osg::Geode* geode = new osg::Geode;
  osg::Geometry* geom = 
osg::createTexturedQuadGeometry(osg::Vec3(0,0,0),osg::Vec3(256.0,0.0,0.0),osg::Vec3(0.0,256.0,0.0));
  
geom->getOrCreateStateSet()->setTextureAttributeAndModes(0,ocean->m_textureRefractions,osg::StateAttribute::ON);
  geode->addDrawable(geom);
  osg::CameraNode* camera = new osg::CameraNode;
  camera->setProjectionMatrix(osg::Matrix::ortho2D(0,512,0,512));
  camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
  camera->setViewMatrix(osg::Matrix::identity());
  camera->setViewport(50,600-2*260,512,512);
  camera->setClearMask(GL_DEPTH_BUFFER_BIT);
  camera->setRenderOrder(osg::CameraNode::POST_RENDER);
  camera->addChild(geode);
  root->addChild(camera);
 }
 // pass the loaded scene graph to the viewer.
 viewer.setSceneData(root);
 //double ne,fa;
 //ne = 1.0;
 //fa = 15000.0;
 
//sh->getSceneView()->setComputeNearFarMode(osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR);
 //sh->getSceneView()->setProjectionMatrixAsPerspective(45,1.0,ne,fa);

    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.
    viewer.sync();
    // run a clean up frame to delete all OpenGL objects.
    viewer.cleanup_frame();
    // wait for all the clean up frame to complete.
    viewer.sync();
 
    return 0;
}

 
  osgProducer::Viewer::SceneHandlerList::iterator sh_it = 
viewer.getSceneHandlerList().begin();
   osgProducer::OsgSceneHandler* sh =sh_it->get();

在2008-01-16,GuiYe <[EMAIL PROTECTED]> 写道:

 
 
 
  Hello,robert!
  I want to use the code:
 
>    osgProducer::Viewer::SceneHandlerList::iterator sh_it =
> viewer.getSceneHandlerList().begin();
>    osgProducer::OsgSceneHandler* sh =sh_it->get();

 
to  load the nodes from the commandline arguments.
 
  What should I do ?
  
  Thank you~~~
 

 
>Hi GuiYei,

>The osgViewer doesn't have an OsgSceneHandler and their isn't an equivalent.

>Perhaps you could provide a bit of information about what you were
>doing with the OsgSceneHandler then we might be able to suggest how to
>replace this code.

>Robert.


>2008/1/16 GuiYe <[EMAIL PROTECTED]>:
>
>
>
>   Hello~~
>
>   I once wrote some in OpenSceneGraph1.2.Now I use OpenSceneGraph2.2,I want
> to rewrite it.But I can't find some functions to replace some code:
>
>    osgProducer::Viewer::SceneHandlerList::iterator sh_it =
> viewer.getSceneHandlerList().begin();
>    osgProducer::OsgSceneHandler* sh =sh_it->get();
>
>
>     Who can give me some code to replace all above code in
> OpenSceneGraph2.2??
>
>     Thank you~~~~~~
>
>
>  ________________________________
>
>  又过年了,千万别忘了把你的爱带回家
> 为爱珍选,唯有金典
> _______________________________________________
> osg-users mailing list
> osg-users@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org




又过年了,千万别忘了把你的爱带回家
为爱珍选,唯有金典



又过年了,千万别忘了把你的爱带回家
为爱珍选,唯有金典



又过年了,千万别忘了把你的爱带回家
为爱珍选,唯有金典
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to