00001
00002
00003
00004 #include "PTAM/OpenGL.h"
00005 #include "GLWindow2.h"
00006 #include "GLWindowMenu.h"
00007 #include <stdlib.h>
00008 #include <gvars3/GStringUtil.h>
00009 #include <gvars3/instances.h>
00010 #include <TooN/helpers.h>
00011
00012 using namespace CVD;
00013 using namespace std;
00014 using namespace GVars3;
00015 using namespace TooN;
00016
00017 GLWindow2::GLWindow2(ImageRef irSize, string sTitle, MouseKeyHandler* handler)
00018 : GLWindow(irSize, sTitle)
00019 {
00020
00021 myHandler = handler;
00022
00023 mirVideoSize = irSize;
00024 GUI.RegisterCommand("GLWindow.AddMenu", GUICommandCallBack, this);
00025 glSetFont("sans");
00026 mvMCPoseUpdate=Zeros;
00027 mvLeftPoseUpdate=Zeros;
00028 };
00029
00030
00031 void GLWindow2::AddMenu(string sName, string sTitle)
00032 {
00033 GLWindowMenu* pMenu = new GLWindowMenu(sName, sTitle);
00034 mvpGLWindowMenus.push_back(pMenu);
00035 }
00036
00037 void GLWindow2::GUICommandCallBack(void* ptr, string sCommand, string sParams)
00038 {
00039 ((GLWindow2*) ptr)->GUICommandHandler(sCommand, sParams);
00040 }
00041
00042 void GLWindow2::GUICommandHandler(string sCommand, string sParams)
00043 {
00044 vector<string> vs=ChopAndUnquoteString(sParams);
00045 if(sCommand=="GLWindow.AddMenu")
00046 {
00047 switch(vs.size())
00048 {
00049 case 1:
00050 AddMenu(vs[0], "Root");
00051 return;
00052 case 2:
00053 AddMenu(vs[0], vs[1]);
00054 return;
00055 default:
00056 cout << "? AddMenu: need one or two params (internal menu name, [caption])." << endl;
00057 return;
00058 };
00059 };
00060
00061
00062
00063 cout << "! GLWindow::GUICommandHandler: unhandled command "<< sCommand << endl;
00064 exit(1);
00065 };
00066
00067 void GLWindow2::DrawMenus()
00068 {
00069 glDisable(GL_STENCIL_TEST);
00070 glDisable(GL_DEPTH_TEST);
00071 glDisable(GL_TEXTURE_2D);
00072 glDisable(GL_TEXTURE_RECTANGLE_ARB);
00073 glDisable(GL_LINE_SMOOTH);
00074 glDisable(GL_POLYGON_SMOOTH);
00075 glEnable(GL_BLEND);
00076 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00077 glColorMask(1,1,1,1);
00078 glMatrixMode(GL_MODELVIEW);
00079 glLoadIdentity();
00080 SetupWindowOrtho();
00081 glLineWidth(1);
00082
00083 int nTop = 30;
00084 int nHeight = 30;
00085 for(vector<GLWindowMenu*>::iterator i = mvpGLWindowMenus.begin();
00086 i!= mvpGLWindowMenus.end();
00087 i++)
00088 {
00089 (*i)->Render(nTop, nHeight, size()[0], *this);
00090 nTop+=nHeight+1;
00091 }
00092
00093 }
00094
00095 void GLWindow2::SetupUnitOrtho()
00096 {
00097 glMatrixMode(GL_PROJECTION);
00098 glLoadIdentity();
00099 glOrtho(0,1,1,0,0,1);
00100 }
00101
00102 void GLWindow2::SetupWindowOrtho()
00103 {
00104 glMatrixMode(GL_PROJECTION);
00105 glLoadIdentity();
00106 glOrtho(size());
00107 }
00108
00109 void GLWindow2::SetupVideoOrtho()
00110 {
00111 glMatrixMode(GL_PROJECTION);
00112 glLoadIdentity();
00113 glOrtho(-0.5,(double)mirVideoSize.x - 0.5, (double) mirVideoSize.y - 0.5, -0.5, -1.0, 1.0);
00114 }
00115
00116 void GLWindow2::SetupVideoRasterPosAndZoom()
00117 {
00118 glRasterPos2d(-0.5,-0.5);
00119 double adZoom[2];
00120 adZoom[0] = (double) size()[0] / (double) mirVideoSize[0];
00121 adZoom[1] = (double) size()[1] / (double) mirVideoSize[1];
00122 glPixelZoom((float)adZoom[0], -(float)adZoom[1]);
00123 }
00124
00125 void GLWindow2::SetupViewport()
00126 {
00127 glViewport(0, 0, size()[0], size()[1]);
00128 }
00129
00130 void GLWindow2::PrintString(CVD::ImageRef irPos, std::string s)
00131 {
00132 glMatrixMode(GL_PROJECTION);
00133 glPushMatrix();
00134 glTranslatef((float)irPos.x, (float)irPos.y, 0.0);
00135 glScalef(8,-8,1);
00136 glDrawText(s, NICE, 1.6, 0.1);
00137 glPopMatrix();
00138 }
00139
00140 void GLWindow2::DrawCaption(string s)
00141 {
00142 if(s.length() == 0)
00143 return;
00144
00145 SetupWindowOrtho();
00146
00147
00148 int nLines = 0;
00149 {
00150 string sendl("\n");
00151 string::size_type st=0;
00152 while(1)
00153 {
00154 nLines++;
00155 st = s.find(sendl, st);
00156 if(st==string::npos)
00157 break;
00158 else
00159 st++;
00160 }
00161 }
00162
00163 int nTopOfBox = size().y - nLines * 17;
00164
00165
00166 glColor4f(0,0,0,0.4f);
00167 glEnable(GL_BLEND);
00168 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00169 glBegin(GL_QUADS);
00170 glVertex2d(-0.5, nTopOfBox);
00171 glVertex2d(size().x, nTopOfBox);
00172 glVertex2d(size().x, size().y);
00173 glVertex2d(-0.5, size().y);
00174 glEnd();
00175
00176
00177 glColor3f(1,1,0);
00178 PrintString(ImageRef(10,nTopOfBox + 13), s);
00179 }
00180
00181
00182 void GLWindow2::HandlePendingEvents()
00183 {
00184 handle_events(*this);
00185 }
00186
00187 void GLWindow2::on_mouse_move(GLWindow& win, CVD::ImageRef where, int state)
00188 {
00189 ImageRef irMotion = where - mirLastMousePos;
00190 mirLastMousePos = where;
00191
00192 double dSensitivity = 0.01;
00193 if(state & BUTTON_LEFT && ! (state & BUTTON_RIGHT))
00194 {
00195 mvMCPoseUpdate[3] -= irMotion[1] * dSensitivity;
00196 mvMCPoseUpdate[4] += irMotion[0] * dSensitivity;
00197 }
00198 else if(!(state & BUTTON_LEFT) && state & BUTTON_RIGHT)
00199 {
00200 mvLeftPoseUpdate[4] -= irMotion[0] * dSensitivity;
00201 mvLeftPoseUpdate[3] += irMotion[1] * dSensitivity;
00202 }
00203 else if(state & BUTTON_MIDDLE || (state & BUTTON_LEFT && state & BUTTON_RIGHT))
00204 {
00205 mvLeftPoseUpdate[5] -= irMotion[0] * dSensitivity;
00206 mvLeftPoseUpdate[2] += irMotion[1] * dSensitivity;
00207 }
00208
00209 myHandler->on_mouse_move(where, state);
00210 }
00211
00212 void GLWindow2::on_mouse_down(GLWindow& win, CVD::ImageRef where, int state, int button)
00213 {
00214 myHandler->on_mouse_down(where, state, button);
00215 }
00216
00217 void GLWindow2::on_event(GLWindow& win, int event)
00218 {
00219 myHandler->on_event(event);
00220 }
00221
00222 pair<Vector<6>, Vector<6> > GLWindow2::GetMousePoseUpdate()
00223 {
00224 pair<Vector<6>, Vector<6> > result = make_pair(mvLeftPoseUpdate, mvMCPoseUpdate);
00225 mvLeftPoseUpdate = Zeros;
00226 mvMCPoseUpdate = Zeros;
00227 return result;
00228 }
00229
00230
00231
00232
00233 void GLWindow2::on_key_down(GLWindow&, int k)
00234 {
00235 myHandler->on_key_down(k);
00236 }
00237
00238