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

Go to the SVN repository for this file.

1 /* $Id: glresmgr.cpp 43585 2019-08-05 16:38:10Z katargir $
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: Bob Falk
27  *
28  * File Description:
29  * OpenGL resource manager implementation
30  */
31 
32 
33 #include <ncbi_pch.hpp>
34 
35 #include <ncbiconf.h>
36 
37 #include <gui/opengl/glresmgr.hpp>
38 #include <gui/opengl/glutils.hpp>
39 #include "glvbogeom.hpp"
40 #include <gui/opengl/glrender.hpp>
41 
42 #include <gui/opengl/gltexture.hpp>
44 
45 #ifdef USE_METAL
46  #include "mtlrender.hpp"
47  #include "mtlvbogeom.hpp"
48  #include "mtltexture.hpp"
49  #include "mtlframebuffer.hpp"
50  #include "mtltreenodevbo.hpp"
51 #else
52  #include "gltreenodevbo.hpp"
53 #endif // USE_METAL
54 
56 
58 
60 {
61  //static CGlResMgr instance;
62  //return instance;
63  if (m_StaticInstance == NULL) {
64  // Create the instance. This will not succeed if the OpenGL context
65  // has not yet been initialized.
66  try {
68  }
69  catch(...) {
70  // Make sure instance stays NULL, and re-throw
73  "Attempt to access OpenGL prior to context creation");
74  }
75  }
76 
77  return *m_StaticInstance;
78 }
79 
81 {
82  return (m_StaticInstance!=NULL);
83 }
84 
86 {
87  SetApiLevel();
88 
89  CIRef<IRender> rm;
90 
91  if (m_ApiLevel == eOpenGL20) {
92  rm.Reset(new CGlRender20());
93  AddRenderer(rm);
95  }
96  else if (m_ApiLevel == eOpenGL11) {
97  rm.Reset(new CGlRender11());
98  AddRenderer(rm);
100  }
101 #ifdef USE_METAL
102  else if (m_ApiLevel == eMetal) {
103  rm.Reset(new CMtlRender());
104  AddRenderer(rm);
105  SetCurrentRenderer(rm);
106  }
107 #endif // USE_METAL
108  else if (m_ApiLevel == eRenderDebug) {
109  rm.Reset(new CGlRenderDebug());
110  AddRenderer(rm);
111  SetCurrentRenderer(rm);
112  }
113  else {
114  _TRACE("Error - valid OpenGL version not detected.");
115  }
116 }
117 
119 {
120 }
121 
123 {
124 }
125 
127 {
128 #ifdef USE_METAL
129  m_ApiLevel = eMetal;
130  return;
131 #else
132  const char *v = (const char*)glGetString(GL_VERSION);
133 
134  // If OpenGL is not available, NULL will be returned. Throw an exception
135  if (v==NULL) {
137  "Attempt to access OpenGL prior to context creation");
138  }
139 
140  string version = v;
141  vector<string> arr;
142 
143  if (version != "")
144  NStr::Split(version, ". \t", arr);
145 
146  bool version_two = false;
147 
148  // Check major version
149  int major_version = 0;
150  if (arr.size() >= 1) {
151  try {
152  major_version = NStr::StringToInt(arr[0]);
153  if (major_version > 1) {
154  version_two = true;
155  }
156  }
157  catch (CException& ) {
158  //didn't parse as an int...
160  }
161  }
162  else {
164  }
165 
166  // Can check minor version too, if needed:
167  /*
168  if (arr.size() > 1 && major_version == 1) {
169  try {
170  int minor_version = NStr::StringToInt(arr[1]);
171  if (minor_version > 4) {
172  version_two = true;
173  }
174  }
175  catch (CException&) {
176  //minor version didn't parse as an int
177  }
178  }
179  */
180 
181  if (version_two)
183  else
185 #endif // USE_METAL
186 }
188 {
189  m_ApiLevel = level;
190 }
191 
192 IVboGeom* CGlResMgr::CreateVboGeom(GLenum drawMode, const string& name)
193 {
194 #ifdef USE_METAL
195  if (drawMode == GL_LINES)
196  return new CMtlVboGeomLines(name);
197 
198  return new CMtlVboGeom(drawMode, name);
199 #else
200  if (m_ApiLevel == eOpenGL20)
201  return new CGlVboGeom20(drawMode, name);
202  else
203  return new CGlVboGeom11(drawMode, name);
204 #endif // USE_METAL
205 }
206 
208 {
209 #ifdef USE_METAL
210  return new CMtlNarrowTreeEdgeVboGeom(edgeGeom);
211 #else
212  if (m_ApiLevel == eOpenGL20)
213  return new CNarrowTreeEdgeVboGeom20(edgeGeom);
214  else
215  return new CNarrowTreeEdgeVboGeom11(edgeGeom);
216 #endif // USE_METAL
217 }
218 
220 {
221 #ifdef USE_METAL
222  return new CMtlFillerPointVboGeom(edgeGeom);
223 #else
224  if (m_ApiLevel == eOpenGL20)
225  return new CFillerPointVboGeom20(edgeGeom);
226  else
227  return new CFillerPointVboGeom11(edgeGeom);
228 #endif // USE_METAL
229 }
230 
232 {
233 #ifdef USE_METAL
234  return new CMtlTreeNodeVbo(numSegments);
235 #else
236  if (m_ApiLevel == eOpenGL20)
237  return new CGlTreeNodeVbo20(numSegments);
238  else
239  return new CGlTreeNodeVbo11(numSegments);
240 #endif // USE_METAL
241 }
242 
244 {
245  vector<CIRef<IRender> >::iterator iter;
246  for (iter = m_Renderers.begin(); iter != m_Renderers.end(); ++iter) {
247  if ((*iter)->GetApi() == rm->GetApi()) {
248  break;
249  }
250  }
251 
252  if (iter == m_Renderers.end())
253  m_Renderers.push_back(rm);
254 }
255 
257 {
258  vector<CIRef<IRender> >::iterator iter;
259  iter = std::find(m_Renderers.begin(), m_Renderers.end(), rm);
260 
261  if (iter != m_Renderers.end()) {
262  m_Renderers.erase(iter);
263  return *iter;
264  }
265 
266  return CIRef<IRender>();
267 }
268 
270 {
271  vector<CIRef<IRender> >::iterator iter;
272 
273  for (iter=m_Renderers.begin(); iter!=m_Renderers.end(); ++iter) {
274  if ((**iter).GetApi() == target)
275  return *iter;
276  }
277 
278  return CIRef<IRender>();
279 }
280 
282 {
283  vector<CIRef<IRender> >::iterator iter;
284  iter = std::find(m_Renderers.begin(), m_Renderers.end(), rm);
285 
286  // Set current rendermanager to NULL if not found (so caller will know update
287  // didn't work and can fix).
288  if (iter != m_Renderers.end())
289  m_CurrentRenderer = rm;
290  else
292 }
293 
294 
296 {
297  m_Renderers.clear();
298 
300 }
301 
303 {
304 #ifdef USE_METAL
305  return new CMtlTexture(istr, tag);
306 #else
307  return new CGlTexture(istr, tag);
308 #endif // USE_METAL
309 }
310 
312 {
313 #ifdef USE_METAL
314  return new CMtlTexture(image);
315 #else
316  return new CGlTexture(image);
317 #endif // USE_METAL
318 }
319 
321 {
322 #ifdef USE_METAL
323  CRef<CMtlTexture> tex(new CMtlTexture());
324  tex->Load1DRGBA(width, data);
325  return tex.Release();
326 #else
327  CRef<CGlTexture> tex(new CGlTexture());
328  tex->Load1DRGBA(width, data);
329  return tex.Release();
330 #endif // USE_METAL
331 }
332 
334 {
335 #ifdef USE_METAL
336  return new CMtlFrameBuffer(dim);
337 #else
338  return new CGLFrameBuffer(dim);
339 #endif // USE_METAL
340 }
341 
CGLFrameBuffer.
CGlRender11.
Definition: glrender.hpp:306
CGlRender20.
Definition: glrender.hpp:284
CGlRenderDebug.
Definition: glrender.hpp:324
CImage –.
Definition: Image.hpp:66
CMtlFrameBuffer.
char data[12]
Definition: iconv.c:80
#define NULL
Definition: ncbistd.hpp:225
#define _TRACE(message)
Definition: ncbidbg.hpp:122
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
static CGlResMgr & Instance()
Definition: glresmgr.cpp:59
~CGlResMgr()
Destroys all gl objects it created.
Definition: glresmgr.cpp:118
CIRef< IRender > GetRenderer(ERenderTarget target)
Returns first renderer in m_Renderers that renders to 'target'.
Definition: glresmgr.cpp:269
IVboGeom * CreateFillerPointVboGeom(IVboGeom *edgeGeom)
Definition: glresmgr.cpp:219
void Clear()
Deletes all objects.
Definition: glresmgr.cpp:122
std::vector< CIRef< IRender > > m_Renderers
Definition: glresmgr.hpp:119
void ClearRenderers()
Clear all current rendering objects.
Definition: glresmgr.cpp:295
ERenderTarget m_ApiLevel
Definition: glresmgr.hpp:117
void AddRenderer(CIRef< IRender > rm)
Get/Set Renderer.
Definition: glresmgr.cpp:243
CIRef< IRender > RemoveRenderer(CIRef< IRender > rm)
Definition: glresmgr.cpp:256
ERenderTarget
Different api levels based on information from OpenGL driver.
Definition: glstate.hpp:61
static bool Initialized()
return true if static instance has been created (via a call to Instance()
Definition: glresmgr.cpp:80
static CGlResMgr * m_StaticInstance
Definition: glresmgr.hpp:115
I3DFrameBuffer * CreateFrameBuffer(size_t dim)
Definition: glresmgr.cpp:333
void SetApiLevel()
Find GL object (add type option?) TBD.
Definition: glresmgr.cpp:126
void SetCurrentRenderer(CIRef< IRender > rm)
Set current renderer (rm must already be in m_Renderers)
Definition: glresmgr.cpp:281
IVboGeom * CreateTreeNodeVboGeom(size_t numSegments)
Definition: glresmgr.cpp:231
I3DTexture * Create1DRGBATexture(size_t width, float *data)
Definition: glresmgr.cpp:320
IVboGeom * CreateVboGeom(GLenum drawMode, const string &name)
Get geometry object for a node that renders using vertex buffers.
Definition: glresmgr.cpp:192
IVboGeom * CreateNarrowTreeEdgeVboGeom(IVboGeom *edgeGeom)
Definition: glresmgr.cpp:207
void Load1DRGBA(size_t width, float *data)
CIRef< IRender > m_CurrentRenderer
Definition: glresmgr.hpp:120
CGlResMgr()
Needs active OpenGL context when created. Determines best api level.
Definition: glresmgr.cpp:85
I3DTexture * CreateTexture(CNcbiIstream &istr, const string &tag)
Definition: glresmgr.cpp:302
@ eOpenGL20
Definition: glstate.hpp:61
@ eMetal
Definition: glstate.hpp:61
@ eApiUndefined
Definition: glstate.hpp:61
@ eOpenGL11
Definition: glstate.hpp:61
@ eRenderDebug
Definition: glstate.hpp:61
TObjectType * ReleaseOrNull(void)
Release a reference to the object and return a pointer to the object.
Definition: ncbiobj.hpp:816
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
TObjectType * Release(void)
Release a reference to the object and return a pointer to the object.
Definition: ncbiobj.hpp:846
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
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
static int version
Definition: mdb_load.c:29
const char * tag
Front end for a platform-specific configuration summary.
Modified on Wed Jun 19 17:05:39 2024 by modify_doxy.py rev. 669887