Developer Documentation
GLState.cc
1/*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40\*===========================================================================*/
41
42
43//=============================================================================
44//
45// CLASS GLState - IMPLEMENTATION
46//
47//=============================================================================
48
49//== INCLUDES =================================================================
50
51#include <ACG/GL/acg_glew.hh>
52#include "GLState.hh"
53#include <ACG/GL/removedEnums.hh>
54
55#include <OpenMesh/Core/Utils/vector_cast.hh>
56#include <cstring>
57
58
59//== NAMESPACES ===============================================================
60
61
62namespace ACG {
63
64
65//== IMPLEMENTATION ==========================================================
66
67//#define GLSTATE_AVOID_REDUNDANT_GLCALLS
68
69const Vec4f GLState::default_clear_color(0.f, 0.f, 0.f, 1.f);
70const Vec4f GLState::default_base_color(0.f, 0.f, 0.f, 1.f);
71const Vec4f GLState::default_ambient_color(0.2f, 0.2f, 0.2f, 1.f);
72const Vec4f GLState::default_diffuse_color(0.5f, 0.53f, 0.6f, 1.f);
73const Vec4f GLState::default_specular_color(0.75f, 0.8f, 0.85f, 1.f);
74const Vec4f GLState::default_overlay_color(0.f, 0.f, 0.f, 1.f);
75const float GLState::default_shininess(100.f);
76
77
78//-----------------------------------------------------------------------------
79
80bool GLState::depthFuncLock_ = false;
81bool GLState::depthRangeLock_ = false;
82bool GLState::blendFuncSeparateLock_[] = { false };
83bool GLState::blendEquationLock_ = false;
84bool GLState::blendColorLock_ = false;
85bool GLState::alphaFuncLock_ = false;
86bool GLState::shadeModelLock_ = false;
87bool GLState::cullFaceLock_ = false;
88bool GLState::vertexPointerLock_ = false;
89bool GLState::normalPointerLock_ = false;
90bool GLState::texcoordPointerLock_ = false;
91bool GLState::colorPointerLock_ = false;
92bool GLState::drawBufferLock_ = false;
93bool GLState::programLock_ = false;
94
95std::deque <GLStateContext> GLState::stateStack_;
96std::bitset<0xFFFF+1> GLState::glStateLock_;
97int GLState::glBufferTargetLock_[] = {0};
98int GLState::glTextureStageLock_[] = {0};
99bool GLState::framebufferLock_[] = {false};
100int GLState::maxTextureCoords_ = 0;
101int GLState::maxCombinedTextureImageUnits_ = 0;
102int GLState::maxDrawBuffers_ = 0;
103
104int GLState::num_texture_units_ = 0;
105
106GLStateContext::GLStateContext() :
107 activeTexture_(GL_TEXTURE0),
108 drawBufferSingle_(GL_BACK),
109 activeDrawBuffer_(0),
110 program_(0)
111{
112 framebuffers_[0] = framebuffers_[1] = 0;
113 memset(drawBufferState_, GL_BACK, sizeof(drawBufferState_));
114 blendFuncState_[0] = GL_SRC_ALPHA;
115 blendFuncState_[1] = GL_ONE_MINUS_SRC_ALPHA;
116 blendFuncState_[2] = GL_SRC_ALPHA;
117 blendFuncState_[3] = GL_ONE_MINUS_SRC_ALPHA;
118 texGenMode_ = GL_EYE_LINEAR;
119}
120
121GLState::GLState(bool _updateGL, bool _compatibilityProfile)
122 : compatibilityProfile_(_compatibilityProfile),
123 render_pass_(1),
124 max_render_passes_(1),
125 bb_min_(ACG::Vec3d(0.0,0.0,0.0)),
126 bb_max_(ACG::Vec3d(0.0,0.0,0.0)),
127 left_(-1),
128 bottom_(1),
129 width_(2),
130 height_(2),
131 glwidth_(2),
132 glheight_(2),
133 near_plane_(1.0),
134 far_plane_(100.0),
135 multisampling_(false),
136 allow_multisampling_(true),
137 mipmapping_(true),
138 updateGL_(_updateGL),
139 blending_(false),
140 msSinceLastRedraw_ (1),
141 colorPicking_(true)
142{
143
144 if ( stateStack_.empty() )
145 {
146 stateStack_.push_back(GLStateContext());
147
148 memset(glBufferTargetLock_, 0, sizeof(glBufferTargetLock_));
149
150 framebufferLock_[0] = framebufferLock_[1] = false;
151
152 glStateLock_.reset();
153 }
154
155 initialize();
156 ACG::compatibilityProfile(compatibilityProfile_);
157}
158
159//-----------------------------------------------------------------------------
160
161
163{
164 // clear matrix stacks
165 while (!stack_projection_.empty())
166 stack_projection_.pop();
167 while (!stack_modelview_.empty())
168 stack_modelview_.pop();
169 while (!stack_inverse_projection_.empty())
170 stack_inverse_projection_.pop();
171 while (!stack_inverse_modelview_.empty())
172 stack_inverse_modelview_.pop();
173
174
175 // load identity matrix
178
179
180 // colors
188
189
190 // thickness
191 set_point_size(1.0f);
192 set_line_width(1.0f);
193
194 // multisampling
195 set_multisampling(true);
196
197 // Get max number of texture units
198 GLint value;
199 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, &value);
200
201 num_texture_units_ = value;
202
203 // lighting
205}
206
207//-----------------------------------------------------------------------------
208
210{
211 makeCurrent();
212
213 if (compatibilityProfile_ ) {
214
215 // projection matrix
216 glMatrixMode(GL_PROJECTION);
217 glLoadMatrixd(projection_.get_raw_data());
218 glMatrixMode(GL_MODELVIEW);
219
220 // modelview matrix
221 glLoadMatrixd(modelview_.get_raw_data());
222
223 }
224
225 // clear color
226 glClearColor(clear_color_[0], clear_color_[1], clear_color_[2], clear_color_[3]);
227
228 if (compatibilityProfile_ ) {
229 // base color
230 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, base_color_.data());
231
232 // ambient color
233 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT , ambient_color_.data());
234
235 // diffuse color
236 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE , diffuse_color_.data());
237
238 // specular color
239 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular_color_.data());
240
241 // shininess
242 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess_);
243 }
244
245 // point size
246 glPointSize(point_size_);
247
248 // line width
249 glLineWidth(line_width_);
250
251 if ( compatibilityProfile_ ) {
252 // two sided lighting
253 if (twosided_lighting_ )
254 glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE );
255 else
256 glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
257 }
258
259 // viewport
260 glViewport(left_, bottom_, width_, height_);
261}
262
263
264//-----------------------------------------------------------------------------
265
267{
268
269 if ( compatibilityProfile_ ) {
270 glPushAttrib (GL_ALL_ATTRIB_BITS);
271 }
272
273 GLState::disable(GL_DEPTH_TEST);
274 GLState::disable(GL_DITHER);
275
276 if ( compatibilityProfile_ ) {
277 glShadeModel(GL_FLAT);
278
279 GLState::disable(GL_LIGHTING);
280 glMatrixMode(GL_PROJECTION);
281 glPushMatrix();
282 glLoadIdentity ();
283
284 glMatrixMode(GL_MODELVIEW);
285 glPushMatrix();
286 glLoadIdentity ();
287 }
288
289
290 // GetoriginalScissor settings
291 GLboolean scissor = glIsEnabled(GL_SCISSOR_TEST);
292
293 GLint origBox[4];
294 glGetIntegerv(GL_SCISSOR_BOX,&origBox[0]);
295
296 //Enable scissor
297 if (!scissor)
298 GLState::enable(GL_SCISSOR_TEST);
299
300 // Restrict to our current viewport
301 glScissor( left_,bottom_,width_,height_ );
302
303 // Clear restricted region
304 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
305
306 // Reset to originalsettings
307 glScissor( origBox[0], origBox[1], origBox[2], origBox[3] );
308
309 if (!scissor)
310 GLState::disable(GL_SCISSOR_TEST);
311
312 if ( compatibilityProfile_ ) {
313 glPopMatrix ();
314 glMatrixMode(GL_PROJECTION);
315 glPopMatrix();
316 glMatrixMode(GL_MODELVIEW);
317 glPopAttrib ();
318 }
319}
320
321//-----------------------------------------------------------------------------
322
323void GLState::setCompatibilityProfile( bool _compatibility ) {
324 compatibilityProfile_ = _compatibility;
325 ACG::compatibilityProfile(_compatibility);
326}
327
328bool GLState::compatibilityProfile() const {
329 return compatibilityProfile_;
330}
331
332//-----------------------------------------------------------------------------
333
335{
336 projection_.identity();
337 inverse_projection_.identity();
338
339 if (updateGL_ && compatibilityProfile_ )
340 {
341 makeCurrent();
342 glMatrixMode(GL_PROJECTION);
343 glLoadIdentity();
344 glMatrixMode(GL_MODELVIEW);
345 }
346}
347
348
349//-----------------------------------------------------------------------------
350
351
352void GLState::set_projection(const GLMatrixd& _m, const GLMatrixd& _inv_m)
353{
354 projection_ = _m;
355 inverse_projection_ = _inv_m;
356
357 if (updateGL_ && compatibilityProfile_)
358 {
359 makeCurrent();
360 glMatrixMode(GL_PROJECTION);
361 glLoadMatrixd(projection_.get_raw_data());
362 glMatrixMode(GL_MODELVIEW);
363 }
364}
365
366
367//-----------------------------------------------------------------------------
368
369
371{
372 modelview_.identity();
373 inverse_modelview_.identity();
374
375 if (updateGL_ && compatibilityProfile_ )
376 {
377 makeCurrent();
378 glLoadIdentity();
379 }
380}
381
382
383//-----------------------------------------------------------------------------
384
385
386void GLState::set_modelview(const GLMatrixd& _m, const GLMatrixd& _inv_m)
387{
388 modelview_ = _m;
389 inverse_modelview_ = _inv_m;
390
391 if (updateGL_ && compatibilityProfile_)
392 {
393 makeCurrent();
394 glLoadMatrixd(modelview_.get_raw_data());
395 }
396}
397
398
399//-----------------------------------------------------------------------------
400
401
402void GLState::ortho( double _left, double _right,
403 double _bottom, double _top,
404 double _n, double _f )
405{
406 near_plane_ = _n;
407 far_plane_ = _f;
408
409 projection_.ortho(_left, _right, _bottom, _top, _n, _f);
410 inverse_projection_.inverse_ortho(_left,_right,_bottom,_top,_n,_f);
411
412 if (updateGL_ && compatibilityProfile_ )
413 {
414 makeCurrent();
415 glMatrixMode(GL_PROJECTION);
416 glOrtho(_left, _right, _bottom, _top, _n, _f);
417 glMatrixMode(GL_MODELVIEW);
418 }
419}
420
421
422//-----------------------------------------------------------------------------
423
424
425void GLState::frustum( double _left, double _right,
426 double _bottom, double _top,
427 double _n, double _f )
428{
429 near_plane_ = _n;
430 far_plane_ = _f;
431
432 projection_.frustum(_left, _right, _bottom, _top, _n, _f);
433 inverse_projection_.inverse_frustum(_left,_right,_bottom,_top,_n,_f);
434
435 if (updateGL_ && compatibilityProfile_)
436 {
437 makeCurrent();
438 glMatrixMode(GL_PROJECTION);
439 glFrustum(_left, _right, _bottom, _top, _n, _f);
440 glMatrixMode(GL_MODELVIEW);
441 }
442}
443
444
445//-----------------------------------------------------------------------------
446
447
448void GLState::perspective( double _fovY, double _aspect,
449 double _n, double _f )
450{
451 near_plane_ = _n;
452 far_plane_ = _f;
453
454 projection_.perspective(_fovY, _aspect, _n, _f);
455 inverse_projection_.inverse_perspective(_fovY, _aspect, _n, _f);
456
457 if (updateGL_ && compatibilityProfile_)
458 {
459 makeCurrent();
460 glMatrixMode(GL_PROJECTION);
461 glLoadMatrixd(projection_.data());
462 glMatrixMode(GL_MODELVIEW);
463 }
464}
465
466
467//-----------------------------------------------------------------------------
468
469
470void GLState::viewport( int _left, int _bottom,
471 int _width, int _height,
472 int _glwidth, int _glheight)
473{
474 left_ = _left;
475 bottom_ = _bottom;
476 width_ = _width;
477 height_ = _height;
478
479 if (_glwidth < _width || _glheight < _height)
480 {
481 glwidth_ = _width;
482 glheight_ = _height;
483 } else {
484 glwidth_ = _glwidth;
485 glheight_ = _glheight;
486 }
487
488 window2viewport_.identity();
489 window2viewport_(0,0) = 0.5f * width_;
490 window2viewport_(0,3) = 0.5f * width_ + left_;
491 window2viewport_(1,1) = 0.5f * height_;
492 window2viewport_(1,3) = 0.5f * height_ + bottom_;
493 window2viewport_(2,2) = 0.5f;
494 window2viewport_(2,3) = 0.5f;
495
496 inverse_window2viewport_.identity();
497 inverse_window2viewport_(0,0) = 2.0f / width_;
498 inverse_window2viewport_(0,3) = -(2.0*left_ + width_) / width_;
499 inverse_window2viewport_(1,1) = 2.0f / height_;
500 inverse_window2viewport_(1,3) = -(2.0*bottom_ + height_) / height_;
501 inverse_window2viewport_(2,2) = 2.0f;
502 inverse_window2viewport_(2,3) = -1.0f;
503
504 if (updateGL_)
505 {
506 makeCurrent();
507 glViewport(_left, _bottom, _width, _height);
508 }
509}
510
511
512//-----------------------------------------------------------------------------
513
514
515void GLState::lookAt( const Vec3d& _eye,
516 const Vec3d& _center,
517 const Vec3d& _up )
518{
519 modelview_.lookAt(_eye, _center, _up);
520 inverse_modelview_.inverse_lookAt(_eye, _center, _up);
521
522 if (updateGL_ && compatibilityProfile_)
523 {
524 makeCurrent();
525 glLoadMatrixd(modelview_.data());
526 }
527}
528
529
530//-----------------------------------------------------------------------------
531
532
533void GLState::translate( double _x, double _y, double _z,
534 MultiplyFrom _mult_from )
535{
536 if (_mult_from == MULT_FROM_RIGHT)
537 {
538 modelview_.translate(_x, _y, _z);
539 inverse_modelview_.translate(-_x, -_y, -_z, MULT_FROM_LEFT);
540 }
541 else
542 {
543 modelview_.translate(_x, _y, _z, MULT_FROM_LEFT);
544 inverse_modelview_.translate(-_x, -_y, -_z);
545 }
546
547 if (updateGL_ && compatibilityProfile_)
548 {
549 makeCurrent();
550 glLoadMatrixd(modelview_.get_raw_data());
551 }
552}
553
554//-----------------------------------------------------------------------------
555
557 MultiplyFrom _mult_from ) {
558 translate( _vector[0] , _vector[1] , _vector[2] ,_mult_from);
559}
560
561//-----------------------------------------------------------------------------
562
563
564void GLState::rotate( double _angle, double _x, double _y, double _z,
565 MultiplyFrom _mult_from )
566{
567 if (_mult_from == MULT_FROM_RIGHT)
568 {
569 modelview_.rotate(_angle, _x, _y, _z);
570 inverse_modelview_.rotate(-_angle, _x, _y, _z, MULT_FROM_LEFT);
571 }
572 else
573 {
574 modelview_.rotate(_angle, _x, _y, _z, MULT_FROM_LEFT);
575 inverse_modelview_.rotate(-_angle, _x, _y, _z);
576 }
577
578 if (updateGL_ && compatibilityProfile_)
579 {
580 makeCurrent();
581 glLoadMatrixd(modelview_.get_raw_data());
582 }
583}
584
585
586//-----------------------------------------------------------------------------
587
588
589void GLState::scale( double _sx, double _sy, double _sz,
590 MultiplyFrom _mult_from )
591{
592 if (_mult_from == MULT_FROM_RIGHT)
593 {
594 modelview_.scale(_sx, _sy, _sz, MULT_FROM_RIGHT);
595 inverse_modelview_.scale(1.0f/_sx, 1.0f/_sy, 1.0f/_sz, MULT_FROM_LEFT);
596 }
597 else
598 {
599 modelview_.scale(_sx, _sy, _sz, MULT_FROM_LEFT);
600 inverse_modelview_.scale(1.0f/_sx, 1.0f/_sy, 1.0f/_sz, MULT_FROM_RIGHT);
601 }
602
603 if (updateGL_ && compatibilityProfile_)
604 {
605 makeCurrent();
606 glLoadMatrixd(modelview_.get_raw_data());
607 }
608}
609
610
611//-----------------------------------------------------------------------------
612
613
614void GLState::mult_matrix( const GLMatrixd& _m, const GLMatrixd& _inv_m,
615 MultiplyFrom _mult_from )
616{
617 if (_mult_from == MULT_FROM_RIGHT)
618 {
619 modelview_ *= _m;
620 inverse_modelview_.leftMult(_inv_m);
621 }
622 else
623 {
624 modelview_.leftMult(_m);
625 inverse_modelview_ *= _inv_m;
626 }
627
628 if (updateGL_ && compatibilityProfile_)
629 {
630 makeCurrent();
631 glLoadMatrixd(modelview_.get_raw_data());
632 }
633}
634
635
636
637//-----------------------------------------------------------------------------
638
639
640Vec3d GLState::project(const Vec3d& _point) const
641{
642 Vec3d t = modelview_.transform_point(_point);
643 t = projection_.transform_point(t);
644 return window2viewport_.transform_point(t);
645}
646
647
648//-----------------------------------------------------------------------------
649
650
651Vec3d GLState::unproject(const Vec3d& _winPoint) const
652{
653 Vec3d t = inverse_window2viewport_.transform_point(_winPoint);
654 t = inverse_projection_.transform_point(t);
655 return inverse_modelview_.transform_point(t);
656}
657
658
659//-----------------------------------------------------------------------------
660
661
663{
664 clear_color_ = _col;
665
666 if (updateGL_)
667 {
668 makeCurrent();
669 glClearColor(_col[0], _col[1], _col[2], _col[3]);
670 }
671}
672
673
674//-----------------------------------------------------------------------------
675
676
678{
679 base_color_ = _col;
680
681 if (updateGL_ && compatibilityProfile_ )
682 {
683 makeCurrent();
684 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, _col.data());
685 }
686}
687
688//-----------------------------------------------------------------------------
689
690
691void GLState::set_color(const Vec4f& _col)
692{
693 color_ = _col;
694
695 if (updateGL_)
696 {
697 makeCurrent();
698 glColor(color_);
699 }
700}
701
702
703
704//-----------------------------------------------------------------------------
705
706
708{
709 ambient_color_ = _col;
710
711 if (updateGL_ && compatibilityProfile_)
712 {
713 makeCurrent();
714 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, _col.data());
715 }
716}
717
718
719//-----------------------------------------------------------------------------
720
721
723{
724 diffuse_color_ = _col;
725
726 if (updateGL_ && compatibilityProfile_)
727 {
728 makeCurrent();
729 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, _col.data());
730 }
731}
732
733
734//-----------------------------------------------------------------------------
735
736
738{
739 specular_color_ = _col;
740
741 if (updateGL_ && compatibilityProfile_)
742 {
743 makeCurrent();
744 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, _col.data());
745 }
746
747}
748
749//-----------------------------------------------------------------------------
750
751
753{
754 overlay_color_ = _col;
755}
756
757
758//-----------------------------------------------------------------------------
759
760
761void GLState::set_shininess(float _shininess)
762{
763 shininess_ = _shininess;
764
765 if (updateGL_ && compatibilityProfile_)
766 {
767 makeCurrent();
768 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, std::min(128.f, _shininess));
769 }
770}
771
772
773//-----------------------------------------------------------------------------
774
775
777{
778 point_size_ = _f;
779
780 if (updateGL_)
781 {
782 makeCurrent();
783 glPointSize(point_size_);
784 }
785}
786
787
788//-----------------------------------------------------------------------------
789
790
792{
793 line_width_ = _f;
794if(compatibilityProfile())
795{
796 if (updateGL_)
797 {
798 makeCurrent();
799 glLineWidth(line_width_);
800 }
801}
802}
803
804//-----------------------------------------------------------------------------
805
807 bb_min_ = _min;
808 bb_max_ = _max;
809}
810
811//-----------------------------------------------------------------------------
812
814 _min = bb_min_;
815 _max = bb_max_;
816}
817
818
819//-----------------------------------------------------------------------------
820
821
823{
824 twosided_lighting_ = _b;
825
826 if (updateGL_ && compatibilityProfile_ )
827 {
828 makeCurrent();
829 if (twosided_lighting_)
830 glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE );
831 else
832 glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
833 }
834}
835
836
837//-----------------------------------------------------------------------------
838
840{
841
842 multisampling_ = _b;
843
844 if (updateGL_)
845 {
846 makeCurrent();
847 if ( allow_multisampling_ ) {
848
849 if ( _b )
850 GLState::enable( GL_MULTISAMPLE );
851 else
852 GLState::disable( GL_MULTISAMPLE );
853
854 } else {
855
856 multisampling_ = false;
857
858 if ( glIsEnabled( GL_MULTISAMPLE ) )
859 GLState::disable( GL_MULTISAMPLE );
860
861 }
862 }
863
864}
865
866//-----------------------------------------------------------------------------
867
868double GLState::fovy() const
869{
870 assert(projection_(1,1) != 0.0);
871
872 return atan(1.0/projection_(1,1))*2.0;
873}
874
875//-----------------------------------------------------------------------------
876
877double GLState::aspect() const
878{
879 assert(projection_(0,0) != 0.0);
880
881 return projection_(1,1) / projection_(0,0);
882}
883
884//-----------------------------------------------------------------------------
885
887{
888 return inverse_modelview_.transform_point(Vec3d(0.0, 0.0, 0.0));
889}
890
891//-----------------------------------------------------------------------------
892
893
895{
896 Vec3d dir = ( unproject(Vec3d(_x, _y, 1.0)) -
897 unproject(Vec3d(_x, _y, 0.0)) );
898 dir.normalize();
899 return dir;
900}
901
902
903//-----------------------------------------------------------------------------
904
905
907{
908 Vec3d dir( unproject(Vec3d(0.5*width_, height_-1, 0.0)) -
909 unproject(Vec3d(0.5*width_, 0.5*height_, 0.0)) );
910 dir.normalize();
911 return dir;
912}
913
914
915//-----------------------------------------------------------------------------
916
917
919{
920 Vec3d dir( unproject(Vec3d(width_-1, 0.5*height_, 0.0)) -
921 unproject(Vec3d(0.5*width_, 0.5*height_, 0.0)) );
922 dir.normalize();
923 return dir;
924}
925
926
927//-----------------------------------------------------------------------------
928
929
930void GLState::viewing_ray( int _x, int _y,
931 Vec3d& _origin, Vec3d& _direction) const
932{
933 _origin = unproject(Vec3d(_x, _y, 0.0));
934 _direction = unproject(Vec3d(_x, _y, 1.0)) - _origin;
935 _direction.normalize();
936}
937
938
939//-----------------------------------------------------------------------------
940
941const GLenum& GLState::depthFunc() const
942{
943 return stateStack_.back().depthFunc_;
944}
945
946//-----------------------------------------------------------------------------
947
948void GLState:: set_depthFunc(const GLenum& _depth_func)
949{
950 depthFunc(_depth_func);
951}
952
953//-----------------------------------------------------------------------------
954
955void GLState::depthFunc(GLenum _depthFunc)
956{
957 if (!depthFuncLock_)
958 {
959#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
960 if (stateStack_.back().depthFunc_ != _depthFunc)
961#endif
962 {
963 glDepthFunc(_depthFunc);
964 stateStack_.back().depthFunc_ = _depthFunc;
965 }
966 }
967}
968
969//-----------------------------------------------------------------------------
970
972{
973 stack_projection_.push(projection_);
974 stack_inverse_projection_.push(inverse_projection_);
975
976 if (updateGL_ && compatibilityProfile_)
977 {
978 makeCurrent();
979 glMatrixMode(GL_PROJECTION);
980 glPushMatrix();
981 glMatrixMode(GL_MODELVIEW);
982 }
983}
984
985
986//-----------------------------------------------------------------------------
987
988
990{
991 projection_ = stack_projection_.top();
992 inverse_projection_ = stack_inverse_projection_.top();
993
994 stack_projection_.pop();
995 stack_inverse_projection_.pop();
996
997 if (updateGL_ && compatibilityProfile_)
998 {
999 makeCurrent();
1000 glMatrixMode(GL_PROJECTION);
1001 glPopMatrix();
1002 glMatrixMode(GL_MODELVIEW);
1003 }
1004}
1005
1006
1007//-----------------------------------------------------------------------------
1008
1009
1011{
1012 stack_modelview_.push(modelview_);
1013 stack_inverse_modelview_.push(inverse_modelview_);
1014
1015 if (updateGL_ && compatibilityProfile_)
1016 {
1017 makeCurrent();
1018 glPushMatrix();
1019 }
1020}
1021
1022
1023//-----------------------------------------------------------------------------
1024
1025
1027{
1028 modelview_ = stack_modelview_.top();
1029 inverse_modelview_ = stack_inverse_modelview_.top();
1030
1031 stack_modelview_.pop();
1032 stack_inverse_modelview_.pop();
1033
1034 if (updateGL_ && compatibilityProfile_)
1035 {
1036 makeCurrent();
1037 glPopMatrix();
1038 }
1039}
1040
1041//-----------------------------------------------------------------------------
1042
1043void GLState::pick_init (bool _color)
1044{
1045 colorPicking_ = _color;
1046 colorStack_.initialize ( this);
1047}
1048
1049//-----------------------------------------------------------------------------
1050
1052{
1053 bool rv = colorStack_.setMaximumIndex (_idx);
1054 if (colorPicking_)
1055 return rv;
1056 return true;
1057}
1058
1059//-----------------------------------------------------------------------------
1060
1061void GLState::pick_set_name (size_t _idx)
1062{
1063 colorStack_.setIndex (_idx);
1064}
1065
1066//-----------------------------------------------------------------------------
1067
1069{
1070 if (colorPicking_)
1071 return colorStack_.getIndexColor (_idx);
1072 return Vec4uc (0, 0, 0, 0);
1073}
1074
1075//-----------------------------------------------------------------------------
1076
1078{
1079 Vec4f rv(0.0f, 0.0f, 0.0f, 0.0f);
1080 if (colorPicking_)
1081 {
1082 Vec4uc color_abs = colorStack_.getIndexColor(_idx);
1083 rv = OpenMesh::vector_cast<Vec4f, Vec4uc>(color_abs) / 255.0f;
1084 }
1085 return rv;
1086}
1087
1088//-----------------------------------------------------------------------------
1089
1090void GLState::pick_push_name (size_t _idx)
1091{
1092 colorStack_.pushIndex (_idx);
1093}
1094
1095//-----------------------------------------------------------------------------
1096
1098{
1099 colorStack_.popIndex ();
1100}
1101
1102//-----------------------------------------------------------------------------
1103
1104std::vector<size_t> GLState::pick_color_to_stack (Vec4uc _rgba) const
1105{
1106 if (colorPicking_ && colorStack_.initialized ())
1107 return colorStack_.colorToStack (_rgba);
1108 return std::vector<size_t> ();
1109}
1110
1111//-----------------------------------------------------------------------------
1112
1114{
1115 if (colorPicking_ && colorStack_.initialized ())
1116 return colorStack_.freeIndicies ();
1117 return -1;
1118}
1119
1120//-----------------------------------------------------------------------------
1121
1123{
1124 if (colorPicking_)
1125 return colorStack_.error ();
1126 return false;
1127}
1128
1129//-----------------------------------------------------------------------------
1130
1132{
1133 if (colorPicking_)
1134 return colorStack_.currentIndex ();
1135 else
1136 return 0;
1137}
1138
1139//-----------------------------------------------------------------------------
1140
1142{
1143 return colorPicking_;
1144}
1145
1146//-----------------------------------------------------------------------------
1147
1148GLenum GLState::glStateCaps[] = {GL_ALPHA_TEST,
1149GL_AUTO_NORMAL,
1150GL_MAP2_VERTEX_3,
1151GL_MAP2_VERTEX_4,
1152GL_BLEND,
1153GL_CLIP_PLANE0,
1154GL_CLIP_PLANE1,
1155GL_CLIP_PLANE2,
1156GL_CLIP_PLANE3,
1157GL_CLIP_PLANE4,
1158GL_CLIP_PLANE5,
1159GL_COLOR_LOGIC_OP,
1160GL_COLOR_MATERIAL,
1161GL_COLOR_SUM,
1162GL_COLOR_TABLE,
1163GL_CONVOLUTION_1D,
1164GL_CONVOLUTION_2D,
1165GL_CULL_FACE,
1166GL_DEPTH_TEST,
1167GL_DITHER,
1168GL_FOG,
1169GL_HISTOGRAM,
1170GL_INDEX_LOGIC_OP,
1171GL_LIGHT0,
1172GL_LIGHT1,
1173GL_LIGHT2,
1174GL_LIGHT3,
1175GL_LIGHT4,
1176GL_LIGHT5,
1177GL_LIGHT6,
1178GL_LIGHT7,
1179GL_LIGHTING,
1180GL_LINE_SMOOTH,
1181GL_LINE_STIPPLE,
1182GL_MAP1_COLOR_4,
1183GL_MAP1_INDEX,
1184GL_MAP1_NORMAL,
1185GL_MAP1_TEXTURE_COORD_1,
1186GL_MAP1_TEXTURE_COORD_2,
1187GL_MAP1_TEXTURE_COORD_3,
1188GL_MAP1_TEXTURE_COORD_4,
1189GL_MAP1_VERTEX_3,
1190GL_MAP1_VERTEX_4,
1191GL_MAP2_COLOR_4,
1192GL_MAP2_INDEX,
1193GL_MAP2_NORMAL,
1194GL_MAP2_TEXTURE_COORD_1,
1195GL_MAP2_TEXTURE_COORD_2,
1196GL_MAP2_TEXTURE_COORD_3,
1197GL_MAP2_TEXTURE_COORD_4,
1198GL_MAP2_VERTEX_3,
1199GL_MAP2_VERTEX_4,
1200GL_MINMAX,
1201GL_MULTISAMPLE,
1202GL_NORMALIZE,
1203GL_RESCALE_NORMAL,
1204GL_POINT_SMOOTH,
1205GL_POINT_SPRITE,
1206GL_POLYGON_OFFSET_FILL,
1207GL_FILL,
1208GL_POLYGON_OFFSET_LINE,
1209GL_LINE,
1210GL_POLYGON_OFFSET_POINT,
1211GL_POINT,
1212GL_POLYGON_SMOOTH,
1213GL_POLYGON_STIPPLE,
1214GL_POST_COLOR_MATRIX_COLOR_TABLE,
1215GL_POST_CONVOLUTION_COLOR_TABLE,
1216GL_RESCALE_NORMAL,
1217GL_NORMALIZE,
1218GL_SAMPLE_ALPHA_TO_COVERAGE,
1219GL_SAMPLE_ALPHA_TO_ONE,
1220GL_SAMPLE_COVERAGE,
1221GL_SAMPLE_COVERAGE_INVERT,
1222GL_SEPARABLE_2D,
1223GL_SCISSOR_TEST,
1224GL_STENCIL_TEST,
1225GL_TEXTURE_1D,
1226GL_TEXTURE_2D,
1227GL_TEXTURE_3D,
1228GL_TEXTURE_CUBE_MAP,
1229GL_TEXTURE_GEN_Q,
1230GL_TEXTURE_GEN_R,
1231GL_TEXTURE_GEN_S,
1232GL_TEXTURE_GEN_T,
1233GL_VERTEX_PROGRAM_POINT_SIZE,
1234GL_VERTEX_PROGRAM_TWO_SIDE,
1235GL_COLOR_ARRAY,
1236GL_EDGE_FLAG_ARRAY,
1237GL_FOG_COORD_ARRAY,
1238GL_INDEX_ARRAY,
1239GL_NORMAL_ARRAY,
1240GL_SECONDARY_COLOR_ARRAY,
1241GL_TEXTURE_COORD_ARRAY,
1242GL_VERTEX_ARRAY};
1243
1245{
1246 // get enabled states
1247 GLenum caps[] = {GL_ALPHA_TEST,
1248 GL_AUTO_NORMAL,
1249 GL_MAP2_VERTEX_3,
1250 GL_MAP2_VERTEX_4,
1251 GL_BLEND,
1252 GL_CLIP_PLANE0,
1253 GL_CLIP_PLANE1,
1254 GL_CLIP_PLANE2,
1255 GL_CLIP_PLANE3,
1256 GL_CLIP_PLANE4,
1257 GL_CLIP_PLANE5,
1258 GL_COLOR_LOGIC_OP,
1259 GL_COLOR_MATERIAL,
1260 GL_COLOR_SUM,
1261 GL_COLOR_TABLE,
1262 GL_CONVOLUTION_1D,
1263 GL_CONVOLUTION_2D,
1264 GL_CULL_FACE,
1265 GL_DEPTH_TEST,
1266 GL_DITHER,
1267 GL_FOG,
1268 GL_HISTOGRAM,
1269 GL_INDEX_LOGIC_OP,
1270 GL_LIGHT0,
1271 GL_LIGHT1,
1272 GL_LIGHT2,
1273 GL_LIGHT3,
1274 GL_LIGHT4,
1275 GL_LIGHT5,
1276 GL_LIGHT6,
1277 GL_LIGHT7,
1278 GL_LIGHTING,
1279 GL_LINE_SMOOTH,
1280 GL_LINE_STIPPLE,
1281 GL_MAP1_COLOR_4,
1282 GL_MAP1_INDEX,
1283 GL_MAP1_NORMAL,
1284 GL_MAP1_TEXTURE_COORD_1,
1285 GL_MAP1_TEXTURE_COORD_2,
1286 GL_MAP1_TEXTURE_COORD_3,
1287 GL_MAP1_TEXTURE_COORD_4,
1288 GL_MAP1_VERTEX_3,
1289 GL_MAP1_VERTEX_4,
1290 GL_MAP2_COLOR_4,
1291 GL_MAP2_INDEX,
1292 GL_MAP2_NORMAL,
1293 GL_MAP2_TEXTURE_COORD_1,
1294 GL_MAP2_TEXTURE_COORD_2,
1295 GL_MAP2_TEXTURE_COORD_3,
1296 GL_MAP2_TEXTURE_COORD_4,
1297 GL_MAP2_VERTEX_3,
1298 GL_MAP2_VERTEX_4,
1299 GL_MINMAX,
1300 GL_MULTISAMPLE,
1301 GL_NORMALIZE,
1302 GL_RESCALE_NORMAL,
1303 GL_POINT_SMOOTH,
1304 GL_POINT_SPRITE,
1305 GL_POLYGON_OFFSET_FILL,
1306 GL_POLYGON_OFFSET_LINE,
1307 GL_POLYGON_OFFSET_POINT,
1308 GL_POLYGON_SMOOTH,
1309 GL_POLYGON_STIPPLE,
1310 GL_POST_COLOR_MATRIX_COLOR_TABLE,
1311 GL_POST_CONVOLUTION_COLOR_TABLE,
1312 GL_RESCALE_NORMAL,
1313 GL_NORMALIZE,
1314 GL_SAMPLE_ALPHA_TO_COVERAGE,
1315 GL_SAMPLE_ALPHA_TO_ONE,
1316 GL_SAMPLE_COVERAGE,
1317 GL_SEPARABLE_2D,
1318 GL_SCISSOR_TEST,
1319 GL_STENCIL_TEST,
1320 GL_TEXTURE_1D,
1321 GL_TEXTURE_2D,
1322 GL_TEXTURE_3D,
1323 GL_TEXTURE_CUBE_MAP,
1324 GL_TEXTURE_GEN_Q,
1325 GL_TEXTURE_GEN_R,
1326 GL_TEXTURE_GEN_S,
1327 GL_TEXTURE_GEN_T,
1328 GL_VERTEX_PROGRAM_POINT_SIZE,
1329 GL_VERTEX_PROGRAM_TWO_SIDE,
1330 GL_COLOR_ARRAY,
1331 GL_EDGE_FLAG_ARRAY,
1332 GL_FOG_COORD_ARRAY,
1333 GL_INDEX_ARRAY,
1334 GL_NORMAL_ARRAY,
1335 GL_SECONDARY_COLOR_ARRAY,
1336 GL_TEXTURE_COORD_ARRAY,
1337 GL_VERTEX_ARRAY};
1338
1339 for (unsigned int i = 0; i < sizeof(caps) / sizeof(GLenum); ++i)
1340 {
1341 if (glIsEnabled(caps[i])) stateStack_.back().glStateEnabled_.set(caps[i]);
1342 else stateStack_.back().glStateEnabled_.reset(caps[i]);
1343 }
1344
1345 GLint getparam;
1346
1347#ifdef GL_VERSION_1_4
1348 glGetIntegerv(GL_BLEND_SRC_RGB, &getparam);
1349 stateStack_.back().blendFuncState_[0] = getparam;
1350
1351 glGetIntegerv(GL_BLEND_DST_ALPHA, &getparam);
1352 stateStack_.back().blendFuncState_[1] = getparam;
1353
1354 glGetIntegerv(GL_BLEND_SRC_ALPHA, &getparam);
1355 stateStack_.back().blendFuncState_[2] = getparam;
1356
1357 glGetIntegerv(GL_BLEND_DST_ALPHA, &getparam);
1358 stateStack_.back().blendFuncState_[3] = getparam;
1359#else
1360 glGetIntegerv(GL_BLEND_SRC, &getparam);
1361 stateStack_.back().blendFuncState_[0] = getparam;
1362
1363 glGetIntegerv(GL_BLEND_DST, &getparam);
1364 stateStack_.back().blendFuncState_[1] = getparam;
1365#endif
1366
1367
1368 glGetIntegerv(GL_BLEND_EQUATION_RGB, &getparam);
1369 stateStack_.back().blendEquationState_ = getparam;
1370
1371 glGetFloatv(GL_BLEND_COLOR, stateStack_.back().blendColorState_);
1372
1373 glGetIntegerv(GL_ALPHA_TEST_FUNC, &getparam);
1374 stateStack_.back().alphaFuncState_ = getparam;
1375
1376 glGetFloatv(GL_ALPHA_TEST_REF, &stateStack_.back().alphaRefState_);
1377
1378 glGetIntegerv(GL_DEPTH_FUNC, &getparam);
1379 stateStack_.back().depthFunc_ = getparam;
1380
1381 glGetDoublev(GL_DEPTH_RANGE, stateStack_.back().depthRange_);
1382
1383 // bound buffers
1384
1385 GLenum bufGets[8] = {
1386 GL_ARRAY_BUFFER_BINDING, GL_ARRAY_BUFFER,
1387 GL_ELEMENT_ARRAY_BUFFER_BINDING, GL_ELEMENT_ARRAY_BUFFER,
1388 GL_PIXEL_PACK_BUFFER_BINDING, GL_PIXEL_PACK_BUFFER,
1389 GL_PIXEL_UNPACK_BUFFER_BINDING, GL_PIXEL_UNPACK_BUFFER};
1390
1391 for (int i = 0; i < 4; ++i)
1392 glGetIntegerv(bufGets[i*2], (GLint*)stateStack_.back().glBufferTargetState_ + getBufferTargetIndex(bufGets[i*2+1]));
1393
1394
1395 // bound textures
1396 glGetIntegerv(GL_ACTIVE_TEXTURE, &getparam);
1397 stateStack_.back().activeTexture_ = getparam;
1398
1399 GLenum texBufGets[] = {
1400 GL_TEXTURE_BINDING_1D, GL_TEXTURE_1D,
1401 GL_TEXTURE_BINDING_2D, GL_TEXTURE_2D,
1402 GL_TEXTURE_BINDING_3D, GL_TEXTURE_3D,
1403 GL_TEXTURE_BINDING_CUBE_MAP, GL_TEXTURE_CUBE_MAP
1404 , GL_TEXTURE_BINDING_RECTANGLE_ARB, GL_TEXTURE_RECTANGLE_ARB
1405 };
1406
1407 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &maxTextureCoords_);
1408 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxCombinedTextureImageUnits_);
1409
1410 // safe clamp
1411 if (maxTextureCoords_ > 16) maxTextureCoords_ = 16;
1412 if (maxCombinedTextureImageUnits_ > 16) maxCombinedTextureImageUnits_ = 16;
1413
1414 int numTexUnits = maxTextureCoords_;
1415 if (numTexUnits < maxCombinedTextureImageUnits_) numTexUnits = maxCombinedTextureImageUnits_;
1416
1417 for (int i = 0; i < numTexUnits; ++i)
1418 {
1419 glActiveTexture(GL_TEXTURE0 + i);
1420
1421 getparam = 0;
1422 // for each texture stage query 5 texture types: 1D, 2D, 3D, Cube, Rect
1423 for (int k = 0; k < 5 && !getparam; ++k)
1424 {
1425 glGetIntegerv(texBufGets[k*2], &getparam);
1426 if (getparam)
1427 {
1428 stateStack_.back().glTextureStage_[i].buf_ = getparam;
1429 stateStack_.back().glTextureStage_[i].target_ = texBufGets[k*2+1];
1430 }
1431 }
1432 }
1433
1434 // restore active texture unit
1435 if (numTexUnits > 0)
1436 glActiveTexture(stateStack_.back().activeTexture_);
1437
1438
1439 // shade model
1440 glGetIntegerv(GL_SHADE_MODEL, &getparam);
1441 stateStack_.back().shadeModel_ = getparam;
1442
1443 // cull face
1444 glGetIntegerv(GL_CULL_FACE_MODE, &getparam);
1445 stateStack_.back().cullFace_ = getparam;
1446
1447
1448 // vertex pointers
1449 {
1450 GLenum ptrEnums[] = {
1451 GL_VERTEX_ARRAY_SIZE, GL_VERTEX_ARRAY_TYPE,
1452 GL_VERTEX_ARRAY_STRIDE, GL_VERTEX_ARRAY_POINTER,
1453 GL_COLOR_ARRAY_SIZE, GL_COLOR_ARRAY_TYPE,
1454 GL_COLOR_ARRAY_STRIDE, GL_COLOR_ARRAY_POINTER,
1455 GL_TEXTURE_COORD_ARRAY_SIZE, GL_TEXTURE_COORD_ARRAY_TYPE,
1456 GL_TEXTURE_COORD_ARRAY_STRIDE, GL_TEXTURE_COORD_ARRAY_POINTER};
1457
1458 GLStateContext::GLVertexPointer* ptrs[] = {&stateStack_.back().vertexPointer_,
1459 &stateStack_.back().colorPointer_, &stateStack_.back().texcoordPointer_};
1460
1461 for (int i = 0; i < 3 ; ++i)
1462 {
1463 glGetIntegerv(ptrEnums[i*4], &getparam);
1464 ptrs[i]->size = getparam;
1465 glGetIntegerv(ptrEnums[i*4+1], &getparam);
1466 ptrs[i]->type = getparam;
1467 glGetIntegerv(ptrEnums[i*4+2], &getparam);
1468 ptrs[i]->stride = getparam;
1469 glGetPointerv(ptrEnums[i*4+3], (GLvoid**)&ptrs[i]->pointer);
1470 }
1471
1472 glGetIntegerv(GL_NORMAL_ARRAY_STRIDE, &getparam);
1473 stateStack_.back().normalPointer_.size = getparam;
1474 glGetIntegerv(GL_NORMAL_ARRAY_TYPE, &getparam);
1475 stateStack_.back().normalPointer_.type = getparam;
1476 glGetPointerv(GL_NORMAL_ARRAY_POINTER, (GLvoid**)&stateStack_.back().normalPointer_.pointer);
1477 }
1478
1479
1480 // draw buffer state
1481 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers_);
1482 if (maxDrawBuffers_ > 16) maxDrawBuffers_ = 16;
1483
1484 for (int i = 0; i < maxDrawBuffers_; ++i)
1485 {
1486 glGetIntegerv(GL_DRAW_BUFFER0 + i, &getparam);
1487 stateStack_.back().drawBufferState_[i] = getparam;
1488 }
1489
1490 glGetIntegerv(GL_DRAW_BUFFER, &getparam);
1491 stateStack_.back().drawBufferSingle_ = getparam;
1492
1493 // framebuffer
1494 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &getparam);
1495 stateStack_.back().framebuffers_[0] = getparam;
1496 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &getparam);
1497 stateStack_.back().framebuffers_[1] = getparam;
1498
1499 // shader program
1500 glGetIntegerv(GL_CURRENT_PROGRAM, &getparam);
1501 stateStack_.back().program_ = getparam;
1502}
1503
1504//-----------------------------------------------------------------------------
1505
1506
1507void GLState::enable(GLenum _cap, bool _warn)
1508{
1509 if (!glStateLock_.test(_cap))
1510 {
1511#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1512 if (!stateStack_.back().glStateEnabled_.test(_cap))
1513#endif
1514 {
1515 if (ACG::compatibilityProfile() || removedEnums.find(_cap) == removedEnums.end())
1516 glEnable(_cap);
1517 else
1518 {
1519 if(_warn)
1520 std::cerr << "OpenGL Warning:You are trying to use a GLenum that has been removed for OpenGL core profiles. ( Enum Nuber is: " << _cap << ")" << std::endl;
1521 }
1522 stateStack_.back().glStateEnabled_.set(_cap);
1523 }
1524 }
1525}
1526
1527void GLState::disable(GLenum _cap, bool _warn)
1528{
1529 if (!glStateLock_.test(_cap))
1530 {
1531#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1532 if (stateStack_.back().glStateEnabled_.test(_cap))
1533#endif
1534 {
1535 if (ACG::compatibilityProfile() || removedEnums.find(_cap) == removedEnums.end())
1536 glDisable(_cap);
1537 else
1538 {
1539 if (_warn)
1540 std::cerr << "OpenGL Warning:You are trying to use a GLenum that has been removed for OpenGL core profiles." << std::endl;
1541 }
1542 stateStack_.back().glStateEnabled_.reset(_cap);
1543 }
1544 }
1545}
1546
1547void GLState::lockState(GLenum _cap)
1548{
1549 glStateLock_.set(_cap);
1550}
1551
1552void GLState::unlockState(GLenum _cap)
1553{
1554 glStateLock_.reset(_cap);
1555}
1556
1557bool GLState::isStateLocked(GLenum _cap)
1558{
1559 return glStateLock_.test(_cap);
1560}
1561
1563{
1564 return stateStack_.back().glStateEnabled_.test(_cap);
1565}
1566
1567//-----------------------------------------------------------------------------
1568// client state functions
1569
1571{
1572 if (!glStateLock_.test(_cap))
1573 {
1574#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1575 if (!stateStack_.back().glStateEnabled_.test(_cap))
1576#endif
1577 {
1578 glEnableClientState(_cap);
1579 stateStack_.back().glStateEnabled_.set(_cap);
1580 }
1581 }
1582}
1583
1585{
1586 if (!glStateLock_.test(_cap))
1587 {
1588#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1589 if (stateStack_.back().glStateEnabled_.test(_cap))
1590#endif
1591 {
1592 glDisableClientState(_cap);
1593 stateStack_.back().glStateEnabled_.reset(_cap);
1594 }
1595 }
1596}
1597
1599{
1600 glStateLock_.set(_cap);
1601}
1602
1604{
1605 glStateLock_.reset(_cap);
1606}
1607
1609{
1610 return glStateLock_.test(_cap);
1611}
1612
1614{
1615 return stateStack_.back().glStateEnabled_.test(_cap);
1616}
1617
1618//-----------------------------------------------------------------------------
1619// blending functions
1620
1621void GLState::blendFuncSeparate(GLenum _srcRGB, GLenum _dstRGB, GLenum _srcAlpha, GLenum _dstAlpha)
1622{
1623 // fix parameters according to lock status
1624 if (blendFuncSeparateLock_[0])
1625 {
1626 _srcRGB = stateStack_.back().blendFuncState_[0];
1627 _dstRGB = stateStack_.back().blendFuncState_[1];
1628 }
1629
1630 if (blendFuncSeparateLock_[1])
1631 {
1632 _srcAlpha = stateStack_.back().blendFuncState_[2];
1633 _dstAlpha = stateStack_.back().blendFuncState_[3];
1634 }
1635
1636 if (!blendFuncSeparateLock_[0] || !blendFuncSeparateLock_[1])
1637 {
1638#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1639 if (stateStack_.back().blendFuncState_[0] != _srcRGB || stateStack_.back().blendFuncState_[1] != _dstRGB ||
1640 stateStack_.back().blendFuncState_[2] != _srcAlpha || stateStack_.back().blendFuncState_[3] != _dstAlpha)
1641#endif
1642 {
1643#ifdef GL_VERSION_1_4
1644 // check if glew has loaded glBlendFuncSeparate already
1645 if (glBlendFuncSeparate)
1646 glBlendFuncSeparate(_srcRGB, _dstRGB, _srcAlpha, _dstAlpha);
1647 else
1648 glBlendFunc(_srcRGB, _dstRGB);
1649 stateStack_.back().blendFuncState_[0] = _srcRGB;
1650 stateStack_.back().blendFuncState_[1] = _dstRGB;
1651 stateStack_.back().blendFuncState_[2] = _srcAlpha;
1652 stateStack_.back().blendFuncState_[3] = _dstAlpha;
1653#else
1654 glBlendFunc(_srcRGB, _dstRGB);
1655 stateStack_.back().blendFuncState_[0] = _srcRGB;
1656 stateStack_.back().blendFuncState_[1] = _dstRGB;
1657 stateStack_.back().blendFuncState_[2] = _srcRGB;
1658 stateStack_.back().blendFuncState_[3] = _dstRGB;
1659#endif
1660 }
1661 }
1662}
1663
1664void GLState::getBlendFuncSeparate(GLenum* _srcRGB, GLenum* _dstRGB, GLenum* _srcAlpha, GLenum* _dstAlpha)
1665{
1666 if (_srcRGB) *_srcRGB = stateStack_.back().blendFuncState_[0];
1667 if (_dstRGB) *_dstRGB = stateStack_.back().blendFuncState_[1];
1668 if (_srcAlpha) *_srcAlpha = stateStack_.back().blendFuncState_[2];
1669 if (_dstAlpha) *_dstAlpha = stateStack_.back().blendFuncState_[3];
1670}
1671
1672void GLState::blendEquation(GLenum _mode)
1673{
1674 if (!blendEquationLock_)
1675 {
1676#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1677 if (stateStack_.back().blendEquationState_ != _mode)
1678#endif
1679 {
1680 glBlendEquation(_mode);
1681 stateStack_.back().blendEquationState_ = _mode;
1682 }
1683 }
1684}
1685
1686void GLState::blendColor(GLclampf _red, GLclampf _green, GLclampf _blue, GLclampf _alpha)
1687{
1688 if (!blendColorLock_)
1689 {
1690#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1691 if (stateStack_.back().blendColorState_[0] != _red || stateStack_.back().blendColorState_[1] != _green ||
1692 stateStack_.back().blendColorState_[2] != _blue || stateStack_.back().blendColorState_[3] != _alpha)
1693#endif
1694 {
1695 glBlendColor(_red, _green, _blue, _alpha);
1696 stateStack_.back().blendColorState_[0] = _red; stateStack_.back().blendColorState_[1] = _green;
1697 stateStack_.back().blendColorState_[2] = _blue; stateStack_.back().blendColorState_[3] = _alpha;
1698 }
1699 }
1700}
1701
1702void GLState::getBlendColor(GLclampf* _col)
1703{
1704 for (int i = 0; i < 4; ++i) _col[i] = stateStack_.back().blendColorState_[i];
1705}
1706
1707
1708void GLState::alphaFunc(GLenum _func, GLclampf _ref)
1709{
1710 if (!alphaFuncLock_)
1711 {
1712#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1713 if (stateStack_.back().alphaFuncState_ != _func || stateStack_.back().alphaRefState_ != _ref)
1714#endif
1715 {
1716 glAlphaFunc(_func, _ref);
1717 stateStack_.back().alphaFuncState_ = _func;
1718 stateStack_.back().alphaRefState_ = _ref;
1719 }
1720 }
1721}
1722
1723void GLState::getAlphaFunc(GLenum* _func, GLclampf* _ref)
1724{
1725 if (_func) *_func = stateStack_.back().alphaFuncState_;
1726 if (_ref) *_ref = stateStack_.back().alphaRefState_;
1727}
1728
1729void GLState::shadeModel(GLenum _mode)
1730{
1731 if (!shadeModelLock_)
1732 {
1733#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1734 if (stateStack_.back().shadeModel_ != _mode)
1735#endif
1736 {
1737 glShadeModel(_mode);
1738 stateStack_.back().shadeModel_ = _mode;
1739 }
1740 }
1741}
1742
1743void GLState::cullFace(GLenum _mode)
1744{
1745 if (!cullFaceLock_)
1746 {
1747#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1748 if (stateStack_.back().cullFace_ != _mode)
1749#endif
1750 {
1751 glCullFace(_mode);
1752 stateStack_.back().cullFace_ = _mode;
1753 }
1754 }
1755}
1756
1757void GLState::depthRange(GLclampd _zNear, GLclampd _zFar)
1758{
1759 if (!depthRangeLock_)
1760 {
1761 #ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1762 if (abs(_zNear - stateStack_.back().depthRange_[0]) > 1e-6 ||
1763 abs(_zFar - stateStack_.back().depthRange_[1]) > 1e-6)
1764 #endif
1765 {
1766 glDepthRange(_zNear, _zFar);
1767 stateStack_.back().depthRange_[0] = _zNear;
1768 stateStack_.back().depthRange_[1] = _zFar;
1769 }
1770 }
1771}
1772
1773void GLState::getDepthRange(GLclampd* _zNearOut, GLclampd* _zFarOut)
1774{
1775 if (_zNearOut) *_zNearOut = stateStack_.back().depthRange_[0];
1776 if (_zFarOut) *_zFarOut = stateStack_.back().depthRange_[1];
1777}
1778
1779//-----------------------------------------------------------------------------
1780
1782{
1783 switch (_target)
1784 {
1785 case GL_ARRAY_BUFFER: return 0;
1786 case GL_ELEMENT_ARRAY_BUFFER: return 1;
1787 case GL_PIXEL_PACK_BUFFER: return 2;
1788 case GL_PIXEL_UNPACK_BUFFER: return 3;
1789#ifdef GL_ARB_uniform_buffer_object
1790 case GL_UNIFORM_BUFFER: return 4;
1791#endif
1792#ifdef GL_ARB_shader_storage_buffer_object
1793 case GL_SHADER_STORAGE_BUFFER: return 5;
1794#endif
1795#ifdef GL_ARB_shader_atomic_counters
1796 case GL_ATOMIC_COUNTER_BUFFER: return 6;
1797#endif
1798#ifdef GL_ARB_copy_buffer
1799 case GL_COPY_READ_BUFFER: return 7;
1800 case GL_COPY_WRITE_BUFFER: return 8;
1801#endif
1802#ifdef GL_ARB_compute_shader
1803 case GL_DISPATCH_INDIRECT_BUFFER: return 9;
1804#endif
1805#ifdef GL_ARB_draw_indirect
1806 case GL_DRAW_INDIRECT_BUFFER: return 10;
1807#endif
1808#ifdef GL_ARB_query_buffer_object
1809 case GL_QUERY_BUFFER: return 11;
1810#endif
1811 case GL_TEXTURE_BUFFER: return 12;
1812#ifdef GL_VERSION_3_0
1813 case GL_TRANSFORM_FEEDBACK_BUFFER: return 13;
1814#endif
1815 }
1816 std::cerr << "error : GLState::bindBuffer - unknown buffer target type" << _target << std::endl;
1817 return -1;
1818}
1819
1820void GLState::bindBuffer(GLenum _target, GLuint _buffer)
1821{
1822 int idx = getBufferTargetIndex(_target);
1823 if (idx >= 0 && !glBufferTargetLock_[idx])
1824 {
1825#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1826 if (stateStack_.back().glBufferTargetState_[idx] != _buffer)
1827#endif
1828 {
1829 glBindBuffer(_target, _buffer);
1830 stateStack_.back().glBufferTargetState_[idx] = _buffer;
1831 }
1832 }
1833}
1834
1835void GLState::lockBufferTarget(GLenum _target)
1836{
1837 glBufferTargetLock_[getBufferTargetIndex(_target)] = 1;
1838}
1839
1841{
1842 glBufferTargetLock_[getBufferTargetIndex(_target)] = 0;
1843}
1844
1846{
1847 return glBufferTargetLock_[getBufferTargetIndex(_target)] != 0;
1848}
1849
1850GLuint GLState::getBoundBuf(GLenum _target)
1851{
1852 return stateStack_.back().glBufferTargetState_[getBufferTargetIndex(_target)];
1853}
1854
1855
1856GLenum GLState::getShaderType(GLuint _shader)
1857{
1858 auto isShader = glIsShader(_shader);
1859 if(isShader == GL_FALSE) { return GL_NONE; }
1860 GLint type = GL_NONE;
1861 glGetShaderiv(_shader, GL_SHADER_TYPE, &type);
1862 return static_cast<GLenum>(type);
1863}
1864
1865GLuint GLState::getAttachedShader(GLuint _program, GLenum _type)
1866{
1867 if(glIsProgram(_program) == GL_FALSE) { return GL_NONE; }
1868 // vertex, fragment, tess control, tess eval, geometry
1869 static const GLsizei maxNumShaders = 5;
1870 GLuint shaders[maxNumShaders] = {};
1871 GLsizei numShaders = 0;
1872 glGetAttachedShaders(_program, maxNumShaders, &numShaders, shaders);
1873 for(auto i = 0; i < numShaders; ++i) {
1874 const auto& shader = shaders[i];
1875 if(getShaderType(shader) == _type) {
1876 return shader;
1877 }
1878 }
1879 return GL_NONE;
1880}
1881
1883{
1884 if(glIsShader(_shader) == GL_FALSE) { return 0; }
1885 GLint length = 0;
1886 glGetShaderiv(_shader, GL_SHADER_SOURCE_LENGTH, &length);
1887 return static_cast<size_t>(length);
1888}
1889
1890size_t GLState::getShaderSource(GLuint _shader, char *_buffer, size_t _bufferSize)
1891{
1892 if(glIsShader(_shader) == GL_FALSE) { return 0; }
1893 GLsizei length = 0;
1894 glGetShaderSource(_shader, static_cast<GLsizei>(_bufferSize), &length, _buffer);
1895 return static_cast<size_t>(length);
1896}
1897
1898//-----------------------------------------------------------------------------
1899
1900void GLState::activeTexture(GLenum _texunit)
1901{
1902#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1903 if (stateStack_.back().activeTexture_ != _texunit)
1904#endif
1905 {
1906 glActiveTexture(_texunit);
1907 stateStack_.back().activeTexture_ = _texunit;
1908 }
1909}
1910
1911void GLState::bindTexture(GLenum _target, GLuint _buffer)
1912{
1913 int activeTex = getActiveTextureIndex();
1914
1915 assert(activeTex >= 0);
1916
1917 GLStateContext::TextureStage* stage = stateStack_.back().glTextureStage_ + activeTex;
1918
1919 if (!glTextureStageLock_[activeTex])
1920 {
1921#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1922 if (_buffer != stage->buf_ || _target != stage->target_)
1923#endif
1924 {
1925 glBindTexture(_target, _buffer);
1926
1927 stage->target_ = _target;
1928 stage->buf_ = _buffer;
1929 }
1930 }
1931}
1932
1934{
1935 glTextureStageLock_[getActiveTextureIndex()] = 1;
1936}
1937
1939{
1940 glTextureStageLock_[getActiveTextureIndex()] = 0;
1941}
1942
1944{
1945 return glTextureStageLock_[getActiveTextureIndex()] != 0;
1946}
1947
1949{
1950 return stateStack_.back().glTextureStage_[getActiveTextureIndex()].buf_;
1951}
1952
1954{
1955 return stateStack_.back().glTextureStage_[getActiveTextureIndex()].target_;
1956}
1957
1958//----------------------------------------------------------
1959// vertex pointers
1960
1961void GLState::vertexPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid* _pointer)
1962{
1963 if (!vertexPointerLock_)
1964 {
1965#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1966 if (!stateStack_.back().vertexPointer_.equals(_size, _type, _stride, _pointer))
1967#endif
1968 {
1969 glVertexPointer(_size, _type, _stride, _pointer);
1970 stateStack_.back().vertexPointer_.set(_size, _type, _stride, _pointer);
1971 }
1972 }
1973}
1974
1975void GLState::getVertexPointer(GLint* _size, GLenum* _type, GLsizei* _stride, const GLvoid** _pointer)
1976{
1977 if (_size) *_size = stateStack_.back().vertexPointer_.size;
1978 if (_stride) *_stride = stateStack_.back().vertexPointer_.stride;
1979 if (_type) *_type = stateStack_.back().vertexPointer_.type;
1980 if (_pointer) *_pointer = stateStack_.back().vertexPointer_.pointer;
1981}
1982
1983void GLState::normalPointer(GLenum _type, GLsizei _stride, const GLvoid* _pointer)
1984{
1985 if (!normalPointerLock_)
1986 {
1987#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
1988 if (!stateStack_.back().normalPointer_.equals(stateStack_.back().normalPointer_.size, _type, _stride, _pointer))
1989#endif
1990 {
1991 glNormalPointer(_type, _stride, _pointer);
1992 stateStack_.back().normalPointer_.set(3, _type, _stride, _pointer);
1993 }
1994 }
1995}
1996
1997void GLState::getNormalPointer(GLenum* _type, GLsizei* _stride, const GLvoid** _pointer)
1998{
1999 if (_type) *_type = stateStack_.back().normalPointer_.type;
2000 if (_stride) *_stride = stateStack_.back().normalPointer_.stride;
2001 if (_pointer) *_pointer = stateStack_.back().normalPointer_.pointer;
2002}
2003
2004
2005void GLState::colorPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid* _pointer)
2006{
2007 if (!colorPointerLock_)
2008 {
2009#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
2010 if (!stateStack_.back().colorPointer_.equals(_size, _type, _stride, _pointer))
2011#endif
2012 {
2013 glColorPointer(_size, _type, _stride, _pointer);
2014 stateStack_.back().colorPointer_.set(_size, _type, _stride, _pointer);
2015 }
2016 }
2017}
2018
2019void GLState::getColorPointer(GLint* _size, GLenum* _type, GLsizei* _stride, const GLvoid** _pointer)
2020{
2021 if (_size) *_size = stateStack_.back().colorPointer_.size;
2022 if (_stride) *_stride = stateStack_.back().colorPointer_.stride;
2023 if (_type) *_type = stateStack_.back().colorPointer_.type;
2024 if (_pointer) *_pointer = stateStack_.back().colorPointer_.pointer;
2025}
2026
2027void GLState::texcoordPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid* _pointer)
2028{
2029 if (!texcoordPointerLock_)
2030 {
2031#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
2032 if (!stateStack_.back().texcoordPointer_.equals(_size, _type, _stride, _pointer))
2033#endif
2034 {
2035 glTexCoordPointer(_size, _type, _stride, _pointer);
2036 stateStack_.back().texcoordPointer_.set(_size, _type, _stride, _pointer);
2037 }
2038 }
2039}
2040
2041void GLState::getTexcoordPointer(GLint* _size, GLenum* _type, GLsizei* _stride, const GLvoid** _pointer)
2042{
2043 if (_size) *_size = stateStack_.back().texcoordPointer_.size;
2044 if (_stride) *_stride = stateStack_.back().texcoordPointer_.stride;
2045 if (_type) *_type = stateStack_.back().texcoordPointer_.type;
2046 if (_pointer) *_pointer = stateStack_.back().texcoordPointer_.pointer;
2047}
2048
2049void GLState::setTexGenMode(GLenum _coord, GLenum _name, GLint _param)
2050{
2052 {
2053 glTexGeni(_coord, _name, _param);
2054 }
2055 else
2056 {
2057 stateStack_.back().texGenMode_ = _param;
2058 }
2059}
2060
2061void GLState::getTexGenMode(GLenum _coord, GLenum _name, GLint* _param)
2062{
2064 {
2065 glGetTexGeniv(_coord, _name, _param);
2066 }
2067 else
2068 {
2069 *_param = stateStack_.back().texGenMode_;
2070 }
2071}
2072
2073//---------------------------------------------------------------------
2074// draw buffer functions
2075
2076void GLState::drawBuffer(GLenum _mode)
2077{
2078 if (!drawBufferLock_)
2079 {
2080#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
2081 if (stateStack_.back().drawBufferSingle_ != _mode || stateStack_.back().activeDrawBuffer_)
2082#endif
2083 {
2084 glDrawBuffer(_mode);
2085 stateStack_.back().drawBufferSingle_ = _mode;
2086 stateStack_.back().activeDrawBuffer_ = 0;
2087 }
2088 }
2089}
2090
2091void GLState::drawBuffers(GLsizei _n, const GLenum* _bufs)
2092{
2093 if (!drawBufferLock_)
2094 {
2095#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
2096 int bChange = !stateStack_.back().activeDrawBuffer_;
2097 for (int i = 0; i < _n && (!bChange); ++i)
2098 {
2099 if (stateStack_.back().drawBufferState_[i] != _bufs[i])
2100 bChange = 1;
2101 }
2102
2103 if (bChange)
2104#endif
2105 {
2106 glDrawBuffers(_n, _bufs);
2107
2108 for (int i = 0; i < _n; ++i)
2109 stateStack_.back().drawBufferState_[i] = _bufs[i];
2110
2111 stateStack_.back().activeDrawBuffer_ = _n;
2112 }
2113 }
2114}
2115
2118{
2119 GLint curfbo;
2120 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, (GLint*)&curfbo);
2121 return curfbo;
2122}
2123
2126{
2127 GLint curfbo;
2128 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, (GLint*)&curfbo);
2129 return curfbo;
2130}
2131
2132void GLState::bindFramebuffer(GLenum _target, GLuint _framebuffer)
2133{
2134 int i = -1;
2135 switch (_target)
2136 {
2137 case GL_FRAMEBUFFER:
2138 {
2139 bindFramebuffer(GL_READ_FRAMEBUFFER, _framebuffer);
2140 bindFramebuffer(GL_DRAW_FRAMEBUFFER, _framebuffer);
2141 return;
2142 }
2143 case GL_DRAW_FRAMEBUFFER: i = 0; break;
2144 case GL_READ_FRAMEBUFFER: i = 1; break;
2145 }
2146
2147 if (i >= 0 && !framebufferLock_[i])
2148 {
2149#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
2150 if (stateStack_.back().framebuffers_[i] != _framebuffer)
2151#endif
2152 {
2153#ifndef __APPLE__
2154 if (glBindFramebuffer == nullptr)
2155 glewInit();
2156#endif
2157 glBindFramebuffer(_target, _framebuffer);
2158 stateStack_.back().framebuffers_[i] = _framebuffer;
2159 }
2160 }
2161}
2162
2163void GLState::lockFramebuffer(GLenum _target)
2164{
2165 switch (_target)
2166 {
2167 case GL_FRAMEBUFFER:
2168 framebufferLock_[0] = framebufferLock_[1] = true; break;
2169
2170 case GL_DRAW_FRAMEBUFFER: framebufferLock_[0] = true; break;
2171 case GL_READ_FRAMEBUFFER: framebufferLock_[1] = true; break;
2172 }
2173}
2174
2175void GLState::unlockFramebuffer(GLenum _target)
2176{
2177 switch (_target)
2178 {
2179 case GL_FRAMEBUFFER:
2180 framebufferLock_[0] = framebufferLock_[1] = false; break;
2181
2182 case GL_DRAW_FRAMEBUFFER: framebufferLock_[0] = false; break;
2183 case GL_READ_FRAMEBUFFER: framebufferLock_[1] = false; break;
2184 }
2185}
2186
2188{
2189 switch (_target)
2190 {
2191 case GL_FRAMEBUFFER:
2192 return framebufferLock_[0] && framebufferLock_[1];
2193
2194 case GL_DRAW_FRAMEBUFFER: return framebufferLock_[0];
2195 case GL_READ_FRAMEBUFFER: return framebufferLock_[1];
2196 }
2197 return false;
2198}
2199
2200void GLState::useProgram(GLuint _program)
2201{
2202 if (!programLock_)
2203 {
2204#ifdef GLSTATE_AVOID_REDUNDANT_GLCALLS
2205 if (stateStack_.back().program_ != _program)
2206#endif
2207 {
2208 glUseProgram(_program);
2209 stateStack_.back().program_ = _program;
2210 }
2211 }
2212}
2213
2214void GLState::genBuffersARB(GLsizei n, GLuint* buffers) {
2215 glGenBuffers(n, buffers);
2216}
2217
2218void GLState::genBuffers(GLsizei n, GLuint* buffers) {
2219 glGenBuffers(n, buffers);
2220}
2221
2223 GLenum target, GLsizeiptrARB size, const GLvoid *data, GLenum usage) {
2224 glBufferData(target, size, data, usage);
2225}
2226
2228 GLenum target, GLsizeiptr size, const GLvoid* data, GLenum usage) {
2229 glBufferData(target, size, data, usage);
2230}
2231
2232void GLState::deleteBuffers(GLsizei n, const GLuint* buffers) {
2233 glDeleteBuffers(n, buffers);
2234}
2235
2236GLvoid* GLState::mapBuffer (GLenum target, GLenum access) {
2237 return glMapBuffer(target, access);
2238}
2239
2240
2241GLboolean GLState::unmapBuffer (GLenum target) {
2242 return glUnmapBuffer(target);
2243}
2244
2245size_t GLState::getBufferSize(GLenum target) {
2246 GLint size = 0;
2247 glGetBufferParameteriv(target, GL_BUFFER_SIZE, &size);
2248 return static_cast<size_t>(size);
2249}
2250
2251//---------------------------------------------------------------------
2252
2253
2254
2255
2256//=============================================================================
2257} // namespace ACG
2258//=============================================================================
Vec4uc getIndexColor(size_t _idx)
gets the color instead of setting it directly
Definition: ColorStack.cc:123
bool setMaximumIndex(size_t _idx)
sets the maximum index number used in current node
Definition: ColorStack.cc:98
size_t freeIndicies() const
returns maximal available index count
Definition: ColorStack.cc:169
void initialize(ACG::GLState *)
init (takes current GL context/ like glInitNames (); glPushName (0))
Definition: ColorStack.cc:84
void popIndex()
pops the current node from the stack (like glPopName)
Definition: ColorStack.cc:147
bool initialized() const
has it been initialized?
Definition: ColorStack.hh:85
void pushIndex(size_t _idx)
creates a new node the stack (like glPushName)
Definition: ColorStack.cc:139
void setIndex(size_t _idx)
sets the current color the given index (like glLoadName)
Definition: ColorStack.cc:112
bool error() const
Did an error occur during picking.
Definition: ColorStack.hh:109
size_t currentIndex() const
returns the current color index
Definition: ColorStack.cc:181
std::vector< size_t > colorToStack(Vec4uc _rgba) const
converts the given color to index values on the stack
Definition: ColorStack.cc:155
void rotate(Scalar angle, Scalar x, Scalar y, Scalar z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
void scale(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with scaling matrix (x,y,z)
void frustum(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self with a perspective projection matrix
void inverse_ortho(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self from left with inverse orthographic projection matrix
void inverse_frustum(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self from left with inverse of perspective projection matrix
void lookAt(const Vec3 &eye, const Vec3 &center, const Vec3 &up)
void perspective(Scalar fovY, Scalar aspect, Scalar near_plane, Scalar far_plane)
multiply self with a perspective projection matrix
void ortho(Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near_plane, Scalar far_plane)
multiply self with orthographic projection matrix
void translate(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with translation matrix (x,y,z)
void inverse_lookAt(const Vec3 &eye, const Vec3 &center, const Vec3 &up)
multiply self from left with inverse lookAt matrix
void inverse_perspective(Scalar fovY, Scalar aspect, Scalar near_plane, Scalar far_plane)
multiply self from left with inverse of perspective projection matrix
void set_specular_color(const Vec4f &_col)
set specular color
Definition: GLState.cc:737
void ortho(double _left, double _right, double _bottom, double _top, double _near_plane, double _far_plane)
orthographic projection
Definition: GLState.cc:402
static void disableClientState(GLenum _cap)
replaces glDisableClientState, supports locking
Definition: GLState.cc:1584
static void colorPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glColorPointer, supports locking
Definition: GLState.cc:2005
void makeCurrent()
does nothing
Definition: GLState.hh:224
static void unlockState(GLenum _cap)
unlocks a specific cap state
Definition: GLState.cc:1552
Vec4f pick_get_name_color_norm(unsigned int _idx)
same as pick_get_name_color, but the resulting color channels are normalized in [0....
Definition: GLState.cc:1077
void set_ambient_color(const Vec4f &_col)
set ambient color
Definition: GLState.cc:707
void pop_modelview_matrix()
pop modelview matrix
Definition: GLState.cc:1026
static size_t getShaderSourceLength(GLuint _shader)
Definition: GLState.cc:1882
void set_depthFunc(const GLenum &_depth_func)
Call glDepthFunc() to actually change the depth comparison function, and store the new value in this ...
Definition: GLState.cc:948
void set_twosided_lighting(bool _b)
set whether transparent or solid objects should be drawn
Definition: GLState.cc:822
static GLboolean unmapBuffer(GLenum target)
Definition: GLState.cc:2241
static void getAlphaFunc(GLenum *_func, GLclampf *_ref)
get alpha function, null-ptr safe
Definition: GLState.cc:1723
static void cullFace(GLenum _mode)
replaces glCullFace, supports locking
Definition: GLState.cc:1743
Vec3d eye() const
get eye point
Definition: GLState.cc:886
void pick_init(bool _color)
initialize name/color picking stack (like glInitNames())
Definition: GLState.cc:1043
static const Vec4f default_clear_color
default value for clear color
Definition: GLState.hh:922
void set_multisampling(bool _b)
Enable or disable multisampling.
Definition: GLState.cc:839
static GLuint getFramebufferRead()
get current read framebuffer of a target
Definition: GLState.cc:2125
Vec3d right() const
get right-vector w.r.t. camera coordinates
Definition: GLState.cc:918
static void vertexPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glVertexPointer, supports locking
Definition: GLState.cc:1961
static const Vec4f default_ambient_color
default value for ambient color
Definition: GLState.hh:926
void lookAt(const Vec3d &_eye, const Vec3d &_center, const Vec3d &_up)
set camera by lookAt
Definition: GLState.cc:515
void setState()
set the whole stored gl state
Definition: GLState.cc:209
Vec4uc pick_get_name_color(size_t _idx)
Definition: GLState.cc:1068
bool pick_error() const
Definition: GLState.cc:1122
static bool isStateLocked(GLenum _cap)
returns true, if a cap state is locked
Definition: GLState.cc:1557
static void enable(GLenum _cap, bool _warnRemoved=true)
replaces glEnable, but supports locking
Definition: GLState.cc:1507
static void lockTextureStage()
locks the current texture stage (set by setActiveTexture)
Definition: GLState.cc:1933
static const float default_shininess
default value for shininess
Definition: GLState.hh:934
static void bindTexture(GLenum _target, GLuint _buffer)
replaces glBindTexture, supports locking
Definition: GLState.cc:1911
static void setTexGenMode(GLenum _coord, GLenum _name, GLint _param)
replaces glVertexPointer, supports locking
Definition: GLState.cc:2049
static void normalPointer(GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glNormalPointer, supports locking
Definition: GLState.cc:1983
void pop_projection_matrix()
pop projection matrix
Definition: GLState.cc:989
bool color_picking() const
Is color picking active?
Definition: GLState.cc:1141
static void useProgram(GLuint _program)
replaces glUseProgram, supports locking
Definition: GLState.cc:2200
static void enableClientState(GLenum _cap)
replaces glEnableClientState, supports locking
Definition: GLState.cc:1570
static void getNormalPointer(GLenum *_type, GLsizei *_stride, const GLvoid **_pointer)
get normal pointer, null-ptr safe
Definition: GLState.cc:1997
void set_projection(const GLMatrixd &_m)
set projection
Definition: GLState.hh:716
void push_projection_matrix()
push projection matrix
Definition: GLState.cc:971
static void syncFromGL()
synchronize this class with the OpenGL state machine
Definition: GLState.cc:1244
void reset_modelview()
reset modelview matrix (load identity)
Definition: GLState.cc:370
void pick_set_name(size_t _idx)
sets the current name/color (like glLoadName(_idx))
Definition: GLState.cc:1061
Vec3d viewing_direction() const
get viewing ray
Definition: GLState.hh:873
static void unlockFramebuffer(GLenum _target)
unlock a framebuffer target
Definition: GLState.cc:2175
static void getDepthRange(GLclampd *_zNearOut, GLclampd *_zFarOut)
get current depth range
Definition: GLState.cc:1773
static void blendColor(GLclampf _red, GLclampf _green, GLclampf _blue, GLclampf _alpha)
replaces glBlendColor, supports locking
Definition: GLState.cc:1686
static void lockBufferTarget(GLenum _target)
lock buffer target
Definition: GLState.cc:1835
static void drawBuffers(GLsizei _n, const GLenum *_bufs)
replaces glDrawBuffers, supports locking
Definition: GLState.cc:2091
bool pick_set_maximum(size_t _idx)
Set the maximal number of primitives/components of your object.
Definition: GLState.cc:1051
void get_bounding_box(ACG::Vec3d &_min, ACG::Vec3d &_max)
Definition: GLState.cc:813
const GLMatrixd & viewport() const
get viewport matrix
Definition: GLState.hh:821
static bool isClientStateEnabled(GLenum _cap)
returns true, if a client state is enabled
Definition: GLState.cc:1613
void scale(double _s)
scale by (_s, _s, _s)
Definition: GLState.hh:775
void pick_pop_name()
pops the current name from the stack (like glPopName())
Definition: GLState.cc:1097
static void unlockClientState(GLenum _cap)
unlocks a client state
Definition: GLState.cc:1603
void viewing_ray(int _x, int _y, Vec3d &_origin, Vec3d &_direction) const
Definition: GLState.cc:930
size_t pick_free_indicies() const
returns the number of still available colors during color picking
Definition: GLState.cc:1113
void translate(double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
translate by (_x, _y, _z)
Definition: GLState.cc:533
static GLuint getBoundBuf(GLenum _target)
get currently bound buffer
Definition: GLState.cc:1850
void reset_projection()
reset projection matrix (load identity)
Definition: GLState.cc:334
void perspective(double _fovY, double _aspect, double _near_plane, double _far_plane)
perspective projection
Definition: GLState.cc:448
static void genBuffersARB(GLsizei n, GLuint *buffers)
Definition: GLState.cc:2214
void set_overlay_color(const Vec4f &_col)
set overlay color
Definition: GLState.cc:752
static const Vec4f default_specular_color
default value for specular color
Definition: GLState.hh:930
static int getActiveTextureIndex()
get active texture as zero based index
Definition: GLState.hh:684
void set_clear_color(const Vec4f &_col)
set background color
Definition: GLState.cc:662
void set_shininess(float _shininess)
set specular shininess (must be in [0, 128])
Definition: GLState.cc:761
static void blendEquation(GLenum _mode)
replaces glBlendEquation, supports locking
Definition: GLState.cc:1672
static bool isBufferTargetLocked(GLenum _target)
get buffer target locking state
Definition: GLState.cc:1845
void set_modelview(const GLMatrixd &_m)
set modelview
Definition: GLState.hh:753
Vec3d unproject(const Vec3d &_winPoint) const
unproject point in window coordinates _winPoint to world coordinates
Definition: GLState.cc:651
static void activeTexture(GLenum _texunit)
replaces glActiveTexture, no locking support
Definition: GLState.cc:1900
void set_bounding_box(ACG::Vec3d _min, ACG::Vec3d _max)
Definition: GLState.cc:806
static void alphaFunc(GLenum _func, GLclampf _ref)
replaces glAlphaFunc, supports locking
Definition: GLState.cc:1708
ACG::Vec3d bb_max_
Definition: GLState.hh:1050
Vec3d project(const Vec3d &_point) const
project point in world coordinates to window coordinates
Definition: GLState.cc:640
void set_color(const Vec4f &_col)
set color
Definition: GLState.cc:691
static const Vec4f default_diffuse_color
default value for diffuse color
Definition: GLState.hh:928
void mult_matrix(const GLMatrixd &_m, const GLMatrixd &_inv_m, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply by a given transformation matrix
Definition: GLState.cc:614
static void unlockBufferTarget(GLenum _target)
unlock buffer target
Definition: GLState.cc:1840
static size_t getBufferSize(GLenum _target)
Definition: GLState.cc:2245
static void getBlendColor(GLclampf *_col)
get blend color, not null-ptr safe, 4 element color output: RGBA
Definition: GLState.cc:1702
static GLuint getBoundTextureBuffer()
get bound texture
Definition: GLState.cc:1948
static void depthRange(GLclampd _zNear, GLclampd _zFar)
replaces glDepthRange, supports locking
Definition: GLState.cc:1757
static void bufferDataARB(GLenum target, GLsizeiptrARB size, const GLvoid *data, GLenum usage)
Definition: GLState.cc:2222
static bool isClientStateLocked(GLenum _cap)
returns true, if a client state is locked
Definition: GLState.cc:1608
static size_t getShaderSource(GLuint _shader, char *_buffer, size_t _bufferSize)
Definition: GLState.cc:1890
static int getBufferTargetIndex(GLenum _target)
bijective map from GLenum buffer_target to [0..3], -1 if unsupported
Definition: GLState.cc:1781
static const Vec4f default_overlay_color
default value for overlay color
Definition: GLState.hh:932
static const Vec4f default_base_color
default value for base color
Definition: GLState.hh:924
static void texcoordPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glTexcoordPointer, supports locking
Definition: GLState.cc:2027
static void disable(GLenum _cap, bool _warnRemoved=true)
replaces glDisable, but supports locking
Definition: GLState.cc:1527
void set_line_width(float _f)
set line width
Definition: GLState.cc:791
void set_point_size(float _f)
set point size
Definition: GLState.cc:776
void frustum(double _left, double _right, double _bottom, double _top, double _near_plane, double _far_plane)
perspective projection
Definition: GLState.cc:425
double fovy() const
get field of view in y direction
Definition: GLState.cc:868
static void getVertexPointer(GLint *_size, GLenum *_type, GLsizei *_stride, const GLvoid **_pointer)
get vertex pointer, null-ptr safe
Definition: GLState.cc:1975
static void genBuffers(GLsizei n, GLuint *buffers)
Definition: GLState.cc:2218
void rotate(double _angle, double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
rotate around axis (_x, _y, _z) by _angle
Definition: GLState.cc:564
size_t pick_current_index() const
Returns the current color picking index (can be used for caching)
Definition: GLState.cc:1131
void initialize()
initialize all state variables (called by constructor)
Definition: GLState.cc:162
Vec3d up() const
get up-vector w.r.t. camera coordinates
Definition: GLState.cc:906
static void getColorPointer(GLint *_size, GLenum *_type, GLsizei *_stride, const GLvoid **_pointer)
get color pointer, null-ptr safe
Definition: GLState.cc:2019
static bool isTextureTargetLocked()
get texture target locking state
Definition: GLState.cc:1943
ACG::Vec3d bb_min_
Definition: GLState.hh:1050
static void bufferData(GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage)
Definition: GLState.cc:2227
static GLenum getShaderType(GLuint _shader)
Definition: GLState.cc:1856
void set_base_color(const Vec4f &_col)
set base color (used when lighting is off)
Definition: GLState.cc:677
static void deleteBuffers(GLsizei n, const GLuint *buffers)
Definition: GLState.cc:2232
static bool isFramebufferLocked(GLenum _target)
get framebuffer target lock state
Definition: GLState.cc:2187
static void getTexGenMode(GLenum _coord, GLenum _name, GLint *_param)
replaces glVertexPointer, supports locking
Definition: GLState.cc:2061
static void shadeModel(GLenum _mode)
replaces glShadeModel, supports locking
Definition: GLState.cc:1729
double aspect() const
get aspect ratio
Definition: GLState.cc:877
static void blendFuncSeparate(GLenum _srcRGB, GLenum _dstRGB, GLenum _srcAlpha, GLenum _dstAlpha)
replaces glBlendFuncSeparate, supports locking
Definition: GLState.cc:1621
static bool isStateEnabled(GLenum _cap)
returns true, if a cpa state is enabled
Definition: GLState.cc:1562
static void getTexcoordPointer(GLint *_size, GLenum *_type, GLsizei *_stride, const GLvoid **_pointer)
get color pointer, null-ptr safe
Definition: GLState.cc:2041
static GLuint getFramebufferDraw()
get current draw framebuffer of a target
Definition: GLState.cc:2117
static void unlockTextureStage()
unlocks the current texture target
Definition: GLState.cc:1938
const GLenum & depthFunc() const
get glDepthFunc() that is supposed to be active
Definition: GLState.cc:941
static void drawBuffer(GLenum _mode)
replaces glDrawBuffer, supports locking
Definition: GLState.cc:2076
std::vector< size_t > pick_color_to_stack(Vec4uc _rgba) const
Definition: GLState.cc:1104
static GLvoid * mapBuffer(GLenum target, GLenum access)
Definition: GLState.cc:2236
void clearBuffers()
clear buffers viewport rectangle
Definition: GLState.cc:266
static GLuint getAttachedShader(GLuint _program, GLenum _type)
Definition: GLState.cc:1865
static void bindBuffer(GLenum _target, GLuint _buffer)
replaces glBindBuffer, supports locking
Definition: GLState.cc:1820
static GLenum getBoundTextureTarget()
get bound texture target
Definition: GLState.cc:1953
void push_modelview_matrix()
push modelview matrix
Definition: GLState.cc:1010
static void lockFramebuffer(GLenum _target)
lock a framebuffer target
Definition: GLState.cc:2163
void set_diffuse_color(const Vec4f &_col)
set diffuse color
Definition: GLState.cc:722
void pick_push_name(size_t _idx)
creates a new name the stack (like glPushName())
Definition: GLState.cc:1090
static void bindFramebuffer(GLenum _target, GLuint _framebuffer)
replaces glBindFramebuffer, supports locking
Definition: GLState.cc:2132
static void getBlendFuncSeparate(GLenum *_srcRGB, GLenum *_dstRGB, GLenum *_srcAlpha, GLenum *_dstAlpha)
get blend function, null-ptr safe
Definition: GLState.cc:1664
static void lockState(GLenum _cap)
locks a specific cap state, such that enable() or disable() has no effect
Definition: GLState.cc:1547
static void lockClientState(GLenum _cap)
locks a client state
Definition: GLState.cc:1598
const Scalar * get_raw_data() const
Definition: Matrix4x4T.hh:257
void identity()
setup an identity matrix
VectorT< T, 3 > transform_point(const VectorT< T, 3 > &_v) const
transform point (x',y',z',1) = M * (x,y,z,1)
Scalar * data()
access to Scalar array
Definition: Vector11T.hh:201
auto normalize() -> decltype(*this/=std::declval< VectorT< S, DIM > >().norm())
Definition: Vector11T.hh:454
Namespace providing different geometric functions concerning angles.
void glColor(const Vec3f &_v)
Wrapper: glColor for Vec3f.
Definition: gl.hh:144
VectorT< unsigned char, 4 > Vec4uc
Definition: VectorT.hh:128
MultiplyFrom
Definition: GLMatrixT.hh:73
void compatibilityProfile(bool _enableCoreProfile)
Store opengl core profile setting.
Definition: gl.cc:166
VectorT< double, 3 > Vec3d
Definition: VectorT.hh:121