Back to index

salome-gui  6.5.0
OpenGLUtils_FrameBuffer.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 //  File   : OpenGLUtils_FrameBuffer.cxx
00021 //  Module : SALOME
00022 //
00023 #include "OpenGLUtils_FrameBuffer.h"
00024 
00025 #include <utilities.h>
00026 
00027 #include <cstring>
00028 
00029 #ifndef WNT
00030 # ifndef GLX_GLXEXT_LEGACY
00031 #  define GLX_GLXEXT_LEGACY
00032 # endif
00033 # include <GL/glx.h>
00034 # include <dlfcn.h>
00035 #else
00036 # include <wingdi.h>
00037 #endif
00038 
00039 #ifndef APIENTRY
00040 #define APIENTRY
00041 #endif
00042 #ifndef APIENTRYP
00043 #define APIENTRYP APIENTRY *
00044 #endif
00045 
00046 #ifndef GL_FRAMEBUFFER_EXT
00047 #define GL_FRAMEBUFFER_EXT                0x8D40
00048 #endif
00049 
00050 #ifndef GL_RENDERBUFFER_EXT
00051 #define GL_RENDERBUFFER_EXT               0x8D41
00052 #endif
00053 
00054 #ifndef GL_COLOR_ATTACHMENT0_EXT
00055 #define GL_COLOR_ATTACHMENT0_EXT          0x8CE0
00056 #endif
00057 
00058 #ifndef GL_DEPTH_ATTACHMENT_EXT
00059 #define GL_DEPTH_ATTACHMENT_EXT           0x8D00
00060 #endif
00061 
00062 #ifndef GL_FRAMEBUFFER_COMPLETE_EXT
00063 #define GL_FRAMEBUFFER_COMPLETE_EXT       0x8CD5
00064 #endif
00065 
00066 typedef void (APIENTRYP PFNGLGENFRAMEBUFFERSEXTPROC) (GLsizei n, GLuint *framebuffers);
00067 typedef void (APIENTRYP PFNGLBINDFRAMEBUFFEREXTPROC) (GLenum target, GLuint framebuffer);
00068 typedef void (APIENTRYP PFNGLFRAMEBUFFERTEXTURE2DEXTPROC) (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level);
00069 typedef GLenum (APIENTRYP PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC) (GLenum target);
00070 typedef void (APIENTRYP PFNGLDELETEFRAMEBUFFERSEXTPROC) (GLsizei n, const GLuint *framebuffers);
00071 typedef void (APIENTRYP PFNGLGENRENDERBUFFERSEXTPROC) (GLsizei n, GLuint *renderbuffers);
00072 typedef void (APIENTRYP PFNGLBINDRENDERBUFFEREXTPROC) (GLenum target, GLuint renderbuffer);
00073 typedef void (APIENTRYP PFNGLRENDERBUFFERSTORAGEEXTPROC) (GLenum target, GLenum internalformat, GLsizei width, GLsizei height);
00074 typedef void (APIENTRYP PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC) (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer);
00075 typedef void (APIENTRYP PFNGLDELETERENDERBUFFERSEXTPROC) (GLsizei n, const GLuint *renderbuffers);
00076 
00077 static PFNGLGENFRAMEBUFFERSEXTPROC vglGenFramebuffersEXT = NULL;
00078 static PFNGLBINDFRAMEBUFFEREXTPROC vglBindFramebufferEXT = NULL;
00079 static PFNGLFRAMEBUFFERTEXTURE2DEXTPROC vglFramebufferTexture2DEXT = NULL;
00080 static PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC vglCheckFramebufferStatusEXT = NULL;
00081 static PFNGLDELETEFRAMEBUFFERSEXTPROC vglDeleteFramebuffersEXT = NULL;
00082 static PFNGLGENRENDERBUFFERSEXTPROC vglGenRenderbuffersEXT = NULL;
00083 static PFNGLBINDRENDERBUFFEREXTPROC vglBindRenderbufferEXT = NULL;
00084 static PFNGLRENDERBUFFERSTORAGEEXTPROC vglRenderbufferStorageEXT = NULL;
00085 static PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC vglFramebufferRenderbufferEXT = NULL;
00086 static PFNGLDELETERENDERBUFFERSEXTPROC vglDeleteRenderbuffersEXT = NULL;
00087 
00088 #ifndef WNT
00089 #define GL_GetProcAddress( x ) glXGetProcAddressARB( (const GLubyte*)x )
00090 #else
00091 #define GL_GetProcAddress( x ) wglGetProcAddress( (const LPCSTR)x )
00092 #endif
00093 
00094 bool InitializeEXT()
00095 {
00096   vglGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC)GL_GetProcAddress( "glGenFramebuffersEXT" );
00097   vglBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)GL_GetProcAddress( "glBindFramebufferEXT" );
00098   vglFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)GL_GetProcAddress( "glFramebufferTexture2DEXT" );
00099   vglCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)GL_GetProcAddress( "glCheckFramebufferStatusEXT" );
00100   vglDeleteFramebuffersEXT = (PFNGLDELETEFRAMEBUFFERSEXTPROC)GL_GetProcAddress( "glDeleteFramebuffersEXT" );
00101   vglGenRenderbuffersEXT = (PFNGLGENRENDERBUFFERSEXTPROC)GL_GetProcAddress( "glGenRenderbuffersEXT" );
00102   vglBindRenderbufferEXT = (PFNGLBINDRENDERBUFFEREXTPROC)GL_GetProcAddress( "glBindRenderbufferEXT" );
00103   vglRenderbufferStorageEXT = (PFNGLRENDERBUFFERSTORAGEEXTPROC)GL_GetProcAddress( "glRenderbufferStorageEXT" );
00104   vglFramebufferRenderbufferEXT = (PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC)GL_GetProcAddress( "glFramebufferRenderbufferEXT" );
00105   vglDeleteRenderbuffersEXT = (PFNGLDELETERENDERBUFFERSEXTPROC)GL_GetProcAddress( "glDeleteRenderbuffersEXT" );
00106 
00107   bool ok = vglGenFramebuffersEXT && vglBindFramebufferEXT && vglFramebufferTexture2DEXT &&
00108             vglCheckFramebufferStatusEXT && vglDeleteFramebuffersEXT && vglGenRenderbuffersEXT &&
00109             vglBindRenderbufferEXT && vglRenderbufferStorageEXT && vglFramebufferRenderbufferEXT &&
00110             vglDeleteRenderbuffersEXT;
00111 
00112   return ok;
00113 }
00114 
00115 static bool IsEXTInitialized = InitializeEXT();
00116 
00117 OpenGLUtils_FrameBuffer::OpenGLUtils_FrameBuffer()
00118   : textureId( 0 ),
00119     fboId( 0 ),
00120     rboId( 0 )
00121 {
00122 }
00123 
00124 OpenGLUtils_FrameBuffer::~OpenGLUtils_FrameBuffer()
00125 {
00126   release();
00127 }
00128 
00129 bool OpenGLUtils_FrameBuffer::init( const GLsizei& xSize, const GLsizei& ySize )
00130 {
00131   char* ext = (char*)glGetString( GL_EXTENSIONS );
00132   if( !IsEXTInitialized ||
00133       strstr( ext, "GL_EXT_framebuffer_object" ) == NULL )
00134   {
00135     MESSAGE( "Initializing OpenGL FrameBuffer extension failed" );
00136     return false;
00137   }
00138 
00139   // create a texture object
00140   glEnable( GL_TEXTURE_2D );
00141   glGenTextures( 1, &textureId );
00142   glBindTexture( GL_TEXTURE_2D, textureId );
00143   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
00144   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00145   glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, xSize, ySize, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
00146   glBindTexture( GL_TEXTURE_2D, 0 );
00147 
00148   // create a renderbuffer object to store depth info
00149   vglGenRenderbuffersEXT( 1, &rboId );
00150   vglBindRenderbufferEXT( GL_RENDERBUFFER_EXT, rboId );
00151   vglRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, xSize, ySize );
00152   vglBindRenderbufferEXT( GL_RENDERBUFFER_EXT, 0 );
00153 
00154   // create a framebuffer object
00155   vglGenFramebuffersEXT( 1, &fboId );
00156   vglBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fboId );
00157 
00158   // attach the texture to FBO color attachment point
00159   vglFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, textureId, 0 );
00160 
00161   // attach the renderbuffer to depth attachment point
00162   vglFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, rboId );
00163 
00164   // check FBO status
00165   GLenum status = vglCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
00166 
00167   // Unbind FBO
00168   vglBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
00169 
00170   return status == GL_FRAMEBUFFER_COMPLETE_EXT;
00171 }
00172 
00173 void OpenGLUtils_FrameBuffer::release()
00174 {
00175   if( !IsEXTInitialized )
00176     return;
00177 
00178   glDeleteTextures( 1, &textureId );
00179   textureId = 0;
00180 
00181   vglDeleteFramebuffersEXT( 1, &fboId );
00182   fboId = 0;
00183 
00184   vglDeleteRenderbuffersEXT( 1, &rboId );
00185   rboId = 0;
00186 }
00187 
00188 void OpenGLUtils_FrameBuffer::bind()
00189 {
00190   if( !IsEXTInitialized )
00191     return;
00192 
00193   vglBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fboId );
00194 }
00195 
00196 void OpenGLUtils_FrameBuffer::unbind()
00197 {
00198   if( !IsEXTInitialized )
00199     return;
00200 
00201   vglBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
00202 }