00001 #include "ndt_visualisation/NDTVizGlut.hh"
00002
00003
00004
00005
00006
00007
00008 static NDTVizGlut* glut3d_ptr = 0x0;
00009
00010
00011 void win_reshape_(int w, int h) { glut3d_ptr->win_reshape (w,h); }
00012 void win_redraw_() { glut3d_ptr->win_redraw(); }
00013 void win_key_(unsigned char key, int x, int y) { glut3d_ptr->win_key(key, x, y); }
00014 void win_mouse_(int button, int state, int x, int y) { glut3d_ptr->win_mouse(button, state, x, y); }
00015 void win_motion_(int x, int y) { glut3d_ptr->win_motion(x, y); }
00016 void win_idle_() { glut3d_ptr->win_idle(); }
00017 void win_close_() { glut3d_ptr->win_close(); }
00018
00019 void * glthread(void * pParam)
00020 {
00021 int argc=0;
00022 char** argv = NULL;
00023
00024 glutInit(&argc, argv);
00025
00026
00027 glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
00028 glutInitWindowSize(640,480);
00029
00030 win = glutCreateWindow("NDTVizGlut");
00031
00032 glEnable(GL_DEPTH_TEST);
00033
00034 glEnable(GL_LIGHTING);
00035 glEnable(GL_LIGHT0);
00036
00037
00038 GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
00039 GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
00040 GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
00041 GLfloat position[] = { -1.5f, 1.0f, -4.0f, 1.0f };
00042
00043
00044 glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
00045 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
00046 glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
00047 glLightfv(GL_LIGHT0, GL_POSITION, position);
00048
00049
00050
00051 glEnable(GL_COLOR_MATERIAL);
00052
00053 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
00054 glClearColor(0.6f, 0.6f, 0.6f, 1.0f);
00055
00056 glutReshapeFunc(win_reshape_);
00057 glutDisplayFunc(win_redraw_);
00058 glutKeyboardFunc(win_key_);
00059 glutMouseFunc(win_mouse_);
00060 glutMotionFunc(win_motion_);
00061 glutPassiveMotionFunc(NULL);
00062
00063
00064 glutIdleFunc(win_idle_);
00065
00066
00067 glutCloseFunc(win_close_);
00068
00069
00070 while (true) {
00071 usleep(1000);
00072 for (int i = 0; i < 10; i++)
00073 glutMainLoopEvent();
00074 glut3d_ptr->update_cam();
00075 win_redraw_();
00076 }
00077
00078
00079 return NULL;
00080 }
00081
00082
00083 NDTVizGlut::NDTVizGlut()
00084 {
00085
00086 gui_pause = 0;
00087
00088
00089 cam_radius = 10.0f;
00090 cam_azim = 0.5f;
00091 cam_sweep_ang = 0.0f;
00092
00093
00094
00095
00096
00097 cam_sweep_speed = 0.002;
00098
00099 cam_sweep = 0;
00100
00101 glut3d_ptr = this;
00102
00103 save_inc_counter = 0;
00104 do_save_inc = false;
00105
00106 update_cam();
00107
00108 open = true;
00109 }
00110
00111 NDTVizGlut::~NDTVizGlut()
00112 {
00113
00114 }
00115
00116
00117 void
00118 NDTVizGlut::update_cam()
00119 {
00120 glLoadIdentity();
00121 Eigen::Vector3f cp = camera.getPosition();
00122 Eigen::Vector3f fp = camera.getFocalPoint();
00123 Eigen::Vector3f up = camera.getUpVector();
00124 gluLookAt(cp[0], cp[1], cp[2],
00125 fp[0], fp[1], fp[2],
00126 up[0], up[1], up[2]);
00127 }
00128
00129 void
00130 NDTVizGlut::win_key(unsigned char key, int x, int y)
00131 {
00132 this->pressed_keys.push_back(key);
00133 }
00134
00135
00136 void
00137 NDTVizGlut::win_mouse(int button, int state, int x, int y)
00138 {
00139
00140 camera.update_mouse(button, state, x, y);
00141 update_cam();
00142
00143 return;
00144 }
00145
00146 void
00147 NDTVizGlut::win_motion(int x, int y)
00148 {
00149
00150 camera.update_motion(x, y);
00151 update_cam();
00152
00153 return;
00154 }
00155
00156
00157
00158 void
00159 NDTVizGlut::win_reshape(int width, int height)
00160 {
00161
00162
00163 if(height == 0)
00164 height = 1;
00165
00166 float ratio = 1.0f * width / height;
00167
00168 glMatrixMode(GL_PROJECTION);
00169 glLoadIdentity();
00170
00171
00172 glViewport(0, 0, width, height);
00173
00174
00175 gluPerspective(45,ratio,1,1000);
00176 glMatrixMode(GL_MODELVIEW);
00177
00178 update_cam();
00179 win_redraw();
00180 return;
00181 }
00182
00183
00184
00185 void
00186 NDTVizGlut::win_redraw()
00187 {
00188 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00189
00190 this->draw_origin();
00191
00192
00193 this->draw();
00194
00195 glutSwapBuffers();
00196 return;
00197 }
00198
00199
00200 void
00201 NDTVizGlut::draw()
00202 {
00203
00204 glColor3f(0.5, 0.5, 0.5);
00205 glBegin(GL_LINE_LOOP);
00206 glVertex3f(-2, -2, 0);
00207 glVertex3f(+2, -2, 0);
00208 glVertex3f(+2, +2, 0);
00209 glVertex3f(-2, +2, 0);
00210 glEnd();
00211
00212 for (size_t i = 0; i < objects.size(); i++)
00213 {
00214 objects[i]->draw();
00215 }
00216 }
00217
00218
00219 void
00220 NDTVizGlut::win_idle()
00221 {
00222 if (!gui_pause)
00223 {
00224 glutPostRedisplay();
00225 }
00226 else
00227 usleep(100000);
00228
00229 return;
00230 }
00231
00232 void
00233 NDTVizGlut::win_close()
00234 {
00235 std::cerr << "Window closed. " << std::endl;
00236 open = false;
00237 }
00238
00239 bool
00240 NDTVizGlut::isOpen() const {
00241 return open;
00242 }
00243
00244 bool
00245 NDTVizGlut::keyHit() const {
00246 return !pressed_keys.empty();
00247 }
00248
00249 unsigned char
00250 NDTVizGlut::getPushedKey() {
00251 unsigned char ret = pressed_keys.front();
00252 pressed_keys.pop_front();
00253 return ret;
00254 }
00255
00256 void *
00257 start_glut_loop(void *ptr)
00258 {
00259 if (ptr == 0x0)
00260 {
00261 }
00262 glutMainLoop();
00263 return 0x0;
00264 }
00265
00266 void
00267 NDTVizGlut::process_events()
00268 {
00269
00270
00271
00272 }
00273
00274
00275 int
00276 NDTVizGlut::win_run(int *argc, char **argv)
00277 {
00278 std::cerr << "win_run" << std::endl;
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 return 0;
00325 }
00326
00327 void
00328 NDTVizGlut::draw_origin()
00329 {
00330 glBegin(GL_LINES);
00331 glColor3f(1, 0, 0);
00332 glVertex3f(0.0f, 0.0f, 0.0f);
00333 glVertex3f(1.0f, 0.0f, 0.0f);
00334 glColor3f(0, 1, 0);
00335 glVertex3f(0.0f, 0.0f, 0.0f);
00336 glVertex3f(0.0f, 1.0f, 0.0f);
00337 glColor3f(0, 0, 1);
00338 glVertex3f(0.0f, 0.0f, 0.0f);
00339 glVertex3f(0.0f, 0.0f, 1.0f);
00340 glEnd( );
00341 }
00342
00343 int
00344 NDTVizGlut::save_inc()
00345 {
00346 std::string file_name = "mov";
00347 save_inc_counter++;
00348 return save(file_name);
00349 }
00350
00351 int
00352 NDTVizGlut::save(const std::string &fileName)
00353 {
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385 return 1;
00386 }
00387
00388 void
00389 NDTVizGlut::repaint() {
00390
00391
00392 }
00393
00394 void
00395 NDTVizGlut::clearScene() {
00396 objects.clear();
00397 }
00398
00399 void
00400 NDTVizGlut::setCameraPointingToPoint(double x, double y, double z) {
00401 camera.setFocalPoint(Eigen::Vector3f(x,y,z));
00402 update_cam();
00403 }
00404
00405 void
00406 NDTVizGlut::setCameraPosition(double x, double y, double z) {
00407
00408 this->setCameraPointingToPoint(x,y,z);
00409 }