NCBI C++ ToolKit
glcanvas.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: glcanvas.cpp 47352 2023-02-08 00:33:30Z evgeniev $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Roman Katargin
27  *
28  * File Description:
29  */
30 
31 #include <ncbi_pch.hpp>
32 
34 
35 #include <gui/opengl.h>
36 #include <gui/opengl/glutils.hpp>
38 
39 #include <wx/dcclient.h>
40 
41 
43 
45 {
46 public:
47  static CGLContextCache& GetInstance();
48  void AddRef();
49  void Release();
50  wxGLContext* GetContext(wxGLCanvas* win);
51 
52 #ifdef GLEW_MX
53  GLEWContext* GetGlewContext() { return &m_GLEWContext; }
54 #endif
55 
56 private:
59  {
60  // We could try to delete the context here but, at least on windows,
61  // by the time this dtor is called, the memory is already cleand up
62  // so an exception is thrown when we try to delete the (already deleted)
63  // wxGLContext
64  }
65 
66 private:
67  wxGLContext* m_GLContext;
68  int m_RefCnt;
69 
70 #ifdef GLEW_MX
71  GLEWContext m_GLEWContext;
72 #endif
73 };
74 
75 
77 {
78  // Declare the static instance (singleton)
79  static CGLContextCache instance;
80 
81 #ifdef GLEW_MX
82  // push the address of the glew context to the static instance of CGLGlewContext
83  // since that is in the base opengl library which needs the (glew) context, but
84  // does not have any dependencies on wxWidgets.
85  if (CGLGlewContext::GetInstance().GetGlewContext() == NULL)
86  CGLGlewContext::GetInstance().SetGlewContext(&instance.m_GLEWContext);
87 #endif
88 
89  return instance;
90 }
91 
93 {
94  ++m_RefCnt;
95 }
96 
98 {
99  --m_RefCnt;
100  if (m_RefCnt == 0) {
101  // Keep context - we share one context for all windows and will
102  // reuse it when the next window opens. Also, some classes (e.g.
103  // CGlTextureFont) may save gl objects like texture maps (fonts)
104  // between window instances so context must stay alive.
105  }
106 }
107 
108 wxGLContext* CGLContextCache::GetContext(wxGLCanvas* win)
109 {
110  if (m_GLContext == 0) {
111  m_GLContext = new wxGLContext(win);
112 
113  m_GLContext->SetCurrent(*win);
114 
115 #ifdef GLEW_MX
116  GLenum err = glewContextInit(&m_GLEWContext);
117  if (GLEW_OK != err)
118  {
119  // Problem: Error creating glew context
120  _TRACE("Error creating glew context");
121  }
122  else {
123  err = glewInit();
124  if (GLEW_OK != err)
125  {
126  // Problem: glewInit failed, something is seriously wrong.
127  // No extenstions will be available.
128  _TRACE("Error loading opengl extensions");
129  }
130  }
131 #else
132  GLenum err = glewInit();
133  if (GLEW_OK != err)
134  {
135  // Problem: glewInit failed, something is seriously wrong.
136  // No extenstions will be available.
137  _TRACE("Error loading opengl extensions");
138  }
139 #endif
140 
141  }
142  return m_GLContext;
143 }
144 
145 
146 BEGIN_EVENT_TABLE(CGLCanvas, wxGLCanvas)
147  EVT_PAINT(CGLCanvas::OnPaint)
149 
150 /// Not having all programs share the same window attributes has caused
151 /// crashing on Linux, so each window will use these attributes (this should
152 /// be the superset of attributes needed by program windows).
153 
154 ///
155 /// X2Go X-Server doesn't allow to specify number of bitplanes of alpha/stencil
156 /// To make gbench work with X2Go the following code
157 ///
158 
159 const wxGLAttributes& CGLCanvas::x_GetAttribList() const
160 {
161  static wxGLAttributes dispAttrs;
162  static wxGLAttributes dispAttrsDefaults;
163  dispAttrs.PlatformDefaults().RGBA().DoubleBuffer().MinRGBA(8, 8, 8, 8).Stencil(8).EndList();
164  dispAttrsDefaults.PlatformDefaults();
165 
166  if (wxGLCanvas::IsDisplaySupported(dispAttrs))
167  return dispAttrs;
168  else
169  return dispAttrsDefaults;
170 }
171 
172 CGLCanvas::CGLCanvas(wxWindow* parent, wxWindowID id,
173  const wxPoint& pos, const wxSize& size, long style) :
174  wxGLCanvas(parent, x_GetAttribList(), id, pos, size, style),
175  m_DoNotUpdate(false)
176 {
177 #ifdef GLEW_MX
179 #endif
180 
181  SetBackgroundStyle(wxBG_STYLE_PAINT);
182 
183  // Check that we have an opengl level of at least 1.4 and display
184  // a warning message if we don't
185 /*
186  static bool opengl_level_checked = false;
187  if (!opengl_level_checked) {
188  opengl_level_checked = true;
189  x_CheckGlVersion();
190  }
191 */
192 
193  m_ClearColor[0] = 1;
194  m_ClearColor[1] = 1;
195  m_ClearColor[2] = 1;
196  m_ClearColor[3] = 0;
197 }
198 
199 void CGLCanvas::SetClearColor(float r, float g, float b, float a)
200 {
201  m_ClearColor[0] = r;
202  m_ClearColor[1] = g;
203  m_ClearColor[2] = b;
204  m_ClearColor[3] = a;
205 }
206 
208 {
210 }
211 
213 {
215  string version = (const char*)glGetString(GL_VERSION);
216  vector<string> arr;
217 
218  if (version != "")
219  NStr::Split(version, ". \t", arr);
220 
221  bool version_passed = false;
222 
223  // Check major version
224  int major_version = 0;
225  if (arr.size() >= 1) {
226  try {
227  major_version = NStr::StringToInt(arr[0]);
228  if (major_version > 1) {
229  version_passed = true;
230  }
231  }
232  catch (CException& ) {
233  //didn't parse as an int...
234  }
235  }
236 
237  // If major version is only 1, check minor version:
238  if (arr.size() > 1 && major_version == 1) {
239  try {
240  int minor_version = NStr::StringToInt(arr[1]);
241  if (minor_version > 4) {
242  version_passed = true;
243  }
244  }
245  catch (CException&) {
246  //minor version didn't parse as an int
247  }
248  }
249 
250  string user_warning;
251  if (!version_passed) {
252  user_warning = "Warning - Your computer's graphics do not meet the\n";
253  user_warning += "minimum level required by Genome Workbench. Visual or \n";
254  user_warning += "stability issues may occur.\n\n";
255  user_warning += "OpenGL version 1.5 or better is needed. Your graphics are:\n\n";
256  user_warning += version;
257  user_warning += "\n";
258  user_warning += (const char*)glGetString(GL_RENDERER);
259 
260  NcbiWarningBox(user_warning, "Graphics Warning");
261  }
262 }
263 
265 {
266  wxGLContext* context = CGLContextCache::GetInstance().GetContext(this);
267  context->SetCurrent(*this);
268 
269  glDisable(GL_DEPTH_TEST);
270  glDisable(GL_LIGHTING);
271 
272  glMatrixMode(GL_MODELVIEW);
273  glLoadIdentity();
274 }
275 
276 void CGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
277 {
278  //LOG_POST("CGLCanvas::OnPaint()");
279 
280  // This is a dummy, to avoid an endless succession of paint messages.
281  // OnPaint handlers must always create a wxPaintDC.
282  wxPaintDC dc(this);
283 
284  // The hack is used for tooltip window
285  // SwapBuffers ovewrites child windows on some systems
286  // Ubuntu with Intel videocard (netbook).
287  if (m_DoNotUpdate) {
288  m_DoNotUpdate = false;
289  return;
290  }
291 
293 
294  glClearColor(m_ClearColor[0], m_ClearColor[1], m_ClearColor[2], m_ClearColor[3]);
295  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
296 
297  x_Render();
298 
299  // For some implementations of graphics card drivers,
300  // the update of the stored image underneath the mouse
301  // cursor is not sychronized with the udpate of OpenGL
302  // window. Here we force to update the stored image
303  // after the frame butter gets updated.
304  // Currently, This works on Windows only.
305 #if defined __WXMSW__
306  ShowCursor(false);
307 #endif
308 
309  SwapBuffers();
310 
311 #if defined __WXMSW__
312  ShowCursor(true);
313 #endif
314 }
315 
317 {
318  wxColour clr = GetBackgroundColour();
319  glClearColor(clr.Red()/255.f, clr.Green()/255.f, clr.Blue()/255.f, 0);
320  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
321 }
322 
CGLCanvas.
Definition: glcanvas.hpp:55
void x_CheckGlVersion()
Definition: glcanvas.cpp:212
void SetClearColor(float r, float g, float b, float a)
Definition: glcanvas.cpp:199
void x_SetupGLContext()
Definition: glcanvas.cpp:264
virtual void x_Render()
Definition: glcanvas.cpp:316
void OnPaint(wxPaintEvent &event)
Definition: glcanvas.cpp:276
bool m_DoNotUpdate
Definition: glcanvas.hpp:75
CGLCanvas(wxWindow *parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0)
Definition: glcanvas.cpp:172
virtual ~CGLCanvas()
Definition: glcanvas.cpp:207
float m_ClearColor[4]
Definition: glcanvas.hpp:77
wxGLContext * m_GLContext
Definition: glcanvas.cpp:67
void Release()
Definition: glcanvas.cpp:97
wxGLContext * GetContext(wxGLCanvas *win)
Definition: glcanvas.cpp:108
static CGLContextCache & GetInstance()
Definition: glcanvas.cpp:76
#define false
Definition: bool.h:36
#define NULL
Definition: ncbistd.hpp:225
#define _TRACE(message)
Definition: ncbidbg.hpp:122
void NcbiWarningBox(const string &message, const string &title="Warning")
specialized Message Box function for reporting non-critical errors
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static int StringToInt(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to int.
Definition: ncbistr.cpp:630
static list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)
Split a string using specified delimiters.
Definition: ncbistr.cpp:3461
END_EVENT_TABLE()
static int version
Definition: mdb_load.c:29
const struct ncbi::grid::netcache::search::fields::SIZE size
unsigned int a
Definition: ncbi_localip.c:102
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
Standard mechanism to include OpenGL headers for all platforms.
int g(Seg_Gsm *spe, Seq_Mtf *psm, Thd_Gsm *tdg)
Definition: thrddgri.c:44
static CS_CONTEXT * context
Definition: will_convert.c:21
#define const
Definition: zconf.h:232
Modified on Thu Jun 13 17:28:05 2024 by modify_doxy.py rev. 669887