GLState.cc 53.4 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Jan Möbius's avatar
Jan Möbius committed
4
5
6
7
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
Jan Möbius's avatar
Jan Möbius committed
8
9
 *                                                                           *
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
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
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. 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.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may 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 COPYRIGHT HOLDER *
 * 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.              *
Jan Möbius's avatar
Jan Möbius committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                       *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/


//=============================================================================
//
//  CLASS GLState - IMPLEMENTATION
//
//=============================================================================

//== INCLUDES =================================================================

59
#include <ACG/GL/acg_glew.hh>
Jan Möbius's avatar
Jan Möbius committed
60
#include "GLState.hh"
61
#include <cstring>
Jan Möbius's avatar
Jan Möbius committed
62
63
64
65
66
67
68
69
70
71


//== NAMESPACES ===============================================================


namespace ACG {


//== IMPLEMENTATION ==========================================================

72
//#define GLSTATE_AVOID_REDUNDANT_GLCALLS
Jan Möbius's avatar
Jan Möbius committed
73

Matthias Möller's avatar
Matthias Möller committed
74
75
76
77
78
79
80
const Vec4f    GLState::default_clear_color(0.f, 0.f, 0.f, 1.f);
const Vec4f    GLState::default_base_color(0.f, 0.f, 0.f, 1.f);
const Vec4f    GLState::default_ambient_color(0.2f, 0.2f, 0.2f, 1.f);
const Vec4f    GLState::default_diffuse_color(0.5f, 0.53f, 0.6f, 1.f);
const Vec4f    GLState::default_specular_color(0.75f, 0.8f, 0.85f, 1.f);
const Vec4f    GLState::default_overlay_color(0.f, 0.f, 0.f, 1.f);
const float    GLState::default_shininess(100.f);
Jan Möbius's avatar
Jan Möbius committed
81
82
83
84


//-----------------------------------------------------------------------------

85
bool GLState::depthFuncLock_ = false;
Christopher Tenter's avatar
Christopher Tenter committed
86
bool GLState::depthRangeLock_ = false;
87
bool GLState::blendFuncSeparateLock_[2] = { false };
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
bool GLState::blendEquationLock_ = false;
bool GLState::blendColorLock_ = false;
bool GLState::alphaFuncLock_ = false;
bool GLState::shadeModelLock_ = false;
bool GLState::cullFaceLock_ = false;
bool GLState::vertexPointerLock_ = false;
bool GLState::normalPointerLock_ = false;
bool GLState::texcoordPointerLock_ = false;
bool GLState::colorPointerLock_ = false;
bool GLState::drawBufferLock_ = false;
bool GLState::programLock_ = false;

std::deque <GLStateContext> GLState::stateStack_;
std::bitset<0xFFFF+1> GLState::glStateLock_;
int GLState::glBufferTargetLock_[4] = {0};
103
int GLState::glTextureStageLock_[16] = {0};
104
105
106
107
108
109
bool GLState::framebufferLock_[2] = {false};
int GLState::maxTextureCoords_ = 0;
int GLState::maxCombinedTextureImageUnits_ = 0;
int GLState::maxDrawBuffers_ = 0;

int GLState::num_texture_units_ = 0;
Jan Möbius's avatar
Jan Möbius committed
110

111
112
113
114
115
GLStateContext::GLStateContext() :
    activeTexture_(GL_TEXTURE0),
    drawBufferSingle_(GL_BACK),
    activeDrawBuffer_(0),
    program_(0)
116
117
118
119
120
{
  framebuffers_[0] = framebuffers_[1] = 0;
  memset(drawBufferState_, GL_BACK, sizeof(drawBufferState_));
}

121
122
123
GLState::GLState(bool _updateGL, bool _compatibilityProfile)
  : compatibilityProfile_(_compatibilityProfile),
    render_pass_(1),
Jan Möbius's avatar
Jan Möbius committed
124
125
126
    max_render_passes_(1),
    bb_min_(ACG::Vec3d(0.0,0.0,0.0)),
    bb_max_(ACG::Vec3d(0.0,0.0,0.0)),
Matthias Möller's avatar
Matthias Möller committed
127
128
129
130
131
132
    left_(-1),
    bottom_(1),
    width_(2),
    height_(2),
    glwidth_(2),
    glheight_(2),
133
134
    near_plane_(1.0),
    far_plane_(100.0),
Jan Möbius's avatar
Jan Möbius committed
135
136
137
138
139
    multisampling_(false),    
    allow_multisampling_(true),
    mipmapping_(true),
    updateGL_(_updateGL),
    blending_(false),
140
141
    msSinceLastRedraw_ (1),
    colorPicking_(true)
Jan Möbius's avatar
Jan Möbius committed
142
{
143

Jan Möbius's avatar
Jan Möbius committed
144
  if ( stateStack_.empty() )
145
146
  {
    stateStack_.push_back(GLStateContext());
Jan Möbius's avatar
Jan Möbius committed
147

148
    memset(glBufferTargetLock_, 0, sizeof(glBufferTargetLock_));
Jan Möbius's avatar
Jan Möbius committed
149

150
    framebufferLock_[0] = framebufferLock_[1] = false;
Jan Möbius's avatar
Jan Möbius committed
151

152
153
    glStateLock_.reset();
  }
154

Jan Möbius's avatar
Jan Möbius committed
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
  initialize();
}

//-----------------------------------------------------------------------------


void GLState::initialize()
{
  // clear matrix stacks
  while (!stack_projection_.empty())
    stack_projection_.pop();
  while (!stack_modelview_.empty())
    stack_modelview_.pop();
  while (!stack_inverse_projection_.empty())
    stack_inverse_projection_.pop();
  while (!stack_inverse_modelview_.empty())
    stack_inverse_modelview_.pop();


  // load identity matrix
  reset_projection();
  reset_modelview();


  // colors
  set_clear_color(default_clear_color);
  set_base_color(default_base_color);
  set_ambient_color(default_ambient_color);
  set_diffuse_color(default_diffuse_color);
  set_specular_color(default_specular_color);
185
  set_overlay_color(default_overlay_color);
Jan Möbius's avatar
Jan Möbius committed
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
  set_shininess(default_shininess);


  // thickness
  set_point_size(1.0f);
  set_line_width(1.0f);

  // multisampling
  set_multisampling(true);

  // Get max number of texture units
  GLint value;
  glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, &value);
  
  num_texture_units_ = value;
  
  // lighting
  set_twosided_lighting(true);
}

//-----------------------------------------------------------------------------

void GLState::setState ()
{
  makeCurrent();

212
  if (compatibilityProfile_ ) {
Jan Möbius's avatar
Jan Möbius committed
213

214
215
216
217
218
219
220
221
222
    // projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadMatrixd(projection_.get_raw_data());
    glMatrixMode(GL_MODELVIEW);

    // modelview matrix
    glLoadMatrixd(modelview_.get_raw_data());

  }
Jan Möbius's avatar
Jan Möbius committed
223
224
225
226

  // clear color
  glClearColor(clear_color_[0], clear_color_[1], clear_color_[2], clear_color_[3]);

227
228
229
  if (compatibilityProfile_ ) {
    // base color
    glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, base_color_.data());
Jan Möbius's avatar
Jan Möbius committed
230

231
232
    // ambient color
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT , ambient_color_.data());
Jan Möbius's avatar
Jan Möbius committed
233

234
235
    // diffuse color
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE , diffuse_color_.data());
Jan Möbius's avatar
Jan Möbius committed
236

237
238
    // specular color
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular_color_.data());
Jan Möbius's avatar
Jan Möbius committed
239

240
241
242
    // shininess
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess_);
  }
Jan Möbius's avatar
Jan Möbius committed
243
244
245
246
247
248
249

  // point size
  glPointSize(point_size_);

  // line width
  glLineWidth(line_width_);

250
251
252
253
254
255
256
  if ( compatibilityProfile_ ) {
    // two sided lighting
    if (twosided_lighting_  )
      glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE );
    else
      glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
  }
Jan Möbius's avatar
Jan Möbius committed
257
258
259
260
261
262
263
264
265
266

  // viewport
  glViewport(left_, bottom_, width_, height_);
}


//-----------------------------------------------------------------------------

void GLState::clearBuffers ()
{
267
268

  if ( compatibilityProfile_ ) {
Jan Möbius's avatar
Jan Möbius committed
269
    glPushAttrib (GL_ALL_ATTRIB_BITS);
270
  }
Jan Möbius's avatar
Jan Möbius committed
271

272
273
274
  GLState::disable(GL_DEPTH_TEST);
  GLState::disable(GL_DITHER);
  glShadeModel( GL_FLAT );
Jan Möbius's avatar
Jan Möbius committed
275

276
277
  if ( compatibilityProfile_ ) {
    GLState::disable(GL_LIGHTING);
Jan Möbius's avatar
Jan Möbius committed
278
279
280
281
282
283
284
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity ();

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity ();
285
286
287
288
289
290
291
292
  }


  // GetoriginalScissor settings
  GLboolean scissor =  glIsEnabled(GL_SCISSOR_TEST);

  GLint origBox[4];
  glGetIntegerv(GL_SCISSOR_BOX,&origBox[0]);
Jan Möbius's avatar
Jan Möbius committed
293

294
295
296
  //Enable scissor
  if (!scissor)
    GLState::enable(GL_SCISSOR_TEST);
Jan Möbius's avatar
Jan Möbius committed
297

298
299
300
301
302
303
304
305
306
307
308
309
310
  // Restrict to our current viewport
  glScissor(  left_,bottom_,width_,height_ );

  // Clear restricted region
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

  // Reset to originalsettings
  glScissor(  origBox[0], origBox[1], origBox[2], origBox[3] );

  if (!scissor)
    GLState::disable(GL_SCISSOR_TEST);

  if ( compatibilityProfile_ ) {
Jan Möbius's avatar
Jan Möbius committed
311
312
313
314
315
    glPopMatrix ();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPopAttrib ();
316
317
318
319
320
321
322
323
324
325
326
  }
}

//-----------------------------------------------------------------------------

void GLState::setCompatibilityProfile( bool _compatibility ) {
  compatibilityProfile_ = _compatibility;
}

bool GLState::compatibilityProfile() const  {
  return compatibilityProfile_;
Jan Möbius's avatar
Jan Möbius committed
327
328
329
330
331
332
333
334
335
}

//-----------------------------------------------------------------------------

void GLState::reset_projection()
{
  projection_.identity();
  inverse_projection_.identity();

336
  if (updateGL_ && compatibilityProfile_ )
Jan Möbius's avatar
Jan Möbius committed
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
  {
    makeCurrent();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
  }

  update_matrices();
}


//-----------------------------------------------------------------------------


void GLState::set_projection(const GLMatrixd& _m, const GLMatrixd& _inv_m)
{
  projection_ = _m;
  inverse_projection_ = _inv_m;

356
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
  {
    makeCurrent();
    glMatrixMode(GL_PROJECTION);
    glLoadMatrixd(projection_.get_raw_data());
    glMatrixMode(GL_MODELVIEW);
  }

  update_matrices();
}


//-----------------------------------------------------------------------------


void GLState::reset_modelview()
{
  modelview_.identity();
  inverse_modelview_.identity();

376
  if (updateGL_ && compatibilityProfile_ )
Jan Möbius's avatar
Jan Möbius committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
  {
    makeCurrent();
    glLoadIdentity();
  }

  update_matrices();
}


//-----------------------------------------------------------------------------


void GLState::set_modelview(const GLMatrixd& _m, const GLMatrixd& _inv_m)
{
  modelview_ = _m;
  inverse_modelview_ = _inv_m;

394
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
  {
    makeCurrent();
    glLoadMatrixd(modelview_.get_raw_data());
  }

  update_matrices();
}


//-----------------------------------------------------------------------------


void GLState::ortho( double _left, double _right,
		     double _bottom, double _top,
		     double _n, double _f )
{
  near_plane_ = _n;
  far_plane_  = _f;

  projection_.ortho(_left, _right, _bottom, _top, _n, _f);
  inverse_projection_.inverse_ortho(_left,_right,_bottom,_top,_n,_f);

417
  if (updateGL_ && compatibilityProfile_ )
Jan Möbius's avatar
Jan Möbius committed
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
  {
    makeCurrent();
    glMatrixMode(GL_PROJECTION);
    glOrtho(_left, _right, _bottom, _top, _n, _f);
    glMatrixMode(GL_MODELVIEW);
  }

  update_matrices();
}


//-----------------------------------------------------------------------------


void GLState::frustum( double _left, double _right,
		       double _bottom, double _top,
		       double _n, double _f )
{
  near_plane_ = _n;
  far_plane_  = _f;

  projection_.frustum(_left, _right, _bottom, _top, _n, _f);
  inverse_projection_.inverse_frustum(_left,_right,_bottom,_top,_n,_f);

442
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
  {
    makeCurrent();
    glMatrixMode(GL_PROJECTION);
    glFrustum(_left, _right, _bottom, _top, _n, _f);
    glMatrixMode(GL_MODELVIEW);
  }

  update_matrices();
}


//-----------------------------------------------------------------------------


void GLState::perspective( double _fovY, double _aspect,
			   double _n, double _f )
{
  near_plane_ = _n;
  far_plane_  = _f;

  projection_.perspective(_fovY, _aspect, _n, _f);
  inverse_projection_.inverse_perspective(_fovY, _aspect, _n, _f);

466
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
467
468
469
  {
    makeCurrent();
    glMatrixMode(GL_PROJECTION);
470
    glLoadMatrixd(projection_.data());
Jan Möbius's avatar
Jan Möbius committed
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
    glMatrixMode(GL_MODELVIEW);
  }

  update_matrices();
}


//-----------------------------------------------------------------------------


void GLState::viewport( int _left, int _bottom,
			int _width, int _height,
			int _glwidth, int _glheight)
{
  left_   = _left;
  bottom_ = _bottom;
  width_  = _width;
  height_ = _height;

  if (_glwidth < _width || _glheight < _height)
  {
    glwidth_ = _width;
    glheight_ = _height;
  } else {
    glwidth_ = _glwidth;
    glheight_ = _glheight;
  }

  window2viewport_.identity();
  window2viewport_(0,0) = 0.5f * width_;
  window2viewport_(0,3) = 0.5f * width_ + left_;
  window2viewport_(1,1) = 0.5f * height_;
  window2viewport_(1,3) = 0.5f * height_ + bottom_;
  window2viewport_(2,2) = 0.5f;
  window2viewport_(2,3) = 0.5f;

  inverse_window2viewport_.identity();
  inverse_window2viewport_(0,0) =  2.0f / width_;
  inverse_window2viewport_(0,3) = -(2.0*left_ + width_) / width_;
  inverse_window2viewport_(1,1) =  2.0f / height_;
  inverse_window2viewport_(1,3) = -(2.0*bottom_ + height_) / height_;
  inverse_window2viewport_(2,2) =  2.0f;
  inverse_window2viewport_(2,3) = -1.0f;

  if (updateGL_)
  {
    makeCurrent();
    glViewport(_left, _bottom, _width, _height);
  }

  update_matrices();
}


//-----------------------------------------------------------------------------


void GLState::lookAt( const Vec3d& _eye,
		      const Vec3d& _center,
		      const Vec3d& _up )
{
  modelview_.lookAt(_eye, _center, _up);
  inverse_modelview_.inverse_lookAt(_eye, _center, _up);

535
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
536
537
  {
    makeCurrent();
538
    glLoadMatrixd(modelview_.data());
Jan Möbius's avatar
Jan Möbius committed
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
  }

  update_matrices();
}


//-----------------------------------------------------------------------------


void GLState::translate( double _x, double _y, double _z,
			 MultiplyFrom _mult_from )
{
  if (_mult_from == MULT_FROM_RIGHT)
  {
    modelview_.translate(_x, _y, _z);
    inverse_modelview_.translate(-_x, -_y, -_z, MULT_FROM_LEFT);
  }
  else
  {
    modelview_.translate(_x, _y, _z, MULT_FROM_LEFT);
    inverse_modelview_.translate(-_x, -_y, -_z);
  }

562
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
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
  {
    makeCurrent();
    glLoadMatrixd(modelview_.get_raw_data());
  }

  update_matrices();
}

//-----------------------------------------------------------------------------

void GLState::translate( Vec3d _vector,
                MultiplyFrom _mult_from ) {
  translate( _vector[0] , _vector[1] , _vector[2] ,_mult_from);
}

//-----------------------------------------------------------------------------


void GLState::rotate( double _angle, double _x, double _y, double _z,
		      MultiplyFrom _mult_from )
{
  if (_mult_from == MULT_FROM_RIGHT)
  {
    modelview_.rotate(_angle, _x, _y, _z);
    inverse_modelview_.rotate(-_angle, _x, _y, _z, MULT_FROM_LEFT);
  }
  else
  {
    modelview_.rotate(_angle, _x, _y, _z, MULT_FROM_LEFT);
    inverse_modelview_.rotate(-_angle, _x, _y, _z);
  }

595
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
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
  {
    makeCurrent();
    glLoadMatrixd(modelview_.get_raw_data());
  }

  update_matrices();
}


//-----------------------------------------------------------------------------


void GLState::scale( double _sx, double _sy, double _sz,
		     MultiplyFrom _mult_from )
{
  if (_mult_from == MULT_FROM_RIGHT)
  {
    modelview_.scale(_sx, _sy, _sz, MULT_FROM_RIGHT);
    inverse_modelview_.scale(1.0f/_sx, 1.0f/_sy, 1.0f/_sz, MULT_FROM_LEFT);
  }
  else
  {
    modelview_.scale(_sx, _sy, _sz, MULT_FROM_LEFT);
    inverse_modelview_.scale(1.0f/_sx, 1.0f/_sy, 1.0f/_sz, MULT_FROM_RIGHT);
  }

622
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
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
  {
    makeCurrent();
    glLoadMatrixd(modelview_.get_raw_data());
  }

  update_matrices();
}


//-----------------------------------------------------------------------------


void GLState::mult_matrix( const GLMatrixd& _m, const GLMatrixd& _inv_m,
			   MultiplyFrom _mult_from )
{
  if (_mult_from == MULT_FROM_RIGHT)
  {
    modelview_ *= _m;
    inverse_modelview_.leftMult(_inv_m);
  }
  else
  {
    modelview_.leftMult(_m);
    inverse_modelview_ *= _inv_m;
  }

649
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
650
651
652
653
654
655
656
657
658
659
660
661
  {
    makeCurrent();
    glLoadMatrixd(modelview_.get_raw_data());
  }

  update_matrices();
}


//-----------------------------------------------------------------------------


662
663
664
void GLState::update_matrices(bool _changedModelView,
  bool _changedProjection,
  bool _changedViewport)
Jan Möbius's avatar
Jan Möbius committed
665
{
666
667
668
  forward_projection_   = window2viewport_;
  forward_projection_  *= projection_;
  forward_projection_  *= modelview_;
Jan Möbius's avatar
Jan Möbius committed
669

670
  backward_projection_  = inverse_modelview_;
Jan Möbius's avatar
Jan Möbius committed
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
  backward_projection_ *= inverse_projection_;
  backward_projection_ *= inverse_window2viewport_;
}


//-----------------------------------------------------------------------------


Vec3d GLState::project(const Vec3d& _point) const
{
  return forward_projection_.transform_point(_point);
}


//-----------------------------------------------------------------------------


Vec3d GLState::unproject(const Vec3d& _winPoint) const
{
  return backward_projection_.transform_point(_winPoint);
}


//-----------------------------------------------------------------------------


void GLState::set_clear_color(const Vec4f& _col)
{
  clear_color_ = _col;

  if (updateGL_)
  {
    makeCurrent();
    glClearColor(_col[0], _col[1], _col[2], _col[3]);
  }
}


//-----------------------------------------------------------------------------


void GLState::set_base_color(const Vec4f& _col)
{
  base_color_ = _col;

716
  if (updateGL_ && compatibilityProfile_ )
Jan Möbius's avatar
Jan Möbius committed
717
718
  {
    makeCurrent();
719
    glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, _col.data());
Jan Möbius's avatar
Jan Möbius committed
720
721
722
  }
}

723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
//-----------------------------------------------------------------------------


void GLState::set_color(const Vec4f& _col)
{
  color_ = _col;

  if (updateGL_)
  {
    makeCurrent();
    glColor(color_);
  }
}


Jan Möbius's avatar
Jan Möbius committed
738
739
740
741
742
743
744
745

//-----------------------------------------------------------------------------


void GLState::set_ambient_color(const Vec4f& _col)
{
  ambient_color_ = _col;

746
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
747
748
749
750
751
752
753
754
755
756
757
758
759
760
  {
    makeCurrent();
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, _col.data());
  }
}


//-----------------------------------------------------------------------------


void GLState::set_diffuse_color(const Vec4f& _col)
{
  diffuse_color_ = _col;

761
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
762
763
764
765
766
767
768
769
770
771
772
773
774
775
  {
    makeCurrent();
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, _col.data());
  }
}


//-----------------------------------------------------------------------------


void GLState::set_specular_color(const Vec4f& _col)
{
  specular_color_ = _col;

776
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
777
778
779
780
  {
    makeCurrent();
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, _col.data());
  }
781
782
783
784
785
786
787
788
789

}

//-----------------------------------------------------------------------------


void GLState::set_overlay_color(const Vec4f& _col)
{
  overlay_color_ = _col;
Jan Möbius's avatar
Jan Möbius committed
790
791
792
793
794
795
796
797
798
799
}


//-----------------------------------------------------------------------------


void GLState::set_shininess(float _shininess)
{
  shininess_ = _shininess;

800
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
  {
    makeCurrent();
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, _shininess);
  }
}


//-----------------------------------------------------------------------------


void GLState::set_point_size(float _f)
{
  point_size_ = _f;

  if (updateGL_)
  {
    makeCurrent();
    glPointSize(point_size_);
  }
}


//-----------------------------------------------------------------------------


void GLState::set_line_width(float _f)
{
  line_width_ = _f;

  if (updateGL_)
  {
    makeCurrent();
    glLineWidth(line_width_);
  }
}

//-----------------------------------------------------------------------------

void GLState::set_bounding_box(ACG::Vec3d _min, ACG::Vec3d _max ) {
  bb_min_ = _min;
  bb_max_ = _max;
}

//-----------------------------------------------------------------------------

void GLState::get_bounding_box(ACG::Vec3d& _min, ACG::Vec3d& _max ) {
  _min = bb_min_;
  _max = bb_max_;
}


//-----------------------------------------------------------------------------


void GLState::set_twosided_lighting(bool _b)
{
  twosided_lighting_ = _b;

859
  if (updateGL_ && compatibilityProfile_ )
Jan Möbius's avatar
Jan Möbius committed
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
  {
    makeCurrent();
    if (twosided_lighting_)
      glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE );
    else
      glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
  }
}


//-----------------------------------------------------------------------------

void GLState::set_multisampling(bool _b)
{

  multisampling_ = _b;

  if (updateGL_)
  {
    makeCurrent();
    if ( allow_multisampling_ ) {

      if ( _b )
883
        GLState::enable( GL_MULTISAMPLE );
Jan Möbius's avatar
Jan Möbius committed
884
      else
885
        GLState::disable( GL_MULTISAMPLE );
Jan Möbius's avatar
Jan Möbius committed
886
887
888
889
890
891

    } else {

      multisampling_ = false;

      if ( glIsEnabled( GL_MULTISAMPLE ) )
892
        GLState::disable( GL_MULTISAMPLE );
Jan Möbius's avatar
Jan Möbius committed
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975

    }
  }

}

//-----------------------------------------------------------------------------

double GLState::fovy() const
{
    assert(projection_(1,1) != 0.0);

    return atan(1.0/projection_(1,1))*2.0;
}

//-----------------------------------------------------------------------------

double GLState::aspect() const
{
    assert(projection_(0,0) != 0.0);

    return projection_(1,1) / projection_(0,0);
}

//-----------------------------------------------------------------------------

Vec3d GLState::eye() const
{
  return inverse_modelview_.transform_point(Vec3d(0.0, 0.0, 0.0));
}

//-----------------------------------------------------------------------------


Vec3d GLState::viewing_direction(int _x, int _y) const
{
  Vec3d dir = ( unproject(Vec3d(_x, _y, 1.0)) -
		unproject(Vec3d(_x, _y, 0.0)) );
  dir.normalize();
  return dir;
}


//-----------------------------------------------------------------------------


Vec3d GLState::up() const
{
  Vec3d dir( unproject(Vec3d(0.5*width_, height_-1,   0.0)) -
	     unproject(Vec3d(0.5*width_, 0.5*height_, 0.0)) );
  dir.normalize();
  return dir;
}


//-----------------------------------------------------------------------------


Vec3d GLState::right() const
{
  Vec3d dir( unproject(Vec3d(width_-1,   0.5*height_, 0.0)) -
	     unproject(Vec3d(0.5*width_, 0.5*height_, 0.0)) );
  dir.normalize();
  return dir;
}


//-----------------------------------------------------------------------------


void GLState::viewing_ray( int _x, int _y,
			   Vec3d& _origin, Vec3d& _direction) const
{
  _origin = unproject(Vec3d(_x, _y, 0.0));
  _direction = unproject(Vec3d(_x, _y, 1.0)) - _origin;
  _direction.normalize();
}


//-----------------------------------------------------------------------------

const GLenum& GLState::depthFunc() const
{
976
  return stateStack_.back().depthFunc_;
Jan Möbius's avatar
Jan Möbius committed
977
978
979
980
981
982
}

//-----------------------------------------------------------------------------

void GLState:: set_depthFunc(const GLenum& _depth_func)
{
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
  depthFunc(_depth_func);
}

//-----------------------------------------------------------------------------

void GLState::depthFunc(GLenum _depthFunc)
{
  if (!depthFuncLock_)
  {
#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
    if (stateStack_.back().depthFunc_ != _depthFunc)
#endif
    {
      glDepthFunc(_depthFunc);
      stateStack_.back().depthFunc_ = _depthFunc;
    }
  }
Jan Möbius's avatar
Jan Möbius committed
1000
}