viewer/main.cpp
Go to the documentation of this file.
1 #include <sys/param.h>
2 #include <iostream>
3 #ifdef __APPLE__
4 #include <GLUT/glut.h>
5 #else
6 #include <GL/glut.h>
7 #endif
8 #include <SDL/SDL_timer.h>
9 #include <hrpModel/ModelLoaderUtil.h>
10 #include "hrpsys/util/GLlink.h"
11 #include "hrpsys/util/GLbody.h"
12 #include "hrpsys/util/GLutil.h"
13 #include "hrpsys/util/SDLUtil.h"
14 #include "OnlineViewer_impl.h"
15 #include "GLscene.h"
16 
17 using namespace OpenHRP;
18 
19 void print_usage(char* progname)
20 {
21  std::cerr << "Usage:" << progname << " [model file] [options]" << std::endl;
22  std::cerr << "Options:" << std::endl;
23  std::cerr << " -size [pixels] : specify window size in pixels" << std::endl;
24  std::cerr << " -no-default-lights : disable ambient light (simulation environment will be dark)" << std::endl;
25  std::cerr << " -max-edge-length [value] : specify maximum length of polygon edge (if exceed, polygon will be divided to improve rendering quality)" << std::endl;
26  std::cerr << " -bg [r] [g] [b] : specify background color" << std::endl;
27  std::cerr << " -h --help : show this help message" << std::endl;
28  std::cerr << "Example:" << std::endl;
29  std::cerr << " run the view server and wait for the client to connect"<< std::endl;
30  std::cerr << " $ " << progname << std::endl;
31  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;
32  std::cerr << " $ " << progname << " /usr/share/OpenHRP-3.1/sample/model/PA10/pa10.main.wrl -bg 0 0.3 0"<< std::endl;
33 }
34 
35 int main(int argc, char *argv[])
36 {
37  int wsize=0;
38  double maxEdgeLen=0.0;
39  bool useDefaultLights=true;
40  float bgColor[3]={0,0,0};
41 
42  for (int i=1; i<argc; i++){
43  if (strcmp(argv[i], "-size")==0){
44  wsize = atoi(argv[++i]);
45  }else if(strcmp(argv[i], "-max-edge-length")==0){
46  maxEdgeLen = atof(argv[++i]);
47  }else if(strcmp(argv[i], "-no-default-lights")==0){
48  useDefaultLights=false;
49  }else if(strcmp(argv[i], "-bg")==0){
50  bgColor[0] = atof(argv[++i]);
51  bgColor[1] = atof(argv[++i]);
52  bgColor[2] = atof(argv[++i]);
53  }else if(strcmp(argv[i], "-h")==0 || strcmp(argv[i], "--help")==0){
54  print_usage(argv[0]);
55  return 1;
56  }
57  }
58 
59  CORBA::ORB_var orb = CORBA::ORB::_nil();
60 
61  try {
62 
63  orb = CORBA::ORB_init(argc, argv);
64 
65  CORBA::Object_var obj;
66 
67  obj = orb->resolve_initial_references("RootPOA");
68  PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
69  if(CORBA::is_nil(poa)){
70  throw std::string("error: failed to narrow root POA.");
71  }
72 
73  PortableServer::POAManager_var poaManager = poa->the_POAManager();
74  if(CORBA::is_nil(poaManager)){
75  throw std::string("error: failed to narrow root POA manager.");
76  }
77 
79  GLscene scene(&log);
80  scene.setBackGroundColor(bgColor);
81  scene.maxEdgeLen(maxEdgeLen);
82 
83  OnlineViewer_impl* OnlineViewerImpl
84  = new OnlineViewer_impl(orb, poa, &scene, &log);
85  poa->activate_object(OnlineViewerImpl);
86  OnlineViewer_var OnlineViewer = OnlineViewerImpl->_this();
87  OnlineViewerImpl->_remove_ref();
88 
89  obj = orb->resolve_initial_references("NameService");
90  CosNaming::NamingContext_var namingContext = CosNaming::NamingContext::_narrow(obj);
91  if(CORBA::is_nil(namingContext)){
92  throw std::string("error: failed to narrow naming context.");
93  }
94 
95  CosNaming::Name name;
96  name.length(1);
97  name[0].id = CORBA::string_dup("OnlineViewer");
98  name[0].kind = CORBA::string_dup("");
99  namingContext->rebind(name, OnlineViewer);
100 
101  poaManager->activate();
102 
103  if (argc >= 2 && argv[1][0] != '-'){
104  OpenHRP::ModelLoader_var ml = hrp::getModelLoader(namingContext);
105  if (CORBA::is_nil(ml)){
106  std::cerr << "openhrp-model-loader is not running" << std::endl;
107  return 1;
108  }
109  OpenHRP::ModelLoader::ModelLoadOption opt;
110  opt.readImage = true;
111  opt.AABBdata.length(0);
112  opt.AABBtype = OpenHRP::ModelLoader::AABB_NUM;
113  GLbody *glbody = new GLbody();
114  std::string url = argv[1];
115  if (argv[1][0] != '/'){
116  char buf[MAXPATHLEN];
117  std::string cwd = getcwd(buf, MAXPATHLEN);
118  url = cwd + '/' + url;
119  }
120  hrp::BodyPtr body(glbody);
121  body->setName("model");
122  OpenHRP::BodyInfo_var binfo = ml->getBodyInfoEx(url.c_str(), opt);
123  hrp::loadBodyFromBodyInfo(body, binfo, false, GLlinkFactory);
124  loadShapeFromBodyInfo(glbody, binfo);
125  scene.addBody(body);
126  }
127 
130 
131  SDLwindow window(&scene, &log);
132  window.init(wsize, wsize);
133  if (!useDefaultLights) scene.defaultLights(false);
134 
135  while(window.oneStep());
136 
137  }
138  catch(OpenHRP::ModelLoader::ModelLoaderException ex){
139  std::cerr << ex.description << std::endl;
140  }
141  catch (CORBA::SystemException& ex) {
142  std::cerr << ex._rep_id() << std::endl;
143  }
144  catch (const std::string& error){
145  std::cerr << error << std::endl;
146  }
147 
148  try {
149  orb->destroy();
150  }
151  catch(...){
152 
153  }
154 
155  return 0;
156 }
HRPMODEL_API OpenHRP::ModelLoader_var getModelLoader(CosNaming::NamingContext_var cxt)
void addBody(GLbody *i_body)
Definition: GLmodel.cpp:293
void setBackGroundColor(float rgb[3])
png_infop png_charpp name
bool init(int w=0, int h=0, bool resizable=true)
Definition: SDLUtil.cpp:54
HRPMODEL_API bool loadBodyFromBodyInfo(BodyPtr body, OpenHRP::BodyInfo_ptr bodyInfo, bool loadGeometryForCollisionDetection=false, Link *(*f)()=NULL)
void error(char *msg) const
void loadShapeFromBodyInfo(GLbody *body, BodyInfo_var i_binfo, GLshape *(*shapeFactory)())
Definition: GLutil.cpp:181
png_uint_32 i
Definition: GLbody.h:11
bool oneStep()
Definition: SDLUtil.cpp:342
void defaultLights(bool flag)
static void useAbsTransformToDraw()
Definition: GLbody.cpp:9
png_bytep buf
void print_usage(char *progname)
Definition: viewer/main.cpp:19
void maxEdgeLen(double i_len)
int main(int argc, char *argv[])
Definition: viewer/main.cpp:35
obj


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:50