-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathGLWindow2.cc
430 lines (373 loc) · 10.5 KB
/
GLWindow2.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// Copyright 2009 Isis Innovation Limited
#include "OpenGL.h"
#include "GLWindow2.h"
#include "GLWindowMenu.h"
#include <stdlib.h>
#include <gvars3/GStringUtil.h>
#include <gvars3/instances.h>
#include <TooN/helpers.h>
namespace PTAMM {
using namespace CVD;
using namespace std;
using namespace GVars3;
using namespace TooN;
GLWindow2::GLWindow2(ImageRef irSize, string sTitle)
: GLWindow(irSize, sTitle)
{
#ifdef WIN32
// On windows, have to initialise GLEW at the start to enable access
// to GL extensions
static bool bGLEWIsInit = false;
if(!bGLEWIsInit)
{
GLenum err = glewInit();
if (GLEW_OK != err)
{
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
exit(0);
}
bGLEWIsInit = true;
}
#endif
mirVideoSize = irSize;
GUI.RegisterCommand("GLWindow.AddMenu", GUICommandCallBack, this);
glSetFont("sans");
mvMCPoseUpdate=Zeros;
mvLeftPoseUpdate=Zeros;
};
void GLWindow2::AddMenu(string sName, string sTitle)
{
GLWindowMenu* pMenu = new GLWindowMenu(sName, sTitle);
mvpGLWindowMenus.push_back(pMenu);
}
void GLWindow2::GUICommandCallBack(void* ptr, string sCommand, string sParams)
{
((GLWindow2*) ptr)->GUICommandHandler(sCommand, sParams);
}
void GLWindow2::GUICommandHandler(string sCommand, string sParams) // Called by the callback func..
{
vector<string> vs=ChopAndUnquoteString(sParams);
if(sCommand=="GLWindow.AddMenu")
{
switch(vs.size())
{
case 1:
AddMenu(vs[0], "Root");
return;
case 2:
AddMenu(vs[0], vs[1]);
return;
default:
cout << "? AddMenu: need one or two params (internal menu name, [caption])." << endl;
return;
};
};
// Should have returned to caller by now - if got here, a command which
// was not handled was registered....
cout << "! GLWindow::GUICommandHandler: unhandled command "<< sCommand << endl;
exit(1);
};
void GLWindow2::DrawMenus()
{
glDisable(GL_STENCIL_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_RECTANGLE_ARB);
glDisable(GL_LINE_SMOOTH);
glDisable(GL_POLYGON_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColorMask(1,1,1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
SetupWindowOrtho();
glLineWidth(1);
int nTop = 30;
int nHeight = 30;
for(vector<GLWindowMenu*>::iterator i = mvpGLWindowMenus.begin();
i!= mvpGLWindowMenus.end();
i++)
{
(*i)->Render(nTop, nHeight, size()[0], *this);
nTop+=nHeight+1;
}
}
void GLWindow2::SetupUnitOrtho()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,1,1,0,0,1);
}
const void GLWindow2::SetupWindowOrtho() const
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(size());
}
void GLWindow2::SetupVideoOrtho()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-0.5,(double)mirVideoSize.x - 0.5, (double) mirVideoSize.y - 0.5, -0.5, -1.0, 1.0);
}
void GLWindow2::SetupVideoRasterPosAndZoom()
{
glRasterPos2d(-0.5,-0.5);
double adZoom[2];
adZoom[0] = (double) size()[0] / (double) mirVideoSize[0];
adZoom[1] = (double) size()[1] / (double) mirVideoSize[1];
glPixelZoom(static_cast<float>(adZoom[0]), static_cast<float>(-adZoom[1]));
}
void GLWindow2::SetupViewport()
{
glViewport(0, 0, size()[0], size()[1]);
}
const void GLWindow2::PrintString(CVD::ImageRef irPos, std::string s) const
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glTranslatef(static_cast<float>(irPos.x), static_cast<float>(irPos.y), 0.0f);
glScalef(8,-8,1);
glDrawText(s, NICE, 1.6, 0.1);
glPopMatrix();
}
void GLWindow2::DrawCaption(string s)
{
if(s.length() == 0)
return;
SetupWindowOrtho();
// Find out how many lines are in the caption:
// Count the endls
int nLines = 0;
{
string sendl("\n");
string::size_type st=0;
while(1)
{
nLines++;
st = s.find(sendl, st);
if(st==string::npos)
break;
else
st++;
}
}
int nTopOfBox = size().y - nLines * 17;
// Draw a grey background box for the text
glColor4f(0.0f,0.0f,0.0f,0.4f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
glVertex2d(-0.5, nTopOfBox);
glVertex2d(size().x, nTopOfBox);
glVertex2d(size().x, size().y);
glVertex2d(-0.5, size().y);
glEnd();
// Draw the caption text in yellow
glColor3f(1,1,0);
PrintString(ImageRef(10,nTopOfBox + 13), s);
}
/**
* Draw a grey, semi transparent box
* @param x Top left corner x
* @param y Top left corner y
* @param w Width of box
* @param nLines Number of lines of text
*/
const void GLWindow2::DrawBox(int x, int y, int w, int nLines, float fAlpha) const
{
SetupWindowOrtho();
int h = nLines * 17;
// Draw a grey background box for the text
glColor4f(0,0,0,fAlpha);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
glVertex2d( x, y );
glVertex2d( x + w, y );
glVertex2d( x + w, y + h );
glVertex2d( x, y + h );
glEnd();
}
void GLWindow2::HandlePendingEvents()
{
handle_events(*this);
}
void GLWindow2::on_mouse_move(GLWindow& win, CVD::ImageRef where, int state)
{
ImageRef irMotion = where - mirLastMousePos;
mirLastMousePos = where;
double dSensitivity = 0.01;
if( (state & BUTTON_LEFT) && ! (state & BUTTON_RIGHT) )
{
mvMCPoseUpdate[3] -= irMotion[1] * dSensitivity;
mvMCPoseUpdate[4] += irMotion[0] * dSensitivity;
}
else if( !(state & BUTTON_LEFT) && state & BUTTON_RIGHT )
{
mvLeftPoseUpdate[4] -= irMotion[0] * dSensitivity;
mvLeftPoseUpdate[3] += irMotion[1] * dSensitivity;
}
else if( (state & BUTTON_MIDDLE) || (state & BUTTON_LEFT && state & BUTTON_RIGHT) )
{
mvLeftPoseUpdate[5] -= irMotion[0] * dSensitivity;
mvLeftPoseUpdate[2] += irMotion[1] * dSensitivity;
}
}
/**
* What to do when the user presses a mouse button
* @param win the window
* @param where the location
* @param state the button state
* @param button the button pressed
*/
void GLWindow2::on_mouse_down(GLWindow& win, CVD::ImageRef where, int state, int button)
{
bool bHandled = false;
for(unsigned int i=0; !bHandled && i<mvpGLWindowMenus.size(); i++) {
bHandled |= mvpGLWindowMenus[i]->HandleClick(button, state, where.x, where.y);
}
if( !bHandled ) {
ostringstream os;
os << "Mouse.Click " << button << " " << where.x << " " << where.y << endl;
GUI.ParseLine( os.str() );
}
mmMouseButtonPressed[ button ] = true;
}
/**
* What to do when the user releases a mouse button
* @param win the window
* @param where the location
* @param state the button state
* @param button the button pressed
*/
void GLWindow2::on_mouse_up(GLWindow& win, CVD::ImageRef where, int state, int button)
{
mmMouseButtonPressed[ button ] = false;
}
void GLWindow2::on_event(GLWindow& win, int event)
{
if(event==EVENT_CLOSE)
GUI.ParseLine("quit");
}
pair<Vector<6>, Vector<6> > GLWindow2::GetMousePoseUpdate()
{
pair<Vector<6>, Vector<6> > result = make_pair(mvLeftPoseUpdate, mvMCPoseUpdate);
mvLeftPoseUpdate = Zeros;
mvMCPoseUpdate = Zeros;
return result;
}
/**
* Process the key press, and notify System using the command system
* @param glWindow The GL window.
* @param k the key code.
*/
#ifndef WIN32
#include <X11/keysym.h>
void GLWindow2::on_key_down(GLWindow&, int k)
{
string s;
switch(k)
{
case XK_a: case XK_A: s="a"; break;
case XK_b: case XK_B: s="b"; break;
case XK_c: case XK_C: s="c"; break;
case XK_d: case XK_D: s="d"; break;
case XK_e: case XK_E: s="e"; break;
case XK_f: case XK_F: s="f"; break;
case XK_g: case XK_G: s="g"; break;
case XK_h: case XK_H: s="h"; break;
case XK_i: case XK_I: s="i"; break;
case XK_j: case XK_J: s="j"; break;
case XK_k: case XK_K: s="k"; break;
case XK_l: case XK_L: s="l"; break;
case XK_m: case XK_M: s="m"; break;
case XK_n: case XK_N: s="n"; break;
case XK_o: case XK_O: s="o"; break;
case XK_p: case XK_P: s="p"; break;
case XK_q: case XK_Q: s="q"; break;
case XK_r: case XK_R: s="r"; break;
case XK_s: case XK_S: s="s"; break;
case XK_t: case XK_T: s="t"; break;
case XK_u: case XK_U: s="u"; break;
case XK_v: case XK_V: s="v"; break;
case XK_w: case XK_W: s="w"; break;
case XK_x: case XK_X: s="x"; break;
case XK_y: case XK_Y: s="y"; break;
case XK_z: case XK_Z: s="z"; break;
case XK_1: s="1"; break;
case XK_2: s="2"; break;
case XK_3: s="3"; break;
case XK_4: s="4"; break;
case XK_5: s="5"; break;
case XK_6: s="6"; break;
case XK_7: s="7"; break;
case XK_8: s="8"; break;
case XK_9: s="9"; break;
case XK_0: s="0"; break;
case XK_KP_Prior: case XK_Page_Up: s="PageUp"; break;
case XK_KP_Next: case XK_Page_Down: s="PageDown"; break;
case XK_Return: s="Enter"; break;
case XK_space: s="Space"; break;
case XK_BackSpace: s="BackSpace"; break;
case XK_Escape: s="Escape"; break;
default: ;
}
if(s!="")
GUI.ParseLine("try KeyPress "+s);
}
#else
void GLWindow2::on_key_down(GLWindow&, int k)
{
string s;
// ASCI chars can be mapped directly:
if( (k >= 48 && k <=57) || ( k >=97 && k <= 122) || (k >= 65 && k <= 90))
{
char c = k;
if(c >= 65 && c <= 90)
c = c + 32;
s = c;
}
else switch (k) // Some special chars are translated:
{
case 33: s="PageUp"; break;
case 34: s="PageDown"; break;
case 13: s="Enter"; break;
case 32: s="Space"; break;
case 8: s="BackSpace"; break;
case 27: s="Escape"; break;
default: break;
}
if(s!="")
GUI.ParseLine("try KeyPress "+s);
}
#endif
/**
* Calculate the video image coordinates from the window coordinates.
* This is needed when the window has been resized
* @param v2WinCoords the window coordinates
* @return the video coordinates
*/
Vector<2> GLWindow2::VidFromWinCoords( CVD::ImageRef irWin )
{
double dXScale = (double) mirVideoSize[0] / (double) size().x;
double dYScale = (double) mirVideoSize[1] / (double) size().y;
Vector<2> v2;
v2[0] = dXScale * (double) irWin.x;
v2[1] = dYScale * (double) irWin.y;
return v2;
}
/**
* Find out if a mouse button is currently pressed.
* @param button the button id to query
* @return true if down, false if not or unknown.
*/
const bool GLWindow2::IsMouseButtonPressed( const int button ) const
{
std::map <int, bool >::const_iterator it = mmMouseButtonPressed.find( button );
if( it == mmMouseButtonPressed.end() ) {
return false;
}
return (*it).second;
}
}