GLState.hh

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 7220 $                                                       *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-09-29 17:19:21 +0200 (Di, 29. Sep 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 //=============================================================================
00045 //
00046 //  CLASS GLState
00047 //
00048 //=============================================================================
00049 
00050 
00051 #ifndef ACG_GLSTATE_HH
00052 #define ACG_GLSTATE_HH
00053 
00054 
00055 //== INCLUDES =================================================================
00056 
00057 
00058 #include "gl.hh"
00059 #include "../Math/GLMatrixT.hh"
00060 #include "../Math/VectorT.hh"
00061 #include "../Config/ACGDefines.hh"
00062 #include "ColorStack.hh"
00063 #include <stack>
00064 #include <vector>
00065 
00066 #ifdef WIN32
00067         #pragma warning(push)
00068         #pragma warning(disable:4251)
00069 #endif
00070 
00071 //== NAMESPACES ===============================================================
00072 
00073 
00074 namespace ACG {
00075 
00076 
00077 //== CLASS DEFINITION =========================================================
00078 
00079 
00090 class ACGDLLEXPORT GLState
00091 {
00092 public:
00093 
00095   GLState(bool _updateGL = true);
00096 
00098   ~GLState() {}
00099 
00100 
00101   void makeCurrent() {  }
00102 
00104   void initialize();
00105 
00107   bool updateGL() const { return updateGL_; }
00109   void set_updateGL(bool _b) { updateGL_ = _b; }
00110 
00112   unsigned int msSinceLastRedraw () const { return msSinceLastRedraw_; }
00113 
00115   void set_msSinceLastRedraw (unsigned int _ms) { msSinceLastRedraw_ = _ms; }
00116 
00118   void setState ();
00119 
00121   void clearBuffers ();
00122 
00123   //---  set GL projection matrix ---------------------------------------------
00124 
00125 
00127   void reset_projection();
00128 
00130   void set_projection(const GLMatrixd& _m) {
00131     GLMatrixd inv_m(_m);  inv_m.invert();  set_projection(_m, inv_m);
00132   }
00134   void set_projection(const GLMatrixd& _m, const GLMatrixd& _inv_m);
00135 
00137   void ortho( double _left, double _right,
00138               double _bottom, double _top,
00139               double _near_plane, double _far_plane );
00140 
00142   void frustum( double _left, double _right,
00143                 double _bottom, double _top,
00144                 double _near_plane, double _far_plane );
00145 
00147   void perspective( double _fovY, double _aspect,
00148                     double _near_plane, double _far_plane );
00149 
00151   void viewport( int _left, int _bottom, int _width, int _height,
00152                  int _glwidth = 0, int _glheight = 0);
00153 
00154 
00155 
00156 
00157 
00158   //---  set GL modelview matrix ----------------------------------------------
00159 
00160 
00162   void reset_modelview();
00163 
00165   void set_modelview(const GLMatrixd& _m) {
00166     GLMatrixd inv_m(_m);  inv_m.invert();  set_modelview(_m, inv_m);
00167   }
00169   void set_modelview(const GLMatrixd& _m, const GLMatrixd& _inv_m);
00170 
00172   void lookAt( const Vec3d& _eye, const Vec3d& _center, const Vec3d& _up );
00173 
00175   void translate( double _x, double _y, double _z,
00176                   MultiplyFrom _mult_from = MULT_FROM_RIGHT );
00177 
00179   void rotate( double _angle, double _x, double _y, double _z,
00180                MultiplyFrom _mult_from = MULT_FROM_RIGHT );
00181 
00183   void scale( double _s )
00184   { scale(_s, _s, _s); }
00185 
00187   void scale( double _s,
00188               MultiplyFrom /* _mult_from = MULT_FROM_RIGHT  */ )
00189   { scale(_s, _s, _s); }
00190 
00192   void scale( double _sx, double _sy, double _sz,
00193               MultiplyFrom _mult_from = MULT_FROM_RIGHT );
00194 
00196   void mult_matrix( const GLMatrixd& _m, const GLMatrixd& _inv_m,
00197                     MultiplyFrom _mult_from = MULT_FROM_RIGHT );
00198 
00199 
00201   void push_projection_matrix();
00203   void pop_projection_matrix();
00204 
00206   void push_modelview_matrix();
00208   void pop_modelview_matrix();
00209 
00210 
00211   //---  get GL states and matrices -------------------------------------------
00212 
00213 
00215   const GLMatrixd& projection() const {
00216     return projection_;
00217   }
00218 
00220   const GLMatrixd& modelview() const {
00221     return modelview_;
00222   }
00223 
00225   const GLMatrixd& viewport() const {
00226     return window2viewport_;
00227   }
00228 
00230   const GLMatrixd& forward_projection() const {
00231     return forward_projection_;
00232   }
00233 
00235   const GLMatrixd& inverse_projection() const {
00236     return inverse_projection_;
00237   }
00238 
00240   const GLMatrixd& inverse_modelview() const {
00241     return inverse_modelview_;
00242   }
00243 
00245   void get_viewport( int& _left, int& _bottom,
00246                      int& _width, int& _height ) const {
00247     _left = left_; _bottom = bottom_; _width = width_; _height = height_;
00248   }
00249 
00251   int viewport_width() const { return width_; }
00253   int viewport_height() const { return height_; }
00254 
00256   int context_width() const { return glwidth_; }
00257 
00259   int context_height() const { return glheight_; }
00260 
00262   double near_plane() const { return near_plane_; }
00263 
00265   double far_plane() const { return far_plane_; }
00266 
00268   double fovy() const;
00269 
00271   double aspect() const;
00272 
00274   Vec3d eye() const;
00275 
00277   Vec3d viewing_direction() const {
00278     return viewing_direction(width_>>1, height_>>1);
00279   }
00280 
00282   Vec3d viewing_direction(int _x, int _y) const;
00283 
00285   Vec3d up() const;
00286 
00288   Vec3d right() const;
00289 
00293   void viewing_ray(int _x, int _y, Vec3d& _origin, Vec3d& _direction) const;
00294 
00295 
00296   //--- project and unproject points ------------------------------------------
00297 
00299   Vec3d project(const Vec3d& _point) const;
00300 
00302   Vec3d unproject(const Vec3d& _winPoint) const;
00303 
00304 
00305 
00306 
00307   //--- color & material ------------------------------------------------------
00308 
00310   static const Vec4f default_clear_color;
00312   static const Vec4f default_base_color;
00314   static const Vec4f default_ambient_color;
00316   static const Vec4f default_diffuse_color;
00318   static const Vec4f default_specular_color;
00320   static const float default_shininess;
00321 
00322 
00324   void set_clear_color(const Vec4f& _col);
00326   const Vec4f& clear_color() const { return clear_color_; }
00327 
00329   void set_base_color(const Vec4f& _col);
00331   const Vec4f& base_color() const { return base_color_; }
00332 
00334   void set_ambient_color(const Vec4f& _col);
00336   const Vec4f& ambient_color() const { return ambient_color_; }
00337 
00339   void set_diffuse_color(const Vec4f& _col);
00341   const Vec4f& diffuse_color() const { return diffuse_color_; }
00342 
00344   void set_specular_color(const Vec4f& _col);
00346   const Vec4f& specular_color() const { return specular_color_; }
00347 
00349   void set_shininess(float _shininess);
00351   float shininess() const { return shininess_; }
00352 
00353 
00354 
00355 
00356   //--- thickness -------------------------------------------------------------
00357 
00359   void set_point_size(float _f);
00361   float point_size() const { return point_size_; }
00362 
00364   void set_line_width(float _f);
00366   float line_width() const { return line_width_; }
00367 
00368 
00369 
00370 
00371   //--- misc ------------------------------------------------------------------
00372 
00374   void set_blending(bool _b) { blending_ = _b; }
00376   bool blending() { return blending_; }
00377 
00379   void set_twosided_lighting(bool _b);
00381   bool twosided_lighting() { return twosided_lighting_; }
00382 
00383   //--- Multi Sampling --------------------------------------------------------
00384 
00386   void set_multisampling( bool _b );
00387 
00389   bool multisampling(){ return multisampling_; };
00390 
00392   void allow_multisampling( bool _b ) { allow_multisampling_ = _b; };
00393 
00395   bool multisampling_alloowed(){ return allow_multisampling_; };
00396 
00397   //--- picking ---------------------------------------------------------------
00398 
00407 
00408   void pick_init (bool _color);
00409 
00413   bool pick_set_maximum (unsigned int _idx);
00414 
00416   void pick_set_name (unsigned int _idx);
00417 
00421   Vec4uc pick_get_name_color (unsigned int _idx);
00422 
00424   void pick_push_name (unsigned int _idx);
00425 
00427   void pick_pop_name ();
00428 
00431   std::vector<unsigned int> pick_color_to_stack (Vec4uc _rgba) const;
00432 
00434   unsigned int pick_free_indicies () const;
00435 
00438   bool pick_error () const;
00439 
00443   unsigned int pick_current_index () const;
00444 
00446   bool color_picking () const;
00447 
00448 
00449 private: //--------------------------------------------------------------------
00450 
00451   // update forward projection and backward projection matrices
00452   void update_matrices();
00453 
00454 
00455   // matrix stacks
00456   std::stack<GLMatrixd>  stack_projection_,
00457                          stack_modelview_,
00458                          stack_inverse_projection_,
00459                          stack_inverse_modelview_;
00460 
00461   // current matrices
00462   GLMatrixd  projection_,
00463              inverse_projection_,
00464              modelview_,
00465              inverse_modelview_,
00466              window2viewport_,
00467              inverse_window2viewport_,
00468              forward_projection_,
00469              backward_projection_;
00470 
00471   // viewport
00472   int left_, bottom_, width_, height_;
00473 
00474   // gl context
00475   int glwidth_, glheight_;
00476 
00477   // projection
00478   double near_plane_, far_plane_;
00479 
00480   // colors & materials
00481   Vec4f   clear_color_,
00482           base_color_,
00483           ambient_color_,
00484           diffuse_color_,
00485           specular_color_;
00486 
00487   float shininess_;
00488 
00489   // thickness
00490   float  point_size_, line_width_;
00491 
00492   // lighting
00493   bool twosided_lighting_;
00494 
00495   // Multisampling settings
00496   bool multisampling_;
00497   bool allow_multisampling_;
00498 
00499   // helper: should GL matrices be updated
00500   bool updateGL_;
00501 
00502   // true -> draw tranparent Objects
00503   bool blending_;
00504 
00505   // time since last redraw
00506   unsigned int msSinceLastRedraw_;
00507 
00508   // stack for color picking
00509   ColorStack colorStack_;
00510 
00511   // are we using color picking
00512   bool colorPicking_;
00513 
00514 };
00515 
00516 #ifdef WIN32
00517         #pragma warning(push)
00518 #endif
00519 
00520 //=============================================================================
00521 } // namespace ACG
00522 //=============================================================================
00523 #endif // ACG_GLSTATE_HH defined
00524 //=============================================================================

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .