NDTVizGlut.cc
Go to the documentation of this file.
00001 #include "ndt_visualisation/NDTVizGlut.hh"
00002 // #include <assert.h>
00003 // #include <unistd.h>
00004 // #include <math.h>
00005 // #include <iostream>
00006 
00007 // // Needed for the wrapper calls.
00008 static NDTVizGlut* glut3d_ptr = 0x0;
00009 
00010 // Need some wrapper functions to handle the callbacks functions.
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      // Create a window
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      // Create light components
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      // Assign created components to GL_LIGHT0
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      // enable color tracking
00051      glEnable(GL_COLOR_MATERIAL);
00052      // set material properties which will be assigned by glColor
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      // Idle loop callback
00064      glutIdleFunc(win_idle_);
00065 
00066      // Window close function
00067      glutCloseFunc(win_close_);
00068     /* Thread will loop here */
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      //glutMainLoop();
00078      
00079      return NULL;
00080 }
00081 
00082 
00083 NDTVizGlut::NDTVizGlut()
00084 {
00085      // GUI settings
00086      gui_pause = 0;
00087 
00088 
00089      cam_radius = 10.0f;
00090      cam_azim = 0.5f;
00091      cam_sweep_ang = 0.0f;
00092 
00093      // cam_sweep_origin.x = 0.0f;
00094      // cam_sweep_origin.y = 0.0f;
00095      // cam_sweep_origin.z = 0.0f;
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 // Mouse callback
00136 void
00137 NDTVizGlut::win_mouse(int button, int state, int x, int y)
00138 {
00139 //    std::cerr << "win_mouse - b:" << button << " s: " << state << "[" << x << "," << y << "]" << std::endl;
00140     camera.update_mouse(button, state, x, y);
00141     update_cam();
00142     //win_redraw();
00143     return;
00144 }
00145 
00146 void
00147 NDTVizGlut::win_motion(int x, int y)
00148 {
00149 //    std::cerr << "win_motion : " << x << "," << y << std::endl;
00150     camera.update_motion(x, y);
00151     update_cam();
00152     //win_redraw();
00153     return;
00154 }
00155 
00156 
00157 // Handle window reshape events
00158 void
00159 NDTVizGlut::win_reshape(int width, int height)
00160 {
00161      // Prevent a divide by zero, when window is too short
00162      // (you cant make a window of zero width).
00163      if(height == 0)
00164           height = 1;
00165      
00166      float ratio = 1.0f * width / height;
00167      // Reset the coordinate system before modifying
00168      glMatrixMode(GL_PROJECTION);
00169      glLoadIdentity();
00170      
00171      // Set the viewport to be the entire window
00172      glViewport(0, 0, width, height);
00173      
00174      // Set the clipping volume
00175      gluPerspective(45,ratio,1,1000);
00176      glMatrixMode(GL_MODELVIEW);
00177      
00178      update_cam();
00179      win_redraw();
00180      return;
00181 }
00182 
00183 
00184 // Redraw the window
00185 void 
00186 NDTVizGlut::win_redraw()
00187 {
00188      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00189 
00190      this->draw_origin();
00191 
00192      // Do the drawing...
00193      this->draw();
00194 
00195      glutSwapBuffers();
00196      return;
00197 }
00198 
00199 
00200 void
00201 NDTVizGlut::draw()
00202 {
00203 //     NDTVizGlut::draw();
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      // Draw the objects.
00212      for (size_t i = 0; i < objects.size(); i++)
00213       {
00214           objects[i]->draw();
00215       }
00216 }
00217 
00218 // Idle callback
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 //     if (do_save_inc)
00270 //        save_inc();
00271 //     glutMainLoopEvent();
00272 }
00273 
00274 // Run the GUI
00275 int 
00276 NDTVizGlut::win_run(int *argc, char **argv)
00277 {
00278     std::cerr << "win_run" << std::endl;
00279      // glutInit(argc, argv);
00280           
00281      // // Create a window
00282      // glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
00283      // glutInitWindowSize(640,480);
00284      
00285      // win = glutCreateWindow("NDTVizGlut");
00286      
00287      // glEnable(GL_DEPTH_TEST);
00288      
00289      // glEnable(GL_LIGHTING);
00290      // glEnable(GL_LIGHT0);
00291      
00292      // // Create light components
00293      // GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
00294      // GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
00295      // GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
00296      // GLfloat position[] = { -1.5f, 1.0f, -4.0f, 1.0f };
00297      
00298      // // Assign created components to GL_LIGHT0
00299      // glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
00300      // glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
00301      // glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
00302      // glLightfv(GL_LIGHT0, GL_POSITION, position);
00303 
00304 
00305      // // enable color tracking
00306      // glEnable(GL_COLOR_MATERIAL);
00307      // // set material properties which will be assigned by glColor
00308      // glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
00309      
00310 //     glutIgnoreKeyRepeat(1);
00311      // glutReshapeFunc(win_reshape_);
00312      // glutDisplayFunc(win_redraw_);
00313      // glutKeyboardFunc(win_key_);
00314      // glutMouseFunc(win_mouse_);
00315      // glutMotionFunc(win_motion_);
00316      // glutPassiveMotionFunc(NULL);
00317 
00318      // // Idle loop callback
00319      // glutIdleFunc(win_idle_);
00320 
00321      // // Window close function
00322      // glutCloseFunc(win_close_);
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); // origin of the FIRST line
00333      glVertex3f(1.0f, 0.0f, 0.0f); // ending point of the FIRST line
00334      glColor3f(0, 1, 0);
00335      glVertex3f(0.0f, 0.0f, 0.0f); // origin of the SECOND line
00336      glVertex3f(0.0f, 1.0f, 0.0f); // ending point of the SECOND line
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";// + getIntString(save_inc_counter, 4) + ".jpg";
00347      save_inc_counter++;
00348      return save(file_name);
00349 }
00350 
00351 int
00352 NDTVizGlut::save(const std::string &fileName)
00353 {
00354      // IplImage*          img;
00355      // GLint           viewport[4];
00356      // GLint           width, height;
00357      
00358      // glReadBuffer(GL_FRONT);
00359      // glGetIntegerv(GL_VIEWPORT, viewport);
00360      
00361      // width = viewport[2];
00362      // height = viewport[3];
00363 
00364      // // allocate the image
00365      // CvSize size = cvSize(width, height);
00366      // img = cvCreateImage( size, IPL_DEPTH_8U, 3 );
00367      
00368      // glFinish();
00369      // glPixelStorei(GL_PACK_ALIGNMENT, 4);
00370      // glPixelStorei(GL_PACK_ROW_LENGTH, 0);
00371      // glPixelStorei(GL_PACK_SKIP_ROWS, 0);
00372      // glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
00373      
00374      
00375      // glReadPixels(0, 0, width, height,
00376      //           GL_BGR,
00377      //           GL_UNSIGNED_BYTE,
00378      //           img->imageData);
00379 
00380      // cvFlip(img); // The images are drawn upside down.
00381      // int ret_val = ocvSaveImage(img, fileName.c_str());
00382      // cvReleaseImage(&img);
00383 
00384      // return ret_val;
00385     return 1;
00386 }
00387 
00388 void
00389 NDTVizGlut::repaint() {
00390     //this->win_redraw();
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     // Not really useful in this context. This will instead be interpreted as setCameraPointingAt. Note that the setCameraPointingAt will also move the camera. Is this function anyway used at all?
00408     this->setCameraPointingToPoint(x,y,z);
00409 }


ndt_visualisation
Author(s): Todor Stoyanov, Jari Saarinen, Henrik Andreasson
autogenerated on Wed Aug 26 2015 15:24:57