GL_TEXTURE_2D_Sink.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00076
00077
00078
00079
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
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
00129
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
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
00182
00183
00184
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
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
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 }
00282 #endif //ENABLE_GL_TEXTURE_2D_SINK