File: glh_glut2.h

package info (click to toggle)
openni 1.5.4.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,208 kB
  • sloc: cpp: 116,706; ansic: 58,794; sh: 10,287; cs: 7,698; java: 7,402; python: 1,544; makefile: 492; xml: 167
file content (667 lines) | stat: -rw-r--r-- 16,363 bytes parent folder | download | duplicates (36)
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
    glh - is a platform-indepenedent C++ OpenGL helper library 


    Copyright (c) 2000 Cass Everitt
	Copyright (c) 2000 NVIDIA Corporation
    All rights reserved.

    Redistribution and use in source and binary forms, with or
	without modification, are permitted provided that the following
	conditions are met:

     * Redistributions of source code must retain the above
	   copyright notice, this list of conditions and the following
	   disclaimer.

     * Redistributions in binary form must reproduce the above
	   copyright notice, this list of conditions and the following
	   disclaimer in the documentation and/or other materials
	   provided with the distribution.

     * The names of contributors to this software may not be used
	   to endorse or promote products derived from this software
	   without specific prior written permission. 

       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
	   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	   REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
	   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
	   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
	   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
	   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
	   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
	   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
	   POSSIBILITY OF SUCH DAMAGE. 


    Cass Everitt - cass@r3.nu
*/

#ifndef GLH_GLUT_H
#define GLH_GLUT_H

// some helper functions and objects to
// make writing simple glut programs even easier! :-)
#ifndef _WIN32
#include <string.h>
#endif
#include <algorithm>
#include <list>

#ifdef MACOS
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <glh/glh_interactors.h>
#include <glh/glh_convenience.h>

namespace glh
{

  class glut_interactor
  {
  public:
	glut_interactor() { enabled = true; }

	virtual void display() {}
	virtual void idle() {}
	virtual void keyboard(unsigned char key, int x, int y) {}
	virtual void menu_status(int status, int x, int y) {}
	virtual void motion(int x, int y) {}
	virtual void mouse(int button, int state, int x, int y) {}
	virtual void passive_motion(int x, int y) {}
	virtual void reshape(int w, int h) {}
	virtual void special(int  key, int x, int y) {}
	virtual void timer(int value) {}
	virtual void visibility(int v) {}

	virtual void enable()  { enabled = true; }
	virtual void disable() { enabled = false; }

	bool enabled;
  };

  std::list<glut_interactor *> interactors;
  bool propagate;

  void glut_display_function()
  {
	propagate = true;
	for(std::list<glut_interactor *>::iterator it=interactors.begin(); it != interactors.end() && propagate; it++)
		(*it)->display();
  }

  void glut_idle_function()
  {
	propagate = true;
	for(std::list<glut_interactor *>::iterator it=interactors.begin(); it != interactors.end() && propagate; it++)
		(*it)->idle();
  }

  void glut_keyboard_function(unsigned char k, int x, int y)
  {
	propagate = true;
	for(std::list<glut_interactor *>::iterator it=interactors.begin(); it != interactors.end() && propagate; it++)
	  (*it)->keyboard(k, x, y);
  }

  void glut_menu_status_function(int status, int x, int y)
  {
	propagate = true;
	for(std::list<glut_interactor *>::iterator it=interactors.begin(); it != interactors.end() && propagate; it++)
		(*it)->menu_status(status, x, y);
  }

  void glut_motion_function(int x, int y)
  {
	propagate = true;
	for(std::list<glut_interactor *>::iterator it=interactors.begin(); it != interactors.end() && propagate; it++)
		(*it)->motion(x, y);
  }

  void glut_mouse_function(int button, int state, int x, int y)
  {
	propagate = true;
	for(std::list<glut_interactor *>::iterator it=interactors.begin(); it != interactors.end() && propagate; it++)
	  (*it)->mouse(button, state, x, y);
  }
  
  void glut_passive_motion_function(int x, int y)
  {
	propagate = true;
	for(std::list<glut_interactor *>::iterator it=interactors.begin(); it != interactors.end() && propagate; it++)
		(*it)->passive_motion(x, y);
  }

  void glut_reshape_function(int w, int h)
  {
	propagate = true;
	for(std::list<glut_interactor *>::iterator it=interactors.begin(); it != interactors.end() && propagate; it++)
	  (*it)->reshape(w,h);
  }

  void glut_special_function(int k, int x, int y)
  {
	propagate = true;
	for(std::list<glut_interactor *>::iterator it=interactors.begin(); it != interactors.end() && propagate; it++)
	  (*it)->special(k, x, y);
  }

  void glut_timer_function(int v)
  {
	propagate = true;
	for(std::list<glut_interactor *>::iterator it=interactors.begin(); it != interactors.end() && propagate; it++)
	  (*it)->timer(v);
  }

  void glut_visibility_function(int v)
  {
	propagate = true;
	for(std::list<glut_interactor *>::iterator it=interactors.begin(); it != interactors.end() && propagate; it++)
	  (*it)->visibility(v);
  }

  // stop processing the current event 
  inline void glut_event_processed()
  {
	  propagate = false;
  }

  inline void glut_helpers_initialize()
  {
	glutDisplayFunc(glut_display_function);
	glutIdleFunc(0);
	glutKeyboardFunc(glut_keyboard_function);
	glutMenuStatusFunc(glut_menu_status_function);
	glutMotionFunc(glut_motion_function);
	glutMouseFunc(glut_mouse_function);
	glutPassiveMotionFunc(glut_passive_motion_function);
	glutReshapeFunc(glut_reshape_function);
	glutSpecialFunc(glut_special_function);
	glutVisibilityFunc(glut_visibility_function);
  }

  inline void glut_remove_interactor(glut_interactor *gi)
  {
      if (interactors.empty())
	  return;
	  std::list<glut_interactor *>::iterator it = 
		  std::find(interactors.begin(), interactors.end(), gi);
	if(it != interactors.end())
	  interactors.erase(it);
  }

  inline void glut_add_interactor(glut_interactor *gi, bool append=true)
  {
	glut_remove_interactor(gi);
	if(append)
		interactors.push_back(gi);
	else
		interactors.push_front(gi);
  }

  inline void glut_timer(int msec, int value)
  {
	glutTimerFunc(msec, glut_timer_function, value);
  }

  inline void glut_idle(bool do_idle)
  {
	glutIdleFunc(do_idle ? glut_idle_function : 0);
  }

  class glut_callbacks : public glut_interactor
  {
  public:
	glut_callbacks() :
		display_function(0),
		idle_function(0),
		keyboard_function(0),
		menu_status_function(0),
		motion_function(0),
		mouse_function(0),
		passive_motion_function(0),
		reshape_function(0),
		special_function(0),
		timer_function(0),
		visibility_function()
	{}

	virtual void display()
	{ if(display_function) display_function(); }

	virtual void idle()
	{ if(idle_function) idle_function(); }

	virtual void keyboard(unsigned char k, int x, int y)
	{ if(keyboard_function) keyboard_function(k, x, y); }

	virtual void menu_status(int status, int x, int y)
	{ if(menu_status_function) menu_status_function(status, x, y); }

	virtual void motion(int x, int y)
	{ if(motion_function) motion_function(x,y); }

	virtual void mouse(int button, int state, int x, int y)
	{ if(mouse_function) mouse_function(button, state, x, y); }

	virtual void passive_motion(int x, int y)
	{ if(passive_motion_function) passive_motion_function(x, y); }

	virtual void reshape(int w, int h)
	{ if(reshape_function) reshape_function(w, h); }

	virtual void special(int key, int x, int y)
	{ if(special_function) special_function(key, x, y); }

	virtual void timer(int value)
	{ if(timer_function) timer_function(value); }

	virtual void visibility(int v)
	{ if(visibility_function) visibility_function(v); }

	void (* display_function) ();
	void (* idle_function) ();
	void (* keyboard_function) (unsigned char, int, int);
	void (* menu_status_function) (int, int, int);
	void (* motion_function) (int, int);
	void (* mouse_function) (int, int, int, int);
	void (* passive_motion_function) (int, int);
	void (* reshape_function) (int, int);
	void (* special_function) (int, int, int);
	void (* timer_function) (int);
	void (* visibility_function) (int);

  };



  class glut_perspective_reshaper : public glut_interactor
  {
  public:
	  glut_perspective_reshaper(float infovy = 60.f, float inzNear = .1f, float inzFar = 10.f)
		  : fovy(infovy), zNear(inzNear), zFar(inzFar), aspect_factor(1) {}
	  
	  void reshape(int w, int h)
	  {
		  width = w; height = h;

		  if(enabled) apply();
	  }

	  void apply()
	  {
		  glViewport(0,0,width,height);
		  glMatrixMode(GL_PROJECTION);
		  glLoadIdentity();
		  apply_projection();
		  glMatrixMode(GL_MODELVIEW);
	  }

	  matrix4f get_projection()
	  {
		  aspect = aspect_factor * float(width)/float(height);
          if ( aspect < 1 )
		  {
			  // fovy is a misnomer.. we really mean the fov applies to the
			  // smaller dimension
			  float fovx = fovy; 
			  float real_fov = to_degrees(2 * atan(tan(to_radians(fovx/2))/aspect));
              return perspective(real_fov, aspect, zNear, zFar);
		  }
          else
		    return perspective(fovy, aspect, zNear, zFar);
	  }

	  void apply_projection()
	  {
		glMultMatrixf(get_projection().m);
	  }

	  matrix4f get_projection_inverse()
	  {
		  aspect = aspect_factor * float(width)/float(height);
          if ( aspect < 1 )
		  {
			  // fovy is a misnomer.. we really mean the fov applies to the
			  // smaller dimension
			  float fovx = fovy; 
			  float real_fov = to_degrees(2 * atan(tan(to_radians(fovx/2))/aspect));
              return perspective_inverse(real_fov, aspect, zNear, zFar);
		  }
          else
		    return perspective_inverse(fovy, aspect, zNear, zFar);
	  }

	  void apply_projection_inverse()
	  {
		  glMultMatrixf(get_projection_inverse().m);
	  }

	  int width, height;
	  float fovy, aspect, zNear, zFar;
	  float aspect_factor;
  };


  // activates/deactivates on particular mouse button/modifier combinations
  // and calculates deltas while active
  class glut_simple_interactor : public glut_interactor
  {
  public:
	glut_simple_interactor()
	{
	  num_modes = 0;
	  active = false;
	  width = height = 0;
	  x0 = y0 = x = y = dx = dy = 0;
	}

	virtual void mouse(int button, int state, int X, int Y)
	{
		for(int i=0; i < num_modes; i++)
		{
			if(enabled && button == cond[i].activate_on && state == GLUT_DOWN &&
				(! cond[i].use_modifiers || (cond[i].modifiers == glutGetModifiers())) )
			{
				mode = i;
				active = true;
				x = x0 = X;
				y = y0 = height - Y;
				dx = dy = 0;
				break;
			}
			else if (enabled && button == cond[i].activate_on && state == GLUT_UP)
			{
				if(dx == 0 && dy == 0)
					update();
				active = false;
				dx = dy = 0;
				mode = -1;
				break;
			}
		}
	}

	virtual void motion(int X, int Y)
	{
	  if(enabled && active)
	  {
		dx = X - x;   dy = (height - Y) - y;
		x = X;   y = height - Y;
		update();
	  }
	}

	void reshape(int w, int h)
	{
		  width = w; height = h;
	}

	virtual void apply_transform() = 0; 
	virtual void apply_inverse_transform() = 0;
	virtual matrix4f get_transform() = 0;
	virtual matrix4f get_inverse_transform() = 0;

	virtual void update() {}

	struct activate_condition
	{
		activate_condition()
		{
			activate_on = GLUT_LEFT_BUTTON;
			use_modifiers = true;
			modifiers = 0;
		}
		int activate_on;
		bool use_modifiers;
		int modifiers;
	};

	activate_condition cond[2];
	bool active;
	int x0, y0;
	int x, y;
	int dx, dy;
	int width, height;
	int num_modes;
	int mode;
  };


  class glut_translator : public translator, public glut_simple_interactor
  {
  public:

	void update()
	{
	  if(mode == 0)
		  pan(dx, dy);
	  else if(mode == 1)
		  dolly(-dy);
	  glutPostRedisplay();
	}

	void apply_transform()
	{
	  //cerr << "Applying transform: " << (x - x0) << ", " << (y - y0) << endl;
	  glTranslatef(t[0], t[1], t[2]);
	}

	void apply_inverse_transform()
	{
	  //cerr << "Applying transform: " << (x - x0) << ", " << (y - y0) << endl;
	  glTranslatef(-t[0], -t[1], -t[2]);
	}

	matrix4f get_transform()
	{
		return translator::get_transform();
	}

	matrix4f get_inverse_transform()
	{
		return translator::get_inverse_transform();
	}

  };


  class glut_trackball : public glut_simple_interactor, public trackball
  {
  public:

	void update()
	{
		radius = width < height ? width : height;
		radius /= 2.f;
		offset = vec3f(width/2.f, height/2.f, 0);
		rotate(x-dx, y-dy, x, y);
		glutPostRedisplay();
	}

	void apply_transform()
	{
		glTranslatef(centroid[0], centroid[1], centroid[2]);
		glh_rotate(r);
		glTranslatef(-centroid[0], -centroid[1], -centroid[2]);
	}

	void apply_inverse_transform()
	{
		glTranslatef(centroid[0], centroid[1], centroid[2]);
		glh_rotate(r.inverse());
		glTranslatef(-centroid[0], -centroid[1], -centroid[2]);
	}

	matrix4f get_transform()
	{
		return trackball::get_transform();
	}

	matrix4f get_inverse_transform()
	{
		return trackball::get_inverse_transform();
	}

  }; 

  inline void glut_exit_on_escape(unsigned char k, int x = 0, int y = 0)
  { if(k==27) exit(0); }

  struct glut_simple_mouse_interactor : public glut_interactor
  {

  public:
	glut_simple_mouse_interactor(int num_buttons_to_use=3)
	{
	  configure_buttons(num_buttons_to_use);
	  camera_mode = false;
	}

	void enable()
	{
		trackball.enable();
		translator.enable();
	}

	void disable()
	{
		trackball.disable();
		translator.disable();
	}

	void set_camera_mode(bool cam)
	{
		camera_mode = cam;
		if(camera_mode)
		{
			trackball.invert_increment = true;
			translator.invert_increment = true;
			translator.parent_rotation = & trackball.r;
		}
		else
		{
			trackball.invert_increment = false;
			translator.invert_increment = false;
			if(translator.parent_rotation == &trackball.r) translator.parent_rotation = 0;
		}
	}
	void configure_buttons(int num_buttons_to_use = 3)
	{
	  switch(num_buttons_to_use)
	  {
	  case 1:

		trackball.num_modes = 1;
		trackball.cond[0].activate_on = GLUT_LEFT_BUTTON;
		trackball.cond[0].modifiers = 0;
		trackball.cond[0].use_modifiers = true;

		translator.num_modes = 2;
		translator.cond[0].activate_on = GLUT_LEFT_BUTTON;
		translator.cond[0].modifiers = GLUT_ACTIVE_SHIFT;
		translator.cond[0].use_modifiers = true;
		translator.cond[1].activate_on = GLUT_LEFT_BUTTON;
		translator.cond[1].modifiers = GLUT_ACTIVE_CTRL;
		translator.cond[1].use_modifiers = true;
		break;

	  case 2:

		trackball.num_modes = 1;
		trackball.cond[0].activate_on = GLUT_LEFT_BUTTON;
		trackball.cond[0].modifiers = 0;
		trackball.cond[0].use_modifiers = true;

		translator.num_modes = 2;
		translator.cond[0].activate_on = GLUT_MIDDLE_BUTTON;
		translator.cond[0].modifiers = 0;
		translator.cond[0].use_modifiers = true;
		translator.cond[1].activate_on = GLUT_LEFT_BUTTON;
		translator.cond[1].modifiers = GLUT_ACTIVE_CTRL;
		translator.cond[1].use_modifiers = true;

		break;

	  case 3:
	  default:

		trackball.num_modes = 1;
		trackball.cond[0].activate_on = GLUT_LEFT_BUTTON;
		trackball.cond[0].modifiers = 0;
		trackball.cond[0].use_modifiers = true;

		translator.num_modes = 2;
		translator.cond[0].activate_on = GLUT_MIDDLE_BUTTON;
		translator.cond[0].modifiers = 0;
		translator.cond[0].use_modifiers = true;
		translator.cond[1].activate_on = GLUT_RIGHT_BUTTON;
		translator.cond[1].modifiers = 0;
		translator.cond[1].use_modifiers = true;

		break;
	  }
	}

	virtual void motion(int x, int y)
	{ 
		trackball.motion(x,y);
		translator.motion(x,y);
	}

	virtual void mouse(int button, int state, int x, int y)
	{
	  trackball.mouse(button, state, x, y);
	  translator.mouse(button, state, x, y);
	}

	virtual void reshape(int x, int y)
	{
		trackball.reshape(x,y);
		translator.reshape(x,y);
	}

	void apply_transform()
	{
	  translator.apply_transform();
	  trackball.apply_transform();
	}

	void apply_inverse_transform()
	{
	  trackball.apply_inverse_transform();
	  translator.apply_inverse_transform();
	}

	matrix4f get_transform()
	{
	  return ( translator.get_transform()       *
			   trackball.get_transform() );
	}

	matrix4f get_inverse_transform()
	{
	  return ( trackball.get_inverse_transform() *
			   translator.get_inverse_transform()       );
	}

	void set_parent_rotation(rotationf *rp)
	{
		trackball.parent_rotation = rp;
		translator.parent_rotation = rp;
	}

	bool camera_mode;
	glut_trackball trackball;
	glut_translator  translator;
  };

}

#endif