main.cpp
Go to the documentation of this file.
00001 #include <sys/param.h>
00002 #include <iostream>
00003 #ifdef __APPLE__
00004 #include <GLUT/glut.h>
00005 #else
00006 #include <GL/glut.h>
00007 #endif
00008 #include <SDL/SDL_timer.h>
00009 #include <hrpModel/ModelLoaderUtil.h>
00010 #include "hrpsys/util/GLlink.h"
00011 #include "hrpsys/util/GLbody.h"
00012 #include "hrpsys/util/GLutil.h"
00013 #include "hrpsys/util/SDLUtil.h"
00014 #include "OnlineViewer_impl.h"
00015 #include "GLscene.h"
00016 
00017 using namespace OpenHRP;
00018 
00019 void print_usage(char* progname)
00020 {
00021     std::cerr << "Usage:" << progname << " [model file] [options]" << std::endl;
00022     std::cerr << "Options:" << std::endl;
00023     std::cerr << " -size [pixels]     : specify window size in pixels" << std::endl;
00024     std::cerr << " -no-default-lights : disable ambient light (simulation environment will be dark)" << std::endl;
00025     std::cerr << " -max-edge-length [value] : specify maximum length of polygon edge (if exceed, polygon will be divided to improve rendering quality)" << std::endl;
00026     std::cerr << " -bg [r] [g] [b]    : specify background color" << std::endl;
00027     std::cerr << " -h --help          : show this help message" << std::endl;
00028     std::cerr << "Example:" << std::endl;
00029     std::cerr << " run the view server and wait for the client to connect"<< std::endl;
00030     std::cerr << "  $ " << progname << std::endl;
00031     std::cerr << " run the view server, load the pa10 robot model, set the background to green and wait for the client to connect"<< std::endl;
00032     std::cerr << "  $ " << progname << " /usr/share/OpenHRP-3.1/sample/model/PA10/pa10.main.wrl -bg 0 0.3 0"<< std::endl;
00033 }
00034 
00035 int main(int argc, char *argv[])
00036 {
00037     int wsize=0;
00038     double maxEdgeLen=0.0;
00039     bool useDefaultLights=true;
00040     float bgColor[3]={0,0,0};
00041     
00042     for (int i=1; i<argc; i++){
00043         if (strcmp(argv[i], "-size")==0){
00044             wsize = atoi(argv[++i]);
00045         }else if(strcmp(argv[i], "-max-edge-length")==0){
00046             maxEdgeLen = atof(argv[++i]);
00047         }else if(strcmp(argv[i], "-no-default-lights")==0){
00048             useDefaultLights=false;
00049         }else if(strcmp(argv[i], "-bg")==0){
00050             bgColor[0] = atof(argv[++i]);
00051             bgColor[1] = atof(argv[++i]);
00052             bgColor[2] = atof(argv[++i]);
00053         }else if(strcmp(argv[i], "-h")==0 || strcmp(argv[i], "--help")==0){
00054             print_usage(argv[0]);
00055             return 1;
00056         }
00057     }
00058     
00059     CORBA::ORB_var orb = CORBA::ORB::_nil();
00060     
00061     try {
00062         
00063         orb = CORBA::ORB_init(argc, argv);
00064         
00065         CORBA::Object_var obj;
00066         
00067         obj = orb->resolve_initial_references("RootPOA");
00068         PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
00069         if(CORBA::is_nil(poa)){
00070             throw std::string("error: failed to narrow root POA.");
00071         }
00072         
00073         PortableServer::POAManager_var poaManager = poa->the_POAManager();
00074         if(CORBA::is_nil(poaManager)){
00075             throw std::string("error: failed to narrow root POA manager.");
00076         }
00077         
00078         LogManager<OpenHRP::WorldState> log;
00079         GLscene scene(&log);
00080         scene.setBackGroundColor(bgColor);
00081         scene.maxEdgeLen(maxEdgeLen);
00082         
00083         OnlineViewer_impl* OnlineViewerImpl 
00084             = new OnlineViewer_impl(orb, poa, &scene, &log);
00085         poa->activate_object(OnlineViewerImpl);
00086         OnlineViewer_var OnlineViewer = OnlineViewerImpl->_this();
00087         OnlineViewerImpl->_remove_ref();
00088         
00089         obj = orb->resolve_initial_references("NameService");
00090         CosNaming::NamingContext_var namingContext = CosNaming::NamingContext::_narrow(obj);
00091         if(CORBA::is_nil(namingContext)){
00092             throw std::string("error: failed to narrow naming context.");
00093         }
00094         
00095         CosNaming::Name name;
00096         name.length(1);
00097         name[0].id = CORBA::string_dup("OnlineViewer");
00098         name[0].kind = CORBA::string_dup("");
00099         namingContext->rebind(name, OnlineViewer);
00100 
00101         poaManager->activate();
00102         
00103         if (argc >= 2 && argv[1][0] != '-'){
00104             OpenHRP::ModelLoader_var ml = hrp::getModelLoader(namingContext);
00105             if (CORBA::is_nil(ml)){
00106                 std::cerr << "openhrp-model-loader is not running" << std::endl;
00107                 return 1;
00108             }
00109             OpenHRP::ModelLoader::ModelLoadOption opt;
00110             opt.readImage = true;
00111             opt.AABBdata.length(0);
00112             opt.AABBtype = OpenHRP::ModelLoader::AABB_NUM;
00113             GLbody *glbody = new GLbody();
00114             std::string url = argv[1];
00115             if (argv[1][0] != '/'){
00116                 char buf[MAXPATHLEN];
00117                 std::string cwd = getcwd(buf, MAXPATHLEN);
00118                 url = cwd + '/' + url;
00119             }
00120             hrp::BodyPtr body(glbody);
00121             body->setName("model");
00122             OpenHRP::BodyInfo_var binfo = ml->getBodyInfoEx(url.c_str(), opt);
00123             hrp::loadBodyFromBodyInfo(body, binfo, false, GLlinkFactory);
00124             loadShapeFromBodyInfo(glbody, binfo);
00125             scene.addBody(body);
00126         }
00127         
00128         GLlink::useAbsTransformToDraw();
00129         GLbody::useAbsTransformToDraw();
00130 
00131         SDLwindow window(&scene, &log);
00132         window.init(wsize, wsize);
00133         if (!useDefaultLights) scene.defaultLights(false);
00134         
00135         while(window.oneStep());
00136         
00137     }
00138     catch(OpenHRP::ModelLoader::ModelLoaderException ex){
00139         std::cerr << ex.description << std::endl;
00140     }
00141     catch (CORBA::SystemException& ex) {
00142         std::cerr << ex._rep_id() << std::endl;
00143     }
00144     catch (const std::string& error){
00145         std::cerr << error << std::endl;
00146     }
00147     
00148     try {
00149         orb->destroy();
00150     }
00151     catch(...){
00152         
00153     }
00154     
00155     return 0;
00156 }


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Wed Sep 6 2017 02:35:55