Carna  Version 3.3.2
GLContext.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 - 2015 Leonid Kostrykin
3  *
4  * Chair of Medical Engineering (mediTEC)
5  * RWTH Aachen University
6  * Pauwelsstr. 20
7  * 52074 Aachen
8  * Germany
9  *
10  */
11 
12 #ifndef GLCONTEXT_H_6014714286
13 #define GLCONTEXT_H_6014714286
14 
15 #include <Carna/Carna.h>
17 #include <Carna/base/noncopyable.h>
18 #include <Carna/base/Log.h>
19 #include <memory>
20 
25 namespace Carna
26 {
27 
28 namespace base
29 {
30 
31 
32 
33 // ----------------------------------------------------------------------------------
34 // GLContext
35 // ----------------------------------------------------------------------------------
36 
62 class CARNA_LIB GLContext
63 {
64 
66 
67  struct Details;
68  const std::unique_ptr< Details > pimpl;
69 
70 protected:
71 
75  explicit GLContext( bool isDoubleBuffered );
76 
77  friend class RenderState;
78 
82  void pushRenderState( const RenderState& rs );
83 
87  void popRenderState();
88 
92  const RenderState& currentRenderState() const;
93 
94 public:
95 
96  const static unsigned int DEPTH_BUFFER_BIT;
97  const static unsigned int COLOR_BUFFER_BIT;
98 
102  virtual ~GLContext();
103 
107  const bool isDoubleBuffered;
108 
112  static GLContext& current();
113 
117  void makeCurrent() const;
118 
123  bool isCurrent() const;
124 
129  void setShader( const ShaderProgram& shader );
130 
135  const ShaderProgram& shader() const;
136 
141  void clearBuffers( unsigned int flags );
142 
143 protected:
144 
148  virtual void activate() const = 0;
149 
150 }; // GLContext
151 
152 
153 
154 // ----------------------------------------------------------------------------------
155 // QGLContextAdapter
156 // ----------------------------------------------------------------------------------
157 
173 template< typename QGLContext, typename QGLFormat >
175 {
176 
177  QGLContext& qglcontext;
178 
179 public:
180 
185 
189  static QGLFormat desiredFormat();
190 
191 protected:
192 
193  virtual void activate() const override;
194 
195 }; // QGLContextAdapter
196 
197 
198 template< typename QGLContext, typename QGLFormat >
200  : GLContext( QGLContext::currentContext()->format().doubleBuffer() )
201  , qglcontext( const_cast< QGLContext& >( *QGLContext::currentContext() ) )
202 {
203  const QGLFormat& format = QGLContext::currentContext()->format();
204  if( format.majorVersion() < 3 || ( format.majorVersion() == 3 && format.minorVersion() < 3 ) )
205  {
206  std::stringstream msg;
207  msg << "OpenGL context version " << format.majorVersion() << "." << format.minorVersion() << " is too low.";
208  CARNA_FAIL( msg.str() );
209  }
210  else
211  {
212  CARNA_ASSERT( format.profile() != QGLFormat::NoProfile );
213  std::stringstream msg;
214  msg << "Recognized OpenGL " << format.majorVersion() << "." << format.minorVersion() << " context (";
215  msg << ( format.profile() == QGLFormat::CoreProfile ? "core" : "compatibility" ) << " profile)";
216  Log::instance().record( Log::debug, msg.str() );
217  }
218 }
219 
220 
221 template< typename QGLContext, typename QGLFormat >
223 {
224  QGLFormat format = QGLFormat::defaultFormat();
225  format.setVersion( 3, 3 );
226  format.setProfile( QGLFormat::CompatibilityProfile );
227  return format;
228 }
229 
230 
231 template< typename QGLContext, typename QGLFormat >
233 {
234  qglcontext.makeCurrent();
235 }
236 
237 
238 
239 } // namespace Carna :: base
240 
241 } // namespace Carna
242 
243 #endif // GLCONTEXT_H_6014714286
QGLContextAdapter()
Creates GLContext wrapper for the current QGLContext object.
Definition: GLContext.h:199
Manages the OpenGL render state.
Definition: RenderState.h:85
static const unsigned int COLOR_BUFFER_BIT
Wraps GL_COLOR_BUFFER_BIT
Definition: GLContext.h:97
Indicates messages that might be of interest when searching bugs.
Definition: Log.h:88
virtual void activate() const override
Activates the OpenGL context represented by this object.
Definition: GLContext.h:232
static QGLFormat desiredFormat()
Holds the recommended format that shall be used to create a QGLContext.
Definition: GLContext.h:222
#define CARNA_FAIL(description)
Causes a break point in debug mode and throws an AssertionFailure.
Defines Carna::base::Log.
Maintains an OpenGL shader program. Realizes the RAII-idiom.
Definition: ShaderProgram.h:47
static Log & instance()
Returns the only instance from class InstanceType.
Definition: Singleton.h:109
const bool isDoubleBuffered
Tells whether the represented OpenGL context uses double buffering.
Definition: GLContext.h:107
If you&#39;re using Qt, this class template implements the abstract GLContext class as an adapter to the ...
Definition: GLContext.h:174
static const unsigned int DEPTH_BUFFER_BIT
Wraps GL_DEPTH_BUFFER_BIT.
Definition: GLContext.h:96
Defines Carna::base::CarnaException, Carna::base::AssertionFailure.
Wraps and represents an OpenGL context.
Definition: GLContext.h:62
void record(Severity severity, const std::string &entry) const
Instructs current writer to write entry with severity.
#define CARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
#define NON_COPYABLE
Features class it is placed in as non-copyable.
Definition: noncopyable.h:109