GLWindow2.cc
Go to the documentation of this file.
00001 #include "ptam/OpenGL.h"
00002 #include "ptam/GLWindow2.h"
00003 #include "ptam/GLWindowMenu.h"
00004 #include <stdlib.h>
00005 #include <gvars3/GStringUtil.h>
00006 #include <gvars3/instances.h>
00007 #include <TooN/helpers.h>
00008 
00009 using namespace CVD;
00010 using namespace std;
00011 using namespace GVars3;
00012 using namespace TooN;
00013 
00014 GLWindow2::GLWindow2(ImageRef irSize, string sTitle)
00015 : GLWindow(irSize, sTitle)
00016 {
00017 
00018 #ifdef WIN32
00019   // On windows, have to initialise GLEW at the start to enable access
00020   // to GL extensions
00021   static bool bGLEWIsInit = false;
00022   if(!bGLEWIsInit)
00023   {
00024     GLenum err = glewInit();
00025     if (GLEW_OK != err)
00026     {
00027       fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
00028       exit(0);
00029     }
00030     bGLEWIsInit = true;
00031   }
00032 #endif
00033 
00034   mirVideoSize = irSize;
00035   GUI.RegisterCommand("GLWindow.AddMenu", GUICommandCallBack, this);
00036   glSetFont("sans");
00037   mvMCPoseUpdate=Zeros;
00038   mvLeftPoseUpdate=Zeros;
00039 };
00040 
00041 
00042 void GLWindow2::AddMenu(string sName, string sTitle)
00043 {
00044   GLWindowMenu* pMenu = new GLWindowMenu(sName, sTitle); 
00045   mvpGLWindowMenus.push_back(pMenu);
00046 }
00047 
00048 void GLWindow2::GUICommandCallBack(void* ptr, string sCommand, string sParams)
00049 {
00050   ((GLWindow2*) ptr)->GUICommandHandler(sCommand, sParams);
00051 }
00052 
00053 void GLWindow2::GUICommandHandler(string sCommand, string sParams)  // Called by the callback func..
00054 {
00055   vector<string> vs=ChopAndUnquoteString(sParams);
00056   if(sCommand=="GLWindow.AddMenu")
00057   {
00058     switch(vs.size())
00059     {
00060       case 1:
00061         AddMenu(vs[0], "Root");
00062         return;
00063       case 2:
00064         AddMenu(vs[0], vs[1]);
00065         return;
00066       default:
00067         cout << "? AddMenu: need one or two params (internal menu name, [caption])." << endl;
00068         return;
00069     };
00070   };
00071 
00072   // Should have returned to caller by now - if got here, a command which 
00073   // was not handled was registered....
00074   cout << "! GLWindow::GUICommandHandler: unhandled command "<< sCommand << endl;
00075   exit(1);
00076 }; 
00077 
00078 void GLWindow2::DrawMenus()
00079 {
00080   glDisable(GL_STENCIL_TEST);
00081   glDisable(GL_DEPTH_TEST);
00082   glDisable(GL_TEXTURE_2D);
00083   glDisable(GL_TEXTURE_RECTANGLE_ARB);
00084   glDisable(GL_LINE_SMOOTH);
00085   glDisable(GL_POLYGON_SMOOTH);
00086   glEnable(GL_BLEND);
00087   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00088   glColorMask(1,1,1,1);
00089   glMatrixMode(GL_MODELVIEW);
00090   glLoadIdentity();
00091   SetupWindowOrtho();
00092   glLineWidth(1);
00093 
00094   int nTop = 30;
00095   int nHeight = 30;
00096   for(vector<GLWindowMenu*>::iterator i = mvpGLWindowMenus.begin();
00097       i!= mvpGLWindowMenus.end();
00098       i++)
00099   {
00100     (*i)->Render(nTop, nHeight, size()[0], *this);
00101     nTop+=nHeight+1;
00102   }
00103 
00104 }
00105 
00106 void GLWindow2::SetupUnitOrtho()
00107 {
00108   glMatrixMode(GL_PROJECTION);
00109   glLoadIdentity();
00110   glOrtho(0,1,1,0,0,1);
00111 }
00112 
00113 void GLWindow2::SetupWindowOrtho()
00114 {
00115   glMatrixMode(GL_PROJECTION);
00116   glLoadIdentity();
00117   glOrtho(size());
00118 }
00119 
00120 void GLWindow2::SetupVideoOrtho()
00121 {
00122   glMatrixMode(GL_PROJECTION);
00123   glLoadIdentity();
00124   glOrtho(-0.5,(double)mirVideoSize.x - 0.5, (double) mirVideoSize.y - 0.5, -0.5, -1.0, 1.0);
00125 }
00126 
00127 void GLWindow2::SetupVideoRasterPosAndZoom()
00128 { 
00129   glRasterPos2d(-0.5,-0.5);
00130   double adZoom[2];
00131   adZoom[0] = (double) size()[0] / (double) mirVideoSize[0];
00132   adZoom[1] = (double) size()[1] / (double) mirVideoSize[1];
00133   glPixelZoom(adZoom[0], -adZoom[1]);
00134 }
00135 
00136 void GLWindow2::SetupViewport()
00137 {
00138   glViewport(0, 0, size()[0], size()[1]);
00139 }
00140 
00141 void GLWindow2::PrintString(CVD::ImageRef irPos, std::string s)
00142 {
00143   glMatrixMode(GL_PROJECTION);
00144   glPushMatrix();
00145   glTranslatef(irPos.x, irPos.y, 0.0);
00146   glScalef(8,-8,1);
00147   glDrawText(s, NICE, 1.6, 0.1);
00148   glPopMatrix();
00149 }
00150 
00151 void GLWindow2::DrawCaption(string s)
00152 {
00153   if(s.length() == 0)
00154     return;
00155 
00156   SetupWindowOrtho();
00157   // Find out how many lines are in the caption:
00158   // Count the endls
00159   int nLines = 0;
00160   {
00161     string sendl("\n");
00162     string::size_type st=0;
00163     while(1)
00164     {
00165       nLines++;
00166       st = s.find(sendl, st);
00167       if(st==string::npos)
00168         break;
00169       else
00170         st++;
00171     }
00172   }
00173 
00174   int nTopOfBox = size().y - nLines * 17;
00175 
00176   // Draw a grey background box for the text
00177   glColor4f(0,0,0,0.4);
00178   glEnable(GL_BLEND);
00179   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00180   glBegin(GL_QUADS);
00181   glVertex2d(-0.5, nTopOfBox);
00182   glVertex2d(size().x, nTopOfBox);
00183   glVertex2d(size().x, size().y);
00184   glVertex2d(-0.5, size().y);
00185   glEnd();
00186 
00187   // Draw the caption text in yellow
00188   glColor3f(1,1,0);      
00189   PrintString(ImageRef(10,nTopOfBox + 13), s);
00190 }
00191 
00192 
00193 void GLWindow2::HandlePendingEvents()
00194 {
00195   handle_events(*this);
00196 }
00197 
00198 void GLWindow2::on_mouse_move(GLWindow& win, CVD::ImageRef where, int state)
00199 {
00200   ImageRef irMotion = where - mirLastMousePos;
00201   mirLastMousePos = where;
00202 
00203   double dSensitivity = 0.01;
00204   if(state & BUTTON_LEFT && ! (state & BUTTON_RIGHT))
00205   {
00206     mvMCPoseUpdate[3] -= irMotion[1] * dSensitivity;
00207     mvMCPoseUpdate[4] += irMotion[0] * dSensitivity;
00208   }
00209   else if(!(state & BUTTON_LEFT) && state & BUTTON_RIGHT)
00210   {
00211     mvLeftPoseUpdate[4] -= irMotion[0] * dSensitivity;
00212     mvLeftPoseUpdate[3] += irMotion[1] * dSensitivity;
00213   }
00214   else if(state & BUTTON_MIDDLE  || (state & BUTTON_LEFT && state & BUTTON_RIGHT))
00215   {
00216     mvLeftPoseUpdate[5] -= irMotion[0] * dSensitivity;
00217     mvLeftPoseUpdate[2] += irMotion[1] * dSensitivity;
00218   }
00219 
00220 }
00221 
00222 void GLWindow2::on_mouse_down(GLWindow& win, CVD::ImageRef where, int state, int button)
00223 {
00224   bool bHandled = false;
00225   for(unsigned int i=0; !bHandled && i<mvpGLWindowMenus.size(); i++)
00226     bHandled = mvpGLWindowMenus[i]->HandleClick(button, state, where.x, where.y);
00227 
00228 }
00229 
00230 void GLWindow2::on_event(GLWindow& win, int event)
00231 {
00232   if(event==EVENT_CLOSE)
00233     GUI.ParseLine("quit");
00234 }
00235 
00236 pair<Vector<6>, Vector<6> > GLWindow2::GetMousePoseUpdate()
00237 {
00238   pair<Vector<6>, Vector<6> > result = make_pair(mvLeftPoseUpdate, mvMCPoseUpdate);
00239   mvLeftPoseUpdate = Zeros;
00240   mvMCPoseUpdate = Zeros;
00241   return result;
00242 }
00243 
00244 
00245 
00246 #ifndef WIN32
00247 #include <X11/keysym.h>
00248 void GLWindow2::on_key_down(GLWindow&, int k)
00249 {
00250   string s;
00251   switch(k)
00252   {
00253     case XK_a:   case XK_A:  s="a"; break;
00254     case XK_b:   case XK_B:  s="b"; break;
00255     case XK_c:   case XK_C:  s="c"; break;
00256     case XK_d:   case XK_D:  s="d"; break;
00257     case XK_e:   case XK_E:  s="e"; break;
00258     case XK_f:   case XK_F:  s="f"; break;
00259     case XK_g:   case XK_G:  s="g"; break;
00260     case XK_h:   case XK_H:  s="h"; break;
00261     case XK_i:   case XK_I:  s="i"; break;
00262     case XK_j:   case XK_J:  s="j"; break;
00263     case XK_k:   case XK_K:  s="k"; break;
00264     case XK_l:   case XK_L:  s="l"; break;
00265     case XK_m:   case XK_M:  s="m"; break;
00266     case XK_n:   case XK_N:  s="n"; break;
00267     case XK_o:   case XK_O:  s="o"; break;
00268     case XK_p:   case XK_P:  s="p"; break;
00269     case XK_q:   case XK_Q:  s="q"; break;
00270     case XK_r:   case XK_R:  s="r"; break;
00271     case XK_s:   case XK_S:  s="s"; break;
00272     case XK_t:   case XK_T:  s="t"; break;
00273     case XK_u:   case XK_U:  s="u"; break;
00274     case XK_v:   case XK_V:  s="v"; break;
00275     case XK_w:   case XK_W:  s="w"; break;
00276     case XK_x:   case XK_X:  s="x"; break;
00277     case XK_y:   case XK_Y:  s="y"; break;
00278     case XK_z:   case XK_Z:  s="z"; break;
00279     case XK_1:   s="1"; break;
00280     case XK_2:   s="2"; break;
00281     case XK_3:   s="3"; break;
00282     case XK_4:   s="4"; break;
00283     case XK_5:   s="5"; break;
00284     case XK_6:   s="6"; break;
00285     case XK_7:   s="7"; break;
00286     case XK_8:   s="8"; break;
00287     case XK_9:   s="9"; break;
00288     case XK_0:   s="0"; break;
00289     case XK_KP_Prior: case XK_Page_Up:     s="PageUp"; break;
00290     case XK_KP_Next:  case XK_Page_Down:   s="PageDown"; break;
00291     case XK_Return: s="Enter"; break;
00292     case XK_space:  s="Space"; break;
00293     case XK_BackSpace:  s="BackSpace"; break;
00294     case XK_Escape:  s="Escape"; break;
00295     default: ;
00296   }
00297 
00298   if(s!="")
00299     GUI.ParseLine("try KeyPress "+s);
00300 }
00301 #else
00302 void GLWindow2::on_key_down(GLWindow&, int k)
00303 {
00304   string s;
00305   // ASCI chars can be mapped directly:
00306   if( (k >= 48 && k <=57) || ( k >=97 && k <= 122) || (k >= 65 && k <= 90))
00307   {
00308     char c = k;
00309     if(c >= 65 && c <= 90)
00310       c = c + 32;
00311     s = c;
00312   }
00313   else switch (k) // Some special chars are translated:
00314   {
00315     case 33: s="PageUp"; break;
00316     case 34: s="PageDown"; break;
00317     case 13: s="Enter"; break;
00318     case 32:  s="Space"; break;
00319     case 8:  s="BackSpace"; break;
00320     case 27:  s="Escape"; break;
00321     default: break;
00322   }
00323 
00324   if(s!="")
00325     GUI.ParseLine("try KeyPress "+s);
00326 }
00327 #endif
00328 


ptam
Author(s): Markus Achtelik , Stephan Weiss , Simon Lynen
autogenerated on Sun Oct 5 2014 23:52:33