00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
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 <OSGDVRMtexLUTShader.h>
00052
00053 #include <OSGDVRVolume.h>
00054 #include <OSGDVRVolumeTexture.h>
00055 #include <OSGDVRLookupTable.h>
00056
00057 #include <OSGWindow.h>
00058
00059
00060 OSG_USING_NAMESPACE
00061
00067 UInt32 DVRMtexLUTShader::_arbMultitexture =
00068 Window::invalidExtensionID;
00069
00070 UInt32 DVRMtexLUTShader::_nvRegisterCombiners =
00071 Window::invalidExtensionID;
00072
00073
00074 UInt32 DVRMtexLUTShader::_funcMultiTexCoord2dARB =
00075 Window::invalidFunctionID;
00076
00077 UInt32 DVRMtexLUTShader::_funcCombinerParameteriNV =
00078 Window::invalidFunctionID;
00079
00080 UInt32 DVRMtexLUTShader::_funcCombinerParameterfvNV =
00081 Window::invalidFunctionID;
00082
00083 UInt32 DVRMtexLUTShader::_funcCombinerInputNV =
00084 Window::invalidFunctionID;
00085
00086 UInt32 DVRMtexLUTShader::_funcCombinerOutputNV =
00087 Window::invalidFunctionID;
00088
00089 UInt32 DVRMtexLUTShader::_funcFinalCombinerInputNV =
00090 Window::invalidFunctionID;
00091
00092 bool DVRMtexLUTShader::hasRenderCallback(void)
00093 {
00094 return true;
00095 }
00096
00097 bool DVRMtexLUTShader::useMTSlabs(void)
00098 {
00099 return true;
00100 }
00101
00102
00103
00105 DVRMtexLUTShader::DVRMtexLUTShader(void) :
00106 Inherited()
00107 {
00108 _arbMultitexture =
00109 Window::registerExtension( "GL_ARB_multitexture" );
00110
00111 _nvRegisterCombiners =
00112 Window::registerExtension( "GL_NV_register_combiners");
00113
00114
00115 _funcMultiTexCoord2dARB =
00116 Window::registerFunction (OSG_DLSYM_UNDERSCORE"glMultiTexCoord2dARB",
00117 _arbMultitexture);
00118
00119 _funcCombinerParameteriNV =
00120 Window::registerFunction (OSG_DLSYM_UNDERSCORE"glCombinerParameteriNV",
00121 _nvRegisterCombiners);
00122
00123 _funcCombinerParameterfvNV =
00124 Window::registerFunction (OSG_DLSYM_UNDERSCORE"glCombinerParameterfvNV",
00125 _nvRegisterCombiners);
00126
00127 _funcCombinerInputNV =
00128 Window::registerFunction (OSG_DLSYM_UNDERSCORE"glCombinerInputNV",
00129 _nvRegisterCombiners);
00130
00131 _funcCombinerOutputNV =
00132 Window::registerFunction (OSG_DLSYM_UNDERSCORE"glCombinerOutputNV",
00133 _nvRegisterCombiners);
00134
00135 _funcFinalCombinerInputNV =
00136 Window::registerFunction (OSG_DLSYM_UNDERSCORE"glFinalCombinerInputNV",
00137 _nvRegisterCombiners);
00138 }
00139
00141 DVRMtexLUTShader::DVRMtexLUTShader(const DVRMtexLUTShader &source) :
00142 Inherited(source)
00143 {
00144 }
00145
00147 DVRMtexLUTShader::~DVRMtexLUTShader(void)
00148 {
00149 }
00150
00151
00152
00154 void DVRMtexLUTShader::initMethod (void)
00155 {
00156 }
00157
00159 void DVRMtexLUTShader::changed(BitVector whichField, UInt32 origin)
00160 {
00161 SWARNING << "DVRMtexLUTShader::changed" << std::endl;
00162
00163 Inherited::changed(whichField, origin);
00164 }
00165
00167
00168 void DVRMtexLUTShader::dump( UInt32 ,
00169 const BitVector ) const
00170 {
00171 SLOG << "Dump DVRMtexLUTShader NI" << std::endl;
00172 }
00173
00175 bool DVRMtexLUTShader::initialize(DVRVolume *volume, DrawActionBase *action)
00176 {
00177
00178 FWARNING(("DVRMtexLUTShader::initialize\n"));
00179
00180 GLenum nInternalFormat;
00181 GLenum nExternalFormat;
00182
00183 Window *win = action->getWindow();
00184
00185
00186 if(getLutMode() != LM_AUTO)
00187 {
00188
00189 if(isModeSupported(action,
00190 getLutMode(),
00191 volume->getTextureMode(win)) == true)
00192 {
00193 SWARNING << "DVRMtexLUTShader - User specified lookup table mode "
00194 << int(getLutMode())
00195 << std::endl;
00196
00197 m_nTexturePaletteMode = getLutMode();
00198 }
00199 else
00200 {
00201 SWARNING << "DVRMtexLUTShader - Unsupported lookup table mode "
00202 << int(getLutMode())
00203 << " disabling LUT"
00204 << std::endl;
00205
00206 m_nTexturePaletteMode = LM_NO;
00207 }
00208 }
00209 else
00210 {
00211
00212 m_nTexturePaletteMode = selectMode(action,
00213 volume->getTextureMode(win));
00214 }
00215
00216 setActiveLutMode(m_nTexturePaletteMode);
00217
00218
00219 getPaletteFormat(action,
00220 m_nTexturePaletteMode,
00221 nInternalFormat,
00222 nExternalFormat );
00223
00224 DVRLookupTablePtr lut = DVRVOLUME_PARAMETER(volume, DVRLookupTable);
00225 DVRVolumeTexturePtr vol = DVRVOLUME_PARAMETER(volume, DVRVolumeTexture);
00226
00227 if((volume == NULL) || (vol == NullFC))
00228 {
00229 SWARNING << "NO Volume..." << std::endl;
00230 return false;
00231 }
00232
00233
00234
00235
00236 if(!action->getWindow()->hasExtension(_arbMultitexture))
00237 {
00238 SWARNING << "NO multi textures ..." << std::endl;
00239
00240 return false;
00241 }
00242
00243 SWARNING << "Multitextures found" << std::endl;
00244
00245 if(!action->getWindow()->hasExtension(_nvRegisterCombiners))
00246 {
00247 SWARNING << "NO register combiners" << std::endl;
00248 return false;
00249 }
00250
00251 SWARNING << "Register Combiners found" << std::endl;
00252
00253 m_nTextureId = volume->getTextureManager().registerTexture(
00254 vol->getImage(),
00255 nInternalFormat,
00256 nExternalFormat,
00257 1,
00258 0,
00259 1);
00260
00261 if(m_nTextureId == -1)
00262 {
00263 SWARNING << "Error registering textures ..." << std::endl;
00264 return false;
00265 }
00266 else
00267 {
00268 SWARNING << "New texture Id: " << m_nTextureId << std::endl;
00269 }
00270
00271 if(lut != NullFC)
00272 lut->setTouched(true);
00273
00274 return true;
00275 }
00276
00277
00279 void DVRMtexLUTShader::activate(DVRVolume *volume, DrawActionBase *action)
00280 {
00281
00282
00283 #if !defined(GL_ARB_multitexture) || !defined(GL_NV_register_combiners)
00284 SWARNING << "DVRtexLUTShader requires ARB_multitexture and "
00285 << "NV_register_combiners extensions"
00286 << std::endl;
00287
00288 volume = volume; action = action;
00289 #else
00290 DVRSimpleLUTShader::activate(volume, action);
00291
00292 if(volume->getDoTextures())
00293 {
00294 glEnable(GL_REGISTER_COMBINERS_NV);
00295
00296 initCombiners(action);
00297 }
00298 #endif // required extensions available
00299 }
00300
00301
00303 void DVRMtexLUTShader::deactivate(DVRVolume *volume, DrawActionBase *action)
00304 {
00305
00306
00307 #if !defined(GL_ARB_multitexture) || !defined(GL_NV_register_combiners)
00308 SWARNING << "DVRtexLUTShader requires ARB_multitexture and "
00309 << "NV_register_combiners extensions"
00310 << std::endl;
00311
00312 volume = volume; action = action;
00313 #else
00314 if (volume->getDoTextures())
00315 {
00316 glDisable(GL_REGISTER_COMBINERS_NV);
00317 }
00318
00319 DVRSimpleLUTShader::deactivate(volume, action);
00320 #endif // required extensions available
00321 }
00322
00324 void DVRMtexLUTShader::cleanup(DVRVolume *volume, DrawActionBase * )
00325 {
00326 if(volume != NULL)
00327 {
00328 if(m_nTextureId != -1)
00329 volume->getTextureManager().unregisterTexture(m_nTextureId);
00330 }
00331 }
00332
00333
00334 typedef void (OSG_APIENTRY *CombinerParameterfvNVFunc)( GLenum,
00335 const GLfloat *);
00336
00337 typedef void (OSG_APIENTRY *MultiTexCoord2dARBFunc )( GLenum,
00338 GLdouble,
00339 GLdouble);
00340
00342 void DVRMtexLUTShader::renderSlice(DVRVolume *volume,
00343 DrawActionBase *action,
00344 Real32 *data,
00345 UInt32 vertices,
00346 UInt32 values)
00347 {
00348 #if !defined(GL_ARB_multitexture) || !defined(GL_NV_register_combiners)
00349 SWARNING << "DVRtexLUTShader requires ARB_multitexture and "
00350 << "NV_register_combiners extensions"
00351 << std::endl;
00352
00353 volume = volume; action = action; data = data;
00354 vertices = vertices; values = values;
00355 #else
00356 Window *win = action->getWindow();
00357
00358 if(!win->hasExtension(_nvRegisterCombiners) ||
00359 !win->hasExtension(_arbMultitexture))
00360 {
00361 return;
00362 }
00363
00364 CombinerParameterfvNVFunc CombinerParameterfvNV =
00365 (CombinerParameterfvNVFunc) win->getFunction(
00366 _funcCombinerParameterfvNV);
00367
00368 MultiTexCoord2dARBFunc MultiTexCoord2dARB =
00369 (MultiTexCoord2dARBFunc) win->getFunction(_funcMultiTexCoord2dARB);
00370
00371
00372
00373
00374 if(values < 6)
00375 {
00376 SWARNING << "DVRMtexLUTShader::renderSlice - insufficient values"
00377 << std::endl;
00378 return;
00379 }
00380
00381
00382 float col[4] =
00383 {
00384 0.f, 0.f, 0.f, data[5]
00385 };
00386
00387 CombinerParameterfvNV(GL_CONSTANT_COLOR0_NV, col);
00388
00389 glFogfv(GL_FOG_COLOR, col);
00390
00391 if(volume->getDoTextures())
00392 {
00393 glBegin(GL_TRIANGLE_FAN);
00394 }
00395 else
00396 {
00398 glColor4f(1.0, 1.0, 1.0, 1.0);
00399
00400 glBegin(GL_LINE_LOOP);
00401 }
00402
00403
00404 for(UInt32 i = 0; i < vertices; i++)
00405 {
00406 Real32 *vert = data + i *values;
00407
00408
00409
00410
00411
00412
00413
00414 MultiTexCoord2dARB(GL_TEXTURE0_ARB, vert[3], vert[4]);
00415 MultiTexCoord2dARB(GL_TEXTURE1_ARB, vert[3], vert[4]);
00416
00417 glVertex3f(vert[0], vert[1], vert[2]);
00418
00419 }
00420 glEnd();
00421
00422 #endif // required extensions
00423 }
00424
00425
00426 void DVRMtexLUTShader::renderSlice(DVRVolume *volume,
00427 DrawActionBase *action,
00428 DVRRenderSlice *clippedSlice)
00429 {
00430 #if !defined(GL_ARB_multitexture) || !defined(GL_NV_register_combiners)
00431 SWARNING << "DVRtexLUTShader requires ARB_multitexture and "
00432 << "NV_register_combiners extensions"
00433 << std::endl;
00434
00435 volume = volume;
00436 action = action;
00437 clippedSlice = clippedSlice;
00438 #else
00439
00440 Window *win = action->getWindow();
00441
00442 if(!win->hasExtension(_nvRegisterCombiners) ||
00443 !win->hasExtension(_arbMultitexture))
00444 {
00445 return;
00446 }
00447
00448 CombinerParameterfvNVFunc CombinerParameterfvNV =
00449 (CombinerParameterfvNVFunc) win->getFunction(
00450 _funcCombinerParameterfvNV);
00451
00452 MultiTexCoord2dARBFunc MultiTexCoord2dARB =
00453 (MultiTexCoord2dARBFunc) win->getFunction(_funcMultiTexCoord2dARB);
00454
00455 GLfloat intFactor = 0;
00456
00457 switch(clippedSlice->orientation)
00458 {
00459 case DVRRenderSlice::XY:
00460 intFactor =
00461 Real32((*(*clippedSlice->begin())->vertices.begin())[5]);
00462 break;
00463
00464 case DVRRenderSlice::XZ:
00465 intFactor =
00466 Real32((*(*clippedSlice->begin())->vertices.begin())[4]);
00467 break;
00468
00469 case DVRRenderSlice::YZ:
00470 intFactor =
00471 Real32((*(*clippedSlice->begin())->vertices.begin())[3]);
00472 break;
00473
00474 default:
00475 break;
00476 }
00477
00478 if(intFactor < 0 || intFactor > 1)
00479 std::cerr << intFactor << std::endl;
00480
00481 float col[4] =
00482 {
00483 0.f, 0.f, 0.f, intFactor
00484 };
00485
00486 CombinerParameterfvNV(GL_CONSTANT_COLOR0_NV, col);
00487
00488 glFogfv(GL_FOG_COLOR, col);
00489
00490 if(volume->getDoTextures())
00491 {
00492
00493 }
00494 else
00495 {
00497 glColor4f(1.0, 1.0, 1.0, 1.0);
00498
00499 }
00500
00501 switch(clippedSlice->orientation)
00502 {
00503 case DVRRenderSlice::XY:
00504
00505 for(std::vector<DVRRenderSlicePrimitive *>::const_iterator prim =
00506 clippedSlice->begin();
00507
00508 prim != clippedSlice->end();
00509 ++prim)
00510 {
00511 glBegin((*prim)->type);
00512
00513 std::vector<GLdouble *>::const_iterator vert =
00514 (*prim)->vertices.begin();
00515
00516 for(; vert != (*prim)->vertices.end(); ++vert)
00517 {
00518 MultiTexCoord2dARB(GL_TEXTURE0_ARB,
00519 (*vert)[3],
00520 (*vert)[4]);
00521
00522 MultiTexCoord2dARB(GL_TEXTURE1_ARB,
00523 (*vert)[3],
00524 (*vert)[4]);
00525
00526 glVertex3dv(*vert);
00527 }
00528 glEnd();
00529 }
00530 break;
00531
00532 case DVRRenderSlice::XZ:
00533
00534 for(std::vector<DVRRenderSlicePrimitive *>::const_iterator prim =
00535 clippedSlice->begin();
00536
00537 prim != clippedSlice->end();
00538 ++prim)
00539 {
00540 glBegin((*prim)->type);
00541
00542 std::vector<GLdouble *>::const_iterator vert =
00543 (*prim)->vertices.begin();
00544
00545 for(; vert != (*prim)->vertices.end(); vert++)
00546 {
00547 MultiTexCoord2dARB(GL_TEXTURE0_ARB,
00548 (*vert)[3],
00549 (*vert)[5]);
00550 MultiTexCoord2dARB(GL_TEXTURE1_ARB,
00551 (*vert)[3],
00552 (*vert)[5]);
00553
00554 glVertex3dv(*vert);
00555 }
00556 glEnd();
00557 }
00558 break;
00559
00560 case DVRRenderSlice::YZ:
00561
00562 for(std::vector<DVRRenderSlicePrimitive*>::const_iterator prim =
00563 clippedSlice->begin();
00564
00565 prim != clippedSlice->end();
00566 ++prim)
00567 {
00568 glBegin((*prim)->type);
00569
00570 std::vector<GLdouble*>::const_iterator vert =
00571 (*prim)->vertices.begin();
00572
00573 for(; vert != (*prim)->vertices.end(); vert++)
00574 {
00575 MultiTexCoord2dARB(GL_TEXTURE0_ARB,
00576 (*vert)[4],
00577 (*vert)[5]);
00578
00579 MultiTexCoord2dARB(GL_TEXTURE1_ARB,
00580 (*vert)[4],
00581 (*vert)[5]);
00582 glVertex3dv(*vert);
00583 }
00584 glEnd();
00585 }
00586 break;
00587
00588 default:
00589 break;
00590 }
00591 #endif // required extensions
00592 }
00593
00594
00595 #if defined(GL_ARB_multitexture) && defined(GL_NV_register_combiners)
00596
00597 typedef void (OSG_APIENTRY *CombinerParameteriNVFunc)( GLenum,
00598 const GLint );
00599 typedef void (OSG_APIENTRY *CombinerInputNVFunc )( GLenum,
00600 GLenum,
00601 GLenum,
00602 GLenum,
00603 GLenum,
00604 GLenum );
00605 typedef void (OSG_APIENTRY *CombinerOutputNVFunc ) ( GLenum,
00606 GLenum,
00607 GLenum,
00608 GLenum,
00609 GLenum,
00610 GLenum,
00611 GLenum,
00612 GLboolean,
00613 GLboolean,
00614 GLboolean);
00615 typedef void (OSG_APIENTRY *FinalCombinerInputNVFunc)( GLenum,
00616 GLenum,
00617 GLenum,
00618 GLenum );
00619
00621 void DVRMtexLUTShader::initCombiners(DrawActionBase *action)
00622 {
00623 Window *win = action->getWindow();
00624
00625 if(!win->hasExtension(_nvRegisterCombiners) ||
00626 !win->hasExtension(_arbMultitexture))
00627 {
00628 return;
00629 }
00630
00631 CombinerParameteriNVFunc CombinerParameteriNV =
00632 (CombinerParameteriNVFunc) win->getFunction(_funcCombinerParameteriNV);
00633
00634 CombinerInputNVFunc CombinerInputNV =
00635 (CombinerInputNVFunc ) win->getFunction(_funcCombinerInputNV );
00636
00637 CombinerOutputNVFunc CombinerOutputNV =
00638 (CombinerOutputNVFunc ) win->getFunction(_funcCombinerOutputNV );
00639
00640 FinalCombinerInputNVFunc FinalCombinerInputNV =
00641 (FinalCombinerInputNVFunc) win->getFunction(_funcFinalCombinerInputNV);
00642
00643
00644 CombinerParameteriNV(GL_NUM_GENERAL_COMBINERS_NV, 1);
00645
00646
00647
00648
00649
00650
00651 CombinerInputNV(
00652 GL_COMBINER0_NV,
00653 GL_RGB,
00654 GL_VARIABLE_A_NV,
00655 GL_CONSTANT_COLOR0_NV,
00656 GL_UNSIGNED_INVERT_NV,
00657 GL_ALPHA);
00658
00659 CombinerInputNV(
00660 GL_COMBINER0_NV,
00661 GL_RGB,
00662 GL_VARIABLE_B_NV,
00663 GL_TEXTURE0_ARB,
00664 GL_UNSIGNED_IDENTITY_NV,
00665 GL_RGB);
00666
00667 CombinerInputNV(
00668 GL_COMBINER0_NV,
00669 GL_RGB,
00670 GL_VARIABLE_C_NV,
00671 GL_CONSTANT_COLOR0_NV,
00672 GL_UNSIGNED_IDENTITY_NV,
00673 GL_ALPHA);
00674
00675 CombinerInputNV(
00676 GL_COMBINER0_NV,
00677 GL_RGB,
00678 GL_VARIABLE_D_NV,
00679 GL_TEXTURE1_ARB,
00680 GL_UNSIGNED_IDENTITY_NV,
00681 GL_RGB);
00682
00683
00684
00685
00686
00687
00688 CombinerInputNV(
00689 GL_COMBINER0_NV,
00690 GL_ALPHA,
00691 GL_VARIABLE_A_NV,
00692 GL_CONSTANT_COLOR0_NV,
00693 GL_UNSIGNED_INVERT_NV,
00694 GL_ALPHA);
00695
00696 CombinerInputNV(
00697 GL_COMBINER0_NV,
00698 GL_ALPHA,
00699 GL_VARIABLE_B_NV,
00700 GL_TEXTURE0_ARB,
00701 GL_UNSIGNED_IDENTITY_NV,
00702 GL_ALPHA);
00703
00704 CombinerInputNV(
00705 GL_COMBINER0_NV,
00706 GL_ALPHA,
00707 GL_VARIABLE_C_NV,
00708 GL_CONSTANT_COLOR0_NV,
00709 GL_UNSIGNED_IDENTITY_NV,
00710 GL_ALPHA);
00711
00712 CombinerInputNV(
00713 GL_COMBINER0_NV,
00714 GL_ALPHA,
00715 GL_VARIABLE_D_NV,
00716 GL_TEXTURE1_ARB,
00717 GL_UNSIGNED_IDENTITY_NV,
00718 GL_ALPHA);
00719
00720
00721
00722
00723 CombinerOutputNV(
00724 GL_COMBINER0_NV,
00725 GL_RGB,
00726 GL_DISCARD_NV,
00727 GL_DISCARD_NV,
00728 GL_SPARE0_NV,
00729 GL_NONE,
00730 GL_NONE,
00731 GL_FALSE,
00732 GL_FALSE,
00733 GL_FALSE);
00734
00735 CombinerOutputNV(
00736 GL_COMBINER0_NV,
00737 GL_ALPHA,
00738 GL_DISCARD_NV,
00739 GL_DISCARD_NV,
00740 GL_SPARE0_NV,
00741 GL_NONE,
00742 GL_NONE,
00743 GL_FALSE,
00744 GL_FALSE,
00745 GL_FALSE);
00746
00747
00748
00749
00750 FinalCombinerInputNV(
00751 GL_VARIABLE_A_NV,
00752 GL_ZERO,
00753 GL_UNSIGNED_IDENTITY_NV,
00754 GL_RGB);
00755
00756 FinalCombinerInputNV(
00757 GL_VARIABLE_B_NV,
00758 GL_ZERO,
00759 GL_UNSIGNED_IDENTITY_NV,
00760 GL_RGB);
00761
00762 FinalCombinerInputNV(
00763 GL_VARIABLE_C_NV,
00764 GL_ZERO,
00765 GL_UNSIGNED_IDENTITY_NV,
00766 GL_RGB);
00767
00768 FinalCombinerInputNV(
00769 GL_VARIABLE_D_NV,
00770 GL_SPARE0_NV,
00771 GL_UNSIGNED_IDENTITY_NV,
00772 GL_RGB);
00773
00774 FinalCombinerInputNV(
00775 GL_VARIABLE_G_NV,
00776 GL_SPARE0_NV,
00777 GL_UNSIGNED_IDENTITY_NV,
00778 GL_ALPHA);
00779 }
00780
00781 #endif // required extensions
00782
00783
00784
00785
00786
00787 #ifdef __sgi
00788 #pragma set woff 1174
00789 #endif
00790
00791 #ifdef OSG_LINUX_ICC
00792 #pragma warning( disable : 177 )
00793 #endif
00794
00795 namespace
00796 {
00797 static char cvsid_cpp[] = "@(#)$Id: $";
00798 static char cvsid_hpp[] = OSGDVRMTEXLUTSHADER_HEADER_CVSID;
00799 static char cvsid_inl[] = OSGDVRMTEXLUTSHADER_INLINE_CVSID;
00800 }
00801