00001 #include "OSDLGLTexture.h"
00002
00003 #include "OSDLSurface.h"
00004 #include "OSDLVideo.h"
00005
00006
00007 #ifdef OSDL_USES_CONFIG_H
00008 #include <OSDLConfig.h>
00009 #endif // OSDL_USES_CONFIG_H
00010
00011 #ifdef OSDL_HAVE_OPENGL
00012 #include "SDL_opengl.h"
00013 #endif // OSDL_HAVE_OPENGL
00014
00015
00016
00017 using std::string ;
00018
00019
00020 using namespace Ceylan ;
00021 using namespace Ceylan::Log ;
00022 using namespace Ceylan::Maths ;
00023
00024 using namespace OSDL::Video::OpenGL ;
00025
00026
00027 GLTexture::TextureMode GLTexture::CurrentTextureMode = TwoDim ;
00028
00029
00030
00031 GLTextureException::GLTextureException( const std::string & reason ) throw() :
00032 OpenGLException( reason )
00033 {
00034
00035 }
00036
00037
00038 GLTextureException::~GLTextureException() throw()
00039 {
00040
00041 }
00042
00043
00044
00045
00046 GLTexture::GLTexture( const std::string imageFilename,
00047 Textureflavour flavour )
00048 throw( GLTextureException ) :
00049 _source( 0 ),
00050 _id( 0 )
00051 {
00052
00053 #if OSDL_USES_OPENGL
00054
00055 Surface * loaded ;
00056
00057 try
00058 {
00059
00060 loaded = & Surface::LoadImage( imageFilename,
00061 false ) ;
00062
00063 }
00064 catch( const TwoDimensional::ImageException & e )
00065 {
00066
00067 throw GLTextureException(
00068 "GLTexture constructor : unable to load source image from file "
00069 + imageFilename + " : " + e.toString() ) ;
00070 }
00071
00072 upload( *loaded, flavour ) ;
00073
00074
00075 delete loaded ;
00076
00077 #else // OSDL_USES_OPENGL
00078
00079 throw GLTextureException( "OpenGL support not available." ) ;
00080
00081 #endif // OSDL_USES_OPENGL
00082
00083 }
00084
00085
00086
00087 GLTexture::GLTexture( Surface & sourceSurface, Textureflavour flavour )
00088 throw( GLTextureException ) :
00089 _source( 0 )
00090 {
00091
00092 upload( sourceSurface, flavour ) ;
00093
00094 }
00095
00096
00097
00098 GLTexture::~GLTexture() throw()
00099 {
00100
00101 #if OSDL_USES_OPENGL
00102
00103 if ( _id != 0 )
00104 glDeleteTextures( 1, & _id ) ;
00105
00106 if ( _source != 0 )
00107 delete _source ;
00108
00109 #endif // OSDL_USES_OPENGL
00110
00111 }
00112
00113
00114
00115 bool GLTexture::canBeUploaded() const throw()
00116 {
00117
00118 return ( _source != 0 ) ;
00119
00120 }
00121
00122
00123 void GLTexture::upload() throw( GLTextureException )
00124 {
00125
00126
00127 #if OSDL_USES_OPENGL
00128
00129 if ( ! canBeUploaded() )
00130 throw GLTextureException( "GLTexture::upload : "
00131 "texture cannot be uploaded into OpenGL context." ) ;
00132
00133
00134
00135
00136 #else // OSDL_USES_OPENGL
00137
00138 throw GLTextureException( "OpenGL support not available." ) ;
00139
00140 #endif // OSDL_USES_OPENGL
00141
00142 }
00143
00144
00145 const string GLTexture::toString( Ceylan::VerbosityLevels level ) const throw()
00146 {
00147
00148 string res = "OpenGL texture, " ;
00149
00150 if ( _id == 0 )
00151 res += "which has no OpenGL identifier" ;
00152 else
00153 res += "whose OpenGL identifier is " + Ceylan::toString( _id ) ;
00154
00155 if ( _source == 0 )
00156 res += ", and which has no available internal surface "
00157 "kept for reload." ;
00158 else
00159 res += ", which owns an internal surface, "
00160 "kept for reloading purposes." ;
00161
00162 return res ;
00163
00164 }
00165
00166
00167
00168
00169
00170
00171 GLTexture::TextureMode GLTexture::GetTextureMode() throw()
00172 {
00173
00174 return CurrentTextureMode ;
00175
00176 }
00177
00178
00179 void GLTexture::SetTextureMode( TextureMode newMode ) throw()
00180 {
00181
00182 CurrentTextureMode = newMode ;
00183
00184 }
00185
00186
00187
00188 void GLTexture::SetTextureFlavour( Textureflavour flavour )
00189 throw( GLTextureException )
00190 {
00191
00192 #if OSDL_USES_OPENGL
00193
00194 switch( flavour )
00195 {
00196
00197 case None:
00198 return ;
00199 break ;
00200
00201
00202 case Basic:
00203
00204
00205
00206
00207
00208
00209 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ) ;
00210
00211
00212
00213
00214
00215
00216 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ) ;
00217
00218
00219
00220
00221
00222
00223 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ) ;
00224
00225
00226
00227
00228
00229
00230 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP ) ;
00231
00232 break ;
00233
00234
00235 default:
00236 LogPlug::warning(
00237 "GLTexture::SetTextureFlavour : unknown flavour : "
00238 + Ceylan::toString( flavour ) + "." ) ;
00239 break ;
00240
00241 }
00242
00243 #else // OSDL_USES_OPENGL
00244
00245 throw GLTextureException( "OpenGL support not available." ) ;
00246
00247 #endif // OSDL_USES_OPENGL
00248
00249 }
00250
00251
00252
00253 void GLTexture::upload( Surface & sourceSurface,
00254 Textureflavour flavour ) throw( GLTextureException )
00255 {
00256
00257 #if OSDL_USES_OPENGL
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453 SDL_Surface * sourceInternal = & sourceSurface.getSDLSurface() ;
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465 Flags savedFlags = sourceInternal->flags & (
00466 Surface::AlphaBlendingBlit | Surface::RLEColorkeyBlitAvailable ) ;
00467
00468 Pixels::ColorElement savedAlpha = sourceInternal->format->alpha ;
00469
00470 bool mustModifyOverallAlpha = static_cast<bool>(
00471 savedFlags & Surface::AlphaBlendingBlit ) ;
00472
00473 if ( mustModifyOverallAlpha )
00474 SDL_SetAlpha( sourceInternal, 0, 0 ) ;
00475
00476 Length width = sourceInternal->w ;
00477 Length height = sourceInternal->h ;
00478
00479
00480 SDL_Surface * convertedSurface = SDL_CreateRGBSurface(
00481 VideoModule::SoftwareSurface,
00482 width, height, 32 ,
00483 RedMask, GreenMask, BlueMask, AlphaMask ) ;
00484
00485 if ( convertedSurface == 0 )
00486 throw GLTextureException(
00487 "GLTexture::upload : RGB surface creation failed." ) ;
00488
00489
00490 int res = SDL_BlitSurface( sourceInternal, 0, convertedSurface, 0 ) ;
00491
00492
00493 if ( mustModifyOverallAlpha )
00494 SDL_SetAlpha( sourceInternal, savedFlags, savedAlpha ) ;
00495
00496 if ( res != 0 )
00497 {
00498 SDL_FreeSurface( convertedSurface ) ;
00499 throw GLTextureException( "GLTexture::upload : blit failed (returned "
00500 + Ceylan::toString( res ) + ")." ) ;
00501 }
00502
00503
00504
00505 glGenTextures( 1, & _id ) ;
00506
00507 glBindTexture( GL_TEXTURE_2D, _id ) ;
00508
00509
00510 SetTextureFlavour( flavour ) ;
00511
00512 if ( IsAPowerOfTwo( width ) && IsAPowerOfTwo( height ) )
00513 glTexImage2D(
00514 GL_TEXTURE_2D,
00515 0,
00516 GL_RGBA,
00517 width,
00518 height,
00519 0,
00520 GL_RGBA,
00521 GL_UNSIGNED_BYTE,
00522 convertedSurface->pixels ) ;
00523 else
00524 gluBuild2DMipmaps(
00525 GL_TEXTURE_2D,
00526 GL_RGBA,
00527 width,
00528 height,
00529 GL_RGBA,
00530 GL_UNSIGNED_BYTE,
00531 convertedSurface->pixels ) ;
00532
00533 SDL_FreeSurface( convertedSurface ) ;
00534
00535 #else // OSDL_USES_OPENGL
00536
00537 throw GLTextureException( "OpenGL support not available." ) ;
00538
00539 #endif // OSDL_USES_OPENGL
00540
00541 }
00542