GLState.cc 54 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 61
#include "GLState.hh"

62
#include <OpenMesh/Core/Utils/vector_cast.hh>
Jan Möbius's avatar
Jan Möbius committed
63 64
#include <cstring>

Jan Möbius's avatar
Jan Möbius committed
65 66 67 68 69 70 71 72 73

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


namespace ACG {


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

74
//#define GLSTATE_AVOID_REDUNDANT_GLCALLS
Jan Möbius's avatar
Jan Möbius committed
75

Matthias Möller's avatar
Matthias Möller committed
76 77 78 79 80 81 82
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
83 84 85 86


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

87
bool GLState::depthFuncLock_ = false;
Christopher Tenter's avatar
Christopher Tenter committed
88
bool GLState::depthRangeLock_ = false;
89
bool GLState::blendFuncSeparateLock_[] = { false };
90 91 92 93 94 95 96 97 98 99 100 101 102 103
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_;
104 105 106
int GLState::glBufferTargetLock_[] = {0};
int GLState::glTextureStageLock_[] = {0};
bool GLState::framebufferLock_[] = {false};
107 108 109 110 111
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
112

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

123 124 125
GLState::GLState(bool _updateGL, bool _compatibilityProfile)
  : compatibilityProfile_(_compatibilityProfile),
    render_pass_(1),
Jan Möbius's avatar
Jan Möbius committed
126 127 128
    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
129 130 131 132 133 134
    left_(-1),
    bottom_(1),
    width_(2),
    height_(2),
    glwidth_(2),
    glheight_(2),
135 136
    near_plane_(1.0),
    far_plane_(100.0),
Jan Möbius's avatar
Jan Möbius committed
137 138 139 140 141
    multisampling_(false),    
    allow_multisampling_(true),
    mipmapping_(true),
    updateGL_(_updateGL),
    blending_(false),
142 143
    msSinceLastRedraw_ (1),
    colorPicking_(true)
Jan Möbius's avatar
Jan Möbius committed
144
{
145

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

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

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

154 155
    glStateLock_.reset();
  }
156

Jan Möbius's avatar
Jan Möbius committed
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
  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);
187
  set_overlay_color(default_overlay_color);
Jan Möbius's avatar
Jan Möbius committed
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
  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();

214
  if (compatibilityProfile_ ) {
Jan Möbius's avatar
Jan Möbius committed
215

216 217 218 219 220 221 222 223 224
    // 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
225 226 227 228

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

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

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

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

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

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

  // point size
  glPointSize(point_size_);

  // line width
  glLineWidth(line_width_);

252 253 254 255 256 257 258
  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
259 260 261 262 263 264 265 266 267 268

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


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

void GLState::clearBuffers ()
{
269 270

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

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

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

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity ();
287 288 289 290 291 292 293 294
  }


  // 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
295

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

300 301 302 303 304 305 306 307 308 309 310 311 312
  // 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
313 314 315 316 317
    glPopMatrix ();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPopAttrib ();
318 319 320 321 322 323 324 325 326 327 328
  }
}

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

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

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

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

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

338
  if (updateGL_ && compatibilityProfile_ )
Jan Möbius's avatar
Jan Möbius committed
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);
  }
}


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


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
  {
    makeCurrent();
    glMatrixMode(GL_PROJECTION);
    glLoadMatrixd(projection_.get_raw_data());
    glMatrixMode(GL_MODELVIEW);
  }
}


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


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

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


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


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

390
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
  {
    makeCurrent();
    glLoadMatrixd(modelview_.get_raw_data());
  }
}


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


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);

411
  if (updateGL_ && compatibilityProfile_ )
Jan Möbius's avatar
Jan Möbius committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
  {
    makeCurrent();
    glMatrixMode(GL_PROJECTION);
    glOrtho(_left, _right, _bottom, _top, _n, _f);
    glMatrixMode(GL_MODELVIEW);
  }
}


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


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);

434
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
  {
    makeCurrent();
    glMatrixMode(GL_PROJECTION);
    glFrustum(_left, _right, _bottom, _top, _n, _f);
    glMatrixMode(GL_MODELVIEW);
  }
}


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


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);

456
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
457 458 459
  {
    makeCurrent();
    glMatrixMode(GL_PROJECTION);
460
    glLoadMatrixd(projection_.data());
Jan Möbius's avatar
Jan Möbius committed
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
    glMatrixMode(GL_MODELVIEW);
  }
}


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


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);
  }
}


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


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

521
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
522 523
  {
    makeCurrent();
524
    glLoadMatrixd(modelview_.data());
Jan Möbius's avatar
Jan Möbius committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
  }
}


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


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);
  }

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

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

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);
  }

577
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
  {
    makeCurrent();
    glLoadMatrixd(modelview_.get_raw_data());
  }
}


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


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);
  }

602
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
  {
    makeCurrent();
    glLoadMatrixd(modelview_.get_raw_data());
  }
}


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


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;
  }

627
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
628 629 630 631 632 633 634 635 636 637 638 639 640
  {
    makeCurrent();
    glLoadMatrixd(modelview_.get_raw_data());
  }
}



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


Vec3d GLState::project(const Vec3d& _point) const
{
641 642 643
  Vec3d t = modelview_.transform_point(_point);
  t = projection_.transform_point(t);
  return window2viewport_.transform_point(t);
Jan Möbius's avatar
Jan Möbius committed
644 645 646 647 648 649 650 651
}


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


Vec3d GLState::unproject(const Vec3d& _winPoint) const
{
652 653 654
  Vec3d t = inverse_window2viewport_.transform_point(_winPoint);
  t = inverse_projection_.transform_point(t);
  return inverse_modelview_.transform_point(t);
Jan Möbius's avatar
Jan Möbius committed
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
}


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


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;

680
  if (updateGL_ && compatibilityProfile_ )
Jan Möbius's avatar
Jan Möbius committed
681 682
  {
    makeCurrent();
683
    glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, _col.data());
Jan Möbius's avatar
Jan Möbius committed
684 685 686
  }
}

687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
//-----------------------------------------------------------------------------


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

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


Jan Möbius's avatar
Jan Möbius committed
702 703 704 705 706 707 708 709

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


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

710
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
711 712 713 714 715 716 717 718 719 720 721 722 723 724
  {
    makeCurrent();
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, _col.data());
  }
}


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


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

725
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739
  {
    makeCurrent();
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, _col.data());
  }
}


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


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

740
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
741 742 743 744
  {
    makeCurrent();
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, _col.data());
  }
745 746 747 748 749 750 751 752 753

}

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


void GLState::set_overlay_color(const Vec4f& _col)
{
  overlay_color_ = _col;
Jan Möbius's avatar
Jan Möbius committed
754 755 756 757 758 759 760 761 762 763
}


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


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

764
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
  {
    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;

823
  if (updateGL_ && compatibilityProfile_ )
Jan Möbius's avatar
Jan Möbius committed
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
  {
    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 )
847
        GLState::enable( GL_MULTISAMPLE );
Jan Möbius's avatar
Jan Möbius committed
848
      else
849
        GLState::disable( GL_MULTISAMPLE );
Jan Möbius's avatar
Jan Möbius committed
850 851 852 853 854 855

    } else {

      multisampling_ = false;

      if ( glIsEnabled( GL_MULTISAMPLE ) )
856
        GLState::disable( GL_MULTISAMPLE );
Jan Möbius's avatar
Jan Möbius committed
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 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

    }
  }

}

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

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
{
940
  return stateStack_.back().depthFunc_;
Jan Möbius's avatar
Jan Möbius committed
941 942 943 944 945 946
}

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

void GLState:: set_depthFunc(const GLenum& _depth_func)
{
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
  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
964 965 966 967 968 969 970 971 972
}

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

void GLState::push_projection_matrix()
{
  stack_projection_.push(projection_);
  stack_inverse_projection_.push(inverse_projection_);

973
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
  {
    makeCurrent();
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glMatrixMode(GL_MODELVIEW);
  }
}


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


void GLState::pop_projection_matrix()
{
  projection_ = stack_projection_.top();
  inverse_projection_ = stack_inverse_projection_.top();

  stack_projection_.pop();
  stack_inverse_projection_.pop();

994
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
  {
    makeCurrent();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
  }
}


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


void GLState::push_modelview_matrix()
{
  stack_modelview_.push(modelview_);
  stack_inverse_modelview_.push(inverse_modelview_);

1012
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
  {
    makeCurrent();
    glPushMatrix();
  }
}


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


void GLState::pop_modelview_matrix()
{
  modelview_ = stack_modelview_.top();
  inverse_modelview_ = stack_inverse_modelview_.top();

  stack_modelview_.pop();
  stack_inverse_modelview_.pop();

1031
  if (updateGL_ && compatibilityProfile_)
Jan Möbius's avatar
Jan Möbius committed
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
  {
    makeCurrent();
    glPopMatrix();
  }
}

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

void GLState::pick_init (bool _color)
{
  colorPicking_ = _color;
  colorStack_.initialize ();
}

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

1048
bool GLState::pick_set_maximum (size_t _idx)
Jan Möbius's avatar
Jan Möbius committed
1049 1050 1051 1052 1053 1054 1055 1056 1057
{
  bool rv = colorStack_.setMaximumIndex (_idx);
  if (colorPicking_)
    return rv;
  return true;
}

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

1058
void GLState::pick_set_name (size_t _idx)
Jan Möbius's avatar
Jan Möbius committed
1059 1060 1061 1062 1063 1064
{
  colorStack_.setIndex (_idx);
}

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

1065
Vec4uc GLState::pick_get_name_color (size_t _idx)
Jan Möbius's avatar
Jan Möbius committed
1066 1067 1068 1069 1070 1071 1072 1073
{
  if (colorPicking_)
    return colorStack_.getIndexColor (_idx);
  return Vec4uc (0, 0, 0, 0);
}

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

1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
Vec4f GLState::pick_get_name_color_norm (unsigned int _idx)
{
  Vec4f rv(0.0f, 0.0f, 0.0f, 0.0f);
  if (colorPicking_)
  {
    Vec4uc color_abs = colorStack_.getIndexColor(_idx);
    rv = OpenMesh::vector_cast<Vec4f, Vec4uc>(color_abs) / 255.0f;
  }
  return rv;
}

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

1087
void GLState::pick_push_name (size_t _idx)
Jan Möbius's avatar
Jan Möbius committed
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
{
  colorStack_.pushIndex (_idx);
}

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

void GLState::pick_pop_name ()
{
  colorStack_.popIndex ();
}

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

1101
std::vector<size_t> GLState::pick_color_to_stack (Vec4uc _rgba) const
Jan Möbius's avatar
Jan Möbius committed
1102 1103 1104
{
  if (colorPicking_ && colorStack_.initialized ())
    return colorStack_.colorToStack (_rgba);
1105
  return std::vector<size_t> ();
Jan Möbius's avatar
Jan Möbius committed
1106 1107 1108 1109
}

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

1110
size_t GLState::pick_free_indicies () const
Jan Möbius's avatar
Jan Möbius committed
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
{
  if (colorPicking_ && colorStack_.initialized ())
    return colorStack_.freeIndicies ();
  return -1;
}

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

bool GLState::pick_error () const
{
  if (colorPicking_)
    return colorStack_.error ();
  return false;
}

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

1128
size_t GLState::pick_current_index () const
Jan Möbius's avatar
Jan Möbius committed
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
{
  if (colorPicking_)
    return colorStack_.currentIndex ();
  else
    return 0;
}

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

bool GLState::color_picking () const
{
  return colorPicking_;
}

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

1145
GLenum GLState::glStateCaps[] = {GL_ALPHA_TEST,
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
GL_AUTO_NORMAL,
GL_MAP2_VERTEX_3,
GL_MAP2_VERTEX_4,
GL_BLEND,
GL_CLIP_PLANE0,
GL_CLIP_PLANE1,
GL_CLIP_PLANE2,
GL_CLIP_PLANE3,
GL_CLIP_PLANE4,
GL_CLIP_PLANE5,
GL_COLOR_LOGIC_OP,
GL_COLOR_MATERIAL,
GL_COLOR_SUM,
GL_COLOR_TABLE,
GL_CONVOLUTION_1D,
GL_CONVOLUTION_2D,
GL_CULL_FACE,
GL_DEPTH_TEST,
GL_DITHER,
GL_FOG,
GL_HISTOGRAM,
GL_INDEX_LOGIC_OP,
GL_LIGHT0,
GL_LIGHT1,
GL_LIGHT2,
GL_LIGHT3,
GL_LIGHT4,
GL_LIGHT5,
GL_LIGHT6,
GL_LIGHT7,
GL_LIGHTING,
GL_LINE_SMOOTH,
GL_LINE_STIPPLE,
GL_MAP1_COLOR_4,
GL_MAP1_INDEX,
GL_MAP1_NORMAL,
GL_MAP1_TEXTURE_COORD_1,
GL_MAP1_TEXTURE_COORD_2,
GL_MAP1_TEXTURE_COORD_3,
GL_MAP1_TEXTURE_COORD_4,
GL_MAP1_VERTEX_3,
GL_MAP1_VERTEX_4,
GL_MAP2_COLOR_4,
GL_MAP2_INDEX,
GL_MAP2_NORMAL,
GL_MAP2_TEXTURE_COORD_1,
GL_MAP2_TEXTURE_COORD_2,
GL_MAP2_TEXTURE_COORD_3,
GL_MAP2_TEXTURE_COORD_4,
GL_MAP2_VERTEX_3,
GL_MAP2_VERTEX_4,
GL_MINMAX,
GL_MULTISAMPLE,
GL_NORMALIZE,
GL_RESCALE_NORMAL,
GL_POINT_SMOOTH,
GL_POINT_SPRITE,
GL_POLYGON_OFFSET_FILL,
GL_FILL,
GL_POLYGON_OFFSET_LINE,
GL_LINE,
GL_POLYGON_OFFSET_POINT,
GL_POINT,
GL_POLYGON_SMOOTH,
GL_POLYGON_STIPPLE,
GL_POST_COLOR_MATRIX_COLOR_TABLE,
GL_POST_CONVOLUTION_COLOR_TABLE,
GL_RESCALE_NORMAL,
GL_NORMALIZE,
GL_SAMPLE_ALPHA_TO_COVERAGE,
GL_SAMPLE_ALPHA_TO_ONE,
GL_SAMPLE_COVERAGE,
GL_SAMPLE_COVERAGE_INVERT,
GL_SEPARABLE_2D,
GL_SCISSOR_TEST,
GL_STENCIL_TEST,
GL_TEXTURE_1D,
GL_TEXTURE_2D,
GL_TEXTURE_3D,
GL_TEXTURE_CUBE_MAP,
GL_TEXTURE_GEN_Q,
GL_TEXTURE_GEN_R,
GL_TEXTURE_GEN_S,
GL_TEXTURE_GEN_T,
GL_VERTEX_PROGRAM_POINT_SIZE,
GL_VERTEX_PROGRAM_TWO_SIDE,
GL_COLOR_ARRAY,
GL_EDGE_FLAG_ARRAY,
GL_FOG_COORD_ARRAY,
GL_INDEX_ARRAY,
GL_NORMAL_ARRAY,
GL_SECONDARY_COLOR_ARRAY,
GL_TEXTURE_COORD_ARRAY,
GL_VERTEX_ARRAY};

Jan Möbius's avatar
Jan Möbius committed
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
void GLState::syncFromGL()
{
  // get enabled states
  GLenum caps[] = {GL_ALPHA_TEST,
    GL_AUTO_NORMAL,
    GL_MAP2_VERTEX_3,
    GL_MAP2_VERTEX_4,
    GL_BLEND,
    GL_CLIP_PLANE0,
    GL_CLIP_PLANE1,
    GL_CLIP_PLANE2,
    GL_CLIP_PLANE3,
    GL_CLIP_PLANE4,
    GL_CLIP_PLANE5,
    GL_COLOR_LOGIC_OP,
    GL_COLOR_MATERIAL,
    GL_COLOR_SUM,
    GL_COLOR_TABLE,
    GL_CONVOLUTION_1D,
    GL_CONVOLUTION_2D,
    GL_CULL_FACE,
    GL_DEPTH_TEST,
    GL_DITHER,
    GL_FOG,
    GL_HISTOGRAM,
    GL_INDEX_LOGIC_OP,
    GL_LIGHT0,
    GL_LIGHT1,
    GL_LIGHT2,
    GL_LIGHT3,
    GL_LIGHT4,
    GL_LIGHT5,
    GL_LIGHT6,
    GL_LIGHT7,
    GL_LIGHTING,
    GL_LINE_SMOOTH,
    GL_LINE_STIPPLE,
    GL_MAP1_COLOR_4,
    GL_MAP1_INDEX,
    GL_MAP1_NORMAL,
    GL_MAP1_TEXTURE_COORD_1,
    GL_MAP1_TEXTURE_COORD_2,
    GL_MAP1_TEXTURE_COORD_3,
    GL_MAP1_TEXTURE_COORD_4,
    GL_MAP1_VERTEX_3,
    GL_MAP1_VERTEX_4,
    GL_MAP2_COLOR_4,
    GL_MAP2_INDEX,
    GL_MAP2_NORMAL,
    GL_MAP2_TEXTURE_COORD_1,
    GL_MAP2_TEXTURE_COORD_2,
    GL_MAP2_TEXTURE_COORD_3,
    GL_MAP2_TEXTURE_COORD_4,
    GL_MAP2_VERTEX_3,
    GL_MAP2_VERTEX_4,
    GL_MINMAX,
    GL_MULTISAMPLE,
    GL_NORMALIZE,
    GL_RESCALE_NORMAL,
    GL_POINT_SMOOTH,
    GL_POINT_SPRITE,
    GL_POLYGON_OFFSET_FILL,
    GL_POLYGON_OFFSET_LINE,
    GL_POLYGON_OFFSET_POINT,
    GL_POLYGON_SMOOTH,
    GL_POLYGON_STIPPLE,
    GL_POST_COLOR_MATRIX_COLOR_TABLE,
    GL_POST_CONVOLUTION_COLOR_TABLE,
    GL_RESCALE_NORMAL,
    GL_NORMALIZE,
    GL_SAMPLE_ALPHA_TO_COVERAGE,
    GL_SAMPLE_ALPHA_TO_ONE,
    GL_SAMPLE_COVERAGE,
    GL_SEPARABLE_2D,
    GL_SCISSOR_TEST,
    GL_STENCIL_TEST,
    GL_TEXTURE_1D,
    GL_TEXTURE_2D,
    GL_TEXTURE_3D,
    GL_TEXTURE_CUBE_MAP,
    GL_TEXTURE_GEN_Q,
    GL_TEXTURE_GEN_R,
    GL_TEXTURE_GEN_S,
    GL_TEXTURE_GEN_T,
    GL_VERTEX_PROGRAM_POINT_SIZE,
    GL_VERTEX_PROGRAM_TWO_SIDE,
    GL_COLOR_ARRAY,
    GL_EDGE_FLAG_ARRAY,
    GL_FOG_COORD_ARRAY,
    GL_INDEX_ARRAY,
    GL_NORMAL_ARRAY,
    GL_SECONDARY_COLOR_ARRAY,
    GL_TEXTURE_COORD_ARRAY,
    GL_VERTEX_ARRAY};

1336
  for (unsigned int i = 0; i < sizeof(caps) / sizeof(GLenum); ++i)
Jan Möbius's avatar
Jan Möbius committed
1337
  {
1338 1339
    if (glIsEnabled(caps[i])) stateStack_.back().glStateEnabled_.set(caps[i]);
    else stateStack_.back().glStateEnabled_.reset(caps[i]);
Jan Möbius's avatar
Jan Möbius committed
1340 1341 1342
  }
  
  GLint getparam;
1343

Jan's avatar
Jan committed
1344
#ifdef GL_VERSION_1_4
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
  glGetIntegerv(GL_BLEND_SRC_RGB, &getparam);
  stateStack_.back().blendFuncState_[0] = getparam;

  glGetIntegerv(GL_BLEND_DST_ALPHA, &getparam);
  stateStack_.back().blendFuncState_[1] = getparam;

  glGetIntegerv(GL_BLEND_SRC_ALPHA, &getparam);
  stateStack_.back().blendFuncState_[2] = getparam;

  glGetIntegerv(GL_BLEND_DST_ALPHA, &getparam);
  stateStack_.back().blendFuncState_[3] = getparam;
#else
Jan Möbius's avatar
Jan Möbius committed
1357
  glGetIntegerv(GL_BLEND_SRC, &getparam);
1358
  stateStack_.back().blendFuncState_[0] = getparam;
Jan Möbius's avatar
Jan Möbius committed
1359 1360

  glGetIntegerv(GL_BLEND_DST, &getparam);
1361
  stateStack_.back().blendFuncState_[1] = getparam;
1362
#endif
Jan Möbius's avatar
Jan Möbius committed
1363 1364 1365


  glGetIntegerv(GL_BLEND_EQUATION_RGB, &getparam);
1366
  stateStack_.back().blendEquationState_ = getparam;
Jan Möbius's avatar
Jan Möbius committed
1367
  
1368
  glGetFloatv(GL_BLEND_COLOR, stateStack_.back().blendColorState_);
Jan Möbius's avatar
Jan Möbius committed
1369 1370
  
  glGetIntegerv(GL_ALPHA_TEST_FUNC, &getparam);
1371
  stateStack_.back().alphaFuncState_ = getparam;
Jan Möbius's avatar
Jan Möbius committed
1372

1373
  glGetFloatv(GL_ALPHA_TEST_REF, &stateStack_.back().alphaRefState_);
Jan Möbius's avatar
Jan Möbius committed
1374

1375 1376 1377
  glGetIntegerv(GL_DEPTH_FUNC, &getparam);
  stateStack_.back().depthFunc_ = getparam;

Christopher Tenter's avatar
Christopher Tenter committed
1378 1379
  glGetDoublev(GL_DEPTH_RANGE, stateStack_.back().depthRange_);

Jan Möbius's avatar
Jan Möbius committed
1380 1381 1382 1383 1384 1385 1386 1387 1388
  // bound buffers

  GLenum bufGets[8] = {
    GL_ARRAY_BUFFER_BINDING, GL_ARRAY_BUFFER,
    GL_ELEMENT_ARRAY_BUFFER_BINDING, GL_ELEMENT_ARRAY_BUFFER, 
    GL_PIXEL_PACK_BUFFER_BINDING, GL_PIXEL_PACK_BUFFER,
    GL_PIXEL_UNPACK_BUFFER_BINDING, GL_PIXEL_UNPACK_BUFFER};

  for (int i = 0; i < 4; ++i)
1389
    glGetIntegerv(bufGets[i*2], (GLint*)stateStack_.back().glBufferTargetState_ + getBufferTargetIndex(bufGets[i*2+1]));
Jan Möbius's avatar
Jan Möbius committed
1390 1391 1392 1393


  // bound textures
  glGetIntegerv(GL_ACTIVE_TEXTURE, &getparam);
1394
  stateStack_.back().activeTexture_ = getparam;
Jan Möbius's avatar
Jan Möbius committed
1395 1396 1397 1398 1399

  GLenum texBufGets[] = {
    GL_TEXTURE_BINDING_1D, GL_TEXTURE_1D,
    GL_TEXTURE_BINDING_2D, GL_TEXTURE_2D,
    GL_TEXTURE_BINDING_3D, GL_TEXTURE_3D,
1400
    GL_TEXTURE_BINDING_CUBE_MAP, GL_TEXTURE_CUBE_MAP
1401
    , GL_TEXTURE_BINDING_RECTANGLE_ARB, GL_TEXTURE_RECTANGLE_ARB
1402
  };
Jan Möbius's avatar
Jan Möbius committed
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417

  glGetIntegerv(GL_MAX_TEXTURE_COORDS, &maxTextureCoords_);
  glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxCombinedTextureImageUnits_);

  // safe clamp
  if (maxTextureCoords_ > 16) maxTextureCoords_ = 16;
  if (maxCombinedTextureImageUnits_ > 16) maxCombinedTextureImageUnits_ = 16;

  int numTexUnits = maxTextureCoords_;
  if (numTexUnits < maxCombinedTextureImageUnits_) numTexUnits = maxCombinedTextureImageUnits_;

  for (int i = 0; i < numTexUnits; ++i)
  {
    glActiveTexture(GL_TEXTURE0 + i);

1418
    getparam = 0;
1419
    // for each texture stage query 5 texture types: 1D, 2D, 3D, Cube, Rect
1420 1421 1422 1423 1424 1425 1426 1427 1428
    for (int k = 0; k < 5 && !getparam; ++k)
    {
      glGetIntegerv(texBufGets[k*2], &getparam);
      if (getparam)
      {
        stateStack_.back().glTextureStage_[i].buf_ = getparam;
        stateStack_.back().glTextureStage_[i].target_ = texBufGets[k*2+1];
      }
    }
Jan Möbius's avatar
Jan Möbius committed
1429 1430 1431
  }

  // restore active texture unit
1432 1433
  if (numTexUnits > 0)
    glActiveTexture(stateStack_.back().activeTexture_);
Jan Möbius's avatar
Jan Möbius committed
1434 1435 1436 1437


  // shade model
  glGetIntegerv(GL_SHADE_MODEL, &getparam);
1438
  stateStack_.back().shadeModel_ = getparam;
Jan Möbius's avatar
Jan Möbius committed
1439 1440 1441

  // cull face
  glGetIntegerv(GL_CULL_FACE_MODE, &getparam);
1442
  stateStack_.back().cullFace_ = getparam;
Jan Möbius's avatar
Jan Möbius committed
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454


  // vertex pointers
  {
    GLenum ptrEnums[] = {
      GL_VERTEX_ARRAY_SIZE, GL_VERTEX_ARRAY_TYPE,
      GL_VERTEX_ARRAY_STRIDE, GL_VERTEX_ARRAY_POINTER,
      GL_COLOR_ARRAY_SIZE,  GL_COLOR_ARRAY_TYPE,
      GL_COLOR_ARRAY_STRIDE, GL_COLOR_ARRAY_POINTER,
      GL_TEXTURE_COORD_ARRAY_SIZE, GL_TEXTURE_COORD_ARRAY_TYPE,
      GL_TEXTURE_COORD_ARRAY_STRIDE, GL_TEXTURE_COORD_ARRAY_POINTER};

1455 1456
    GLStateContext::GLVertexPointer* ptrs[] = {&stateStack_.back().vertexPointer_, 
      &stateStack_.back().colorPointer_, &stateStack_.back().texcoordPointer_};
Jan Möbius's avatar
Jan Möbius committed
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469

    for (int i = 0; i < 3 ; ++i)
    {
      glGetIntegerv(ptrEnums[i*4], &getparam);
      ptrs[i]->size = getparam;
      glGetIntegerv(ptrEnums[i*4+1], &getparam);
      ptrs[i]->type = getparam;
      glGetIntegerv(ptrEnums[i*4+2], &getparam);
      ptrs[i]->stride = getparam;
      glGetPointerv(ptrEnums[i*4+3], (GLvoid**)&ptrs[i]->pointer);
    }

    glGetIntegerv(GL_NORMAL_ARRAY_STRIDE, &getparam);
1470
    stateStack_.back().normalPointer_.size = getparam;
Jan Möbius's avatar
Jan Möbius committed
1471
    glGetIntegerv(GL_NORMAL_ARRAY_TYPE, &getparam);
1472 1473
    stateStack_.back().normalPointer_.type = getparam;
    glGetPointerv(GL_NORMAL_ARRAY_POINTER, (GLvoid**)&stateStack_.back().normalPointer_.pointer);
Jan Möbius's avatar
Jan Möbius committed
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
  }


  // draw buffer state
  glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers_);
  if (maxDrawBuffers_ > 16) maxDrawBuffers_ = 16;

  for (int i = 0; i < maxDrawBuffers_; ++i)
  {
    glGetIntegerv(GL_DRAW_BUFFER0 + i, &getparam);
1484
    stateStack_.back().drawBufferState_[i] = getparam;
Jan Möbius's avatar
Jan Möbius committed
1485 1486 1487
  }

  glGetIntegerv(GL_DRAW_BUFFER, &getparam);
1488
  stateStack_.back().drawBufferSingle_ = getparam;
Jan Möbius's avatar
Jan Möbius committed
1489 1490 1491

  // framebuffer
  glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &getparam);
1492
  stateStack_.back().framebuffers_[0] = getparam;
Jan Möbius's avatar
Jan Möbius committed
1493
  glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &getparam);
1494
  stateStack_.back().framebuffers_[1] = getparam;
Jan Möbius's avatar
Jan Möbius committed
1495 1496 1497

  // shader program
  glGetIntegerv(GL_CURRENT_PROGRAM, &getparam);
1498
  stateStack_.back().program_ = getparam;
Jan Möbius's avatar
Jan Möbius committed
1499 1500 1501 1502 1503 1504 1505 1506 1507
}

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


void GLState::enable(GLenum _cap)
{
  if (!glStateLock_.test(_cap))
  {
1508
#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1509
    if (!stateStack_.back().glStateEnabled_.test(_cap))
1510
#endif
Jan Möbius's avatar
Jan Möbius committed
1511 1512
    {
      glEnable(_cap);
1513
      stateStack_.back().glStateEnabled_.set(_cap);
Jan Möbius's avatar
Jan Möbius committed
1514 1515 1516 1517 1518 1519 1520 1521
    }
  }
}

void GLState::disable(GLenum _cap)
{
  if (!glStateLock_.test(_cap))
  {
1522
#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1523
    if (stateStack_.back().glStateEnabled_.test(_cap))
1524
#endif
Jan Möbius's avatar
Jan Möbius committed
1525 1526
    {
      glDisable(_cap);
1527
      stateStack_.back().glStateEnabled_.reset(_cap);
Jan Möbius's avatar
Jan Möbius committed
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
    }
  }
}

void GLState::lockState(GLenum _cap)
{
  glStateLock_.set(_cap);
}

void GLState::unlockState(GLenum _cap)
{
  glStateLock_.reset(_cap);
}

bool GLState::isStateLocked(GLenum _cap)
{
  return glStateLock_.test(_cap);
}

bool GLState::isStateEnabled(GLenum _cap)
{
1549
  return stateStack_.back().glStateEnabled_.test(_cap);
Jan Möbius's avatar
Jan Möbius committed
1550 1551 1552 1553 1554 1555 1556 1557 1558
}

//-----------------------------------------------------------------------------
// client state functions

void GLState::enableClientState(GLenum _cap)
{
  if (!glStateLock_.test(_cap))
  {
1559
#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1560
    if (!stateStack_.back().glStateEnabled_.test(_cap))
1561
#endif
Jan Möbius's avatar
Jan Möbius committed
1562 1563
    {
      glEnableClientState(_cap);
1564
      stateStack_.back().glStateEnabled_.set(_cap);
Jan Möbius's avatar
Jan Möbius committed
1565 1566 1567 1568 1569 1570 1571 1572
    }
  }
}

void GLState::disableClientState(GLenum _cap)
{
  if (!glStateLock_.test(_cap))
  {
1573
#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
Christopher Tenter's avatar