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
106
110 virtual ~GLContext();
111
112 const std::string& vendor() const;
113
114 const std::string& renderer() const;
115
120
124 void makeCurrent() const;
125
130 bool isCurrent() const;
131
136 void setShader( const ShaderProgram& shader );
137
142 const ShaderProgram& shader() const;
143
148 void clearBuffers( unsigned int flags );
149
150protected:
151
155 virtual void activate() const = 0;
156
157}; // GLContext
158
159
160
161// ----------------------------------------------------------------------------------
162// QGLContextAdapter
163// ----------------------------------------------------------------------------------
164
179template< typename QGLContext, typename QGLFormat >
181{
182
183 QGLContext& qglcontext;
184
185public:
186
191
195 static QGLFormat desiredFormat();
196
197protected:
198
199 virtual void activate() const override;
200
201}; // QGLContextAdapter
202
203
204template< typename QGLContext, typename QGLFormat >
207 , qglcontext( const_cast< QGLContext& >( *QGLContext::currentContext() ) )
208{
209 const QGLFormat& format = QGLContext::currentContext()->format();
210 if( format.majorVersion() < 3 || ( format.majorVersion() == 3 && format.minorVersion() < 3 ) )
211 {
212 std::stringstream msg;
213 msg << "OpenGL context version " << format.majorVersion() << "." << format.minorVersion() << " is too low.";
214 LIBCARNA_FAIL( msg.str() );
215 }
216 else
217 {
218 LIBCARNA_ASSERT( format.profile() != QGLFormat::NoProfile );
219 std::stringstream msg;
220 msg << "Recognized OpenGL " << format.majorVersion() << "." << format.minorVersion() << " context (";
221 msg << ( format.profile() == QGLFormat::CoreProfile ? "core" : "compatibility" ) << " profile)";
222 Log::instance().record( Log::debug, msg.str() );
223 }
224}
225
226
227template< typename QGLContext, typename QGLFormat >
229{
230 QGLFormat format = QGLFormat::defaultFormat();
231 format.setVersion( 3, 3 );
232 format.setProfile( QGLFormat::CompatibilityProfile );
233 return format;
234}
235
236
237template< typename QGLContext, typename QGLFormat >
239{
240 qglcontext.makeCurrent();
241}
242
243
244
245} // namespace LibCarna :: base
246
247} // namespace LibCarna
248
249#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
const std::string & renderer() const
Wraps glGetString( GL_RENDERER ).
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 std::string & vendor() const
Wraps glGetString( GL_VENDOR ).
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.