GlutViewer.cpp
Go to the documentation of this file.
1 #include "GlutViewer.h"
2 
3 #include "Rotation.h"
4 #include "Platform.h"
5 
6 using namespace std;
7 using namespace alvar;
8 using namespace GlutViewer;
9 
10 Drawable::Drawable(double _scale, double _r, double _g, double _b) {
11  SetScale(_scale);
12  SetColor(_r, _g, _b);
13 }
14 
15 void Drawable::SetScale(double _scale) {
16  scale=_scale;
17 }
18 
19 void Drawable::SetColor(double _r, double _g, double _b) {
20  color[0] = _r;
21  color[1] = _g;
22  color[2] = _b;
23 }
24 
26  //double color[3] = {1, 1, 1};
27  glPushMatrix();
28  glMultMatrixd(gl_mat);
29  DrawAxis(scale, color);
30  glPopMatrix();
31 }
32 
33 void Drawable::DrawAxis(double scale, double color[3])
34 {
35  glEnable(GL_DEPTH_TEST);
36  glDisable(GL_CULL_FACE);
37 
38  glColor3d(color[0], color[1], color[2]);
39  glBegin(GL_QUADS);
40  glVertex3f(-scale/2, -scale/2, 0.0);
41  glVertex3f(-scale/2, scale/2, 0.0);
42 
43  glVertex3f(-scale/2, scale/2, 0.0);
44  glVertex3f( scale/2, scale/2, 0.0);
45 
46  glVertex3f( scale/2, scale/2, 0.0);
47  glVertex3f( scale/2, -scale/2, 0.0);
48 
49  glVertex3f( scale/2, -scale/2, 0.0);
50  glVertex3f(-scale/2, -scale/2, 0.0);
51  glEnd();
52 
53  // Z
54  glColor3d(0.0, 0.0, 1.0);
55  glBegin(GL_LINES);
56  glVertex3f(0.0, 0.0, 0.0);
57  glVertex3f(0.0, 0.0, scale);
58  glEnd();
59 
60  glDisable(GL_DEPTH_TEST);
61 
62  // X
63  glColor3d(1.0, 0.0, 0.0);
64  glBegin(GL_LINES);
65  glVertex3f(0.0, 0.0, 0.0);
66  glVertex3f(scale, 0.0, 0.0);
67  glEnd();
68 
69  // Y
70  glColor3d(0.0, 1.0, 0.0);
71  glBegin(GL_LINES);
72  glVertex3f(0.0, 0.0, 0.0);
73  glVertex3f(0.0, scale, 0.0);
74  glEnd();
75 }
76 
77 void Drawable::SetGLMatTraQuat(double *tra, double *quat, bool flip)
78 {
79  Rotation r;
80  if (quat != 0)
81  {
82  CvMat cv_mat;
83  cvInitMatHeader(&cv_mat, 4, 1, CV_64F, quat);
84  r.SetQuaternion(&cv_mat);
85  }
86 
87  int flp = 1;
88  if (flip)
89  {
90  r.Transpose();
91  //flp=-1;
92  }
93 
94  CvMat cv_gl_mat;
95  cvInitMatHeader(&cv_gl_mat, 4, 4, CV_64F, gl_mat); cvZero(&cv_gl_mat);
96  r.GetMatrix(&cv_gl_mat);
97  cvSet2D(&cv_gl_mat, 0, 3, cvScalar(flp*tra[0]));
98  cvSet2D(&cv_gl_mat, 1, 3, cvScalar(flp*tra[1]));
99  cvSet2D(&cv_gl_mat, 2, 3, cvScalar(flp*tra[2]));
100  cvSet2D(&cv_gl_mat, 3, 3, cvScalar(1));
101 
102  cvTranspose(&cv_gl_mat, &cv_gl_mat);
103 }
104 
105 void Drawable::SetGLMatTraRod(double *tra, double *rod)
106 {
107  // This is the OpenGL augmentation matrix
108  CvMat cv_gl_mat;
109  cvInitMatHeader(&cv_gl_mat, 4, 4, CV_64F, gl_mat); cvSetIdentity(&cv_gl_mat);
110 
111  // Figure out the rotation part
112  double rot_mat_data[3][3];
113  CvMat rot_mat = cvMat(3, 3, CV_64F, rot_mat_data);
114  cvSetIdentity(&rot_mat);
115  if (rod != 0)
116  {
117  CvMat rod_mat;
118  cvInitMatHeader(&rod_mat, 3, 1, CV_64F, rod);
119  cvRodrigues2(&rod_mat, &rot_mat, 0);
120  }
121 
122  // Fill in the rotation part
123  cvmSet(&cv_gl_mat, 0, 0, cvmGet(&rot_mat, 0, 0));
124  cvmSet(&cv_gl_mat, 0, 1, cvmGet(&rot_mat, 0, 1));
125  cvmSet(&cv_gl_mat, 0, 2, cvmGet(&rot_mat, 0, 2));
126  cvmSet(&cv_gl_mat, 1, 0, cvmGet(&rot_mat, 1, 0));
127  cvmSet(&cv_gl_mat, 1, 1, cvmGet(&rot_mat, 1, 1));
128  cvmSet(&cv_gl_mat, 1, 2, cvmGet(&rot_mat, 1, 2));
129  cvmSet(&cv_gl_mat, 2, 0, cvmGet(&rot_mat, 2, 0));
130  cvmSet(&cv_gl_mat, 2, 1, cvmGet(&rot_mat, 2, 1));
131  cvmSet(&cv_gl_mat, 2, 2, cvmGet(&rot_mat, 2, 2));
132 
133  // Fill in the translation part
134  cvSet2D(&cv_gl_mat, 0, 3, cvScalar(tra[0]));
135  cvSet2D(&cv_gl_mat, 1, 3, cvScalar(tra[1]));
136  cvSet2D(&cv_gl_mat, 2, 3, cvScalar(tra[2]));
137 
138  // Transpose into OpenGL presentation order
139  cvTranspose(&cv_gl_mat, &cv_gl_mat);
140 }
141 
143 vector<Drawable*> items;
144 
146 float elev = 0.0, azim = 0.0, rad = 0.0;
147 float panx = 0.0, pany = 0.0;
148 float jaw = 0.0, jawx, jawy, jawz;
149 
152 
153 float off_x=0, off_y=0;
154 
155 unsigned char* image=0;
156 
157 int a_argc;
158 char **a_argv;
159 int width;
160 int height;
161 
163 
164 double proj_mat[16];
165 double modelview_mat[16] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
166 
167 static void *glut_thread(void *lpThreadParameter)
168 {
169  //InitializeCriticalSection(&critical_section_items);
170 
171  glutInit(&a_argc, a_argv);
172  glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE);
173  glutInitWindowSize(width, height);
174 
175  ar_window = glutCreateWindow("AR");
176  glutDisplayFunc(DrawAr);
177  glutSpecialFunc(KeyCallback);
178  glutPositionWindow(0, 0);
179 
180  vr_window = glutCreateWindow("VR");
181  glutDisplayFunc(DrawVr);
182  glutPositionWindow(0, height);
183 
184  glEnable(GL_CULL_FACE);
185  glEnable(GL_DEPTH_TEST);
186  glEnable(GL_COLOR_MATERIAL);
187 
188  glutMouseFunc(Mouse);
189  glutMotionFunc(Motion);
190 
191  atexit(Exit);
192 
193  glutMainLoop();
194  return 0;
195 }
196 
197 void GlutViewer::KeyCallback(int key, int x, int y)
198 {
199  switch(key)
200  {
201  case GLUT_KEY_LEFT:
202  off_x-=1;
203  break;
204  case GLUT_KEY_RIGHT:
205  off_x+=1;
206  break;
207  case GLUT_KEY_UP:
208  off_y-=1;
209  break;
210  case GLUT_KEY_DOWN:
211  off_y+=1;
212  break;
213 
214  }
215 }
216 
218 {
219  return off_x;
220 }
221 
223 {
224  return off_y;
225 }
226 
227 void GlutViewer::Start(int argc, char** argv, int w, int h, float r)
228 {
229  a_argc = argc;
230  a_argv = argv;
231  width = w;
232  height = h;
233  rad = r;
234 
235  threads.create(glut_thread, 0);
236 }
237 
239 {
240  glColor3f(0.5, 0.5, 1.0);
241 
242  glBegin(GL_LINES);
243  for(int i = -20; i <= 20; i+=1)
244  {
245  glVertex3f((float)i, 0.0f, -20);
246  glVertex3f((float)i, 0.0f, 20);
247  glVertex3f(-20, 0.0f, (float)i);
248  glVertex3f( 20, 0.0f, (float)i);
249  }
250  glEnd();
251 }
252 
253 void GlutViewer::Mouse(int button, int state, int x, int y)
254 {
255  cur_button = button;
256 }
257 
258 void GlutViewer::DrawAxis(float scale)
259 {
260  glColor3f(1.0, 0.0, 0.0);
261  glBegin(GL_LINES);
262  glVertex3f(0.0, 0.0, 0.0);
263  glVertex3f(scale, 0.0, 0.0);
264  glEnd();
265  glColor3f(0.0, 1.0, 0.0);
266  glBegin(GL_LINES);
267  glVertex3f(0.0, 0.0, 0.0);
268  glVertex3f(0.0, scale, 0.0);
269  glEnd();
270  glColor3f(0.0, 0.0, 1.0);
271  glBegin(GL_LINES);
272  glVertex3f(0.0, 0.0, 0.0);
273  glVertex3f(0.0, 0.0, scale);
274  glEnd();
275 }
276 
277 void GlutViewer::Motion(int x, int y)
278 {
279  static int oldx, oldy;
280 
281  int dx = oldx-x;
282  int dy = oldy-y;
283 
284  switch(cur_button)
285  {
286  case GLUT_LEFT_BUTTON :
287  if (abs(dx)>abs(dy))
288  {
289  if (dx>0)
290  azim += 3.0;
291  else if (dx<0)
292  azim -= 3.0;
293  }
294  else if (dy>0)
295  elev -= 3.0;
296  else if (dy<0)
297  elev += 3.0;
298  break;
299 
300  case GLUT_MIDDLE_BUTTON :
301  if (abs(dx)>abs(dy))
302  {
303  if (dx>0)
304  panx += 10.5;
305  else if (dx<0)
306  panx -= 10.5;
307  }
308  else if (dy>0)
309  pany -= 10.5;
310  else if (dy<0)
311  pany += 10.5;
312  break;//??
313 
314  case GLUT_RIGHT_BUTTON :
315  if (dy > 0)
316  rad += (10.2);
317  else if (dy < 0)
318  rad -= (10.2);
319  break;
320 
321  default:
322  break;
323  }
324 
325  oldx = x;
326  oldy = y;
327 }
328 
329 
331 {
332  DrawAxis(100.f);
333  Lock lock(&mutex_items);
334  for (unsigned i = 0; i < items.size(); ++i) {
335  items[i]->Draw();
336  }
337 }
338 
339 
341 {
342  glutSetWindow(vr_window);
343  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
344  glClearColor(0.5, 0.2, 0.2, 1.0);
345 
346  //glViewport(0, 0, w, h);
347  glMatrixMode(GL_PROJECTION);
348  glLoadIdentity();
349  //gluPerspective(70, 1, 0.001, 5000);
350 
351  glMatrixMode(GL_MODELVIEW);
352  glLoadIdentity();
353 
354  glTranslatef(panx, pany, -rad);
355  glRotatef(-elev, 1.0, 0.0, 0.0);
356  glRotatef( azim, 0.0, 1.0, 0.0);
357 
358  float pos[4] = {50, 0, 50};
359  glLightfv(GL_LIGHT0, GL_POSITION, pos);
360 
361  DrawContent();
362 
363  //glFlush();
364  glutSwapBuffers();
365  glutPostRedisplay();
366 }
367 
369 {
370  glutSetWindow(ar_window);
371  glClearColor(0.2, 0.5, 0.2, 1.0);
372  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
373 
374  DrawVideo();
375 
376  glMatrixMode(GL_PROJECTION);
377  glLoadMatrixd(proj_mat);
378 
379  glMatrixMode(GL_MODELVIEW);
380  glLoadMatrixd(modelview_mat);
381 
382  DrawContent();
383 
384  glutSwapBuffers();
385  glutPostRedisplay();
386 }
387 
389  memcpy(proj_mat, p, sizeof(double)*16);
390 }
391 
393  memcpy(modelview_mat, p, sizeof(double)*16);
394 }
395 
396 void GlutViewer::Reshape(int w, int h)
397 {
398  h = (h == 0 ? 1 : h);
399  //glViewport(0, 0, w, h);
400 
401  //glMatrixMode(GL_PROJECTION);
402 
403  //glLoadIdentity();
404  //glMultMatrixd(projmat);
405 
406  //glMatrixMode(GL_MODELVIEW);
407 
408  //glutPostRedisplay();
409 }
410 
412 {
413  //DeleteCriticalSection(&critical_section_items);
414 }
415 
417  Lock lock(&mutex_items);
418  items.clear();
419 }
420 
422 {
423  Lock lock(&mutex_items);
424  items.push_back(item);
425 }
426 
427 // TODO ensure that threading doesn't cause any problems...
428 void GlutViewer::SetVideo(const IplImage* _image)
429 {
430  image = (unsigned char*)_image->imageData;
431 }
432 
434 {
435  if(!image) return;
436 
437  glDepthMask(GL_FALSE);
438  glDisable(GL_DEPTH_TEST);
439 
440  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
441  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
442  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
443  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
444 
445  glColor3f(1, 1, 1);
446  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, 512, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, image);
447 
448  glEnable(GL_TEXTURE_2D);
449 
450  glMatrixMode(GL_PROJECTION);
451  glPushMatrix();
452  glLoadIdentity();
453  glOrtho(0, 1, 0, 1, 0, 1);
454 
455  glMatrixMode(GL_MODELVIEW);
456  glLoadIdentity();
457 
458  glBegin( GL_QUADS );
459  glTexCoord2d(0.0,1.0); glVertex2d(0.0,0.0);
460  glTexCoord2d(1.0,1.0); glVertex2d(1.0,0.0);
461  glTexCoord2d(1.0,0.0); glVertex2d(1.0,1.0);
462  glTexCoord2d(0.0,0.0); glVertex2d(0.0,1.0);
463  glEnd();
464 
465  glDisable(GL_TEXTURE_2D);
466  glDepthMask(GL_TRUE);
467  glEnable(GL_DEPTH_TEST);
468 
469  glMatrixMode(GL_PROJECTION);
470  glPopMatrix();
471 }
472 
473 /*
474 void GlutViewer::Init(int argc, char** argv, int w, int h)
475 {
476  glutInit(&argc, argv);
477  glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE);
478  glutInitWindowSize(w, h);
479 
480  ar_window = glutCreateWindow("AR");
481  glutDisplayFunc(DrawAr);
482  //glutReshapeFunc(Reshape);
483 
484  vr_window = glutCreateWindow("VR");
485  glutDisplayFunc(DrawVr);
486  //glutReshapeFunc(Reshape);
487 
488  glEnable(GL_CULL_FACE);
489  glEnable(GL_DEPTH_TEST);
490  glEnable(GL_COLOR_MATERIAL);
491 
492  glutMouseFunc(Mouse);
493  glutMotionFunc(Motion);
494 
495  atexit(Exit);
496 }
497 */
Main ALVAR namespace.
Definition: Alvar.h:174
void DrawVr()
Definition: GlutViewer.cpp:340
double modelview_mat[16]
Definition: GlutViewer.cpp:165
Mutex for synchronizing multiple threads.
Definition: Mutex.h:44
void Mouse(int button, int state, int x, int y)
Definition: GlutViewer.cpp:253
int a_argc
Definition: GlutViewer.cpp:157
f
void SetGLMatTraRod(double *tra, double *rod)
Definition: GlutViewer.cpp:105
int cur_button
Definition: GlutViewer.cpp:145
char ** a_argv
Definition: GlutViewer.cpp:158
void DrawAxis(float scale)
Definition: GlutViewer.cpp:258
Drawable(double _scale=1, double _r=1, double _g=1, double _b=1)
Definition: GlutViewer.cpp:10
Threads vector for handling multiple threads.
Definition: Threads.h:44
unsigned char * image
Definition: GlutViewer.cpp:155
static void * glut_thread(void *lpThreadParameter)
Definition: GlutViewer.cpp:167
void DrawFloor()
Definition: GlutViewer.cpp:238
int height
Definition: GlutViewer.cpp:160
bool create(void *(*method)(void *), void *parameters)
Creates a new thread and returns true on success.
Definition: Threads.cpp:40
void DrawAr()
Definition: GlutViewer.cpp:368
void Transpose()
Definition: Rotation.cpp:66
void KeyCallback(int key, int x, int y)
Definition: GlutViewer.cpp:197
int vr_window
Definition: GlutViewer.cpp:151
This file implements a parametrized rotation.
void SetColor(double _r=1, double _g=1, double _b=1)
Definition: GlutViewer.cpp:19
double GetYOffset()
Definition: GlutViewer.cpp:222
void SetGlProjectionMatrix(double p[16])
Definition: GlutViewer.cpp:388
Mutex mutex_items
Definition: GlutViewer.cpp:142
virtual void Draw()
Definition: GlutViewer.cpp:25
float rad
Definition: GlutViewer.cpp:146
float pany
Definition: GlutViewer.cpp:147
void SetGlModelviewMatrix(double p[16])
Definition: GlutViewer.cpp:392
float azim
Definition: GlutViewer.cpp:146
void DrawContent()
Definition: GlutViewer.cpp:330
Lock for simplifying mutex handling.
Definition: Lock.h:46
void Start(int argc, char **argv, int w, int h, float r=300.0)
Definition: GlutViewer.cpp:227
void DrawableClear()
Definition: GlutViewer.cpp:416
int width
Definition: GlutViewer.cpp:159
Threads threads
Definition: GlutViewer.cpp:162
void DrawableAdd(Drawable *item)
Definition: GlutViewer.cpp:421
float jawy
Definition: GlutViewer.cpp:148
double GetXOffset()
Definition: GlutViewer.cpp:217
float elev
Definition: GlutViewer.cpp:146
float off_x
Definition: GlutViewer.cpp:153
void SetGLMatTraQuat(double *tra, double *quat, bool flip=false)
Definition: GlutViewer.cpp:77
float jaw
Definition: GlutViewer.cpp:148
virtual void DrawAxis(double scale, double color[3])
Definition: GlutViewer.cpp:33
float off_y
Definition: GlutViewer.cpp:153
void Reshape(int w, int h)
Definition: GlutViewer.cpp:396
void SetVideo(const IplImage *_image)
Definition: GlutViewer.cpp:428
void SetScale(double _scale)
Definition: GlutViewer.cpp:15
void Exit()
Definition: GlutViewer.cpp:411
int ar_window
Definition: GlutViewer.cpp:150
double proj_mat[16]
Definition: GlutViewer.cpp:164
float jawx
Definition: GlutViewer.cpp:148
void GetMatrix(CvMat *mat) const
Returns the rotation in matrix form. 3x3 and 4x4 matrices are allowed.
Definition: Rotation.cpp:331
Rotation structure and transformations between different parameterizations.
Definition: Rotation.h:43
void SetQuaternion(CvMat *mat)
Sets the rotation from given quaternion.
Definition: Rotation.cpp:294
r
void DrawVideo()
Definition: GlutViewer.cpp:433
float jawz
Definition: GlutViewer.cpp:148
float panx
Definition: GlutViewer.cpp:147
void Motion(int x, int y)
Definition: GlutViewer.cpp:277
This file implements generic platform specific methods.
vector< Drawable * > items
Definition: GlutViewer.cpp:143


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Mon Jun 10 2019 12:47:04