Carna Version 3.3.3
Loading...
Searching...
No Matches
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>
18#include <Carna/base/Log.h>
19#include <memory>
20
25namespace Carna
26{
27
28namespace base
29{
30
31
32
33// ----------------------------------------------------------------------------------
34// GLContext
35// ----------------------------------------------------------------------------------
36
62class CARNA_LIB GLContext
63{
64
66
67 struct Details;
68 const std::unique_ptr< Details > pimpl;
69
70protected:
71
75 explicit GLContext( bool isDoubleBuffered );
76
77 friend class RenderState;
78
83
88
93
94public:
95
96 const static unsigned int DEPTH_BUFFER_BIT;
97 const static unsigned int COLOR_BUFFER_BIT;
98
102 virtual ~GLContext();
103
108
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
143protected:
144
148 virtual void activate() const = 0;
149
150}; // GLContext
151
152
153
154// ----------------------------------------------------------------------------------
155// QGLContextAdapter
156// ----------------------------------------------------------------------------------
157
173template< typename QGLContext, typename QGLFormat >
175{
176
177 QGLContext& qglcontext;
178
179public:
180
185
189 static QGLFormat desiredFormat();
190
191protected:
192
193 virtual void activate() const override;
194
195}; // QGLContextAdapter
196
197
198template< typename QGLContext, typename QGLFormat >
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
221template< 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
231template< 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
Defines Carna::base::CarnaException, Carna::base::AssertionFailure.
#define CARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
#define CARNA_FAIL(description)
Causes a break point in debug mode and throws an AssertionFailure.
Defines Carna::base::Log.
Represents an association.
Definition Association.h:45
Wraps and represents an OpenGL context.
Definition GLContext.h:63
GLContext(bool isDoubleBuffered)
Instantiates GLContext that represents the current OpenGL context.
static GLContext & current()
References the current OpenGL context wrapper.
const RenderState & currentRenderState() const
References the latest render state.
void makeCurrent() const
Makes the OpenGL context represented by this object the current one.
virtual void activate() const =0
Activates the OpenGL context represented by this object.
virtual ~GLContext()
Deletes.
void popRenderState()
Restores previous render state.
void pushRenderState(const RenderState &rs)
Makes rs the current render state.
static const unsigned int DEPTH_BUFFER_BIT
Wraps GL_DEPTH_BUFFER_BIT.
Definition GLContext.h:96
void setShader(const ShaderProgram &shader)
Makes shader the current shader of the represented OpenGL context.
void clearBuffers(unsigned int flags)
Wraps glClear. Automatically enables on glDepthMask temporarily if the DEPTH_BUFFER_BIT is supplied.
const bool isDoubleBuffered
Tells whether the represented OpenGL context uses double buffering.
Definition GLContext.h:107
bool isCurrent() const
Tells whether the OpenGL context represented by this object is the current one.
const ShaderProgram & shader() const
References the shader set last.
static const unsigned int COLOR_BUFFER_BIT
Wraps GL_COLOR_BUFFER_BIT
Definition GLContext.h:97
@ debug
Indicates messages that might be of interest when searching bugs.
Definition Log.h:88
If you're using Qt, this class template implements the abstract GLContext class as an adapter to the ...
Definition GLContext.h:175
static QGLFormat desiredFormat()
Holds the recommended format that shall be used to create a QGLContext.
Definition GLContext.h:222
virtual void activate() const override
Activates the OpenGL context represented by this object.
Definition GLContext.h:232
QGLContextAdapter()
Creates GLContext wrapper for the current QGLContext object.
Definition GLContext.h:199
Manages the OpenGL render state.
Definition RenderState.h:86
Maintains an OpenGL shader program. Realizes the RAII-idiom.
static Log & instance()
Returns the only instance from class InstanceType.
Definition Singleton.h:109
#define NON_COPYABLE
Features class it is placed in as non-copyable.