47 m_initialized( false ),
51 m_curFbo( DIRECT_RENDERING ),
66 catch(
const std::runtime_error& exc )
68 wxLogError( wxT(
"Run time exception `%s` occurred in OPENGL_COMPOSITOR destructor." ),
101 m_antialiasing = std::make_unique<ANTIALIASING_SUPERSAMPLING>(
this );
109 assert( dims.
x != 0 && dims.
y != 0 );
112 glGetIntegerv( GL_MAX_RENDERBUFFER_SIZE_EXT, &maxBufSize );
114 if( dims.
x < 0 || dims.
y < 0 || dims.
x > maxBufSize || dims.
y >= maxBufSize )
115 throw std::runtime_error(
"Requested render buffer size is not supported" );
120 checkGlError(
"generating framebuffer", __FILE__, __LINE__ );
126 checkGlError(
"generating renderbuffer", __FILE__, __LINE__ );
128 checkGlError(
"binding renderbuffer", __FILE__, __LINE__ );
130 glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8, dims.
x, dims.
y );
131 checkGlError(
"creating renderbuffer storage", __FILE__, __LINE__ );
132 glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_STENCIL_ATTACHMENT,
134 checkGlError(
"attaching renderbuffer", __FILE__, __LINE__ );
167 int maxBuffers, maxTextureSize;
170 glGetIntegerv( GL_MAX_COLOR_ATTACHMENTS, (GLint*) &maxBuffers );
174 throw std::runtime_error(
"Cannot create more framebuffers. OpenGL rendering backend "
175 "requires at least 3 framebuffers. You may try to update/change "
176 "your graphic drivers." );
179 glGetIntegerv( GL_MAX_TEXTURE_SIZE, (GLint*) &maxTextureSize );
181 if( maxTextureSize < (
int) aDimensions.
x || maxTextureSize < (
int) aDimensions.
y )
183 throw std::runtime_error(
"Requested texture size is not supported. "
184 "Could not create a buffer." );
188 GLuint attachmentPoint = GL_COLOR_ATTACHMENT0 +
usedBuffers();
189 GLuint textureTarget;
192 glActiveTexture( GL_TEXTURE0 );
193 glGenTextures( 1, &textureTarget );
194 checkGlError(
"generating framebuffer texture target", __FILE__, __LINE__ );
195 glBindTexture( GL_TEXTURE_2D, textureTarget );
196 checkGlError(
"binding framebuffer texture target", __FILE__, __LINE__ );
199 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
200 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, aDimensions.
x, aDimensions.
y, 0, GL_RGBA,
201 GL_UNSIGNED_BYTE,
nullptr );
202 checkGlError(
"creating framebuffer texture", __FILE__, __LINE__ );
203 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
204 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
208 glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, attachmentPoint, GL_TEXTURE_2D, textureTarget,
212 GLenum status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
214 if( status != GL_FRAMEBUFFER_COMPLETE_EXT )
218 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
219 throw std::runtime_error(
"The framebuffer attachment points are incomplete." );
221 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
222 throw std::runtime_error(
"No images attached to the framebuffer." );
224 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
225 throw std::runtime_error(
"The framebuffer does not have at least one "
226 "image attached to it." );
228 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
229 throw std::runtime_error(
"The framebuffer read buffer is incomplete." );
231 case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
232 throw std::runtime_error(
"The combination of internal formats of the attached "
233 "images violates an implementation-dependent set of "
236 case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT:
237 throw std::runtime_error(
"GL_RENDERBUFFER_SAMPLES is not the same for "
238 "all attached renderbuffers" );
240 case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT:
241 throw std::runtime_error(
"Framebuffer incomplete layer targets errors." );
243 case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
244 throw std::runtime_error(
"Framebuffer attachments have different dimensions" );
247 throw std::runtime_error(
"Unknown error occurred when creating the framebuffer." );
257 OPENGL_BUFFER buffer = { aDimensions, textureTarget, attachmentPoint };
266 wxASSERT( aBufferHandle > 0 && aBufferHandle <=
usedBuffers() );
267 return m_buffers[aBufferHandle - 1].textureTarget;
284 checkGlError(
"setting draw buffer", __FILE__, __LINE__ );
301 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
308 wxASSERT(
m_width <=
static_cast<unsigned int>( std::numeric_limits<coord_t>::max() ) );
309 wxASSERT(
m_height <=
static_cast<unsigned int>( std::numeric_limits<coord_t>::max() ) );
311 return {
static_cast<coord_t
>(
m_width ),
static_cast<coord_t
>(
m_height ) };
330 wxASSERT( aSourceHandle != 0 && aSourceHandle <=
usedBuffers() );
337 glDisable( GL_DEPTH_TEST );
338 glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
341 glEnable( GL_TEXTURE_2D );
342 glBindTexture( GL_TEXTURE_2D,
m_buffers[aSourceHandle - 1].textureTarget );
345 glMatrixMode( GL_MODELVIEW );
348 glMatrixMode( GL_PROJECTION );
352 glBegin( GL_TRIANGLES );
353 glTexCoord2f( 0.0f, 1.0f );
354 glVertex2f( -1.0f, 1.0f );
355 glTexCoord2f( 0.0f, 0.0f );
356 glVertex2f( -1.0f, -1.0f );
357 glTexCoord2f( 1.0f, 1.0f );
358 glVertex2f( 1.0f, 1.0f );
360 glTexCoord2f( 1.0f, 1.0f );
361 glVertex2f( 1.0f, 1.0f );
362 glTexCoord2f( 0.0f, 0.0f );
363 glVertex2f( -1.0f, -1.0f );
364 glTexCoord2f( 1.0f, 0.0f );
365 glVertex2f( 1.0f, -1.0f );
369 glMatrixMode( GL_MODELVIEW );
387 glBindFramebufferEXT( GL_FRAMEBUFFER, aFb );
388 checkGlError(
"switching framebuffer", __FILE__, __LINE__ );
401 glDeleteTextures( 1, &buffer.textureTarget );
405 if( glDeleteFramebuffersEXT )
406 glDeleteFramebuffersEXT( 1, &
m_mainFbo );
408 if( glDeleteRenderbuffersEXT )
A color representation with 4 components: red, green, blue, alpha.
static const COLOR4D BLACK
unsigned int m_height
Height of the buffer (in pixels)
unsigned int m_width
Width of the buffer (in pixels)
virtual ~OPENGL_COMPOSITOR()
VECTOR2D GetAntialiasRenderingOffset() const
OPENGL_ANTIALIASING_MODE m_currentAntialiasingMode
virtual void Begin() override
Call this at the beginning of each frame.
unsigned int usedBuffers()
Returns number of used buffers.
static const unsigned int DIRECT_RENDERING
VECTOR2I GetScreenSize() const
OPENGL_ANTIALIASING_MODE GetAntialiasingMode() const
void SetAntialiasingMode(OPENGL_ANTIALIASING_MODE aMode)
void clean()
Perform freeing of resources.
virtual void Present() override
Call this to present the output buffer to the screen.
bool m_initialized
Initialization status flag.
void bindFb(unsigned int aFb)
Binds a specific Framebuffer Object.
GLuint m_curFbo
Store the used FBO name in case there was more than one compositor used.
virtual void Initialize() override
Perform primary initialization, necessary to use the object.
GLuint m_depthBuffer
Depth buffer handle.
virtual void ClearBuffer(const COLOR4D &aColor) override
Clear the selected buffer (set by the SetBuffer() function).
std::unique_ptr< OPENGL_PRESENTOR > m_antialiasing
virtual void Resize(unsigned int aWidth, unsigned int aHeight) override
Clear the state of COMPOSITOR, so it has to be reinitialized again with the new dimensions.
OPENGL_BUFFERS m_buffers
Stores information about initialized buffers.
virtual void DrawBuffer(unsigned int aBufferHandle) override
Draw the selected buffer to the output buffer.
GLuint m_mainFbo
Main FBO handle (storing all target textures)
int GetAntialiasSupersamplingFactor() const
unsigned int m_curBuffer
Currently used buffer handle.
GLenum GetBufferTexture(unsigned int aBufferHandle)
virtual void SetBuffer(unsigned int aBufferHandle) override
Set the selected buffer as the rendering target.
virtual unsigned int CreateBuffer() override
Prepare a new buffer that may be used as a rendering target.
The Cairo implementation of the graphics abstraction layer.
@ NONE
No updates are required.
Handle multitarget rendering (ie.
int checkGlError(const std::string &aInfo, const char *aFile, int aLine, bool aThrow)
Check if a recent OpenGL operation has failed.
VECTOR2< double > VECTOR2D