OpenVideo Documentation

   Main Page       Modules       Class Hierarchy       Alphabetical List       Compound List       File List       Compound Members       Related Pages   

GL_TEXTURE_2D_Sink.cxx

Go to the documentation of this file.
00001 /* ========================================================================
00002  * Copyright (C) 2005  Graz University of Technology
00003  *
00004  * This framework is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This framework is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this framework; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  * For further information please contact Denis Kalkofen under
00019  * <kalkofen@icg.tu-graz.ac.at> or write to Denis Kalkofen,
00020  * Graz University of Technology, Inffeldgasse 16a, A8010 Graz,
00021  * Austria.
00022  * ========================================================================
00023  * PROJECT: OpenVideo
00024  * ======================================================================== */
00032 #include <openvideo/nodes/GL_TEXTURE_2D_Sink.h>
00033 #include <openvideo/openVideo.h>
00034 
00035 #include <ace/Mutex.h>
00036 
00037 #include <GL/gl.h>
00038 #include <GL/glu.h>
00039 
00040 #ifdef  ENABLE_GL_TEXTURE_2D_SINK
00041 
00042 #if defined(OV_IS_WINXP) && !defined(_IS_KLIMTES_)
00043 #  pragma comment(lib,"opengl32.lib")
00044 #  pragma comment(lib,"glu32.lib")
00045 #  define WIN32_LEAN_AND_MEAN
00046 #endif
00047 
00048 #include <openvideo/State.h>
00049 #include <openvideo/Manager.h>
00050 
00051 namespace openvideo{
00052 
00053 unsigned int 
00054 GL_TEXTURE_2D_Sink::get_video_texture_id()
00055 {
00056 
00057     return video_texture_id[0];
00058 
00059 }
00060 
00061 GL_TEXTURE_2D_Sink::GL_TEXTURE_2D_Sink()
00062 {
00063     name = typeName = "GL_TEXTURE_2D_Sink";
00064     mutex = new ACE_Mutex();
00065     width=height=0; 
00066     isStarted=false;
00067     internalFormat=0;
00068     buffer=0;
00069     video_texture_id[0]=0;
00070 }
00071 
00072 void 
00073 GL_TEXTURE_2D_Sink::initPixelFormats()
00074 {
00075     //format_r8g8b8 = 0,
00076     //format_b8g8r8 = 1,
00077     //format_r8g8b8x8   = 2,
00078     //format_b8g8r8x8   = 3,
00079     //format_l8     = 5,
00080     this->pixelFormats.push_back(PIXEL_FORMAT(FORMAT_R8G8B8));
00081     this->pixelFormats.push_back(PIXEL_FORMAT(FORMAT_B8G8R8));
00082     this->pixelFormats.push_back(PIXEL_FORMAT(FORMAT_R8G8B8X8));
00083     this->pixelFormats.push_back(PIXEL_FORMAT(FORMAT_B8G8R8X8));
00084     this->pixelFormats.push_back(PIXEL_FORMAT(FORMAT_L8));
00085 }
00086 
00087 GL_TEXTURE_2D_Sink::~GL_TEXTURE_2D_Sink()
00088 {
00089     delete mutex;
00090 }
00091 
00095 void 
00096 GL_TEXTURE_2D_Sink::acquire()
00097 {
00098     mutex->acquire();
00099 }
00100 
00101 
00105 void 
00106 GL_TEXTURE_2D_Sink::release()
00107 {
00108     mutex->release();
00109 }
00110 
00111 void
00112 GL_TEXTURE_2D_Sink::init()
00113 {
00114      isStarted=false;
00115     //check if a device context is present
00116 #ifdef WIN32
00117     HGLRC curContext=NULL;
00118     curContext= wglGetCurrentContext();
00119     if(!curContext)
00120     {
00121         return;
00122     }
00123 #endif
00124     Manager::getInstance()->getLogger()->logEx("OpenVideo: init GL_TEXTURE_2D_Sink '%s' \n",name.c_str());
00125     mutex->acquire();
00126 
00127     
00128     //glteximage2d supported formats
00129     //glteximage2d supported formats
00131     //gl_color_index    each element is a single value, a color index. it is converted to fixed point (with an unspecified number of 0 bits to the right of the binary point), shifted left or right depending on the value and sign of gl_index_shift, and added to gl_index_offset (see glpixeltransfer). the resulting index is converted to a set of color components using the gl_pixel_map_i_to_r, gl_pixel_map_i_to_g, gl_pixel_map_i_to_b, and gl_pixel_map_i_to_a tables, and clamped to the range [0,1].
00132     //gl_red    each element is a single red component. it is converted to floating point and assembled into an rgba element by attaching 0.0 for green and blue, and 1.0 for alpha. each component is then multiplied by the signed scale factor gl_c_scale, added to the signed bias gl_c_bias, and clamped to the range [0,1] (see glpixeltransfer).
00133     //gl_green  each element is a single green component. it is converted to floating point and assembled into an rgba element by attaching 0.0 for red and blue, and 1.0 for alpha. each component is then multiplied by the signed scale factor gl_c_scale, added to the signed bias gl_c_bias, and clamped to the range [0,1] (see glpixeltransfer).
00134     //gl_blue   each element is a single blue component. it is converted to floating point and assembled into an rgba element by attaching 0.0 for red and green, and 1.0 for alpha. each component is then multiplied by the signed scale factor gl_c_scale, added to the signed bias gl_c_bias, and clamped to the range [0,1] (see glpixeltransfer).
00135     //gl_alpha  each element is a single red component. it is converted to floating point and assembled into an rgba element by attaching 0.0 for red, green, and blue. each component is then multiplied by the signed scale factor gl_c_scale, added to the signed bias gl_c_bias, and clamped to the range [0,1] (see glpixeltransfer).
00136     //gl_rgb    each element is an rgb triple. it is converted to floating point and assembled into an rgba element by attaching 1.0 for alpha. each component is then multiplied by the signed scale factor gl_c_scale, added to the signed bias gl_c_bias, and clamped to the range [0,1] (see glpixeltransfer).
00137     //gl_rgba   each element is a complete rgba element. it is converted to floating point. each component is then multiplied by the signed scale factor gl_c_scale, added to the signed bias gl_c_bias, and clamped to the range [0,1] (see glpixeltransfer).
00138     //gl_luminance  each element is a single luminance value. it is converted to floating point, and then assembled into an rgba element by replicating the luminance value three times for red, green, and blue, and attaching 1.0 for alpha. each component is then multiplied by the signed scale factor gl_c_scale, added to the signed bias gl_c_bias, and clamped to the range [0,1] (see glpixeltransfer).
00139     //gl_luminance_alpha    each element is a luminance/alpha pair. it is converted to floating point, and then assembled into an rgba element by replicating the luminance value three times for red, green, and blue. each component is then multiplied by the signed scale factor gl_c_scale, added to the signed bias gl_c_bias, and clamped to the range [0,1] (see glpixeltransfer).
00140 
00141     //windows extension
00142     //gl_bgr_ext    each pixel is a group of three components in this order: blue, green, red. gl_bgr_ext provides a format that matches the memory layout of windows device-independent bitmaps (dibs). thus your applications can use the same data with win32 function calls and opengl pixel function calls.
00143     //gl_bgra_ext   each pixel is a group of four components in this order: blue, green, red, alpha. gl_bgra_ext provides a format that matches the memory layout of windows device-independent bitmaps (dibs). thus your applications can use the same data with win32 function calls and opengl pixel function calls.
00144 
00145     //
00146     //enum pixel_format {
00147     //format_r8g8b8 = 0,
00148     //format_b8g8r8 = 1,
00149     //format_r8g8b8x8   = 2,
00150     //format_b8g8r8x8   = 3,
00151     //format_r5g6b5 = 4,
00152     //format_l8     = 5,
00153 
00154     //format_unknown    = 6
00155     //};
00156     
00157     // map ov to gl pixel format and set gl_texture_2d_sink's format
00158     switch(curPixelFormat)
00159     {
00160         case FORMAT_R8G8B8:
00161             format=GL_RGB;
00162             internalFormat=3;
00163             break;
00164 
00165         case FORMAT_B8G8R8:
00166             format=GL_BGR_EXT;
00167             internalFormat=3;
00168             break;
00169         
00170         case FORMAT_R8G8B8X8:
00171             format=GL_RGBA;
00172             internalFormat=4;
00173             break;
00174 
00175         case FORMAT_B8G8R8X8:
00176             format=GL_BGRA_EXT;
00177             internalFormat=4;
00178             break;
00179 
00180 
00181 //      case FORMAT_R5G6B5:
00182 //          format=;
00183 //          internalFormat=;
00184 //          break;
00185 
00186         case FORMAT_L8:
00187             format=GL_LUMINANCE;
00188             internalFormat=GL_LUMINANCE8;
00189             break;
00190 
00191         default:
00192             Manager::getInstance()->getLogger()->logEx("OpenVideo: GL_TEXTURE_2D_Sink does not suppport the current pixel format %s\n",
00193                 (PixelFormat::FormatToString(curPixelFormat)).c_str());
00194             return;
00195     }
00196     //
00197     
00198     state=this->inputs[0]->getState();
00199     if(state)
00200     {
00201         this->width=state->width;
00202         this->height=state->height;
00203     }
00204     flip_h = false; 
00205     flip_v = true;
00206     float u_rt = (float)width / TEXTURE_WIDTH;
00207     float v_rt = (float)height / TEXTURE_HEIGHT;
00208     t_u0 = (flip_h ? u_rt : 0   );
00209     t_u1 = (flip_h ?    0 : u_rt);
00210     t_v0 = (flip_v ? v_rt : 0   );
00211     t_v1 = (flip_v ?    0 : v_rt);
00212     //
00213     //create texture
00214     glEnable(GL_TEXTURE_2D);
00215     long data_size = 4 * sizeof(GLubyte) * TEXTURE_WIDTH * TEXTURE_HEIGHT;
00216     GLubyte *data = (GLubyte*)malloc(data_size);
00217     memset(data, 0xFF, data_size);
00218     glGenTextures(1, &video_texture_id[0]);
00219     glBindTexture(GL_TEXTURE_2D, video_texture_id[0]);
00220     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
00221     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
00222     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00223     glTexImage2D(GL_TEXTURE_2D, 0, this->internalFormat, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0,
00224          this->format, GL_UNSIGNED_BYTE, data);
00225     
00226     glDisable(GL_TEXTURE_2D);
00227     free(data);
00228     mutex->release();
00229     //check gl errors
00230     GLenum e;
00231     if ((e = glGetError ()) != GL_NO_ERROR)
00232     {   
00233         Manager::getInstance()->getLogger()->logEx("GL error: %s\n", gluErrorString(e));
00234         Manager::getInstance()->getLogger()->logEx("OpenVideo: unable to init GL_TEXTURE_2D_Sink -> check if an opengl context is set");
00235         return ;
00236     }
00238     isStarted=true;
00239 
00240 }
00241 
00242 
00243 
00244 void
00245 GL_TEXTURE_2D_Sink::process()
00246 {
00247     if(!Manager::hasGLContext)
00248         return;
00249     if(!isStarted)
00250     {
00251         init();
00252         return;
00253     }
00254 
00255     if(Buffer* buffer = state->getCurrentBuffer())
00256     {
00257         buffer->lock();
00258 
00259         mutex->acquire();
00260         glEnable(GL_TEXTURE_2D);
00261         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00262         glEnable(GL_TEXTURE_2D);
00263         glBindTexture(GL_TEXTURE_2D, video_texture_id[0]);
00264         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width,
00265                         height, this->format, GL_UNSIGNED_BYTE,
00266                         (void*)buffer->getPixels());
00267 
00268         glDisable(GL_TEXTURE_2D);
00269         GLenum e;
00270         while ((e = glGetError ()) != GL_NO_ERROR)
00271         {
00272             printf("checkGLErrors(): GL error: %s\n", gluErrorString(e));
00273 
00274         }
00275         mutex->release();
00276 
00277         buffer->unlock();
00278     }
00279 
00280 }
00281 }//namespace openvideo{
00282 #endif  //ENABLE_GL_TEXTURE_2D_SINK
 This page was generated at Wed May 31 13:04:16 2006 for OpenVideo by Doxygen.
 If you have any comments, please send a message to schmalstieg@icg.tu-graz.ac.at.
www.studierstube.org