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

Go to the SVN repository for this file.

1 /* $Id: gloscontext.cpp 47479 2023-05-02 13:24:02Z ucko $
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: Mike DiCuccio
27  *
28  * File Description:
29  * CGlOsContext -- wrapper for Mesa3D's off-screen rendering extension
30  */
31 
32 #include <ncbi_pch.hpp>
34 
36 
37 //
38 // CGlOsContext ctor
39 // initialize our renderer without a frame buffer. This allows
40 // multiple frame buffers to be used.
41 CGlOsContext::CGlOsContext(unsigned stencilBits)
42 {
43  x_InitContext(stencilBits);
44 }
45 
46 //
47 // CGlOsContext ctor
48 // initialize our renderer with a virtual frame buffer of a given size.
49 // This frame buffer will always be initialized as an RGBA image.
50 CGlOsContext::CGlOsContext(size_t width, size_t height, unsigned stencilBits)
51  : m_Image(new CImage(width, height, 4))
52 {
53  x_InitContext(stencilBits);
54 }
55 
56 //
57 // Called by ctors for context initialization
58 void CGlOsContext::x_InitContext(unsigned stencilBits)
59 {
60  // create our off-screen mesa rendering context
61  // this context will use the screen image we've defined
62 #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
63  m_Ctx = OSMesaCreateContextExt( OSMESA_RGBA, 16, stencilBits, 0, NULL );
64 #else
65  m_Ctx = OSMesaCreateContext( OSMESA_RGBA, NULL );
66 #endif
67 
68  if ( !m_Ctx ) {
69  LOG_POST(Error << "CGlOsContext(): "
70  "Failed to create off-screen rendering context");
71  }
72 }
73 
74 // destructor for cleaning up our OSMesa context
76 {
77  if (m_Ctx) {
78  OSMesaDestroyContext(m_Ctx);
79  }
80 }
81 
82 
83 // make the current frame buffer the active buffer for rendering
85 {
86  if ( !m_Ctx ) {
87  LOG_POST(Error << "CGlOsContext::MakeCurrent(): "
88  "Attempt to make an invalid context current");
89  return false;
90  }
91 
92  if ( !OSMesaMakeCurrent(m_Ctx, m_Image->SetData(), GL_UNSIGNED_BYTE,
93  static_cast<GLsizei>(m_Image->GetWidth()), static_cast<GLsizei>(m_Image->GetHeight())) ) {
94  LOG_POST(Error << "CGlOsContext::MakeCurrent(): "
95  "Failed to make image surface current (w:" << m_Image->GetWidth() << ", h:" << m_Image->GetHeight() << ")");
96  return false;
97  }
98 
99  return true;
100 }
101 
102 // make the current frame buffer the active buffer for rendering
103 bool CGlOsContext::MakeCurrent(size_t width, size_t height)
104 {
105  m_Image->Init(width, height, 4);
106 
107  if ( !m_Ctx ) {
108  LOG_POST(Error << "CGlOsContext::MakeCurrent(): "
109  "Attempt to make an invalid context current");
110  return false;
111  }
112 
113  if ( !OSMesaMakeCurrent(m_Ctx, m_Image->SetData(), GL_UNSIGNED_BYTE,
114  static_cast<GLsizei>(m_Image->GetWidth()), static_cast<GLsizei>(m_Image->GetHeight())) ) {
115  LOG_POST(Error << "CGlOsContext::MakeCurrent(): "
116  "Failed to make image surface current");
117  return false;
118  }
119 
120  return true;
121 }
122 
123 // make the current frame buffer the active buffer for rendering
125 {
126  if ( !m_Ctx ) {
127  LOG_POST(Error << "CGlOsContext::MakeCurrent(): "
128  "Attempt to make an invalid context current");
129  return false;
130  }
131 
132  m_Image.Reset(img);
133 
134  if ( !OSMesaMakeCurrent(m_Ctx, m_Image->SetData(), GL_UNSIGNED_BYTE,
135  static_cast<GLsizei>(m_Image->GetWidth()), static_cast<GLsizei>(m_Image->GetHeight())) ) {
136  LOG_POST(Error << "CGlOsContext::MakeCurrent(): "
137  "Failed to make image surface current");
138  return false;
139  }
140 
141  return true;
142 }
143 
144 void CGlOsContext::GetMaximumBufferSize(GLint& max_width, GLint& max_height)
145 {
146  OSMesaGetIntegerv( OSMESA_MAX_WIDTH, &max_width );
147  OSMesaGetIntegerv( OSMESA_MAX_HEIGHT, &max_height );
148 }
149 
150 void CGlOsContext::SetPixelStore(GLint pname, GLint value)
151 {
152  OSMesaPixelStore(pname, value);
153 }
154 
155 // access the image we use for the virtual frame buffer
156 const CImage& CGlOsContext::GetBuffer(void) const
157 {
158  return *m_Image;
159 }
160 
161 
162 // access the image we use for the virtual frame buffer
164 {
165  return *m_Image;
166 }
167 
168 void CGlOsContext::Resize(size_t width, size_t height)
169 {
170  m_Image.Reset(new CImage(width, height, 4));
171 }
172 
173 
OSMesaContext m_Ctx
CImage & SetBuffer(void)
void SetPixelStore(GLint pname, GLint value)
CRef< CImage > m_Image
bool MakeCurrent(void)
Definition: gloscontext.cpp:84
void GetMaximumBufferSize(GLint &max_width, GLint &max_height)
virtual ~CGlOsContext()
Definition: gloscontext.cpp:75
void Resize(size_t width, size_t height)
Resize frame buffer size.
void x_InitContext(unsigned stencilBits)
Definition: gloscontext.cpp:58
const CImage & GetBuffer(void) const
CGlOsContext(unsigned stencilBits=0)
Definition: gloscontext.cpp:41
CImage –.
Definition: Image.hpp:66
size_t GetWidth(void) const
Definition: image.hpp:98
void Init(size_t width, size_t height, size_t depth)
Definition: image.cpp:62
size_t GetHeight(void) const
Definition: image.hpp:99
unsigned char * SetData(void)
Definition: image.cpp:92
#define NULL
Definition: ncbistd.hpp:225
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Modified on Sun May 19 04:46:47 2024 by modify_doxy.py rev. 669887