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