1 #ifndef __openglhelper__
2 #define __openglhelper__
3 
4 #include "qglobal.h"
5 
6 #ifdef __APPLE__
7 #include <OpenGL/glu.h>
8 #else
9 #ifdef Q_OS_WIN
10 #include "windows.h"
11 #endif
12 #include <GL/glu.h>
13 #endif
14 
15 namespace Qwt3D {
16 
17 #ifndef QWT3D_NOT_FOR_DOXYGEN
18 
19 class GLStateBewarer
20 {
21 public:
22     GLStateBewarer(GLenum what, bool on, bool persist = false)
23     {
24         state_ = what;
25         stateval_ = glIsEnabled(what);
26         if (on)
27             turnOn(persist);
28         else
29             turnOff(persist);
30     }
31 
~GLStateBewarer()32     ~GLStateBewarer()
33     {
34         if (stateval_)
35             glEnable(state_);
36         else
37             glDisable(state_);
38     }
39 
40     void turnOn(bool persist = false)
41     {
42         glEnable(state_);
43         if (persist)
44             stateval_ = true;
45     }
46 
47     void turnOff(bool persist = false)
48     {
49         glDisable(state_);
50         if (persist)
51             stateval_ = false;
52     }
53 
54 private:
55     GLenum state_;
56     bool stateval_;
57 };
58 
gl_error()59 inline const GLubyte *gl_error()
60 {
61     GLenum errcode;
62     const GLubyte *err = 0;
63 
64     if ((errcode = glGetError()) != GL_NO_ERROR) {
65         err = gluErrorString(errcode);
66     }
67     return err;
68 }
69 
SaveGlDeleteLists(GLuint & lstidx,GLsizei range)70 inline void SaveGlDeleteLists(GLuint &lstidx, GLsizei range)
71 {
72     if (glIsList(lstidx))
73         glDeleteLists(lstidx, range);
74     lstidx = 0;
75 }
76 
77 //! get OpenGL transformation matrices
78 /**
79         Don't rely on (use) this in display lists !
80         \param modelMatrix should be a GLdouble[16]
81         \param projMatrix should be a GLdouble[16]
82         \param viewport should be a GLint[4]
83 */
getMatrices(GLdouble * modelMatrix,GLdouble * projMatrix,GLint * viewport)84 inline void getMatrices(GLdouble *modelMatrix, GLdouble *projMatrix, GLint *viewport)
85 {
86     glGetIntegerv(GL_VIEWPORT, viewport);
87     glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
88     glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
89 }
90 
91 //! simplified glut routine (glUnProject): windows coordinates_p --> object coordinates_p
92 /**
93         Don't rely on (use) this in display lists !
94 */
ViewPort2World(double & objx,double & objy,double & objz,double winx,double winy,double winz)95 inline bool ViewPort2World(double &objx, double &objy, double &objz, double winx, double winy,
96                            double winz)
97 {
98     GLdouble modelMatrix[16];
99     GLdouble projMatrix[16];
100     GLint viewport[4];
101 
102     getMatrices(modelMatrix, projMatrix, viewport);
103     int res =
104             gluUnProject(winx, winy, winz, modelMatrix, projMatrix, viewport, &objx, &objy, &objz);
105 
106     return (res == GL_FALSE) ? false : true;
107 }
108 
109 //! simplified glut routine (glProject): object coordinates_p --> windows coordinates_p
110 /**
111         Don't rely on (use) this in display lists !
112 */
World2ViewPort(double & winx,double & winy,double & winz,double objx,double objy,double objz)113 inline bool World2ViewPort(double &winx, double &winy, double &winz, double objx, double objy,
114                            double objz)
115 {
116     GLdouble modelMatrix[16];
117     GLdouble projMatrix[16];
118     GLint viewport[4];
119 
120     getMatrices(modelMatrix, projMatrix, viewport);
121     int res = gluProject(objx, objy, objz, modelMatrix, projMatrix, viewport, &winx, &winy, &winz);
122 
123     return (res == GL_FALSE) ? false : true;
124 }
125 
126 #endif // QWT3D_NOT_FOR_DOXYGEN
127 
128 } // ns
129 
130 #endif
131