2013-07-24 13:06:59 +00:00
|
|
|
/*
|
2013-07-23 16:39:07 +00:00
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2015-02-15 01:18:35 +00:00
|
|
|
* Copyright (C) 2013-2015 CERN
|
2013-07-23 16:39:07 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
2014-02-24 10:17:49 +00:00
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
2013-07-23 16:39:07 +00:00
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file opengl_compositor.cpp
|
2015-02-15 01:18:35 +00:00
|
|
|
* @brief Class that handles multitarget rendering (i.e. to different textures/surfaces) and
|
2013-07-23 16:39:07 +00:00
|
|
|
* later compositing into a single image (OpenGL flavour).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gal/opengl/opengl_compositor.h>
|
2015-02-15 01:18:35 +00:00
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <cassert>
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
using namespace KIGFX;
|
2013-07-23 16:39:07 +00:00
|
|
|
|
|
|
|
OPENGL_COMPOSITOR::OPENGL_COMPOSITOR() :
|
2014-07-09 09:22:42 +00:00
|
|
|
m_initialized( false ), m_current( 0 ), m_currentFbo( DIRECT_RENDERING )
|
2013-07-23 16:39:07 +00:00
|
|
|
{
|
2015-03-13 16:48:42 +00:00
|
|
|
// Avoid not initialized members:
|
|
|
|
m_framebuffer = 0;
|
|
|
|
m_depthBuffer = 0;
|
2013-07-23 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OPENGL_COMPOSITOR::~OPENGL_COMPOSITOR()
|
|
|
|
{
|
|
|
|
if( m_initialized )
|
|
|
|
clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OPENGL_COMPOSITOR::Initialize()
|
|
|
|
{
|
|
|
|
if( m_initialized )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// We need framebuffer objects for drawing the screen contents
|
|
|
|
// Generate framebuffer and a depth buffer
|
2013-10-29 16:28:29 +00:00
|
|
|
glGenFramebuffersEXT( 1, &m_framebuffer );
|
2014-11-13 15:30:30 +00:00
|
|
|
glBindFramebufferEXT( GL_FRAMEBUFFER, m_framebuffer );
|
2013-07-23 16:39:07 +00:00
|
|
|
m_currentFbo = m_framebuffer;
|
|
|
|
|
|
|
|
// Allocate memory for the depth buffer
|
|
|
|
// Attach the depth buffer to the framebuffer
|
2013-10-29 16:28:29 +00:00
|
|
|
glGenRenderbuffersEXT( 1, &m_depthBuffer );
|
|
|
|
glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, m_depthBuffer );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
|
|
|
// Use here a size of 24 bits for the depth buffer, 8 bits for the stencil buffer
|
|
|
|
// this is required later for anti-aliasing
|
2015-09-09 08:49:25 +00:00
|
|
|
glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT16, m_width, m_height );
|
2013-10-29 16:28:29 +00:00
|
|
|
glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT,
|
2014-07-09 09:22:42 +00:00
|
|
|
GL_RENDERBUFFER_EXT, m_depthBuffer );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
|
|
|
// Unbind the framebuffer, so by default all the rendering goes directly to the display
|
2013-10-29 16:28:29 +00:00
|
|
|
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, DIRECT_RENDERING );
|
2014-07-09 09:22:42 +00:00
|
|
|
m_currentFbo = DIRECT_RENDERING;
|
2013-07-23 16:39:07 +00:00
|
|
|
|
|
|
|
m_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OPENGL_COMPOSITOR::Resize( unsigned int aWidth, unsigned int aHeight )
|
|
|
|
{
|
|
|
|
if( m_initialized )
|
|
|
|
clean();
|
|
|
|
|
|
|
|
m_width = aWidth;
|
|
|
|
m_height = aHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-19 09:02:38 +00:00
|
|
|
unsigned int OPENGL_COMPOSITOR::CreateBuffer()
|
2013-07-23 16:39:07 +00:00
|
|
|
{
|
2015-02-15 01:18:35 +00:00
|
|
|
assert( m_initialized );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2014-11-13 15:37:15 +00:00
|
|
|
unsigned int maxBuffers;
|
2014-07-09 09:22:42 +00:00
|
|
|
|
2014-11-13 15:37:15 +00:00
|
|
|
// Get the maximum number of buffers
|
2014-07-09 09:22:42 +00:00
|
|
|
glGetIntegerv( GL_MAX_COLOR_ATTACHMENTS, (GLint*) &maxBuffers );
|
|
|
|
|
|
|
|
if( usedBuffers() >= maxBuffers )
|
2013-10-14 09:39:21 +00:00
|
|
|
{
|
2015-02-15 01:18:35 +00:00
|
|
|
throw std::runtime_error("Cannot create more framebuffers. OpenGL rendering "
|
2013-11-11 09:48:49 +00:00
|
|
|
"backend requires at least 3 framebuffers. You may try to update/change "
|
2015-02-15 01:18:35 +00:00
|
|
|
"your graphic drivers.");
|
2013-10-14 09:39:21 +00:00
|
|
|
}
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-07-24 13:06:59 +00:00
|
|
|
// GL_COLOR_ATTACHMENTn are consecutive integers
|
|
|
|
GLuint attachmentPoint = GL_COLOR_ATTACHMENT0 + usedBuffers();
|
|
|
|
GLuint textureTarget;
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-07-24 13:06:59 +00:00
|
|
|
// Generate the texture for the pixel storage
|
|
|
|
glGenTextures( 1, &textureTarget );
|
|
|
|
glBindTexture( GL_TEXTURE_2D, textureTarget );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-07-24 13:06:59 +00:00
|
|
|
// Set texture parameters
|
|
|
|
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
|
2014-07-09 09:22:42 +00:00
|
|
|
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, m_width, m_height, 0, GL_RGBA,
|
2013-07-24 13:06:59 +00:00
|
|
|
GL_UNSIGNED_BYTE, NULL );
|
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
|
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-07-24 13:06:59 +00:00
|
|
|
// Bind the texture to the specific attachment point, clear and rebind the screen
|
2013-10-29 16:28:29 +00:00
|
|
|
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_framebuffer );
|
2013-07-24 13:06:59 +00:00
|
|
|
m_currentFbo = m_framebuffer;
|
2014-07-09 09:22:42 +00:00
|
|
|
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, attachmentPoint,
|
|
|
|
GL_TEXTURE_2D, textureTarget, 0 );
|
2013-08-02 08:34:23 +00:00
|
|
|
|
|
|
|
// Check the status, exit if the framebuffer can't be created
|
2013-10-29 16:28:29 +00:00
|
|
|
GLenum status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
|
2013-08-02 08:34:23 +00:00
|
|
|
|
2013-10-29 16:28:29 +00:00
|
|
|
if( status != GL_FRAMEBUFFER_COMPLETE_EXT )
|
2013-08-02 08:34:23 +00:00
|
|
|
{
|
|
|
|
switch( status )
|
|
|
|
{
|
2013-10-29 16:28:29 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
|
2015-02-15 01:18:35 +00:00
|
|
|
throw std::runtime_error( "Cannot create the framebuffer." );
|
2013-08-02 08:34:23 +00:00
|
|
|
break;
|
|
|
|
|
2013-10-29 16:28:29 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
|
2015-02-15 01:18:35 +00:00
|
|
|
throw std::runtime_error( "The framebuffer attachment points are incomplete." );
|
2013-08-02 08:34:23 +00:00
|
|
|
break;
|
|
|
|
|
2013-10-29 16:28:29 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
|
2015-02-15 01:18:35 +00:00
|
|
|
throw std::runtime_error( "The framebuffer does not have at least one "
|
|
|
|
"image attached to it." );
|
2013-08-02 08:34:23 +00:00
|
|
|
break;
|
|
|
|
|
2013-10-29 16:28:29 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
|
2015-02-15 01:18:35 +00:00
|
|
|
throw std::runtime_error( "The framebuffer read buffer is incomplete." );
|
2013-08-02 08:34:23 +00:00
|
|
|
break;
|
|
|
|
|
2013-10-29 16:28:29 +00:00
|
|
|
case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
|
2015-02-15 01:18:35 +00:00
|
|
|
throw std::runtime_error( "The combination of internal formats of the attached "
|
|
|
|
"images violates an implementation-dependent set of restrictions." );
|
2013-08-02 08:34:23 +00:00
|
|
|
break;
|
|
|
|
|
2013-10-29 16:28:29 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT:
|
2015-02-15 01:18:35 +00:00
|
|
|
throw std::runtime_error( "GL_RENDERBUFFER_SAMPLES is not the same for "
|
|
|
|
"all attached renderbuffers" );
|
2013-08-02 08:34:23 +00:00
|
|
|
break;
|
|
|
|
|
2013-10-29 16:28:29 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT:
|
2015-02-15 01:18:35 +00:00
|
|
|
throw std::runtime_error( "Framebuffer incomplete layer targets errors." );
|
2013-08-02 08:34:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-02-15 01:18:35 +00:00
|
|
|
throw std::runtime_error( "Cannot create the framebuffer." );
|
2013-08-02 08:34:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-10-14 09:39:21 +00:00
|
|
|
|
|
|
|
return 0;
|
2013-08-02 08:34:23 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 13:06:59 +00:00
|
|
|
ClearBuffer();
|
2013-09-04 14:25:57 +00:00
|
|
|
|
|
|
|
// Return to direct rendering (we were asked only to create a buffer, not switch to one)
|
2013-10-29 16:28:29 +00:00
|
|
|
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, DIRECT_RENDERING );
|
2013-08-19 09:02:38 +00:00
|
|
|
m_currentFbo = DIRECT_RENDERING;
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-07-24 13:06:59 +00:00
|
|
|
// Store the new buffer
|
|
|
|
OPENGL_BUFFER buffer = { textureTarget, attachmentPoint };
|
|
|
|
m_buffers.push_back( buffer );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-07-24 13:06:59 +00:00
|
|
|
return usedBuffers();
|
2013-07-23 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OPENGL_COMPOSITOR::SetBuffer( unsigned int aBufferHandle )
|
|
|
|
{
|
2013-07-24 13:06:59 +00:00
|
|
|
if( aBufferHandle > usedBuffers() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Change the rendering destination to the selected attachment point
|
2013-08-19 09:02:38 +00:00
|
|
|
if( aBufferHandle == DIRECT_RENDERING )
|
2013-07-30 15:09:06 +00:00
|
|
|
{
|
2013-08-19 09:02:38 +00:00
|
|
|
m_currentFbo = DIRECT_RENDERING;
|
2013-07-30 15:09:06 +00:00
|
|
|
}
|
|
|
|
else if( m_currentFbo != m_framebuffer )
|
2013-07-23 16:39:07 +00:00
|
|
|
{
|
2013-10-29 16:28:29 +00:00
|
|
|
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_framebuffer );
|
2013-07-24 13:06:59 +00:00
|
|
|
m_currentFbo = m_framebuffer;
|
|
|
|
}
|
|
|
|
|
2013-08-19 09:02:38 +00:00
|
|
|
if( m_currentFbo != DIRECT_RENDERING )
|
2013-07-24 13:06:59 +00:00
|
|
|
{
|
|
|
|
m_current = aBufferHandle - 1;
|
|
|
|
glDrawBuffer( m_buffers[m_current].attachmentPoint );
|
2013-07-23 16:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OPENGL_COMPOSITOR::ClearBuffer()
|
|
|
|
{
|
2015-02-15 01:18:35 +00:00
|
|
|
assert( m_initialized );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
|
|
|
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
|
|
|
|
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-24 13:06:59 +00:00
|
|
|
void OPENGL_COMPOSITOR::DrawBuffer( unsigned int aBufferHandle )
|
2013-07-23 16:39:07 +00:00
|
|
|
{
|
2015-02-15 01:18:35 +00:00
|
|
|
assert( m_initialized );
|
|
|
|
assert( aBufferHandle != 0 && aBufferHandle <= usedBuffers() );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
|
|
|
// Switch to the main framebuffer and blit the scene
|
2013-10-29 16:28:29 +00:00
|
|
|
glBindFramebufferEXT( GL_FRAMEBUFFER, DIRECT_RENDERING );
|
2013-08-19 09:02:38 +00:00
|
|
|
m_currentFbo = DIRECT_RENDERING;
|
2013-07-23 16:39:07 +00:00
|
|
|
|
|
|
|
// Depth test has to be disabled to make transparency working
|
|
|
|
glDisable( GL_DEPTH_TEST );
|
|
|
|
glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
|
|
|
|
|
|
|
|
// Enable texturing and bind the main texture
|
|
|
|
glEnable( GL_TEXTURE_2D );
|
|
|
|
glBindTexture( GL_TEXTURE_2D, m_buffers[aBufferHandle - 1].textureTarget );
|
|
|
|
|
|
|
|
// Draw a full screen quad with the texture
|
|
|
|
glMatrixMode( GL_MODELVIEW );
|
|
|
|
glPushMatrix();
|
|
|
|
glLoadIdentity();
|
|
|
|
glMatrixMode( GL_PROJECTION );
|
|
|
|
glPushMatrix();
|
|
|
|
glLoadIdentity();
|
|
|
|
|
|
|
|
glBegin( GL_TRIANGLES );
|
|
|
|
glTexCoord2f( 0.0f, 1.0f );
|
2013-07-24 13:06:59 +00:00
|
|
|
glVertex2f( -1.0f, -1.0f );
|
2013-07-23 16:39:07 +00:00
|
|
|
glTexCoord2f( 1.0f, 1.0f );
|
2013-07-24 13:06:59 +00:00
|
|
|
glVertex2f( 1.0f, -1.0f );
|
2013-07-23 16:39:07 +00:00
|
|
|
glTexCoord2f( 1.0f, 0.0f );
|
2013-07-24 13:06:59 +00:00
|
|
|
glVertex2f( 1.0f, 1.0f );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
|
|
|
glTexCoord2f( 0.0f, 1.0f );
|
2013-07-24 13:06:59 +00:00
|
|
|
glVertex2f( -1.0f, -1.0f );
|
2013-07-23 16:39:07 +00:00
|
|
|
glTexCoord2f( 1.0f, 0.0f );
|
2013-07-24 13:06:59 +00:00
|
|
|
glVertex2f( 1.0f, 1.0f );
|
2013-07-23 16:39:07 +00:00
|
|
|
glTexCoord2f( 0.0f, 0.0f );
|
2013-07-24 13:06:59 +00:00
|
|
|
glVertex2f( -1.0f, 1.0f );
|
2013-07-23 16:39:07 +00:00
|
|
|
glEnd();
|
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
glMatrixMode( GL_MODELVIEW );
|
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OPENGL_COMPOSITOR::clean()
|
|
|
|
{
|
2015-02-15 01:18:35 +00:00
|
|
|
assert( m_initialized );
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2014-07-09 09:22:42 +00:00
|
|
|
glBindFramebufferEXT( GL_FRAMEBUFFER, DIRECT_RENDERING );
|
|
|
|
m_currentFbo = DIRECT_RENDERING;
|
2013-07-23 16:39:07 +00:00
|
|
|
|
2013-07-24 13:06:59 +00:00
|
|
|
OPENGL_BUFFERS::const_iterator it;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-07-23 16:39:07 +00:00
|
|
|
for( it = m_buffers.begin(); it != m_buffers.end(); ++it )
|
|
|
|
{
|
|
|
|
glDeleteTextures( 1, &it->textureTarget );
|
|
|
|
}
|
2013-07-30 16:29:54 +00:00
|
|
|
|
2013-07-23 16:39:07 +00:00
|
|
|
m_buffers.clear();
|
|
|
|
|
2014-07-09 09:22:42 +00:00
|
|
|
glDeleteFramebuffersEXT( 1, &m_framebuffer );
|
|
|
|
glDeleteRenderbuffersEXT( 1, &m_depthBuffer );
|
|
|
|
|
2013-07-23 16:39:07 +00:00
|
|
|
m_initialized = false;
|
|
|
|
}
|