NCBI C++ ToolKit
glutils.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_OPENGL___GL_UTILS__HPP
2 #define GUI_OPENGL___GL_UTILS__HPP
3 
4 /* $Id: glutils.hpp 47292 2022-12-21 00:32:34Z evgeniev $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Mike DiCuccio
30  *
31  * File Description:
32  * OpenGL utility functions
33  */
34 
35 
36 #include <corelib/ncbistd.hpp>
37 #include <gui/gui.hpp>
38 #include <gui/utils/rgba_color.hpp>
39 #include <gui/opengl/gltypes.hpp>
41 
42 /** @addtogroup GUI_OPENGL
43  *
44  * @{
45  */
46 
48 
49 
50 /////////////////////////////////////////////////////////////////////////////
51 // CGlUtils::
52 //
53 
55 {
56 public:
57  /// status of hardware acceleration.
58  enum EAccelState {
61  eNotAccelerated
62  };
63 
64  // diagnostic (error handling) mode
65  enum EGlDiagMode {
70  eAbort
71  };
72 
73  /// Check if there are any OpenGL errors
74  static bool CheckGlError();
75 
76  /// Get current diagnostic (error handling) mode
77  static EGlDiagMode GetDiagnosticMode();
78 
79  /// Get message for specified OpenGL error
80  static string GetErrMsg(GLint error);
81 
82  /// Dump many of the most common OpenGL states
83  static void DumpState();
84 
85  /// Get the status of hardware acceleration. This cannot be determined
86  /// without an active OpenGL context.
87  static EAccelState GetAccelerated(void);
88 
89  /// Set the status of hardware acceleration. This is provided to permit a
90  /// user override of the detected state.
91  static void SetAccelerated(EAccelState acc);
92 
93 private:
94 
95  // hardware acceleration
97 
98 };
99 
100 /// Same as CGlUtils::CheckGlError except the log message will include
101 /// the name of the function logging the message (instead of CheckGlError)
102 #define CHECK_GLERROR() {\
103  GLint error = glGetError(); \
104  if (error != GL_NO_ERROR) { \
105  static CGlUtils::EGlDiagMode mode = CGlUtils::GetDiagnosticMode(); \
106  string msg = CGlUtils::GetErrMsg(error); \
107  \
108  switch (mode) { \
109  case CGlUtils::eLogPost: \
110  LOG_POST(Error << msg); \
111  break; \
112  case CGlUtils::eAbort: \
113  LOG_POST(Error << msg); \
114  Abort(); \
115  break; \
116  case CGlUtils::eThrow: \
117  NCBI_THROW(COpenGLException, eGlError, msg); \
118  break; \
119  case CGlUtils::eUndefined: \
120  case CGlUtils::eIgnore: \
121  default: \
122  break; \
123  } \
124  } \
125  }
126 
127 ///////////////////////////////////////////////////////////////////////////////
128 /// CGlAttrGuard - guard class for restoring OpenGL attributes
130 {
131 public:
132  CGlAttrGuard(GLbitfield mask)
133  {
134  glPushAttrib(mask);
135  }
137  {
138  glPopAttrib();
139  }
140 };
141 
142 #ifdef GLEW_MX
143 ///////////////////////////////////////////////////////////////////////////////
144 /// CGLGlewContext - Store instance of glew context in a singleton that is
145 /// available to all opengl libs, not just ones dependent on wx. When
146 /// the onctext is created (from a wx window) it can be stored here where it
147 /// can be accessed by any opengl lib (normally via "GLEWContext* glewGetContext();"
148 class NCBI_GUIOPENGL_EXPORT CGLGlewContext
149 {
150 public:
151  static CGLGlewContext& GetInstance();
152 
153  GLEWContext* GetGlewContext() { return m_GLEWContext; }
154  void SetGlewContext(GLEWContext* gc) { m_GLEWContext = gc; }
155 
156 private:
157  CGLGlewContext() : m_GLEWContext(NULL) {}
158  GLEWContext* m_GLEWContext;
159 };
160 #endif
161 
162 ///////////////////////////////////////////////////////////////////////////////
163 ///
164 
165 inline void glRectC(const TModelRect& rc)
166 {
167  glRectd(rc.Left(), rc.Bottom(), rc.Right(), rc.Top());
168 }
169 
170 
171 inline void glRectC(const TVPRect& rc)
172 {
173  glRecti(rc.Left(), rc.Bottom(), rc.Right(), rc.Top());
174 }
175 
176 
177 inline void glColorC(const CRgbaColor& color)
178 {
179  glColor4fv(color.GetColorArray());
180 }
181 
182 
183 ///////////////////////////////////////////////////////////////////////////////
184 
185 #ifndef _DEBUG
186 
187 //
188 // in release mode, this function does nothing
189 //
190 inline
191 void CGlUtils::DumpState(void)
192 {
193 }
194 
195 #endif
196 
197 #ifdef NCBI_OS_DARWIN
198 
200  GLdouble objx,
201  GLdouble objy,
202  GLdouble objz,
203  const GLdouble modelMatrix[16],
204  const GLdouble projMatrix[16],
205  const GLint viewport[4],
206  GLdouble* winx,
207  GLdouble* winy,
208  GLdouble* winz);
209 
211  GLdouble winx,
212  GLdouble winy,
213  GLdouble winz,
214  const GLdouble modelMatrix[16],
215  const GLdouble projMatrix[16],
216  const GLint viewport[4],
217  GLdouble* objx,
218  GLdouble* objy,
219  GLdouble* objz);
220 
221 #else
222 
223 inline int gluProjectX(
224  GLdouble objx,
225  GLdouble objy,
226  GLdouble objz,
227  const GLdouble modelMatrix[16],
228  const GLdouble projMatrix[16],
229  const GLint viewport[4],
230  GLdouble* winx,
231  GLdouble* winy,
232  GLdouble* winz)
233 {
234  return gluProject(objx, objy, objz, modelMatrix, projMatrix, viewport, winx, winy, winz);
235 }
236 
237 inline int gluUnProjectX(
238  GLdouble winx,
239  GLdouble winy,
240  GLdouble winz,
241  const GLdouble modelMatrix[16],
242  const GLdouble projMatrix[16],
243  const GLint viewport[4],
244  GLdouble* objx,
245  GLdouble* objy,
246  GLdouble* objz)
247 {
248  return gluUnProject(winx, winy, winz, modelMatrix, projMatrix, viewport, objx, objy, objz);
249 }
250 
251 #endif
252 
253 
255 
256 /* @} */
257 
258 #endif // GUI_OPENGL___GL_UTILS__HPP
ncbi::TMaskedQueryRegions mask
CGlAttrGuard - guard class for restoring OpenGL attributes.
Definition: glutils.hpp:130
class CRgbaColor provides a simple abstraction for managing colors.
Definition: rgba_color.hpp:58
Include a standard set of the NCBI C++ Toolkit most basic headers.
EGlDiagMode
@ eAbort
#define NULL
Definition: ncbistd.hpp:225
int gluProjectX(GLdouble objx, GLdouble objy, GLdouble objz, const GLdouble modelMatrix[16], const GLdouble projMatrix[16], const GLint viewport[4], GLdouble *winx, GLdouble *winy, GLdouble *winz)
Definition: glutils.hpp:223
EAccelState
status of hardware acceleration.
Definition: glutils.hpp:58
T Top() const
Definition: glrect.hpp:84
T Bottom() const
Definition: glrect.hpp:82
void glRectC(const TModelRect &rc)
Definition: glutils.hpp:165
static EAccelState m_Accel
Definition: glutils.hpp:96
T Right() const
Definition: glrect.hpp:83
static void DumpState()
Dump many of the most common OpenGL states.
Definition: glutils.cpp:208
T Left() const
Definition: glrect.hpp:81
CGlAttrGuard(GLbitfield mask)
Definition: glutils.hpp:132
void glColorC(const CRgbaColor &color)
Definition: glutils.hpp:177
static void SetAccelerated(EAccelState acc)
Set the status of hardware acceleration.
int gluUnProjectX(GLdouble winx, GLdouble winy, GLdouble winz, const GLdouble modelMatrix[16], const GLdouble projMatrix[16], const GLint viewport[4], GLdouble *objx, GLdouble *objy, GLdouble *objz)
Definition: glutils.hpp:237
@ eAccelerated
Definition: glutils.hpp:60
@ eNotDetermined
Definition: glutils.hpp:59
@ eLogPost
Definition: glutils.hpp:68
@ eThrow
Definition: glutils.hpp:69
@ eUndefined
Definition: glutils.hpp:66
@ eIgnore
Definition: glutils.hpp:67
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define NCBI_GUIOPENGL_EXPORT
Definition: gui_export.h:514
n background color
Modified on Wed Apr 24 14:10:40 2024 by modify_doxy.py rev. 669887