ViewBuilder.cpp
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 2013 University of Jaume-I.
00003  * All rights reserved. This program and the accompanying materials
00004  * are made available under the terms of the GNU Public License v3.0
00005  * which accompanies this distribution, and is available at
00006  * http://www.gnu.org/licenses/gpl.html
00007  * 
00008  * Contributors:
00009  *     Mario Prats
00010  *     Javier Perez
00011  */
00012 
00013 #include <uwsim/ViewBuilder.h>
00014 #include <uwsim/UWSimUtils.h>
00015 #include <uwsim/TextHUD.h>
00016 #include <uwsim/EventHandler.h>
00017 
00018 #include <osgViewer/Viewer>
00019 #include <osgViewer/ViewerEventHandlers>
00020 #include <osgGA/TrackballManipulator>
00021 #include <osgGA/NodeTrackerManipulator>
00022 #include <osgGA/StateSetManipulator>
00023 #include <osgGA/GUIEventHandler>
00024 #include <osg/Notify>
00025 
00026 #include <osgWidget/Util>
00027 #include <osgWidget/WindowManager>
00028 #include <osgWidget/ViewerEventHandlers>
00029 #include <osg/GraphicsContext>
00030 
00031 ViewBuilder::ViewBuilder(ConfigFile &config, SceneBuilder *scene_builder, int *argc, char **argv)
00032 {
00033   arguments.reset(new osg::ArgumentParser(argc, argv));
00034   init(config, scene_builder);
00035 }
00036 
00037 ViewBuilder::ViewBuilder(ConfigFile &config, SceneBuilder *scene_builder, boost::shared_ptr<osg::ArgumentParser> args)
00038 {
00039   arguments = args;
00040   init(config, scene_builder);
00041 }
00042 
00043 ViewBuilder::ViewBuilder(ConfigFile &config, SceneBuilder *scene_builder)
00044 {
00045   int argc = 0;
00046   char **argv = NULL;
00047   arguments.reset(new osg::ArgumentParser(&argc, argv));
00048   init(config, scene_builder);
00049 }
00050 
00051 bool ViewBuilder::init(ConfigFile &config, SceneBuilder *scene_builder)
00052 {
00053   bool disableTextures = false;
00054   if (arguments->read("--disableTextures"))
00055     disableTextures = true;
00056 
00057   float reswidth = config.resw, resheight = config.resh;
00058   while (arguments->read("--resw", reswidth))
00059     ;
00060   while (arguments->read("--resh", resheight))
00061     ;
00062 
00063   bool freeMotion = config.freeMotion;
00064   if (arguments->read("--freeMotion"))
00065   {
00066     freeMotion = true;
00067   }
00068 
00069   fullScreenNum = -1;
00070   if (!arguments->read("--fullScreen", fullScreenNum) && arguments->read("--fullScreen"))
00071   {
00072     fullScreenNum = 0;
00073   }
00074   //is screen number is higher than any available screen, set it to the last one
00075   if (fullScreenNum + 1 >= osg::GraphicsContext::getWindowingSystemInterface()->getNumScreens())
00076     fullScreenNum = osg::GraphicsContext::getWindowingSystemInterface()->getNumScreens() - 1;
00077   if (fullScreenNum >= 0)
00078   {
00079     osg::GraphicsContext::ScreenSettings settings;
00080     osg::GraphicsContext::ScreenIdentifier screen_id(fullScreenNum);
00081     osg::GraphicsContext::getWindowingSystemInterface()->getScreenSettings(screen_id, settings);
00082     if (settings.width > 0 && settings.height > 0)
00083     {
00084       reswidth = settings.width;
00085       resheight = settings.height;
00086     }
00087   }
00088 
00089   //Initialize viewer
00090   viewer = new osgViewer::Viewer();
00091   viewer->addEventHandler(new osgViewer::StatsHandler);
00092   osg::ref_ptr < TextHUD > hud = new TextHUD;
00093 
00094   viewer->addEventHandler(scene_builder->getScene()->getOceanSceneEventHandler());
00095   viewer->addEventHandler(scene_builder->getScene()->getOceanSurface()->getEventHandler());
00096 
00097   //Set main camera position, lookAt, and other params
00098   if (freeMotion)
00099   {
00100     osg::ref_ptr < osgGA::TrackballManipulator > tb = new osgGA::TrackballManipulator;
00101     tb->setHomePosition(osg::Vec3f(config.camPosition[0], config.camPosition[1], config.camPosition[2]),
00102                         osg::Vec3f(config.camLookAt[0], config.camLookAt[1], config.camLookAt[2]), osg::Vec3f(0, 0, 1));
00103     viewer->setCameraManipulator(tb);
00104   }
00105   else
00106   { //Main camera tracks an object
00107     findRoutedNode findRN(config.vehicleToTrack);
00108     findRN.find(scene_builder->getScene()->getScene());
00109 
00110     osg::ref_ptr < osg::Node > first = findRN.getFirst();
00111     if (first.valid())
00112     {
00113       //target to track found
00114       osg::ref_ptr < osg::Node > emptyNode = new osg::Node;
00115       osg::ref_ptr < osgGA::NodeTrackerManipulator > ntm = new osgGA::NodeTrackerManipulator;
00116       first->asGroup()->addChild(emptyNode);
00117       ntm->setTrackNode(emptyNode);
00118       ntm->setTrackerMode(osgGA::NodeTrackerManipulator::NODE_CENTER);
00119       ntm->setHomePosition(osg::Vec3d(config.camPosition[0], config.camPosition[1], config.camPosition[2]),
00120                            osg::Vec3d(config.camLookAt[0], config.camLookAt[1], config.camLookAt[2]),
00121                            osg::Vec3d(0, 0, 1));
00122       viewer->setCameraManipulator(ntm);
00123     }
00124     else
00125     {
00126       //Target object not found, free camera
00127       osg::ref_ptr < osgGA::TrackballManipulator > tb = new osgGA::TrackballManipulator;
00128       tb->setHomePosition(osg::Vec3f(config.camPosition[0], config.camPosition[1], config.camPosition[2]),
00129                           osg::Vec3f(config.camLookAt[0], config.camLookAt[1], config.camLookAt[2]),
00130                           osg::Vec3f(0, 0, 1));
00131       viewer->setCameraManipulator(tb);
00132     }
00133   }
00134   viewer->addEventHandler(new osgViewer::HelpHandler);
00135   viewer->getCamera()->setName("MainCamera");
00136 
00137   //Main camera projection parameters: view angle (fov), aspect ratio, fustrum near and far
00138   if (config.camNear != -1 && config.camFar != -1)
00139   {
00140     OSG_INFO << "Setting custom near/far planes to " << config.camNear << " " << config.camFar << std::endl;
00141     viewer->getCamera()->setComputeNearFarMode(osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR);
00142     viewer->getCamera()->setProjectionMatrixAsPerspective(config.camFov, config.camAspectRatio, config.camNear,
00143                                                           config.camFar);
00144   }
00145   else
00146   {
00147     OSG_INFO << "Setting custom near/far planes to auto" << std::endl;
00148     viewer->getCamera()->setProjectionMatrixAsPerspective(config.camFov, config.camAspectRatio, 1, 10);
00149   }
00150 
00151   OSG_INFO << "Setting main viewer" << std::endl;
00152   viewer->setSceneData(scene_builder->getRoot());
00153 
00154   OSG_INFO << "Starting window manager..." << std::endl;
00155   wm = new osgWidget::WindowManager(viewer, reswidth, resheight, 0xF0000000, 0);
00156 
00157   OSG_INFO << "Setting widgets..." << std::endl;
00158   wm->setPointerFocusMode(osgWidget::WindowManager::PFM_SLOPPY);
00159   std::vector < osg::ref_ptr<osgWidget::Window> > camWidgets;
00160   int dispx = 0;
00161   int ncamwidgets = 0;
00162   for (unsigned int j = 0; j < scene_builder->iauvFile.size(); j++)
00163   {
00164     for (unsigned int i = 0; i < scene_builder->iauvFile[j]->getNumCams(); i++)
00165     {
00166       if (scene_builder->iauvFile[j]->camview[i].widget)
00167       {
00168         camWidgets.push_back(scene_builder->iauvFile[j]->camview[i].getWidgetWindow());
00169         camWidgets[ncamwidgets]->setX(dispx);
00170         camWidgets[ncamwidgets]->setY(0);
00171         dispx += scene_builder->iauvFile[j]->camview[i].width + 20;
00172         wm->addChild(camWidgets[ncamwidgets]);
00173         camWidgets[ncamwidgets]->hide();
00174         ncamwidgets++;
00175       }
00176     }
00177   }
00178   viewer->addEventHandler(
00179       new SceneEventHandler(camWidgets, hud.get(), scene_builder->getScene(), scene_builder->ROSInterfaces, &config));
00180 
00181   for (unsigned int i = 0; i < scene_builder->realcams.size(); i++)
00182   {
00183     wm->addChild(scene_builder->realcams[i]->getWidgetWindow());
00184   }
00185 
00186   //viewer->addEventHandler( new SceneEventHandler(NULL, hud.get(), scene ) );
00187 
00188   osg::ref_ptr < osg::Group > appgroup = new osg::Group();
00189   osg::ref_ptr < osg::Camera > appcamera = wm->createParentOrthoCamera();
00190 
00191   appgroup->addChild(appcamera);
00192 
00193   if (scene_builder->getRoot())
00194     appgroup->addChild(scene_builder->getRoot());
00195 
00196   viewer->addEventHandler(new osgWidget::MouseHandler(wm));
00197   viewer->addEventHandler(new osgWidget::KeyboardHandler(wm));
00198   viewer->addEventHandler(new osgWidget::ResizeHandler(wm, appcamera));
00199   viewer->addEventHandler(new osgWidget::CameraSwitchHandler(wm, appcamera));
00200   viewer->addEventHandler(new osgViewer::StatsHandler());
00201   viewer->addEventHandler(new osgViewer::WindowSizeHandler());
00202   viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));
00203 
00204   //If --disableTextures, force disabling textures
00205   if (disableTextures)
00206   {
00207     for (unsigned int ii = 0; ii < 4; ii++)
00208     {
00209 #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)
00210       viewer->getCamera()->getOrCreateStateSet()->setTextureMode(
00211           ii, GL_TEXTURE_1D, osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF);
00212 #endif
00213       viewer->getCamera()->getOrCreateStateSet()->setTextureMode(
00214           ii, GL_TEXTURE_2D, osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF);
00215       viewer->getCamera()->getOrCreateStateSet()->setTextureMode(
00216           ii, GL_TEXTURE_3D, osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF);
00217       viewer->getCamera()->getOrCreateStateSet()->setTextureMode(
00218           ii, GL_TEXTURE_RECTANGLE, osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF);
00219       viewer->getCamera()->getOrCreateStateSet()->setTextureMode(
00220           ii, GL_TEXTURE_CUBE_MAP, osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF);
00221     }
00222   }
00223 
00224   wm->resizeAllWindows();
00225 
00226   viewer->setSceneData(appgroup);
00227 
00228   for (unsigned int j = 0; j < scene_builder->iauvFile.size(); j++)
00229     for (unsigned int i = 0; i < scene_builder->iauvFile[j]->devices->all.size(); i++)
00230       scene_builder->iauvFile[j]->devices->all.at(i)->setViewBuilder(this);
00231   return true;
00232 }
00233 void ViewBuilder::init()
00234 {
00235   OSG_INFO << "Creating application..." << std::endl;
00236 
00237   if (fullScreenNum>=0)
00238     viewer->setUpViewOnSingleScreen(fullScreenNum);
00239   else
00240     viewer->setUpViewInWindow(50, 50, static_cast<int>(wm->getWidth()), static_cast<int>(wm->getHeight()));
00241 }


uwsim
Author(s): Mario Prats
autogenerated on Mon Oct 6 2014 08:24:07