LibCarna Version 3.4.0
Loading...
Searching...
No Matches
GLContext.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010 - 2016 Leonid Kostrykin
3 *
4 * Chair of Medical Engineering (mediTEC)
5 * RWTH Aachen University
6 * Pauwelsstr. 20
7 * 52074 Aachen
8 * Germany
9 *
10 *
11 * Copyright (C) 2021 - 2025 Leonid Kostrykin
12 *
13 */
14
15#ifndef GLCONTEXT_H_6014714286
16#define GLCONTEXT_H_6014714286
17
18#include <LibCarna/LibCarna.hpp>
21#include <LibCarna/base/Log.hpp>
22#include <memory>
23
29namespace LibCarna
30{
31
32namespace base
33{
34
35
36
37// ----------------------------------------------------------------------------------
38// GLContext
39// ----------------------------------------------------------------------------------
40
65class LIBCARNA GLContext
66{
67
69
70 struct Details;
71 const std::unique_ptr< Details > pimpl;
72
73protected:
74
78 explicit GLContext( bool isDoubleBuffered );
79
80 friend class RenderState;
81
86
91
96
97public:
98
99 const static unsigned int DEPTH_BUFFER_BIT;
100 const static unsigned int COLOR_BUFFER_BIT;
101
105 virtual ~GLContext();
106
111
116
120 void makeCurrent() const;
121
126 bool isCurrent() const;
127
132 void setShader( const ShaderProgram& shader );
133
138 const ShaderProgram& shader() const;
139
144 void clearBuffers( unsigned int flags );
145
146protected:
147
151 virtual void activate() const = 0;
152
153}; // GLContext
154
155
156
157// ----------------------------------------------------------------------------------
158// QGLContextAdapter
159// ----------------------------------------------------------------------------------
160
175template< typename QGLContext, typename QGLFormat >
177{
178
179 QGLContext& qglcontext;
180
181public:
182
187
191 static QGLFormat desiredFormat();
192
193protected:
194
195 virtual void activate() const override;
196
197}; // QGLContextAdapter
198
199
200template< typename QGLContext, typename QGLFormat >
203 , qglcontext( const_cast< QGLContext& >( *QGLContext::currentContext() ) )
204{
205 const QGLFormat& format = QGLContext::currentContext()->format();
206 if( format.majorVersion() < 3 || ( format.majorVersion() == 3 && format.minorVersion() < 3 ) )
207 {
208 std::stringstream msg;
209 msg << "OpenGL context version " << format.majorVersion() << "." << format.minorVersion() << " is too low.";
210 LIBCARNA_FAIL( msg.str() );
211 }
212 else
213 {
214 LIBCARNA_ASSERT( format.profile() != QGLFormat::NoProfile );
215 std::stringstream msg;
216 msg << "Recognized OpenGL " << format.majorVersion() << "." << format.minorVersion() << " context (";
217 msg << ( format.profile() == QGLFormat::CoreProfile ? "core" : "compatibility" ) << " profile)";
218 Log::instance().record( Log::debug, msg.str() );
219 }
220}
221
222
223template< typename QGLContext, typename QGLFormat >
225{
226 QGLFormat format = QGLFormat::defaultFormat();
227 format.setVersion( 3, 3 );
228 format.setProfile( QGLFormat::CompatibilityProfile );
229 return format;
230}
231
232
233template< typename QGLContext, typename QGLFormat >
235{
236 qglcontext.makeCurrent();
237}
238
239
240
241} // namespace LibCarna :: base
242
243} // namespace LibCarna
244
245#endif // GLCONTEXT_H_6014714286
Defines LibCarna::base::LibCarnaException and LibCarna::base::AssertionFailure.
#define LIBCARNA_FAIL(description)
Causes a break point in debug mode and throws an AssertionFailure.
#define LIBCARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
Contains forward-declarations.
Defines LibCarna::base::Log.
Represents an association.
Wraps and represents an OpenGL context.
Definition GLContext.hpp:66
void makeCurrent() const
Makes the OpenGL context represented by this object the current one.
static const unsigned int DEPTH_BUFFER_BIT
Wraps GL_DEPTH_BUFFER_BIT.
Definition GLContext.hpp:99
const ShaderProgram & shader() const
References the shader set last.
void popRenderState()
Restores previous render state.
void clearBuffers(unsigned int flags)
Wraps glClear. Automatically enables on glDepthMask temporarily if the DEPTH_BUFFER_BIT is supplied.
const RenderState & currentRenderState() const
References the latest render state.
virtual void activate() const =0
Activates the OpenGL context represented by this object.
void pushRenderState(const RenderState &rs)
Makes rs the current render state.
bool isCurrent() const
Tells whether the OpenGL context represented by this object is the current one.
virtual ~GLContext()
Deletes.
const bool isDoubleBuffered
Tells whether the represented OpenGL context uses double buffering.
void setShader(const ShaderProgram &shader)
Makes shader the current shader of the represented OpenGL context.
static const unsigned int COLOR_BUFFER_BIT
Wraps GL_COLOR_BUFFER_BIT
static GLContext & current()
References the current OpenGL context wrapper.
GLContext(bool isDoubleBuffered)
Instantiates GLContext that represents the current OpenGL context.
@ debug
Indicates messages that might be of interest when searching bugs.
Definition Log.hpp:91
If you're using Qt, this class template implements the abstract GLContext class as an adapter to the ...
virtual void activate() const override
Activates the OpenGL context represented by this object.
QGLContextAdapter()
Creates GLContext wrapper for the current QGLContext object.
static QGLFormat desiredFormat()
Holds the recommended format that shall be used to create a QGLContext.
Manages the OpenGL render state.
Maintains an OpenGL shader program. Realizes the RAII-idiom.
static Log & instance()
Returns the only instance from class InstanceType.
Defines LibCarna::base::noncopyable and NON_COPYABLE.
#define NON_COPYABLE
Marks the class that it is placed in as non-copyable.