Radium Engine  1.5.20
Loading...
Searching...
No Matches
OpenGLContext.hpp
1#pragma once
2#include <Core/Utils/Observable.hpp>
3#include <Headless/RaHeadless.hpp>
4
5#include <array>
6#include <functional>
7#include <string>
8
9#include <glbinding-aux/ContextInfo.h>
10#include <glbinding/Version.h>
11#include <glbinding/gl/gl.h>
12
13namespace Ra {
14namespace Headless {
15
22class HEADLESS_API OpenGLContext
23{
24 public:
41 explicit OpenGLContext( const glbinding::Version& glVersion = { 4, 1 },
42 const std::array<int, 2>& size = { { 1, 1 } } ) {
43 CORE_UNUSED( glVersion );
44 CORE_UNUSED( size );
45 };
47 virtual ~OpenGLContext() = default;
49 virtual void makeCurrent() const = 0;
51 virtual void doneCurrent() const = 0;
53 virtual bool isValid() const = 0;
54
56 virtual bool isWindow() const { return false; }
57
59 [[nodiscard]] virtual std::string getInfo() const;
68 enum class EventMode : int { POLL = 0, WAIT, TIMEOUT, NUM_MODES };
70 virtual void show( EventMode /*mode*/, float /*delay*/ ) {};
72 virtual void hide() {};
74 virtual void resize( const std::array<int, 2>& /*size*/ ) {};
76 virtual void renderLoop( std::function<void( float )> /*render*/ ) {};
77
85
86 // TODO : give access to the DPI ratio
87 // https://www.glfw.org/docs/latest/window_guide.html#window_scale
88
96 return m_keyboardObservers;
97 }
98
110
119
127
129 protected:
131 virtual bool processEvents() { return true; };
132
134 void resizeFrameBuffer( int width, int height );
137
139 void keyboardEventCallback( int key, int scancode, int action, int mods );
142
144 void mouseEventCallback( int button, int action, int mods, int x, int y );
147
149 void mouseMoveEventCallback( int x, int y );
152
154 void scrollEventCallback( int xoffset, int yoffset );
157
159 EventMode m_mode { EventMode::POLL };
161 float m_delay { 1.f / 60.f };
162};
163
165 std::stringstream infoText;
166 using ContextInfo = glbinding::aux::ContextInfo;
167 makeCurrent();
168 infoText << "*** OffScreen OpenGL context ***" << std::endl;
169 infoText << "Renderer (glbinding) : " << ContextInfo::renderer() << "\n";
170 infoText << "Vendor (glbinding) : " << ContextInfo::vendor() << "\n";
171 infoText << "OpenGL (glbinding) : " << ContextInfo::version().toString() << "\n";
172 infoText << "GLSL : " << gl::glGetString( gl::GL_SHADING_LANGUAGE_VERSION )
173 << "\n";
174 doneCurrent();
175
176 return infoText.str();
177}
178
179inline void OpenGLContext::resizeFrameBuffer( int width, int height ) {
180 gl::glViewport( 0, 0, width, height );
181 m_resizers.notify( width, height );
182}
183
184inline void OpenGLContext::keyboardEventCallback( int key, int scancode, int action, int mods ) {
185 m_keyboardObservers.notify( key, scancode, action, mods );
186}
187
188inline void OpenGLContext::mouseEventCallback( int button, int action, int mods, int x, int y ) {
189 m_mouseObservers.notify( button, action, mods, x, y );
190}
191
192inline void OpenGLContext::mouseMoveEventCallback( int x, int y ) {
194}
195
196inline void OpenGLContext::scrollEventCallback( int xoffset, int yoffset ) {
197 m_scrollObservers.notify( xoffset, yoffset );
198}
199
200} // namespace Headless
201} // namespace Ra
void notify(Args... p) const
Notify (i.e. call) each attached observer with argument p.
Ra::Core::Utils::Observable< int, int > m_scrollObservers
Scroll event observable.
void scrollEventCallback(int xoffset, int yoffset)
Scroll callback.
void mouseEventCallback(int button, int action, int mods, int x, int y)
Mouse callback.
virtual void resize(const std::array< int, 2 > &)
Resize the window.
virtual ~OpenGLContext()=default
destructor
virtual void doneCurrent() const =0
make the context inactive
Ra::Core::Utils::Observable< int, int > m_resizers
Resize event observable.
Ra::Core::Utils::Observable< int, int > & mouseMoveListener()
virtual void hide()
Hide the window.
Ra::Core::Utils::Observable< int, int > & scrollListener()
Ra::Core::Utils::Observable< int, int > m_mouseMoveObservers
Mouse move event observable.
virtual void renderLoop(std::function< void(float)>)
loop on events and execute the functor render after each event
void mouseMoveEventCallback(int x, int y)
Mouse move callback.
virtual void makeCurrent() const =0
make the context active
EventMode
Identify the event processing method when the window is exposed.
Ra::Core::Utils::Observable< int, int, int, int, int > & mouseListener()
Ra::Core::Utils::Observable< int, int, int, int > & keyboardListener()
OpenGLContext(const glbinding::Version &glVersion={ 4, 1 }, const std::array< int, 2 > &size={ { 1, 1 } })
void resizeFrameBuffer(int width, int height)
Resize callback.
virtual std::string getInfo() const
Return a string identifying the openGL Context and its supported versions.
void keyboardEventCallback(int key, int scancode, int action, int mods)
Keyboard callback.
Ra::Core::Utils::Observable< int, int, int, int > m_keyboardObservers
Keyboard event observable.
Ra::Core::Utils::Observable< int, int, int, int, int > m_mouseObservers
Mouse event observable.
virtual bool processEvents()
Process the pending events according to the window show mode.
virtual void show(EventMode, float)
Show the window.
virtual bool isValid() const =0
Check for validity of the context.
Ra::Core::Utils::Observable< int, int > & resizeListener()
virtual bool isWindow() const
Check if the context is associated to a window.
T endl(T... args)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
T str(T... args)