Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGDVRSimpleLUTShader.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000,2001 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 //---------------------------------------------------------------------------
00040 //  Includes
00041 //---------------------------------------------------------------------------
00042 
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045 
00046 #include <OSGConfig.h>
00047 
00048 #include <OSGGL.h>
00049 #include <OSGGLEXT.h>
00050 
00051 #include <OSGWindow.h>
00052 
00053 #include <OSGImage.h>
00054 
00055 #include "OSGDVRLookupTable.h"
00056 #include "OSGDVRSimpleShader.h"
00057 #include "OSGDVRVolumeTexture.h"
00058 #include "OSGDVRVolume.h"
00059 
00060 #include "OSGDVRSimpleLUTShader.h"
00061 
00062 OSG_USING_NAMESPACE
00063 
00069 UInt32 DVRSimpleLUTShader::_sgiTexColorTable         = 
00070     Window::invalidExtensionID;
00071 
00072 UInt32 DVRSimpleLUTShader::_extPalettedTexture       = 
00073     Window::invalidExtensionID;
00074 
00075 UInt32 DVRSimpleLUTShader::_extSharedPalettedTexture = 
00076     Window::invalidExtensionID;
00077 
00078 
00079 UInt32 DVRSimpleLUTShader::_arbMultitexture          = 
00080     Window::invalidFunctionID;
00081 
00082 UInt32 DVRSimpleLUTShader::_nvTextureShader2         = 
00083     Window::invalidFunctionID;
00084 
00085 UInt32 DVRSimpleLUTShader::_arbFragmentProgram       = 
00086     Window::invalidFunctionID;
00087 
00088 UInt32 DVRSimpleLUTShader::_nvRegisterCombiners      = 
00089     Window::invalidFunctionID;
00090 
00091 UInt32 DVRSimpleLUTShader::_funcColorTableSGI        = 
00092     Window::invalidFunctionID;
00093 
00094 UInt32 DVRSimpleLUTShader::_funcColorTableEXT        = 
00095     Window::invalidFunctionID;
00096 
00097 UInt32 DVRSimpleLUTShader::_funcActiveTextureARB     = 
00098     Window::invalidFunctionID;
00099 
00100 UInt32 DVRSimpleLUTShader::_funcFinalCombinerInputNV = 
00101     Window::invalidFunctionID;
00102 
00103 UInt32 DVRSimpleLUTShader::_funcCombinerInputNV      = 
00104     Window::invalidFunctionID;
00105 
00106 UInt32 DVRSimpleLUTShader::_funcCombinerOutputNV     = 
00107     Window::invalidFunctionID;
00108 
00109 /*----------------------- constructors & destructors ----------------------*/
00110 
00112 
00113 DVRSimpleLUTShader::DVRSimpleLUTShader(void) :
00114     Inherited            (      ),
00115     m_nTexturePaletteMode( LM_NO),
00116     m_nTextureMode       (    -1), 
00117     m_pFragProg          (NullFC),
00118     m_pDepTexture        (NullFC)
00119 {
00120     _sgiTexColorTable         = 
00121        Window::registerExtension("GL_SGI_texture_color_table"                );
00122 
00123     _extPalettedTexture       = 
00124        Window::registerExtension("GL_EXT_paletted_texture"                   );
00125 
00126     _extSharedPalettedTexture = 
00127        Window::registerExtension("GL_EXT_shared_texture_palette"             );
00128 
00129     _arbMultitexture          = 
00130        Window::registerExtension("GL_ARB_multitexture"                       );
00131 
00132     _nvTextureShader2         = 
00133        Window::registerExtension("GL_NV_texture_shader2"                     );
00134 
00135     _arbFragmentProgram       = 
00136        Window::registerExtension("GL_ARB_fragment_program"                   );
00137 
00138     _nvRegisterCombiners      = 
00139        Window::registerExtension("GL_NV_register_combiners"                  );
00140 
00141     _funcColorTableSGI        = 
00142        Window::registerFunction (OSG_DLSYM_UNDERSCORE"glColorTableSGI",
00143                                  _sgiTexColorTable);
00144 
00145     _funcColorTableEXT        = 
00146        Window::registerFunction (OSG_DLSYM_UNDERSCORE"glColorTableEXT",
00147                                  _extPalettedTexture);
00148 
00149     _funcActiveTextureARB     = 
00150        Window::registerFunction(OSG_DLSYM_UNDERSCORE"glActiveTextureARB",
00151                                 _arbMultitexture    );
00152 
00153     _funcFinalCombinerInputNV = 
00154        Window::registerFunction(OSG_DLSYM_UNDERSCORE"glFinalCombinerInputNV",
00155                                 _nvRegisterCombiners);
00156 
00157     _funcCombinerInputNV      = 
00158        Window::registerFunction(OSG_DLSYM_UNDERSCORE"glCombinerInputNV",
00159                                 _nvRegisterCombiners);
00160 
00161     _funcCombinerOutputNV     = 
00162        Window::registerFunction(OSG_DLSYM_UNDERSCORE"glCombinerOutputNV",
00163                                 _nvRegisterCombiners);
00164 }
00165 
00167 
00168 DVRSimpleLUTShader::DVRSimpleLUTShader(const DVRSimpleLUTShader &source) :
00169     Inherited            (source),
00170     m_nTexturePaletteMode( LM_NO),
00171     m_nTextureMode       (    -1), 
00172     m_pFragProg          (NullFC),
00173     m_pDepTexture        (NullFC)
00174 {
00175 }
00176 
00178 
00179 DVRSimpleLUTShader::~DVRSimpleLUTShader(void)
00180 {
00181     if(m_pFragProg != NullFC) 
00182     {
00183         subRefCP(m_pFragProg);
00184 
00185         m_pFragProg = NullFC;
00186     }
00187 
00188     destroyDependentTexture();
00189 }
00190 
00191 /*----------------------------- class specific ----------------------------*/
00192 
00194 
00195 void DVRSimpleLUTShader::getPaletteFormat(DrawActionBase * /*action*/,
00196                                           UInt8           mode,
00197                                           GLenum         &internalFormat,
00198                                           GLenum         &externalFormat)
00199 {
00200     switch(mode)
00201     {
00202         case LM_TABLE_SGI:
00203             internalFormat = GL_INTENSITY; 
00204             externalFormat = GL_LUMINANCE; 
00205             break;
00206 
00207 #if defined GL_EXT_paletted_texture && defined GL_EXT_shared_texture_palette
00208         case LM_PALETTE_EXT:
00209             internalFormat = GL_COLOR_INDEX8_EXT; 
00210             externalFormat = GL_COLOR_INDEX; 
00211             break;
00212 #endif
00213     
00214         case LM_DEPENDENT:
00215             internalFormat = GL_INTENSITY;
00216             externalFormat = GL_LUMINANCE;
00217             break;
00218 
00219         case LM_FRAGPROG:
00220             internalFormat = GL_INTENSITY;
00221             externalFormat = GL_LUMINANCE;
00222             break;
00223     
00224         case LM_RELOAD:
00225             internalFormat = GL_RGBA;  
00226             externalFormat = GL_COLOR_INDEX; 
00227             break;
00228 
00229         case LM_NO:
00230         default:
00231             internalFormat = GL_INTENSITY; 
00232             externalFormat = GL_LUMINANCE;
00233     }
00234 }
00235 
00236 
00238 UInt8 DVRSimpleLUTShader::selectMode(DrawActionBase *action, Int8 textureMode)
00239 {
00240     if(isModeSupported( action, LM_TABLE_SGI, textureMode ))
00241     {
00242         FDEBUG(("Using SGI postshading palette....\n"));
00243         FFATAL(("Using SGI postshading palette....\n"));
00244 
00245         return LM_TABLE_SGI;
00246     }
00247 
00248     if(isModeSupported( action, LM_DEPENDENT, textureMode ))
00249     { 
00250         FDEBUG(("Using dependent texture postshading palette....\n"));
00251         FFATAL(("Using dependent texture postshading palette....\n"));
00252 
00253         return LM_DEPENDENT;
00254     }
00255 
00256     if(isModeSupported( action, LM_FRAGPROG, textureMode ))
00257     {
00258         FDEBUG(("Using fragment program postshading palette....\n"));
00259         FFATAL(("Using fragment program postshading palette....\n"));
00260 
00261         return LM_FRAGPROG;
00262     }
00263 
00264     if(isModeSupported( action, LM_PALETTE_EXT, textureMode ))
00265     {
00266         FDEBUG(("Using preshading palette....\n"));
00267         FFATAL(("Using preshading palette....\n"));
00268 
00269         return LM_PALETTE_EXT;
00270     }
00271 
00272     FDEBUG(("Using palette reload ....\n"));
00273     FFATAL(("Using palette reload ....\n"));
00274 
00275     return LM_RELOAD;
00276 }
00277 
00279 bool DVRSimpleLUTShader::isModeSupported(DrawActionBase *action, 
00280                                          UInt8           mode, 
00281                                          Int8            textureMode)
00282 {
00283     bool result = false;
00284   
00285     switch(mode) 
00286     {
00287         case LM_TABLE_SGI:
00288             result = 
00289                 (action->getWindow()->hasExtension(_sgiTexColorTable) &&
00290                  (0 != strncmp("IMPACT", 
00291                                (char *) glGetString(GL_RENDERER), 
00292                                6)));
00293     break;
00294 
00295       case LM_PALETTE_EXT:
00296           result = 
00297               (action->getWindow()->hasExtension(_extPalettedTexture      ) &&
00298                action->getWindow()->hasExtension(_extSharedPalettedTexture));
00299     break;
00300     
00301         case LM_DEPENDENT:
00302             result = 
00303                 (action->getWindow()->hasExtension(_arbMultitexture    ) &&
00304                  action->getWindow()->hasExtension(_nvTextureShader2   ) &&
00305                  action->getWindow()->hasExtension(_nvRegisterCombiners) &&
00306                  textureMode != TextureManager::TM_2D_Multi               );
00307     break;
00308 
00309         case LM_FRAGPROG:
00310             result = (action->getWindow()->hasExtension(_arbMultitexture   ) &&
00311                       action->getWindow()->hasExtension(_arbFragmentProgram) &&
00312                       textureMode != TextureManager::TM_2D_Multi             );
00313             break;
00314             
00315         case LM_RELOAD:
00316             result = true;
00317             break;
00318 
00319         case LM_NO:
00320             result = true;
00321             break;
00322             
00323         default: 
00324             result = false;
00325     }
00326     
00327     return result;
00328 }
00329 
00331 void DVRSimpleLUTShader::enablePalette(void)
00332 {
00333 }
00334 
00336 void DVRSimpleLUTShader::disablePalette(void)
00337 {
00338 }
00339 
00341 void DVRSimpleLUTShader::initDependentTexture(Int32 size)
00342 {
00343     FDEBUG(("Initializing dependent texture"));
00344     
00345     ImagePtr img = Image::create();
00346     
00347     // we create a dummy 2D texture here since there is only a dependent
00348     // 2D texture lookup with texture shaders
00349 
00350     beginEditCP(img);
00351     {
00352         img->set  (Image::OSG_RGBA_PF, size, 2);
00353         img->clear(128);
00354     }
00355     endEditCP(img);
00356 
00357     m_pDepTexture = TextureChunk::create();
00358 
00359     addRefCP(m_pDepTexture);
00360             
00361     TextureChunkPtr &chunk = m_pDepTexture;
00362 
00363     beginEditCP(chunk);
00364     {
00365         chunk->setImage         (img             );
00366         chunk->setInternalFormat(GL_RGBA         );
00367         chunk->setExternalFormat(GL_RGBA         );
00368             
00369         chunk->setWrapS         (GL_CLAMP_TO_EDGE);
00370         chunk->setWrapT         (GL_CLAMP_TO_EDGE);
00371         chunk->setWrapR         (GL_CLAMP_TO_EDGE);
00372         
00373         chunk->setMinFilter     (GL_LINEAR       );
00374         chunk->setMagFilter     (GL_LINEAR       );
00375         
00376         chunk->setEnvMode       (GL_REPLACE      );
00377     }
00378     endEditCP  (chunk);
00379 
00380     FDEBUG((" - DONE\n"));
00381 }
00382 
00384 void DVRSimpleLUTShader::updateDependentTexture(Int32 size, const UInt8 *data)
00385 {
00386     FDEBUG(("Updating dependent texture\n"));
00387 
00388     ImagePtr img = m_pDepTexture->getImage();
00389 
00390     beginEditCP(img);
00391     {
00392         img->setSubData(0, 0, 0, size, 1, 1, data);
00393         img->setSubData(0, 1, 0, size, 1, 1, data);
00394     }
00395     endEditCP  (img);
00396         
00397     m_pDepTexture->imageContentChanged(); // makes begin/end edit unneccessary
00398 
00399     FDEBUG((" - DONE\n"));
00400 }
00401 
00403 void DVRSimpleLUTShader::destroyDependentTexture(void)
00404 {
00405     if(m_pDepTexture != NullFC) 
00406     {
00407         subRefCP(m_pDepTexture);
00408 
00409         m_pDepTexture = NullFC;
00410     }
00411 }
00412 
00414 
00415 typedef void (OSG_APIENTRY *FinalCombinerInputNVFunc)(GLenum,    
00416                                                       GLenum,    
00417                                                       GLenum, 
00418                                                       GLenum);
00419 typedef void (OSG_APIENTRY *CombinerInputNVFunc     )(GLenum,    
00420                                                       GLenum,    
00421                                                       GLenum, 
00422                                                       GLenum, 
00423                                                       GLenum,    
00424                                                       GLenum);
00425 typedef void (OSG_APIENTRY *CombinerOutputNVFunc    )(GLenum,    
00426                                                       GLenum,    
00427                                                       GLenum, 
00428                                                       GLenum, 
00429                                                       GLenum,    
00430                                                       GLenum,    
00431                                                       GLenum, 
00432                                                       GLboolean, 
00433                                                       GLboolean, 
00434                                                       GLboolean);
00435 
00436 void DVRSimpleLUTShader::setupAlphaCorrectionRegisterCombiners(
00437     DrawActionBase *action)
00438 {
00439     Window *win = action->getWindow();
00440     
00441     if(!win->hasExtension(_nvRegisterCombiners))
00442         return;
00443 
00444     FinalCombinerInputNVFunc FinalCombinerInputNV = NULL;
00445     CombinerInputNVFunc      CombinerInputNV      = NULL;
00446     CombinerOutputNVFunc     CombinerOutputNV     = NULL;
00447     
00448     FinalCombinerInputNV =
00449         (FinalCombinerInputNVFunc) win->getFunction(
00450             _funcFinalCombinerInputNV);
00451 
00452     CombinerInputNV = 
00453         (CombinerInputNVFunc) win->getFunction(_funcCombinerInputNV);
00454     
00455     CombinerOutputNV = 
00456         (CombinerOutputNVFunc) win->getFunction(_funcCombinerOutputNV);
00457     
00458 #if defined GL_NV_register_combiners
00461     // ----------------------------------------------------------------------
00462     
00463     // Input: RGB-portion
00464     CombinerInputNV(GL_COMBINER0_NV, 
00465                     GL_RGB, 
00466                     GL_VARIABLE_A_NV, 
00467                     GL_TEXTURE1_ARB,     
00468                     GL_UNSIGNED_IDENTITY_NV, 
00469                     GL_RGB);
00470 
00471     CombinerInputNV(GL_COMBINER0_NV, 
00472                     GL_RGB, 
00473                     GL_VARIABLE_B_NV, 
00474                     GL_PRIMARY_COLOR_NV, 
00475                     GL_UNSIGNED_IDENTITY_NV, 
00476                     GL_RGB);
00477 
00478     CombinerInputNV(GL_COMBINER0_NV, 
00479                     GL_RGB, 
00480                     GL_VARIABLE_C_NV,
00481                     GL_ZERO, 
00482                     GL_UNSIGNED_IDENTITY_NV,
00483                     GL_RGB);
00484 
00485     CombinerInputNV(GL_COMBINER0_NV, 
00486                     GL_RGB, 
00487                     GL_VARIABLE_D_NV, 
00488                     GL_ZERO,             
00489                     GL_UNSIGNED_IDENTITY_NV, 
00490                     GL_RGB);
00491 
00492     // Input: Alpha Portion
00493     CombinerInputNV(GL_COMBINER0_NV, 
00494                     GL_ALPHA, 
00495                     GL_VARIABLE_A_NV, 
00496                     GL_TEXTURE1_ARB,     
00497                     GL_UNSIGNED_IDENTITY_NV, 
00498                     GL_ALPHA);
00499 
00500     CombinerInputNV(GL_COMBINER0_NV, 
00501                     GL_ALPHA, 
00502                     GL_VARIABLE_B_NV, 
00503                     GL_PRIMARY_COLOR_NV, 
00504                     GL_UNSIGNED_IDENTITY_NV, 
00505                     GL_ALPHA);
00506 
00507     CombinerInputNV(GL_COMBINER0_NV, 
00508                     GL_ALPHA, 
00509                     GL_VARIABLE_C_NV, 
00510                     GL_ZERO,             
00511                     GL_UNSIGNED_IDENTITY_NV, 
00512                     GL_ALPHA);
00513 
00514     CombinerInputNV(GL_COMBINER0_NV, 
00515                     GL_ALPHA, 
00516                     GL_VARIABLE_D_NV, 
00517                     GL_ZERO,             
00518                     GL_UNSIGNED_IDENTITY_NV, 
00519                     GL_ALPHA);
00520 
00521     // Output
00522     CombinerOutputNV(GL_COMBINER0_NV, // stage
00523                      GL_RGB,          // portion
00524                      GL_SPARE0_NV,    // abOutput
00525                      GL_DISCARD_NV,   // cdOutput
00526                      GL_DISCARD_NV,   // sumOutput
00527                      GL_NONE,         // scale
00528                      GL_NONE,         // bias
00529                      GL_FALSE,        // abDotProduct
00530                      GL_FALSE,        // cdDotproduct
00531                      GL_FALSE);       // muxSum
00532 
00533     CombinerOutputNV(GL_COMBINER0_NV, 
00534                      GL_ALPHA, 
00535                      GL_SPARE0_NV,    // abOutput
00536                      GL_DISCARD_NV,   // cdOutput
00537                      GL_DISCARD_NV,   // sumOutput
00538                      GL_NONE, 
00539                      GL_NONE, 
00540                      GL_FALSE, 
00541                      GL_FALSE, 
00542                      GL_FALSE); 
00543     
00546     // -----------------------------------------------------------------------
00547     
00548     // RGB portion
00549     FinalCombinerInputNV(GL_VARIABLE_A_NV, 
00550                          GL_ZERO,      
00551                          GL_UNSIGNED_IDENTITY_NV, 
00552                          GL_RGB);
00553 
00554     FinalCombinerInputNV(GL_VARIABLE_B_NV, 
00555                          GL_ZERO,      
00556                          GL_UNSIGNED_IDENTITY_NV, 
00557                          GL_RGB);
00558 
00559     FinalCombinerInputNV(GL_VARIABLE_C_NV, 
00560                          GL_ZERO,      
00561                          GL_UNSIGNED_IDENTITY_NV, 
00562                          GL_RGB);   
00563 
00564     FinalCombinerInputNV(GL_VARIABLE_E_NV, 
00565                          GL_ZERO,      
00566                          GL_UNSIGNED_IDENTITY_NV, 
00567                          GL_RGB);
00568 
00569     FinalCombinerInputNV(GL_VARIABLE_F_NV, 
00570                          GL_ZERO,      
00571                          GL_UNSIGNED_IDENTITY_NV, 
00572                          GL_RGB);
00573 
00574     FinalCombinerInputNV(GL_VARIABLE_D_NV, 
00575                          GL_SPARE0_NV, 
00576                          GL_UNSIGNED_IDENTITY_NV, 
00577                          GL_RGB);
00578 
00579     // Alpha component
00580     FinalCombinerInputNV(GL_VARIABLE_G_NV, 
00581                          GL_SPARE0_NV, 
00582                          GL_UNSIGNED_IDENTITY_NV, 
00583                          GL_ALPHA);
00584 #endif
00585 }
00586 
00588 void DVRSimpleLUTShader::initMethod(void)
00589 {
00590 }
00591 
00593 void DVRSimpleLUTShader::changed(BitVector whichField, UInt32 origin)
00594 {
00595     FDEBUG(("DVRSimpleLUTShader::changed\n"));
00596     FWARNING(("DVRSimpleLUTShader::changed\n"));
00597     
00598     // trigger re-initialization
00599     if(whichField & LutModeFieldMask)
00600     {
00601         setActiveLutMode(LM_AUTO);
00602     }
00603 
00604     Inherited::changed(whichField, origin);
00605 }
00606 
00608 void DVRSimpleLUTShader::dump(      UInt32    , 
00609                               const BitVector ) const
00610 {
00611     SLOG << "Dump DVRSimpleLUTShader NI" << std::endl;
00612 }
00613 
00614 
00616 bool DVRSimpleLUTShader::initialize(DVRVolume *volume, DrawActionBase *action)
00617 {
00618     FWARNING(("DVRSimpleLUTShader::initialize\n"));
00619     
00620     GLenum nInternalFormat = GL_INTENSITY; 
00621     GLenum nExternalFormat = GL_LUMINANCE; 
00622 
00623     m_nTexturePaletteMode  = LM_NO;
00624     m_nTextureId           = -1; 
00625 
00626     Window *win            = action->getWindow(); 
00627 
00628     // Determine lookup table mechanism
00629     if(getLutMode() != LM_AUTO)
00630     {
00631         // A certain mode has been selected
00632         if(isModeSupported(action, getLutMode(), volume->getTextureMode(win)))
00633         {
00634             SWARNING << "DVRSimpleLUTShader - User specified lookup table "
00635                      << "mode "
00636                      << int(getLutMode()) << std::endl;
00637 
00638             m_nTexturePaletteMode =  getLutMode();
00639         }
00640         else
00641         {
00642             SWARNING << "DVRSimpleLUTShader - Unsupported lookup table mode "
00643                      << int(getLutMode()) << " disabling LUT" << std::endl;
00644 
00645             m_nTexturePaletteMode = LM_NO;
00646         }
00647     }
00648     else 
00649     {
00650         // Use automatic mode selection
00651         m_nTexturePaletteMode = selectMode(action, 
00652                                            volume->getTextureMode(win));
00653     }
00654 
00655     setActiveLutMode(m_nTexturePaletteMode);
00656     
00657     getPaletteFormat(action, 
00658                      m_nTexturePaletteMode, 
00659                      nInternalFormat, 
00660                      nExternalFormat      );
00661 
00662     DVRLookupTablePtr   lut = DVRVOLUME_PARAMETER(volume, DVRLookupTable  );
00663     DVRVolumeTexturePtr vol = DVRVOLUME_PARAMETER(volume, DVRVolumeTexture);
00664 
00665     if((volume == NULL) || (vol == NullFC)) 
00666     {
00667         SWARNING << "NO Volume..." << std::endl;
00668 
00669         return false;
00670     }
00671 
00672     m_nTextureId = volume->getTextureManager().registerTexture(
00673         vol->getImage(), // image
00674         nInternalFormat, // internalFormat
00675         nExternalFormat, // externalFormat
00676         1,               // doBricking
00677         0,               // textureStage0
00678         -1);             // textureStage1
00679     
00680     if(m_nTextureId == -1) 
00681     {
00682         SWARNING << "Error registering textures ..." << std::endl;
00683 
00684         return false;
00685     }
00686     else
00687     {
00688         SWARNING << "New texture Id: " << m_nTextureId << std::endl;
00689 
00691         volume->getTextureManager().reloadTexture(m_nTextureId, action); 
00692     }
00693     
00694     if(lut != NullFC)
00695     {
00696         beginEditCP(lut, DVRLookupTable::TouchedFieldMask);
00697         {
00698             lut->setTouched(true); // force initialization in first render
00699                                    // pass
00700         }
00701         endEditCP  (lut, DVRLookupTable::TouchedFieldMask);
00702     }
00703     
00704     return true;
00705 }
00706 
00707 #include <OSGGLU.h>
00708 
00709 #define CHECK_GL_ERROR(msg)                                              \
00710 {                                                                        \
00711     const GLenum e = glGetError();                                       \
00712                                                                          \
00713     if((e != 0) && (e != GL_NO_ERROR))                                   \
00714     {                                                                    \
00715         std::cout << std::endl << "OpenGL-ERROR <0x"                     \
00716                 << setfill('0') << hex << setw(4) << e << dec            \
00717                 << "> in " __FILE__ "(" << __LINE__ << ")!" << std::endl \
00718                 << gluErrorString(e) << msg << std::endl;                \
00719     }                                                                    \
00720 }
00721 
00722 typedef void (OSG_APIENTRY *ActiveTextureARBFunc)(      GLenum);
00723 
00724 typedef void (OSG_APIENTRY *ColorTableSGIFunc   )(      GLenum, 
00725                                                         GLenum,       
00726                                                         GLsizei, 
00727                                                         GLenum, 
00728                                                         GLenum, 
00729                                                   const GLvoid *);
00730 
00731 typedef void (OSG_APIENTRY *ColorTableEXTFunc    )(      GLenum, 
00732                                                          GLenum,       
00733                                                          GLsizei, 
00734                                                          GLenum, 
00735                                                          GLenum, 
00736                                                    const GLvoid *);
00737 
00739 void DVRSimpleLUTShader::activate(DVRVolume *volume, DrawActionBase *action)
00740 {
00741     // FDEBUG(("DVRSimpleLUTShader::activate - v1.2 (%d)\n", (int) this));
00742 
00743     Window *win = action->getWindow();
00744 
00745     // remove compiler warnings for unused variable
00746 #if defined GL_NV_texture_shader2 
00747     ActiveTextureARBFunc ActiveTextureARB = 
00748         (ActiveTextureARBFunc) win->getFunction(_funcActiveTextureARB);
00749 #endif
00750     
00751     ColorTableSGIFunc ColorTableSGI = NULL;
00752 
00753     if(win->hasExtension(_sgiTexColorTable))
00754     {
00755         ColorTableSGI = 
00756             (ColorTableSGIFunc) win->getFunction(_funcColorTableSGI);
00757     }
00758     
00759     ColorTableEXTFunc ColorTableEXT = NULL;
00760 
00761     if(win->hasExtension(_extSharedPalettedTexture))
00762     {
00763         ColorTableEXT = 
00764             (ColorTableEXTFunc) win->getFunction(_funcColorTableEXT);
00765     }
00766     
00767     DVRSimpleShader::activate(volume, action);
00768     
00769     DVRLookupTablePtr   lut = DVRVOLUME_PARAMETER(volume, DVRLookupTable);
00770     DVRVolumeTexturePtr vol = DVRVOLUME_PARAMETER(volume, DVRVolumeTexture);
00771 
00772     if((volume == NULL) || (vol == NullFC)) 
00773     {
00774         SWARNING << "NO Volume ..." << std::endl;
00775 
00776         return;
00777     }
00778 
00779     
00780     // reinitialize if hardware mode has not yet been chosen or mode has 
00781     // changed
00782 
00783     if(getActiveLutMode() == LM_AUTO) 
00784     {
00785         SNOTICE << "Reinitializing DVRSimpleLUTShader" << std::endl;
00786 
00787         cleanup   (volume, action);
00788         initialize(volume, action);
00789 
00790         // notify volume about shader changed
00791         beginEditCP(DVRVolumePtr(volume), DVRVolume::ShaderFieldMask);
00792         endEditCP  (DVRVolumePtr(volume), DVRVolume::ShaderFieldMask);
00793     }
00794 
00795 
00796     // reload lookup table if neccessary
00797     if((lut != NullFC) && (lut->getTouched())) 
00798     {
00799         Int32  colorTableSize =   lut->getSize(0);
00800         UInt8 *data           = &(lut->getData(0));
00801         
00802         lut->setTouched(false);
00803         
00804         switch(m_nTexturePaletteMode) 
00805         {
00806             case LM_TABLE_SGI:
00807 
00808                 FDEBUG(("Using SGI postshading palette....\n"));
00809 
00810 #if defined GL_SGI_texture_color_table
00811                 glEnable(GL_TEXTURE_COLOR_TABLE_SGI); 
00812 
00813                 ColorTableSGI(
00814                     GL_TEXTURE_COLOR_TABLE_SGI, // GLenum target,
00815                     GL_RGBA,                    // GLenum internalformat, 
00816                     colorTableSize,             // GLsizei width, 
00817                     GL_RGBA,                    // GLenum format, 
00818                     GL_UNSIGNED_BYTE,           // GLenum type, 
00819                     data);                      // const GLvoid *table 
00820 #endif // GL_SGI_texture_color_table     
00821 
00822                 break;
00823                 
00824             case LM_PALETTE_EXT:
00825 
00826                 FDEBUG(("Using preshading palette....\n"));
00827 
00828 #if defined GL_EXT_paletted_texture && defined GL_EXT_shared_texture_palette
00829 
00830                 glEnable(GL_SHARED_TEXTURE_PALETTE_EXT);
00831 
00832                 ColorTableEXT(
00833                     GL_SHARED_TEXTURE_PALETTE_EXT, // GLenum target, 
00834                     GL_RGBA,                       // GLenum internalformat, 
00835                     colorTableSize,                // GLsizei width, 
00836                     GL_RGBA,                       // GLenum format, 
00837                     GL_UNSIGNED_BYTE,              // GLenum type, 
00838                     data);                         // const GLvoid *table  
00839 #endif  
00840 
00841                 break;
00842                 
00843             case LM_RELOAD: 
00844             {
00845                 FDEBUG(("Using palette reload ....\n"));
00846                 FFATAL(("Using palette reload ....\n"));
00847 
00848                 glPixelTransferi(GL_MAP_COLOR, GL_TRUE);
00849 
00850                 glPixelMapfv(GL_PIXEL_MAP_I_TO_R, 
00851                              colorTableSize, 
00852                              &(lut->getDataR(0)));
00853 
00854                 glPixelMapfv(GL_PIXEL_MAP_I_TO_G, 
00855                              colorTableSize, 
00856                              &(lut->getDataG(0)));
00857 
00858                 glPixelMapfv(GL_PIXEL_MAP_I_TO_B, 
00859                              colorTableSize, 
00860                              &(lut->getDataB(0)));
00861 
00862                 glPixelMapfv(GL_PIXEL_MAP_I_TO_A, 
00863                              colorTableSize, 
00864                              &(lut->getDataA(0)));
00865                 
00866                 volume->getTextureManager().reloadTexture(m_nTextureId, 
00867                                                           action);
00868 
00869                 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
00870                 break;
00871             }
00872                 
00873             case LM_DEPENDENT: 
00874             {
00875                 // Initialize dependent texture
00876                 if(m_pDepTexture == NullFC)
00877                     initDependentTexture(lut->getSize(0));
00878                 
00879                 // Setup the correct texture shader mode
00880                 if(volume->getTextureMode(win) != m_nTextureMode) 
00881                 {
00882                     switch (volume->getTextureMode(win)) 
00883                     {
00884                         case TextureManager::TM_3D:
00885                             SLOG << "Setup texture shader for 3D" << std::endl;
00886                             break;
00887 
00888                         case TextureManager::TM_2D:
00889                             SLOG << "Setup texture shader for 2D" << std::endl;
00890                             break;
00891 
00892                         default:
00893                             FFATAL(
00894                                 ( "Texture mode for dependent textures"
00895                                   " not supported by DVRSimpleLUTShader\n"));
00896                             return;
00897                     }
00898 
00899                     m_nTextureMode = volume->getTextureMode(win);
00900                 }
00901                 
00902                 // Update dependent texture if neccessary
00903                 updateDependentTexture(lut->getSize(0), &(lut->getData(0)));
00904                 break;
00905             }
00906                 
00907             case LM_FRAGPROG: 
00908             {
00909                 // Initialize dependent texture
00910                 if(m_pDepTexture == NullFC)
00911                     initDependentTexture(lut->getSize(0));
00912                 
00913                 // Initialize fragment program
00914                 if(m_pFragProg == NullFC) 
00915                 {
00916                     FDEBUG(("Initializing fragment program "));
00917 
00918                     m_pFragProg = FragmentProgramChunk::create();
00919 
00920                     addRefCP( m_pFragProg );
00921                     
00922                     FDEBUG((" - DONE\n"));
00923                 }
00924                 
00925                 // Load the appropriate program
00926                 if(volume->getTextureMode(win) != m_nTextureMode) 
00927                 {
00928                     switch(volume->getTextureMode(win)) 
00929                     {
00930                         case TextureManager::TM_3D:
00931                             //SLOG << "Loading ... lutFragProg3D.asm" 
00932                             //     << std::endl;
00933 
00934                             beginEditCP(m_pFragProg);
00935                             {
00936                                 m_pFragProg->setProgram(_fragProg3D);
00937                                 // m_pFragProg->read("lutFragProg3D.asm");
00938                             }
00939                             endEditCP  (m_pFragProg);
00940                             break;
00941 
00942                         case TextureManager::TM_2D:
00943                             //SLOG << "Loading ... lutFragProg2D.asm" 
00944                             //     << std::endl;
00945 
00946                             beginEditCP(m_pFragProg);
00947                             {
00948                                 m_pFragProg->setProgram(_fragProg2D);
00949                                 // m_pFragProg->read("lutFragProg2D.asm");
00950                             }
00951                             endEditCP  (m_pFragProg);
00952                             break;
00953 
00954                         case TextureManager::TM_2D_Multi:
00955                             //SLOG << "Loading ... lutFragProg2DMulti.asm" 
00956                             //     << std::endl;
00957 
00958                             beginEditCP(m_pFragProg);
00959                             {
00960                                 m_pFragProg->setProgram(_fragProg2DMulti);
00961                                 //m_pFragProg->read("lutFragProg2DMulti.asm");
00962                             }
00963                             endEditCP  (m_pFragProg);
00964                             break;
00965 
00966                         default:
00967                             FFATAL(( "Texture mode for fragment programs"
00968                                      " not supported by DVRSimpleLUTShader" ));
00969                             return;
00970                     }
00971 
00972                     m_nTextureMode = volume->getTextureMode(win);
00973                 }
00974                 
00975                 // Update dependent texture if neccessary
00976                 updateDependentTexture(lut->getSize(0), &(lut->getData(0)));
00977                 break;
00978             }
00979                 
00980             case LM_NO:
00981                 FDEBUG(("Using NO palette reload ....\n"));
00982                 break;
00983         }
00984     }
00985     
00986     
00987     // enable lookup table
00988     if(lut != NullFC)
00989     {
00990         switch(m_nTexturePaletteMode) 
00991         {
00992             case LM_TABLE_SGI:
00993 
00994 #if defined GL_SGI_texture_color_table
00995                 glEnable(GL_TEXTURE_COLOR_TABLE_SGI);
00996 #endif
00997 
00998                 break;
00999 
01000             case LM_PALETTE_EXT:
01001 
01002 #if defined GL_EXT_paletted_texture && defined GL_EXT_shared_texture_palette
01003                 glEnable(GL_SHARED_TEXTURE_PALETTE_EXT);
01004 #endif
01005                 break;
01006                 
01007             case LM_DEPENDENT:
01008 
01009 #if defined GL_NV_texture_shader2
01010                 if(m_pDepTexture == NullFC)
01011                 {
01012                     SWARNING << "LM_DEPENDENT - no dependent texture" 
01013                              << std::endl;
01014                 }
01015 
01016                 //beginEditCP(m_pDepTexture, 
01017                 //            TextureChunk::EnvModeFieldMask | 
01018                 //            TextureChunk::ShaderOperationFieldMask);
01019                 //{
01020                 //   m_pDepTexture->setEnvMode        (GL_REPLACE);
01021                 //    m_pDepTexture->setShaderOperation(
01022                 //        GL_DEPENDENT_AR_TEXTURE_2D_NV);
01023                 //}
01024                 //endEditCP  (m_pDepTexture, 
01025                 //            TextureChunk::EnvModeFieldMask | 
01026                 //            TextureChunk::ShaderOperationFieldMask);
01027 
01028                 m_pDepTexture->activate(action, 1);
01029                 
01031                 glEnable(GL_TEXTURE_SHADER_NV);
01032                 
01033                 ActiveTextureARB(GL_TEXTURE1_ARB);
01034 
01035                 glTexEnvf(GL_TEXTURE_ENV, 
01036                           GL_TEXTURE_ENV_MODE, 
01037                           GL_REPLACE);
01038 
01039                 glTexEnvi(GL_TEXTURE_SHADER_NV, 
01040                           GL_SHADER_OPERATION_NV, 
01041                           GL_DEPENDENT_AR_TEXTURE_2D_NV);
01042                 
01044 
01045                 ActiveTextureARB(GL_TEXTURE0_ARB);
01046 
01047                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
01048                 
01049                 switch(volume->getTextureMode(win)) 
01050                 {
01051                     case TextureManager::TM_3D:
01052 
01053                         glTexEnvi(GL_TEXTURE_SHADER_NV, 
01054                                   GL_SHADER_OPERATION_NV, 
01055                                   GL_TEXTURE_3D);
01056                         break;
01057 
01058                     case TextureManager::TM_2D:
01059 
01060                         glTexEnvi(GL_TEXTURE_SHADER_NV, 
01061                                   GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
01062                         break;
01063 
01064                     default:
01065 
01066                         //FFATAL(("Texture mode for dependent texture not "
01067                         //        "supported by DVRSimpleLUTShader"));
01068                         break;
01069                 }
01070 #endif
01071                 
01072 #if defined GL_NV_register_combiners
01073 
01074                 setupAlphaCorrectionRegisterCombiners(action);
01075                 glEnable(GL_REGISTER_COMBINERS_NV);
01076                 break;
01077 #endif
01078                 
01079             case LM_FRAGPROG:
01080 
01081                 // activate fragment program chunk
01082                 m_pDepTexture->activate(action, 2);
01083                 m_pFragProg  ->activate(action   );
01084 
01085                 break;
01086                 
01087             default:
01088                 break;
01089         }
01090     }
01091     
01092 }
01093 
01095 void DVRSimpleLUTShader::brickActivate(DVRVolume      *volume, 
01096                                        DrawActionBase *action, 
01097                                        Brick          *brick)
01098 {
01099     //FDEBUG(("DVRSimpleLUTShader::brickActivate %d\n", (int) this));
01100 
01101     DVRSimpleShader::brickActivate(volume, action, brick);
01102 
01105 
01106     if(m_nTexturePaletteMode == LM_DEPENDENT) 
01107     {
01108         switch(volume->getTextureMode(action->getWindow())) 
01109         {
01110             case TextureManager::TM_3D:
01111                 glTexEnvi(GL_TEXTURE_SHADER_NV, 
01112                           GL_SHADER_OPERATION_NV, 
01113                           GL_TEXTURE_3D);
01114                 break;
01115 
01116             case TextureManager::TM_2D:
01117                 glTexEnvi(GL_TEXTURE_SHADER_NV, 
01118                           GL_SHADER_OPERATION_NV, 
01119                           GL_TEXTURE_2D);
01120                 break;
01121 
01122             default:
01123                 // FFATAL(("Texture mode for dependent texture not "
01124                 //         "supported by DVRSimpleLUTShader"));
01125                 break;
01126         }
01127     }
01128 }
01129 
01131 void DVRSimpleLUTShader::deactivate(DVRVolume *volume, DrawActionBase *action)
01132 {
01133     FDEBUG(("DVRSimpleLUTShader::deactivate\n"));
01134     
01135 // remove compiler warnings for unused variable
01136 #if defined GL_NV_texture_shader2 
01137     ActiveTextureARBFunc ActiveTextureARB =
01138         (ActiveTextureARBFunc) action->getWindow()->getFunction(
01139             _funcActiveTextureARB);
01140 #endif
01141 
01142     switch(m_nTexturePaletteMode) 
01143     {
01144         case LM_TABLE_SGI:
01145 #if defined GL_SGI_texture_color_table
01146             glDisable(GL_TEXTURE_COLOR_TABLE_SGI);
01147 #endif
01148             break;
01149             
01150         case LM_PALETTE_EXT:
01151 #if defined GL_EXT_paletted_texture && defined GL_EXT_shared_texture_palette
01152             glDisable(GL_SHARED_TEXTURE_PALETTE_EXT);
01153 #endif
01154             break;
01155         
01156         case LM_DEPENDENT:
01157 #if defined GL_NV_register_combiners
01158             glEnable(GL_REGISTER_COMBINERS_NV);
01159 #endif
01160 #if defined GL_NV_texture_shader2
01161             glDisable(GL_TEXTURE_SHADER_NV);
01162             
01163             if(m_pDepTexture != NullFC) 
01164                 m_pDepTexture->deactivate(action, 1);
01165             break;
01166 #endif
01167             
01168         case LM_FRAGPROG:
01169             // de-activate fragment program chunk
01170             if(m_pFragProg != NullFC)
01171             {
01172                 m_pFragProg->deactivate(action);
01173             }
01174             if(