TAPs 0.7.7.3
TAPsModelDeformableGLSL_Visualization_3DTexture.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsModelDeformableGLSL_Visualization_3DTexture.cpp
00003 ******************************************************************************/
00008 /******************************************************************************
00009 SUKITTI PUNAK   (05/12/2007)
00010 UPDATE          (07/21/2007)
00011 
00012 ******************************************************************************/
00013 #include "TAPsModelDeformableGLSL_Visualization_3DTexture.hpp"
00014 // Using Inclusion Model (i.e. definitions are included in declarations)
00015 //                       (this name.cpp is included in name.hpp)
00016 // Each friend is defined directly inside its declaration.
00017 
00018 BEGIN_NAMESPACE_TAPs__OpenGL
00019 //=============================================================================
00020 //-----------------------------------------------------------------------------
00021 // default constructor
00022 /*
00023 template <typename T>
00024 ModelDeformableGLSL_Visualization_3DTexture<T>::
00025 ModelDeformableGLSL_Visualization_3DTexture () 
00026 : m_SimDomain( NULL )
00027 {
00028     //---------------------------------------------------------------
00029     // Since Microsoft gl.h is version 1.1,
00030     // So this fn calls glewInit() for latest OpenGL version 
00031     // and initialize GLSL.
00032     GFnInitGLSL();
00033     Default();
00034     //---------------------------------------------------------------
00035 #ifdef  TAPs_DEBUG_MODE
00036     std::cout << "ModelDeformableGLSL_Visualization_3DTexture<" 
00037                 << typeid(T).name() << "> Constructor\n";
00038 #endif
00039 }
00040 //*/
00041 //-----------------------------------------------------------------------------
00042 // constructor
00043 template <typename T>
00044 ModelDeformableGLSL_Visualization_3DTexture<T>::
00045 ModelDeformableGLSL_Visualization_3DTexture ( 
00046     ModelDeformableGLSL<T> * simDomain
00047 )
00048 {
00049     m_SimDomain = simDomain;
00050     //---------------------------------------------------------------
00051     // Since Microsoft gl.h is version 1.1,
00052     // So this fn calls glewInit() for latest OpenGL version 
00053     // and initialize GLSL.
00054     GFnInitGLSL();
00055     Default();
00056     //---------------------------------------------------------------
00057 #ifdef  TAPs_DEBUG_MODE
00058     std::cout << "ModelDeformableGLSL_Visualization_3DTexture<" 
00059                 << typeid(T).name() << "> Constructor\n";
00060 #endif//TAPs_DEBUG_MODE
00061 }
00062 //-----------------------------------------------------------------------------
00063 // Destructor
00064 template <typename T>
00065 ModelDeformableGLSL_Visualization_3DTexture<T>::
00066 ~ModelDeformableGLSL_Visualization_3DTexture ()
00067 {
00068     Clear();
00069     //---------------------------------------------------------------
00070 #ifdef  TAPs_DEBUG_MODE
00071     std::cout << "ModelDeformableGLSL_Visualization_3DTexture<" 
00072                 << typeid(T).name() << "> Destructor\n";
00073 #endif//TAPs_DEBUG_MODE
00074 }
00075 //-----------------------------------------------------------------------------
00076 // Default
00077 template <typename T>
00078 void ModelDeformableGLSL_Visualization_3DTexture<T>::Default ()
00079 {
00080     //---------------------------------------------------------------
00081     // For 3D Object-Aligned Slice Draw Direction
00082     m_glslFBO_v3DObjAlignedSliceDrawDirection           =   0;
00083     m_glui2DTextureFor3DObjAlignedSliceDrawDirection    =   0;
00084     //-------------------------------------------
00085     // For GLSL
00086     m_glslProgramObjectFor3DTextureDrawDirection    =   NULL;
00087     m_glslProgramObjectFor3DTextureDrawing          =   NULL;
00088     //-------------------------------------------
00089     // For Textures
00090     m_3DTextureVisualization    =   NULL;
00091     m_1DTextureTransferFnLUT    =   NULL;
00092     //-------------------------------------------
00093 }
00094 //-----------------------------------------------------------------------------
00095 // Clear
00096 template <typename T>
00097 void ModelDeformableGLSL_Visualization_3DTexture<T>::Clear ()
00098 {
00099     //---------------------------------------------------------------
00100     // Delete Stuff for 3D Object-Aligned Slice Draw Direction
00101     if ( m_glslFBO_v3DObjAlignedSliceDrawDirection > 0 ) {
00102         glDeleteFramebuffersEXT( 1, &m_glslFBO_v3DObjAlignedSliceDrawDirection );
00103     }
00104     if ( m_glui2DTextureFor3DObjAlignedSliceDrawDirection > 0 ) {
00105         glDeleteTextures( 1, &m_glui2DTextureFor3DObjAlignedSliceDrawDirection );
00106     }
00107     //---------------------------------------------------------------
00108     // Clear GLSL
00109     if ( m_glslProgramObjectFor3DTextureDrawDirection ) {
00110         m_glslShaderManager.Delete( m_glslProgramObjectFor3DTextureDrawDirection );
00111     }
00112     if ( m_glslProgramObjectFor3DTextureDrawing ) {
00113         m_glslShaderManager.Delete( m_glslProgramObjectFor3DTextureDrawing );
00114     }
00115     //---------------------------------------------------------------
00116     // Clear Textures
00117     if ( m_3DTextureVisualization ) delete m_3DTextureVisualization;
00118     if ( m_1DTextureTransferFnLUT ) delete m_1DTextureTransferFnLUT;
00119     //---------------------------------------------------------------
00120     Default();
00121 }
00122 //-----------------------------------------------------------------------------
00123 //=============================================================================
00124 
00125 //=============================================================================
00126 // For Drawing (3D) Object-Aligned Slices
00127 //-----------------------------------------------------------------------------
00128 template <typename T>
00129 bool ModelDeformableGLSL_Visualization_3DTexture<T>::
00130 SetupFBOAndTextureForAlignedSliceDrawDirection ()
00131 {
00132     const int width  = 1;
00133     const int height = 1;
00134     //---------------------------------------------------------------
00135     // Setup 2D Texture for 3D Object-Aligned Slice Draw Direction
00136     glGenTextures( 1, &m_glui2DTextureFor3DObjAlignedSliceDrawDirection );
00137     if ( m_glui2DTextureFor3DObjAlignedSliceDrawDirection == 0 ) {
00138         assert( false );
00139         Clear();
00140         return false;
00141     }
00142     GLfloat data_2D[width*height*4];
00143     //
00144     glBindTexture( GL_TEXTURE_2D, m_glui2DTextureFor3DObjAlignedSliceDrawDirection );
00145     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00146     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
00147     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
00148     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
00149     glTexImage2D( 
00150         GL_TEXTURE_2D,          // target
00151         0,                      // level
00152         GL_RGBA32F_ARB,         // internal format
00153         width,                  // width
00154         height,                 // height
00155         0,                      // border
00156         GL_RGBA,                // pixel format
00157         GL_FLOAT,               // data type
00158         data_2D                 // texels
00159     );
00160     glBindTexture( GL_TEXTURE_2D, 0 );
00161     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00162     //-----------------------
00163     // For 3D Object-Aligned Slice Draw Direction
00164     //-----------------------
00165     glGenFramebuffersEXT( 1, &m_glslFBO_v3DObjAlignedSliceDrawDirection );
00166     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_glslFBO_v3DObjAlignedSliceDrawDirection );
00167     glFramebufferTexture2DEXT(
00168         GL_FRAMEBUFFER_EXT,                     // target
00169         GL_COLOR_ATTACHMENT0_EXT,               // attached frame
00170         GL_TEXTURE_2D,                          // texture target
00171         m_glui2DTextureFor3DObjAlignedSliceDrawDirection,   // texture id
00172         0                                       // level
00173     );
00174     TAPs::OpenGL::Fn::CHECK_FRAMEBUFFER_STATUS();
00175     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
00176     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00177     //---------------------------------
00178     return true;
00179 }
00180 //-----------------------------------------------------------------------------
00181 // END: SetupFBOAndTextureForAlignedSliceDrawDirection
00182 //=============================================================================
00183 
00184 //=============================================================================
00185 // Setup Shaders for Drawing (3D) Object-Aligned Slices
00186 //-----------------------------------------------------------------------------
00187 template <typename T>
00188 bool ModelDeformableGLSL_Visualization_3DTexture<T>::
00189 SetupShadersFor3DTextureDrawDirectionAndDrawing ()
00190 {
00191     // Draw Direction
00192     m_glslProgramObjectFor3DTextureDrawDirection = 
00193         m_glslShaderManager.LoadOnlyAFragmentShaderFromFile(
00194             "Resource/Shader/DefModelGetObjAlignedDrawDirection.frag"
00195     );
00196     if ( !m_glslProgramObjectFor3DTextureDrawDirection ) {
00197         Clear();
00198         return false;
00199     }
00200     // 3D Texture Drawing
00201     m_glslProgramObjectFor3DTextureDrawing = 
00202         m_glslShaderManager.LoadFromFile(
00203             "Resource/Shader/DefModelDraw3DTextureObjAligned.vert",
00204             "Resource/Shader/DefModelDraw3DTextureObjAligned.frag"
00205     );
00206     if ( !m_glslProgramObjectFor3DTextureDrawing ) {
00207         Clear();
00208         return false;
00209     }
00210     return true;
00211 }
00212 //-----------------------------------------------------------------------------
00213 // END: SetupShadersFor3DTextureDrawDirectionAndDrawing
00214 //=============================================================================
00215 
00216 //=============================================================================
00217 // Setup a 1D Texture for Transfer Function Lookup Table
00218 //-----------------------------------------------------------------------------
00219 template <typename T>
00220 bool ModelDeformableGLSL_Visualization_3DTexture<T>::
00221 Setup1DTextureForTransferFnLUT ()
00222 {
00223     //---------------------------------------------------------------
00224     // Create dataRGBA
00225     //GLubyte dataRGBA[256][4];
00226     GLfloat dataRGBA[256][4];
00227     //---------------------------------
00228     for ( int i = 0; i < 256; ++i ) {
00229 
00230         dataRGBA[i][0] = 0.75 + (GLfloat)rand() / (GLfloat)RAND_MAX * 0.05;
00231         dataRGBA[i][1] = 0.73 + (GLfloat)rand() / (GLfloat)RAND_MAX * 0.05;
00232         dataRGBA[i][2] = 0.61 + (GLfloat)rand() / (GLfloat)RAND_MAX * 0.05;
00233         dataRGBA[i][3] = i / 255.0;
00234 
00235         //dataRGBA[i][0] = 0.0;
00236         //dataRGBA[i][1] = 1.0;
00237         //dataRGBA[i][2] = 0.5;
00238         //dataRGBA[i][3] = 1.0;
00239     }
00240     //---------------------------------------------------------------
00241     // Setup 1D Texture
00242     //---------------------------------
00243     glEnable( GL_TEXTURE_1D );
00244     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );
00245     m_1DTextureTransferFnLUT = new Texture( 
00246         GL_TEXTURE_1D,          // target
00247         0,                      // level
00248         GL_RGBA,                // internal format
00249         256,                    // width
00250         0,                      // border
00251         GL_RGBA,                // pixel format
00252         //GL_UNSIGNED_BYTE,     // data type
00253         GL_FLOAT,               // data type
00254         dataRGBA,               // texels
00255         1,                      // number of textures
00256         GL_LINEAR,              // Mag Filter
00257         GL_LINEAR,              // Min Filter
00258         GL_CLAMP                // Wrap S
00259     );
00260     if ( !m_1DTextureTransferFnLUT ) {
00261         assert( false );
00262         Clear();
00263         return false;
00264     }
00265     //---------------------------------
00266     m_1DTextureTransferFnLUT->BindTexture(0);
00267     glDisable( GL_TEXTURE_1D );
00268     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00269     //---------------------------------------------------------------
00270     return true;
00271 }
00272 //-----------------------------------------------------------------------------
00273 // END: Setup1DTextureForTransferFnLUT
00274 //=============================================================================
00275 
00276 //=============================================================================
00277 // Setup a 3D Texture for Visualization
00278 //-----------------------------------------------------------------------------
00279 template <typename T>
00280 bool ModelDeformableGLSL_Visualization_3DTexture<T>::
00281 Setup3DTextureForVisualization ( 
00282     int resolutionX, int resolutionY, int resolutionZ, 
00283     enum GridGenerator<T>::VertexFlag *** pFlagDataForXYZVoxels
00284 )
00285 {
00286     //---------------------------------------------------------------
00287     // Set Texture Parameters
00288     GLenum      target          = GL_TEXTURE_3D;
00289     GLint       level           = 0;
00290     GLint       internalFormat  = GL_RGBA;
00291     //GLint     internalFormat  = GL_RGBA32F_ARB;
00292     GLsizei     width           = resolutionX;
00293     GLsizei     height          = resolutionY;
00294     GLsizei     depth           = resolutionZ;
00295     GLint       border          = 0;
00296     GLenum      pixelFormat     = GL_RGBA;
00297     //GLenum    dataType        = GL_UNSIGNED_BYTE;
00298     GLenum      dataType        = GL_FLOAT;
00299     //---------------------------------------------------------------
00300     // Setup Data for 3D Texture
00301     int iElementRGBALength = resolutionX * resolutionY * resolutionZ * 4;
00302     //===============================================================
00303     // Data from pGridGenerator is arrenged in x, y, and z order.
00304     // Switch it to z, y, and x order for ordering planes in z-direction.
00305     //---------------------------------------------------------------
00306     // Unsigned Byte (Char) Data Type
00307     if ( dataType == GL_UNSIGNED_BYTE ) {
00308         GLubyte * dataRGBA = new GLubyte[ iElementRGBALength ];
00309         int m = 0;
00310         for ( int k = 0; k < resolutionZ; ++k ) {
00311 
00312             //unsigned char alpha1 = 254 / resolutionZ;
00313             //unsigned char alpha2 = 510 / resolutionZ;
00314 
00315             //unsigned char alpha1 = 254 / resolutionZ * 3;
00316             //unsigned char alpha2 = 510 / resolutionZ * 3;
00317 
00318             unsigned char alpha1 = 254;
00319             unsigned char alpha2 = 254;
00320             bool useAssociatedColors = false;
00321 
00322             for ( int j = 0; j < resolutionY; ++j ) {
00323                 for ( int i = 0; i < resolutionX; ++i ) {
00324                     if ( pFlagDataForXYZVoxels[i][j][k] >= GridGenerator<T>::INSIDE_MODEL ) {
00325                         dataRGBA[m  ] = 255;
00326                         dataRGBA[m+1] = 255;
00327                         dataRGBA[m+2] = 0;
00328                         dataRGBA[m+3] = alpha1;
00329                     }
00330                     else if ( pFlagDataForXYZVoxels[i][j][k] >= GridGenerator<T>::RIGHT_INSIDE_BOUNDARY ) {
00331                         dataRGBA[m  ] = 0;
00332                         dataRGBA[m+1] = 120;
00333                         dataRGBA[m+2] = 0;
00334                         dataRGBA[m  ] += i;
00335                         dataRGBA[m+1] += j;
00336                         dataRGBA[m+2] += k;
00337                         dataRGBA[m+3] = alpha2;
00338                         //dataRGBA[m+3] = (double)rand() / (double)RAND_MAX * 254 + 1;
00339                     }
00340                     else if ( pFlagDataForXYZVoxels[i][j][k] >= GridGenerator<T>::ON_BOUNDARY ) {
00341                         dataRGBA[m  ] = 0;
00342                         dataRGBA[m+1] = 0;
00343                         //dataRGBA[m+2] = 1;
00344                         //dataRGBA[m+3] = alpha2;
00345                         dataRGBA[m+2] = 0;
00346                         dataRGBA[m+3] = 0;
00347                     }
00348                     else if ( pFlagDataForXYZVoxels[i][j][k] >= GridGenerator<T>::RIGHT_OUTSIDE_BOUNDARY ) {
00349                         dataRGBA[m  ] = 0;
00350                         dataRGBA[m+1] = 0;
00351                         dataRGBA[m+2] = 0;
00352                         //dataRGBA[m+3] = 1;
00353                         dataRGBA[m+3] = 0;
00354                     }
00355                     else {
00356                         dataRGBA[m  ] = 0;
00357                         dataRGBA[m+1] = 0;
00358                         dataRGBA[m+2] = 0;
00359                         dataRGBA[m+3] = 0;
00360                     }
00361                     //-------------------------------
00362                     if ( useAssociatedColors ) {
00363                         dataRGBA[m  ] *= dataRGBA[m+3];
00364                         dataRGBA[m+1] *= dataRGBA[m+3];
00365                         dataRGBA[m+2] *= dataRGBA[m+3];
00366                     }
00367                     //-------------------------------
00368                     m += 4;
00369                 }
00370             }
00371         }
00372         //-------------------------------------------------
00373         // Setup 3D Texture
00374         //---------------------------------
00375         glEnable( GL_TEXTURE_3D );
00376         glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );
00377         m_3DTextureVisualization = new Texture( 
00378             target, level, internalFormat, 
00379             width, height, depth, 
00380             border, pixelFormat,                
00381             dataType, 
00382             dataRGBA,                       // texels
00383             1,                              // number of textures
00384             GL_LINEAR, GL_LINEAR,           // mag and min filter
00385             GL_CLAMP, GL_CLAMP, GL_CLAMP    // wrap s, t, and r
00386         );
00387         m_3DTextureVisualization->UnbindTexture();
00388         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00389         delete [] dataRGBA;
00390     }
00391     //
00392     //---------------------------------------------------------------
00393     // Float Data Type
00394     else if ( dataType == GL_FLOAT ) {
00395         GLfloat * dataRGBA = new GLfloat[ iElementRGBALength ];
00396         int m = 0;
00397         for ( int k = 0; k < resolutionZ; ++k ) {
00398 
00399             //float alpha1 = 1.0 / resolutionZ;
00400             //float alpha2 = 2.0 / resolutionZ;
00401 
00402             //float alpha1 = 1.0 / resolutionZ * 3;
00403             //float alpha2 = 2.0 / resolutionZ * 3;
00404 
00405             // Can't set to any value greater than 254.0/255.0.
00406             // Otherwise, the transfer function lookup will not get a correct value.
00407             // This error is about texture coordinate out of range.
00408             float alpha1 = 0.995;
00409             float alpha2 = 0.995;
00410             bool useAssociatedColors = false;
00411 
00412             for ( int j = 0; j < resolutionY; ++j ) {
00413                 for ( int i = 0; i < resolutionX; ++i ) {
00414                     if ( pFlagDataForXYZVoxels[i][j][k] >= GridGenerator<T>::INSIDE_MODEL ) {
00415                         dataRGBA[m  ] = 1;
00416                         dataRGBA[m+1] = 1;
00417                         dataRGBA[m+2] = 0;
00418                         dataRGBA[m+3] = alpha1;
00419                     }
00420                     else if ( pFlagDataForXYZVoxels[i][j][k] >= GridGenerator<T>::RIGHT_INSIDE_BOUNDARY ) {
00421                         //dataRGBA[m  ] = 0.5;
00422                         //dataRGBA[m+1] = 0.5;
00423                         //dataRGBA[m+2] = 0.5;
00424                         dataRGBA[m  ] = 0.0;
00425                         dataRGBA[m+1] = 0.5;
00426                         dataRGBA[m+2] = 0.0;
00427                         
00428                         //dataRGBA[m  ] += (float)i/resolutionX;
00429                         //dataRGBA[m+1] += (float)j/resolutionY;
00430                         //dataRGBA[m+2] += (float)k/resolutionZ;
00431                         dataRGBA[m+3] = alpha2;
00432 
00433 //                      dataRGBA[m  ] += (double)rand() / (double)RAND_MAX;
00434 //                      dataRGBA[m+1] += (double)rand() / (double)RAND_MAX;
00435 //                      dataRGBA[m+2] += (double)rand() / (double)RAND_MAX;
00436                         //dataRGBA[m+3] = (double)rand() / (double)RAND_MAX + 0.5;
00437                         if ( dataRGBA[m  ] > 1.0f ) dataRGBA[m  ] = 1.0f;
00438                         if ( dataRGBA[m+1] > 1.0f ) dataRGBA[m+1] = 1.0f;
00439                         if ( dataRGBA[m+2] > 1.0f ) dataRGBA[m+2] = 1.0f;
00440                         if ( dataRGBA[m+3] > 1.0f ) dataRGBA[m+3] = 1.0f;
00441                         //dataRGBA[m+3] = i;
00442                     }
00443                     else if ( pFlagDataForXYZVoxels[i][j][k] >= GridGenerator<T>::ON_BOUNDARY ) {
00444                         dataRGBA[m  ] = 0;
00445                         dataRGBA[m+1] = 0;
00446                         //dataRGBA[m+2] = 1;
00447                         //dataRGBA[m+3] = alpha2;
00448                         dataRGBA[m+2] = 0;
00449                         dataRGBA[m+3] = 0;
00450                     }
00451                     else if ( pFlagDataForXYZVoxels[i][j][k] >= GridGenerator<T>::RIGHT_OUTSIDE_BOUNDARY ) {
00452                         dataRGBA[m  ] = 0;
00453                         dataRGBA[m+1] = 0;
00454                         dataRGBA[m+2] = 0;
00455                         //dataRGBA[m+3] = 0.001;
00456                         dataRGBA[m+3] = 0.0;
00457                         //dataRGBA[m+3] = 1.0;  // DEBUG
00458                     }
00459                     else {
00460                         dataRGBA[m  ] = 0;
00461                         dataRGBA[m+1] = 0;
00462                         dataRGBA[m+2] = 0;
00463                         dataRGBA[m+3] = 0;
00464                     }
00465                     //-------------------------------
00466                     if ( useAssociatedColors ) {
00467                         dataRGBA[m  ] *= dataRGBA[m+3];
00468                         dataRGBA[m+1] *= dataRGBA[m+3];
00469                         dataRGBA[m+2] *= dataRGBA[m+3];
00470                     }
00471                     //-------------------------------
00472                     m += 4;
00473                 }
00474             }
00475         }
00476         //-------------------------------------------------
00477         // Setup 3D Texture
00478         //---------------------------------
00479         glEnable( GL_TEXTURE_3D );
00480         glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );
00481         m_3DTextureVisualization = new Texture( 
00482             target, level, internalFormat, 
00483             width, height, depth, 
00484             border, pixelFormat,                
00485             dataType, 
00486             dataRGBA,                       // texels
00487             1,                              // number of textures
00488             GL_LINEAR, GL_LINEAR,           // mag and min filter
00489             GL_CLAMP, GL_CLAMP, GL_CLAMP    // wrap s, t, and r
00490         );
00491         m_3DTextureVisualization->UnbindTexture();
00492         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00493         delete [] dataRGBA;
00494     }
00495     //
00496     //---------------------------------------------------------------
00497     // Unsupported Data Type!
00498     else {
00499         std::cerr << "The data type of m_3DTextureVisualizationParameters is not supported!" << std::endl;
00500         assert( false );
00501         Clear();
00502         return false;
00503     }
00504     //---------------------------------------------------------------
00505     return true;
00506 }
00507 //-----------------------------------------------------------------------------
00508 // END: Setup3DTextureForVisualization
00509 //=============================================================================
00510 
00511 //=============================================================================
00512 // CalAndRecordFreqsOfHistogramRange_0_255_ToArray
00513 //-----------------------------------------------------------------------------
00514 template <typename T>
00515 void ModelDeformableGLSL_Visualization_3DTexture<T>::
00516 CalAndRecordFreqsOfHistogramRange_0_255_ToArray ( 
00517     GLubyte freqs[256] )
00518 {
00519     if ( !m_3DTextureVisualization ) {
00520         assert( false );
00521         return;
00522     }
00523     //===============================================================
00524     // Read data from GPU
00525     //---------------------------------------------------------------
00526     glPushAttrib( GL_ALL_ATTRIB_BITS );
00527     //-----------------------------------------------------
00528     GLsizei sizeForVisual = 
00529             m_3DTextureVisualization->GetWidth() *
00530             m_3DTextureVisualization->GetHeight() *
00531             m_3DTextureVisualization->GetDepth() *
00532             m_3DTextureVisualization->GetInternalFormatNumberOfComponents();
00533     GLfloat * dataForVisual = new GLfloat[ sizeForVisual ];
00534     {
00535         //=================================================
00536         // Read data for positions from a texture to memory
00537         glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
00538         m_3DTextureVisualization->BindTexture(0);
00539         glGetTexImage( 
00540             m_3DTextureVisualization->GetTarget(),
00541             m_3DTextureVisualization->GetLevel(),
00542             m_3DTextureVisualization->GetPixelFormat(),
00543             m_3DTextureVisualization->GetDataType(),
00544             dataForVisual
00545         );
00546         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00547         //=================================================
00548     }
00549     //---------------------------------------------------------------
00550     glPopAttrib();
00551     //---------------------------------------------------------------
00552     //===============================================================
00553     //---------------------------------------------------------------
00554     // Calculate and Record the 256 frequencies for histogram to the input array
00555     for ( int f = 0; f < 256; ++f ) {
00556         freqs[f] = 0;
00557     }
00558     GLubyte idx;
00559     for ( int i = 3; i < sizeForVisual; i+=4 ) {
00560         double D = dataForVisual[i] * 255.0;
00561         int I    = (int) D;
00562         if ( D-I >= 0.5 )   idx = I+1;
00563         else                idx = I;
00564         if ( idx > 255 )    idx = 255;
00565         else if ( idx < 0 ) idx = 0;
00566         ++freqs[idx];
00567     }
00568 
00569     #ifdef  TAPs_DEBUG_MODE
00570     for ( int f = 0; f < 256; ++f ) {
00571         std::cout << "freqs[" << f << "]: " << (int)freqs[f] << "\n";
00572     }
00573     #endif//TAPs_DEBUG_MODE
00574 
00575     //---------------------------------------------------------------
00576     delete [] dataForVisual;
00577 }
00578 //-----------------------------------------------------------------------------
00579 // END: CalAndRecordFreqsOfHistogramRange_0_255_ToArray
00580 //=============================================================================
00581 
00582 //=============================================================================
00583 //-----------------------------------------------------------------------------
00584 //-----------------------------------------------------------------------------
00585 //=============================================================================
00586 
00587 //=============================================================================
00588 //-----------------------------------------------------------------------------
00589 //-----------------------------------------------------------------------------
00590 //=============================================================================
00591 
00592 //=============================================================================
00593 //-----------------------------------------------------------------------------
00594 //-----------------------------------------------------------------------------
00595 /*
00596 template <typename T>
00597 void ModelDeformableGLSL<T>::DrawByGL_3DTexture ( DrawType drawType )
00598 {
00599     static int iMaxNumberOfSlices = 512;
00600     //---------------------------------------------------------------
00601 #ifdef TAPs_DEBUG_MODE
00602     if ( !m_SimDomain ) return;
00603     //---------------------------------------------------------------
00604     if ( !m_SimDomain->RetObject3DTexturePosition() )   return;
00605     //---------------------------------------------------------------
00606     if ( !m_glslProgramObjectFor3DTextureDrawDirection )    return;
00607     //---------------------------------------------------------------
00608     if ( !m_glslProgramObjectFor3DTextureDrawing )  return;
00609     //---------------------------------------------------------------
00610     if ( !m_3DTextureVisualization )    return;
00611 #endif
00612     //---------------------------------------------------------------
00613     // Find direction to the eye view
00614     glPushAttrib( GL_ALL_ATTRIB_BITS );
00615     glEnable( GL_TEXTURE_2D );
00616     m_glslProgramObject->BeginGLSL();
00617     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_glslFBO_v3DObjAlignedSliceDrawDirection );
00618     TAPs::OpenGL::Fn::CHECK_FRAMEBUFFER_STATUS();
00619     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00620     //
00621     //GLfloat modelViewMatrix[16];
00622     //
00623     {
00624         //-------------------------------------------------
00625         glMatrixMode( GL_PROJECTION );
00626         glPushMatrix();
00627         glLoadIdentity();
00628         gluOrtho2D( 0, 1, 0, 1 );
00629         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00630         //
00631         // Load Model View Matrix
00632         glMatrixMode( GL_MODELVIEW );
00633         glPushMatrix();
00634         //glGetFloatv( GL_MODELVIEW_MATRIX, modelViewMatrix );
00635         //glLoadmatrixf( modelViewMatrix );
00636         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00637         //
00638         // Only one pixel is needed to get the inverse transpose of modelview matrix
00639         glViewport( 0, 0, 1, 1 );
00640         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00641         {
00642             glEnable( GL_TEXTURE_2D );
00643             glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_glslFBO_v3DObjAlignedSliceDrawDirection );
00644             glFramebufferTexture2DEXT(
00645                 GL_FRAMEBUFFER_EXT,                     // target
00646                 GL_COLOR_ATTACHMENT0_EXT,               // attached frame
00647                 GL_TEXTURE_2D,                          // texture target
00648                 m_glui2DTextureFor3DObjAlignedSliceDrawDirection,   // texture id
00649                 0                                       // level
00650             );
00651             glDrawBuffer( GL_COLOR_ATTACHMENT0_EXT );
00652             TAPs::OpenGL::Fn::CHECK_FRAMEBUFFER_STATUS();
00653             TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00654             glClearColor( 0, 0, 0, 0 );
00655             glDisable( GL_DEPTH_TEST );
00656             glClear( GL_COLOR_BUFFER_BIT );
00657             glPolygonMode( GL_FRONT, GL_FILL );
00658             glBegin( GL_POINTS );
00659                 glVertex3f( 0, 0, 0 );
00660             glEnd();
00661         }
00662         glPopMatrix();
00663         glMatrixMode( GL_PROJECTION );
00664         glPopMatrix();
00665         glMatrixMode( GL_MODELVIEW );
00666     }
00667     glDisable( GL_TEXTURE_2D );
00668     glBindTexture( GL_TEXTURE_2D, 0 );
00669     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
00670     m_glslProgramObject->EndGLSL();
00671     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00672     glPopAttrib();
00673     //
00674     //===========================================
00675     // Read drawing direction from the texture
00676     const int width = 1, height = 1;
00677     GLfloat drawDir[width*height*4];
00678     glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
00679     glBindTexture( GL_TEXTURE_2D, m_glui2DTextureFor3DObjAlignedSliceDrawDirection );
00680     glGetTexImage( 
00681         GL_TEXTURE_2D,
00682         0,
00683         GL_RGBA,
00684         GL_FLOAT,
00685         drawDir
00686     );
00687     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00688     //-------------------------------------------
00689     // Find the draw direction
00690     //   The draw direction is the data with maximum absolute value.
00691     GLfloat absDrawDir[3] = { fabs( drawDir[0] ), fabs( drawDir[1] ), fabs( drawDir[2] ) };
00692     int iDrawDir = 0;       // X: -1,1; Y: -2,2; Z: -3,3
00693     if ( absDrawDir[0] > absDrawDir[1] ) {
00694         if ( absDrawDir[0] > absDrawDir[2] ) {
00695             if ( drawDir[0] > 0.0f )    iDrawDir =  1;
00696             else                        iDrawDir = -1;
00697         }
00698         else {
00699             if ( drawDir[2] > 0.0f )    iDrawDir =  3;
00700             else                        iDrawDir = -3;
00701         }
00702     }
00703     else {
00704         if ( absDrawDir[1] > absDrawDir[2] ) {
00705             if ( drawDir[1] > 0.0f )    iDrawDir =  2;
00706             else                        iDrawDir = -2;
00707         }
00708         else {
00709             if ( drawDir[2] > 0.0f )    iDrawDir =  3;
00710             else                        iDrawDir = -3;
00711         }
00712     }
00713     //=================================================
00714 
00719     //GLfloat pModelViewMatrix[16];
00720     //GLfloat pModelViewMatrixInv[16];
00721     //glGetFloatv( GL_MODELVIEW_MATRIX, pModelViewMatrix );
00722     //InverseMatrix( pModelViewMatrix, pModelViewMatrixInv );
00724     //GLfloat pViewVector[4] = { 0.0f, 0.0f, -1.0f, 0.0f };
00725     //MatVecMultiply( pModelViewMatrixInv, pViewVector );
00727     //int nMax = FindAbsMaximum( pViewVector );
00728     //switch ( nMax ) {
00729     //case X:
00730     //  if ( pViewVector[X] > 0.0f ) {
00731     //      DrawSliceStack_PositiveX();
00732     //  }
00733     //  else {
00734     //      DrawSliceStack_NagativeX();
00735     //  }
00736     //  break;
00737     //case Y:
00738     //  if ( pViewVector[Y] > 0.0f ) {
00739     //      DrawSliceStack_PositiveY();
00740     //  }
00741     //  else {
00742     //      DrawSliceStack_NagativeY();
00743     //  }
00744     //  break;
00745     //case Z:
00746     //  if ( pViewVector[Z] > 0.0f ) {
00747     //      DrawSliceStack_PositiveZ();
00748     //  }
00749     //  else {
00750     //      DrawSliceStack_NagativeZ();
00751     //  }
00752     //  break;
00753     //}
00756 
00757     //---------------------------------------------------------------
00758     // Draw The 3D Texture
00759 //  if ( m_glui3DTextureForVisualization ) {
00760     if ( m_3DTextureVisualization ) {
00761         glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT );
00762         glEnable( GL_LIGHTING );
00763         glEnable( GL_COLOR_MATERIAL );
00764         glColor3f( 0.15, 0.45, 0.53 );
00765         glEnable( GL_BLEND );
00766         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00767         glActiveTexture( GL_TEXTURE0 );
00768         m_3DTextureVisualization->EnableTextureTarget();
00769         m_3DTextureVisualization->BindTexture(0);
00770         m_3DTextureVisualization->UseParameters();
00771         Vector3<T> vAABBMinPt = GetBoundingAABBLowPoint();
00772         Vector3<T> vAABBMaxPt = GetBoundingAABBHighPoint();
00773         //-----------------------------------------------------------
00774         int iSubdivision = 8;
00775         int iAxis;
00776         glBegin( GL_QUADS );
00777         //-----------------------------------------------------------
00778         // DrawSliceStack_NegativeZ();  // back-to-front is from -z to +z
00779         if ( iDrawDir == -3 ) {
00780             iAxis = 2;
00781             T zValue = vAABBMinPt[iAxis];
00782             T numberOfSlices = m_vGridResolution[iAxis] * iSubdivision;
00783             if ( numberOfSlices > iMaxNumberOfSlices ) numberOfSlices = iMaxNumberOfSlices;
00784             T zStep  = (vAABBMaxPt[iAxis] - vAABBMinPt[iAxis] ) / (numberOfSlices-1);
00785             T tValue = 0;
00786             T tStep  = 1.0 / (numberOfSlices-1);
00787             for ( int i = 0; i < numberOfSlices; ++i )
00788             {
00789                 glTexCoord3f( 0, 0, tValue );
00790                 glNormal3f( 0, 0, 1 );
00791                 glVertex3f( vAABBMinPt[0], vAABBMinPt[1], zValue );
00792                 //
00793                 glTexCoord3f( 1, 0, tValue );
00794                 glNormal3f( 0, 0, 1 );
00795                 glVertex3f( vAABBMaxPt[0], vAABBMinPt[1], zValue );
00796                 //
00797                 glTexCoord3f( 1, 1, tValue );
00798                 glNormal3f( 0, 0, 1 );
00799                 glVertex3f( vAABBMaxPt[0], vAABBMaxPt[1], zValue );
00800                 //
00801                 glTexCoord3f( 0, 1, tValue );
00802                 glNormal3f( 0, 0, 1 );
00803                 glVertex3f( vAABBMinPt[0], vAABBMaxPt[1], zValue );
00804                 //
00805                 zValue += zStep;
00806                 tValue += tStep;
00807             }
00808         }
00809         //-----------------------------------------------------------
00810         // DrawSliceStack_NegativeX();  // back-to-front is from -x to +x
00811         else if ( iDrawDir == -1 ) {
00812             iAxis = 0;
00813             T zValue = vAABBMinPt[iAxis];
00814             T numberOfSlices = m_vGridResolution[iAxis] * iSubdivision;
00815             if ( numberOfSlices > iMaxNumberOfSlices ) numberOfSlices = iMaxNumberOfSlices;
00816             T zStep  = (vAABBMaxPt[iAxis] - vAABBMinPt[iAxis] ) / (numberOfSlices-1);
00817             T tValue = 0;
00818             T tStep  = 1.0 / (numberOfSlices-1);
00819             for ( int i = 0; i < numberOfSlices; ++i )
00820             {
00821                 glTexCoord3f( tValue, 0, 1 );
00822                 glNormal3f( 1, 0, 0 );
00823                 glVertex3f( zValue, vAABBMinPt[1], vAABBMaxPt[2] );
00824                 //
00825                 glTexCoord3f( tValue, 0, 0 );
00826                 glNormal3f( 1, 0, 0 );
00827                 glVertex3f( zValue, vAABBMinPt[1], vAABBMinPt[2] );
00828                 //
00829                 glTexCoord3f( tValue, 1, 0 );
00830                 glNormal3f( 1, 0, 0 );
00831                 glVertex3f( zValue, vAABBMaxPt[1], vAABBMinPt[2] );
00832                 //
00833                 glTexCoord3f( tValue, 1, 1 );
00834                 glNormal3f( 1, 0, 0 );
00835                 glVertex3f( zValue, vAABBMaxPt[1], vAABBMaxPt[2] );
00836                 //
00837                 zValue += zStep;
00838                 tValue += tStep;
00839             }
00840         }
00841         //-----------------------------------------------------------
00842         // DrawSliceStack_NegativeY();  // back-to-front is from -y to +y
00843         else if ( iDrawDir == -2 ) {
00844             iAxis = 1;
00845             T zValue = vAABBMinPt[iAxis];
00846             T numberOfSlices = m_vGridResolution[iAxis] * iSubdivision;
00847             if ( numberOfSlices > iMaxNumberOfSlices ) numberOfSlices = iMaxNumberOfSlices;
00848             T zStep  = (vAABBMaxPt[iAxis] - vAABBMinPt[iAxis] ) / (numberOfSlices-1);
00849             T tValue = 0;
00850             T tStep  = 1.0 / (numberOfSlices-1);
00851             for ( int i = 0; i < numberOfSlices; ++i )
00852             {
00853                 glTexCoord3f( 0, tValue, 1 );
00854                 glNormal3f( 0, 1, 0 );
00855                 glVertex3f( vAABBMinPt[0], zValue, vAABBMaxPt[2] );
00856                 //
00857                 glTexCoord3f( 1, tValue, 1 );
00858                 glNormal3f( 0, 1, 0 );
00859                 glVertex3f( vAABBMaxPt[0], zValue, vAABBMaxPt[2] );
00860                 //
00861                 glTexCoord3f( 1, tValue, 0 );
00862                 glNormal3f( 0, 1, 0 );
00863                 glVertex3f( vAABBMaxPt[0], zValue, vAABBMinPt[2] );
00864                 //
00865                 glTexCoord3f( 0, tValue, 0 );
00866                 glNormal3f( 0, 1, 0 );
00867                 glVertex3f( vAABBMinPt[0], zValue, vAABBMinPt[2] );
00868                 //
00869                 zValue += zStep;
00870                 tValue += tStep;
00871             }
00872         }
00873         //-----------------------------------------------------------
00874         // DrawSliceStack_PositiveZ();  // back-to-front is from +z to -z
00875         else if ( iDrawDir == 3 ) {
00876             iAxis = 2;
00877             T zValue = vAABBMaxPt[iAxis];
00878             T numberOfSlices = m_vGridResolution[iAxis] * iSubdivision;
00879             if ( numberOfSlices > iMaxNumberOfSlices ) numberOfSlices = iMaxNumberOfSlices;
00880             T zStep  = (vAABBMinPt[iAxis] - vAABBMaxPt[iAxis] ) / (numberOfSlices-1);
00881             T tValue = 1.0;
00882             T tStep  = -1.0 / (numberOfSlices-1);
00883             for ( int i = 0; i < numberOfSlices; ++i )
00884             {
00885                 glTexCoord3f( 0, 0, tValue );
00886                 glNormal3f( 0, 0, -1 );
00887                 glVertex3f( vAABBMinPt[0], vAABBMinPt[1], zValue );
00888                 //
00889                 glTexCoord3f( 0, 1, tValue );
00890                 glNormal3f( 0, 0, -1 );
00891                 glVertex3f( vAABBMinPt[0], vAABBMaxPt[1], zValue );
00892                 //
00893                 glTexCoord3f( 1, 1, tValue );
00894                 glNormal3f( 0, 0, -1 );
00895                 glVertex3f( vAABBMaxPt[0], vAABBMaxPt[1], zValue );
00896                 //
00897                 glTexCoord3f( 1, 0, tValue );
00898                 glNormal3f( 0, 0, -1 );
00899                 glVertex3f( vAABBMaxPt[0], vAABBMinPt[1], zValue );
00900                 //
00901                 zValue += zStep;
00902                 tValue += tStep;
00903             }
00904         }
00905         //-----------------------------------------------------------
00906         // DrawSliceStack_PositiveX();  // back-to-front is from +x to -x
00907         else if ( iDrawDir == 1 ) {
00908             iAxis = 0;
00909             T zValue = vAABBMaxPt[iAxis];
00910             T numberOfSlices = m_vGridResolution[iAxis] * iSubdivision;
00911             if ( numberOfSlices > iMaxNumberOfSlices ) numberOfSlices = iMaxNumberOfSlices;
00912             T zStep  = (vAABBMinPt[iAxis] - vAABBMaxPt[iAxis] ) / (numberOfSlices-1);
00913             T tValue = 1.0;
00914             T tStep  = -1.0 / (numberOfSlices-1);
00915             for ( int i = 0; i < numberOfSlices; ++i )
00916             {
00917                 glTexCoord3f( tValue, 0, 0 );
00918                 glNormal3f( -1, 0, 0 );
00919                 glVertex3f( zValue, vAABBMinPt[1], vAABBMinPt[2] );
00920                 //
00921                 glTexCoord3f( tValue, 0, 1 );
00922                 glNormal3f( -1, 0, 0 );
00923                 glVertex3f( zValue, vAABBMinPt[1], vAABBMaxPt[2] );
00924                 //
00925                 glTexCoord3f( tValue, 1, 1 );
00926                 glNormal3f( -1, 0, 0 );
00927                 glVertex3f( zValue, vAABBMaxPt[1], vAABBMaxPt[2] );
00928                 //
00929                 glTexCoord3f( tValue, 1, 0 );
00930                 glNormal3f( -1, 0, 0 );
00931                 glVertex3f( zValue, vAABBMaxPt[1], vAABBMinPt[2] );
00932                 //
00933                 zValue += zStep;
00934                 tValue += tStep;
00935             }
00936         }
00937         //-----------------------------------------------------------
00938         // DrawSliceStack_PositiveY();  // back-to-front is from +y to -y
00939         else if ( iDrawDir == 2 ) {
00940             iAxis = 1;
00941             T zValue = vAABBMaxPt[iAxis];
00942             T numberOfSlices = m_vGridResolution[iAxis] * iSubdivision;
00943             if ( numberOfSlices > iMaxNumberOfSlices ) numberOfSlices = iMaxNumberOfSlices;
00944             T zStep  = (vAABBMinPt[iAxis] - vAABBMaxPt[iAxis] ) / (numberOfSlices-1);
00945             T tValue = 1.0;
00946             T tStep  = -1.0 / (numberOfSlices-1);
00947             for ( int i = 0; i < numberOfSlices; ++i )
00948             {
00949                 glTexCoord3f( 0, tValue, 1 );
00950                 glNormal3f( 0, -1, 0 );
00951                 glVertex3f( vAABBMinPt[0], zValue, vAABBMaxPt[2] );
00952                 //
00953                 glTexCoord3f( 0, tValue, 0 );
00954                 glNormal3f( 0, -1, 0 );
00955                 glVertex3f( vAABBMinPt[0], zValue, vAABBMinPt[2] );
00956                 //
00957                 glTexCoord3f( 1, tValue, 0 );
00958                 glNormal3f( 0, -1, 0 );
00959                 glVertex3f( vAABBMaxPt[0], zValue, vAABBMinPt[2] );
00960                 //
00961                 glTexCoord3f( 1, tValue, 1 );
00962                 glNormal3f( 0, -1, 0 );
00963                 glVertex3f( vAABBMaxPt[0], zValue, vAABBMaxPt[2] );
00964                 //
00965                 zValue += zStep;
00966                 tValue += tStep;
00967             }
00968         }
00969         //-----------------------------------------------------------
00970         glEnd();
00971         glBindTexture( GL_TEXTURE_3D, 0 );
00972         glPopAttrib();
00973     }
00974 }
00975 //*/
00976 //-----------------------------------------------------------------------------
00977 //=============================================================================
00978 
00979 //=============================================================================
00980 // DrawByGL (Using 3D Texture)
00981 //-----------------------------------------------------------------------------
00982 template <typename T>
00983 void ModelDeformableGLSL_Visualization_3DTexture<T>::
00984 DrawByGL ( int iSubdivision )
00985 {
00986     static int iMaxNumberOfSlices = 512;
00987     //---------------------------------------------------------------
00988 #ifdef TAPs_DEBUG_MODE
00989     if ( !m_SimDomain ) return;
00990     //---------------------------------------------------------------
00991     if ( !m_SimDomain->RetObject3DTexturePosition() )   return;
00992     //---------------------------------------------------------------
00993     if ( !m_glslProgramObjectFor3DTextureDrawDirection )    return;
00994     //---------------------------------------------------------------
00995     if ( !m_glslProgramObjectFor3DTextureDrawing )  return;
00996     //---------------------------------------------------------------
00997     if ( !m_3DTextureVisualization )    return;
00998 #endif
00999     //---------------------------------------------------------------
01000     //===============================================================
01001     // Read data from GPU
01002     //---------------------------------------------------------------
01003     Texture * texture3DPosition     = m_SimDomain->RetObject3DTexturePosition();
01004     Texture * texture3DConnectivity = m_SimDomain->RetObject3DTextureConnectivity();
01005     //---------------------------------------------------------------
01006     glPushAttrib( GL_ALL_ATTRIB_BITS );
01007     //-----------------------------------------------------
01008     GLsizei sizeForPosition = 
01009             texture3DPosition->GetWidth() *
01010             texture3DPosition->GetHeight() *
01011             texture3DPosition->GetDepth() *
01012             texture3DPosition->GetInternalFormatNumberOfComponents();
01013     GLfloat * dataForPosition = new GLfloat[ sizeForPosition ];
01014     //
01015     //GLsizei sizeForConnectivity = 
01016     //      texture3DConnectivity->GetWidth() *
01017     //      texture3DConnectivity->GetHeight() *
01018     //      texture3DConnectivity->GetDepth() *
01019     //      texture3DConnectivity->GetInternalFormatNumberOfComponents();
01020     //GLfloat * dataForConnectivity = new GLfloat[ sizeForConnectivity ];
01021     //
01022     {
01023         int i = 0;
01024         //=================================================
01025         // Read data for positions from a texture to memory
01026         glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
01027         texture3DPosition->BindTexture(0);
01028         glGetTexImage( 
01029             texture3DPosition->GetTarget(),
01030             texture3DPosition->GetLevel(),
01031             texture3DPosition->GetPixelFormat(),
01032             texture3DPosition->GetDataType(),
01033             dataForPosition
01034         );
01035         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01036         //=================================================
01037         //=================================================
01038         // Read data for connectivities from a texture to memory
01039         //glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
01040         //texture3DConnectivity->BindTexture(0);
01041         //glGetTexImage( 
01042         //  texture3DConnectivity->GetTarget(),
01043         //  texture3DConnectivity->GetLevel(),
01044         //  texture3DConnectivity->GetPixelFormat(),
01045         //  texture3DConnectivity->GetDataType(),
01046         //  dataForConnectivity
01047         //);
01048         //TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01049         //=================================================
01050     }
01051     //---------------------------------------------------------------
01052     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01053     //-----------------------------------------------------
01054     glPopAttrib();
01055     //---------------------------------------------------------------
01056     //===============================================================
01057 
01058     //---------------------------------------------------------------
01059     // Find direction to the eye view
01060     glPushAttrib( GL_ALL_ATTRIB_BITS );
01061     glEnable( GL_TEXTURE_2D );
01062     m_glslProgramObjectFor3DTextureDrawDirection->BeginGLSL();
01063     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_glslFBO_v3DObjAlignedSliceDrawDirection );
01064     TAPs::OpenGL::Fn::CHECK_FRAMEBUFFER_STATUS();
01065     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01066     {
01067         //-------------------------------------------------
01068         glMatrixMode( GL_PROJECTION );
01069         glPushMatrix();
01070         glLoadIdentity();
01071         glOrtho( -1000000, 1000000, -1000000, 1000000, -1000000, 1000000 );
01072         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01073         //
01074         // Load Model View Matrix
01075         glMatrixMode( GL_MODELVIEW );
01076         glPushMatrix();
01077         //glGetFloatv( GL_MODELVIEW_MATRIX, modelViewMatrix );
01078         //glLoadmatrixf( modelViewMatrix );
01079         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01080         //
01081         // Only one pixel is needed to get the inverse transpose of modelview matrix
01082         glViewport( 0, 0, 1, 1 );
01083         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01084         {
01085             //glEnable( GL_TEXTURE_2D );
01086             glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_glslFBO_v3DObjAlignedSliceDrawDirection );
01087             glFramebufferTexture2DEXT(
01088                 GL_FRAMEBUFFER_EXT,                     // target
01089                 GL_COLOR_ATTACHMENT0_EXT,               // attached frame
01090                 GL_TEXTURE_2D,                          // texture target
01091                 m_glui2DTextureFor3DObjAlignedSliceDrawDirection,   // texture id
01092                 0                                       // level
01093             );
01094             glDrawBuffer( GL_COLOR_ATTACHMENT0_EXT );
01095             TAPs::OpenGL::Fn::CHECK_FRAMEBUFFER_STATUS();
01096             TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01097             glClearColor( 0, 0, 0, 0 );
01098             glDisable( GL_DEPTH_TEST );
01099             glClear( GL_COLOR_BUFFER_BIT );
01100             glPolygonMode( GL_FRONT, GL_FILL );
01101             glBegin( GL_POINTS );
01102                 glVertex3f( 0, 0, 0 );
01103             glEnd();
01104         }
01105         glPopMatrix();
01106         glMatrixMode( GL_PROJECTION );
01107         glPopMatrix();
01108         glMatrixMode( GL_MODELVIEW );
01109     }
01110     glDisable( GL_TEXTURE_2D );
01111     glBindTexture( GL_TEXTURE_2D, 0 );
01112     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
01113     m_glslProgramObjectFor3DTextureDrawDirection->EndGLSL();
01114     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01115     glPopAttrib();
01116     //
01117     //===========================================
01118     // Read drawing direction from the texture
01119     const int width = 1, height = 1;
01120     GLfloat drawDir[width*height*4];
01121     glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
01122     glBindTexture( GL_TEXTURE_2D, m_glui2DTextureFor3DObjAlignedSliceDrawDirection );
01123     glGetTexImage( 
01124         GL_TEXTURE_2D,
01125         0,
01126         GL_RGBA,
01127         GL_FLOAT,
01128         drawDir
01129     );
01130     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01131     //-------------------------------------------
01132     // Find the draw direction
01133     //   The draw direction is the data with maximum absolute value.
01134     GLfloat absDrawDir[3] = { fabs( drawDir[0] ), fabs( drawDir[1] ), fabs( drawDir[2] ) };
01135     int iDrawDir = 0;       // X: -1,1; Y: -2,2; Z: -3,3
01136     if ( absDrawDir[0] > absDrawDir[1] ) {
01137         if ( absDrawDir[0] > absDrawDir[2] ) {
01138             if ( drawDir[0] > 0.0f )    iDrawDir =  1;
01139             else                        iDrawDir = -1;
01140         }
01141         else {
01142             if ( drawDir[2] > 0.0f )    iDrawDir =  3;
01143             else                        iDrawDir = -3;
01144         }
01145     }
01146     else {
01147         if ( absDrawDir[1] > absDrawDir[2] ) {
01148             if ( drawDir[1] > 0.0f )    iDrawDir =  2;
01149             else                        iDrawDir = -2;
01150         }
01151         else {
01152             if ( drawDir[2] > 0.0f )    iDrawDir =  3;
01153             else                        iDrawDir = -3;
01154         }
01155     }
01156     //=================================================
01157     //-------------------------------------------------
01158     // Draw The 3D Texture -- DrawByGL_Using3DTexture
01159     if ( m_3DTextureVisualization ) {
01160         glPushAttrib( GL_ALL_ATTRIB_BITS );
01161         //glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT );
01162         //glDisable( GL_LIGHTING );
01163         glEnable( GL_LIGHTING );
01164         //glEnable( GL_COLOR_MATERIAL );
01165         //glColor3f( 0.75, 0.75, 0.75 );
01166         //---------------------------------------
01167         // Set Color Material
01168         GLfloat vAmbient[4]   = { 0.2, 0.2, 0.2, 1.0 };
01169         GLfloat vDiffuse[4]   = { 0.8, 0.8, 0.8, 1.0 };
01170         GLfloat vSpecular[4]  = { 0.2, 0.2, 0.2, 1.0 };
01171         GLfloat vEmission[4]  = { 0.0, 0.0, 0.0, 1.0 };
01172         GLfloat vShininess[1] = { 128 };
01173         glMaterialfv( GL_FRONT, GL_AMBIENT,   vAmbient );
01174         glMaterialfv( GL_FRONT, GL_DIFFUSE,   vDiffuse );
01175         glMaterialfv( GL_FRONT, GL_SPECULAR,  vSpecular );
01176         glMaterialfv( GL_FRONT, GL_EMISSION,  vEmission );
01177         glMaterialfv( GL_FRONT, GL_SHININESS, vShininess );
01178         //
01179         glEnable( GL_BLEND );
01180         //glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
01181         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
01182         glActiveTexture( GL_TEXTURE0 );
01183         glEnable( GL_TEXTURE_3D );
01184         //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
01185         //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
01186         //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );
01187         //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_SUBTRACT );
01188         //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
01189         m_3DTextureVisualization->BindTexture(0);
01190         m_3DTextureVisualization->UseParameters();
01191         Vector3<T> vAABBMinPt = m_SimDomain->GetBoundingAABBLowPoint();
01192         Vector3<T> vAABBMaxPt = m_SimDomain->GetBoundingAABBHighPoint();
01193         //-----------------------------------------------------------
01194         Vector3<int> gridResolution = m_SimDomain->GetGridResolution();
01195         Vector3<T>   gridDimension  = m_SimDomain->GetGridDimension();
01196         //-----------------------------------------------------------
01197         int iAxis;
01198         glBegin( GL_QUADS );
01199         //-----------------------------------------------------------
01200         // DrawSliceStack_NegativeZ();  // back-to-front is from -z to +z
01201         if ( iDrawDir == -3 ) {
01202             iAxis = 2;
01203             //
01204             T texCoordStep[3] = { 
01205                 1.0 / (gridResolution[0]-1), 
01206                 1.0 / (gridResolution[1]-1), 
01207                 1.0 / (gridResolution[2]-1) 
01208             };
01209             T texCoordStart[3] = { 0, 0, 0 };
01210             T normalizeScale[3] = {
01211                 vAABBMaxPt[0] - vAABBMinPt[0], 
01212                 vAABBMaxPt[1] - vAABBMinPt[1], 
01213                 vAABBMaxPt[2] - vAABBMinPt[2] 
01214             };
01215             T posStep[3] = { 
01216                 normalizeScale[0] / (gridResolution[0]-1), 
01217                 normalizeScale[1] / (gridResolution[1]-1), 
01218                 normalizeScale[2] / (gridResolution[2]-1) 
01219             };
01220             T posStart[3] = { 
01221                 vAABBMinPt[0], 
01222                 vAABBMinPt[1], 
01223                 vAABBMinPt[2] 
01224             };
01225             T zValue = vAABBMinPt[iAxis];
01226             int numberOfSlices = gridResolution[iAxis] * iSubdivision;
01227             if ( numberOfSlices > iMaxNumberOfSlices ) numberOfSlices = iMaxNumberOfSlices;
01228     
01229             // DEBUG for making one-to-one mapping on z-direction
01230             numberOfSlices = gridResolution[iAxis];
01231 
01232             //T zStep  = (vAABBMaxPt[iAxis] - vAABBMinPt[iAxis] ) / numberOfSlices;
01233             //T tValue = 0;
01234             //T tStep  = 1.0 / numberOfSlices;
01235 
01236             //
01237             T texCoordBegin[3];
01238             T texCoordEnd[3];
01239             T posBegin[3];
01240             T posEnd[3];
01241             //
01242             // Offsets for inverse mapping
01243             //   3---2
01244             //   |   |  and [][3] for x,y,z
01245             //   0---1
01246             int index[4];   // for dataPosition
01247             T offsetInvMapping[4][3];
01248             T texCoordAfterInvMapping[4][3];
01249             //
01250             texCoordBegin[2] = texCoordStart[2];
01251             texCoordEnd[2] = texCoordStart[2] + texCoordStep[2];
01252             posBegin[2] = posStart[2];
01253             posEnd[2] = posStart[2] + posStep[2];
01254             //
01255             //static int countForCalNormals = 0;
01256             double randNormal[4][3];
01257             for ( int i = 0; i < numberOfSlices; ++i ) {
01258                 //
01259                 //index = (m_vGridResolution[1] * m_vGridResolution[0] * 4) * i;
01260                 //
01261                 texCoordBegin[1] = texCoordStart[1];
01262                 texCoordEnd[1] = texCoordStart[1] + texCoordStep[1];
01263                 posBegin[1] = posStart[1];
01264                 posEnd[1] = posStart[1] + posStep[1];
01265                 //
01266                 for ( int y = 0; y < gridResolution[1]-1; ++y ) {
01267                     //
01268                     texCoordBegin[0] = texCoordStart[0];
01269                     texCoordEnd[0] = texCoordStart[0] + texCoordStep[0];
01270                     posBegin[0] = posStart[0];
01271                     posEnd[0] = posStart[0] + posStep[0];
01272                     //
01273                     for ( int x = 0; x < gridResolution[0]-1; ++x ) {
01274                         //
01275                         {
01276                             //
01277                             int nextX = 4;
01278                             int idxX  = nextX * x;
01279                             int nextY = gridResolution[0] * nextX;
01280                             int idxY  = nextY * y;
01281                             int nextZ = gridResolution[1] * nextY;
01282                             int idxZ  = nextZ * i;
01283                             index[0] = idxZ + idxY + idxX;
01284                             index[1] = index[0] + nextX;
01285                             index[2] = index[1] + nextY;
01286                             index[3] = index[0] + nextY;
01287 
01288                             for ( int s = 0; s < 4; ++s ) {
01289                                 randNormal[s][0] = 0;
01290                                 randNormal[s][1] = 0;
01291                                 randNormal[s][2] = 1;
01292                             }
01293                             // Calculate Gradients as Normals
01294                             // DEBUG
01295                             //double randNormal[4][3];
01296                             //if ( ++countForCalNormals < m_vGridResolution[0]*m_vGridResolution[1]*m_vGridResolution[2]*100 ) {
01297                                 if ( ( 1 < i && i < numberOfSlices-1 ) &&
01298                                     ( 1 < y && y < gridResolution[1]-2 ) &&
01299                                     ( 1 < x && x < gridResolution[0]-2 ) )
01300                                 {
01301                                     for ( int s = 0; s < 4; ++s ) {
01302                                         randNormal[s][0] = ( dataForPosition[index[s]+nextX] - dataForPosition[index[s]-nextX] ) / gridDimension[0];
01303                                         randNormal[s][1] = ( dataForPosition[index[s]+nextY] - dataForPosition[index[s]-nextY] ) / gridDimension[1];
01304                                         randNormal[s][2] = ( dataForPosition[index[s]+nextZ] - dataForPosition[index[s]-nextZ] ) / gridDimension[2];
01305                                         double length = sqrt( 
01306                                             randNormal[s][0] * randNormal[s][0] +
01307                                             randNormal[s][1] * randNormal[s][1] +
01308                                             randNormal[s][2] * randNormal[s][2] 
01309                                         );
01310                                         length *= -1;
01311                                         if ( length > 0.00001 ) {
01312                                             randNormal[s][0] /= length;
01313                                             randNormal[s][1] /= length;
01314                                             randNormal[s][2] /= length;
01315                                         }
01316                                     }
01317                                 }
01318                             //}
01319                             //else if ( countForCalNormals > m_vGridResolution[0]*m_vGridResolution[1]*m_vGridResolution[2]*200 ) {
01320                             //  countForCalNormals = 0;
01321                             //}
01322 
01323                             //
01324                             // begin, begin, begin
01325                             offsetInvMapping[0][0] = posBegin[0] - dataForPosition[index[0]  ];
01326                             offsetInvMapping[0][1] = posBegin[1] - dataForPosition[index[0]+1];
01327                             offsetInvMapping[0][2] = posBegin[2] - dataForPosition[index[0]+2];
01328                             // end, begin, begin
01329                             offsetInvMapping[1][0] = posEnd[0]   - dataForPosition[index[1]  ];
01330                             offsetInvMapping[1][1] = posBegin[1] - dataForPosition[index[1]+1];
01331                             offsetInvMapping[1][2] = posBegin[2] - dataForPosition[index[1]+2];
01332                             // end, end, begin
01333                             offsetInvMapping[2][0] = posEnd[0]   - dataForPosition[index[2]  ];
01334                             offsetInvMapping[2][1] = posEnd[1]   - dataForPosition[index[2]+1];
01335                             offsetInvMapping[2][2] = posBegin[2] - dataForPosition[index[2]+2];
01336                             // begin, end, begin
01337                             offsetInvMapping[3][0] = posBegin[0] - dataForPosition[index[3]  ];
01338                             offsetInvMapping[3][1] = posEnd[1]   - dataForPosition[index[3]+1];
01339                             offsetInvMapping[3][2] = posBegin[2] - dataForPosition[index[3]+2];
01340                             //
01341                             for ( int dim = 0; dim < 4; ++dim ) {
01342                                 offsetInvMapping[dim][0] /= normalizeScale[0];
01343                                 offsetInvMapping[dim][1] /= normalizeScale[1];
01344                                 offsetInvMapping[dim][2] /= normalizeScale[2];
01345                             }
01346                             //
01347                             // begin, begin, begin
01348                             texCoordAfterInvMapping[0][0] = texCoordBegin[0] + offsetInvMapping[0][0];
01349                             texCoordAfterInvMapping[0][1] = texCoordBegin[1] + offsetInvMapping[0][1];
01350                             texCoordAfterInvMapping[0][2] = texCoordBegin[2] + offsetInvMapping[0][2];
01351                             // end, begin, begin
01352                             texCoordAfterInvMapping[1][0] = texCoordEnd[0]   + offsetInvMapping[1][0];
01353                             texCoordAfterInvMapping[1][1] = texCoordBegin[1] + offsetInvMapping[1][1];
01354                             texCoordAfterInvMapping[1][2] = texCoordBegin[2] + offsetInvMapping[1][2];
01355                             // end, end, begin
01356                             texCoordAfterInvMapping[2][0] = texCoordEnd[0]   + offsetInvMapping[2][0];
01357                             texCoordAfterInvMapping[2][1] = texCoordEnd[1]   + offsetInvMapping[2][1];
01358                             texCoordAfterInvMapping[2][2] = texCoordBegin[2] + offsetInvMapping[2][2];
01359                             // begin, end, begin
01360                             texCoordAfterInvMapping[3][0] = texCoordBegin[0] + offsetInvMapping[3][0];
01361                             texCoordAfterInvMapping[3][1] = texCoordEnd[1]   + offsetInvMapping[3][1];
01362                             texCoordAfterInvMapping[3][2] = texCoordBegin[2] + offsetInvMapping[3][2];
01363                             //
01364                         }
01365                         //
01366                         //glTexCoord3f( texCoordBegin[0], texCoordBegin[1], texCoordBegin[2] );
01367                         glTexCoord3f( texCoordAfterInvMapping[0][0], texCoordAfterInvMapping[0][1], texCoordAfterInvMapping[0][2] );
01368                         //glNormal3f( 0, 0, 1 );
01369                         glNormal3f( randNormal[0][0], randNormal[0][1], randNormal[0][2] );
01370                         glVertex3f( posBegin[0], posBegin[1], posBegin[2] );
01371                         //
01372                         //glTexCoord3f( texCoordEnd[0], texCoordBegin[1], texCoordBegin[2] );
01373                         glTexCoord3f( texCoordAfterInvMapping[1][0], texCoordAfterInvMapping[1][1], texCoordAfterInvMapping[1][2] );
01374                         //glNormal3f( 0, 0, 1 );
01375                         glNormal3f( randNormal[1][0], randNormal[1][1], randNormal[1][2] );
01376                         glVertex3f( posEnd[0], posBegin[1], posBegin[2] );
01377                         //
01378                         //glTexCoord3f( texCoordEnd[0], texCoordEnd[1], texCoordBegin[2] );
01379                         glTexCoord3f( texCoordAfterInvMapping[2][0], texCoordAfterInvMapping[2][1], texCoordAfterInvMapping[2][2] );
01380                         //glNormal3f( 0, 0, 1 );
01381                         glNormal3f( randNormal[2][0], randNormal[2][1], randNormal[2][2] );
01382                         glVertex3f( posEnd[0], posEnd[1], posBegin[2] );
01383                         //
01384                         //glTexCoord3f( texCoordBegin[0], texCoordEnd[1], texCoordBegin[2] );
01385                         glTexCoord3f( texCoordAfterInvMapping[3][0], texCoordAfterInvMapping[3][1], texCoordAfterInvMapping[3][2] );
01386                         //glNormal3f( 0, 0, 1 );
01387                         glNormal3f( randNormal[3][0], randNormal[3][1], randNormal[3][2] );
01388                         glVertex3f( posBegin[0], posEnd[1], posBegin[2] );
01389                         //
01390                         texCoordBegin[0] = texCoordEnd[0];
01391                         texCoordEnd[0] += texCoordStep[0];
01392                         posBegin[0] = posEnd[0];
01393                         posEnd[0] += posStep[0];
01394                     }
01395                     texCoordBegin[1] = texCoordEnd[1];
01396                     texCoordEnd[1] += texCoordStep[1];
01397                     posBegin[1] = posEnd[1];
01398                     posEnd[1] += posStep[1];
01399                 }
01400                 texCoordBegin[2] = texCoordEnd[2];
01401                 texCoordEnd[2] += texCoordStep[2];
01402                 posBegin[2] = posEnd[2];
01403                 posEnd[2] += posStep[2];
01404             }
01405         }
01406         //-----------------------------------------------------------
01407         // DrawSliceStack_NegativeX();  // back-to-front is from -x to +x
01408         else if ( iDrawDir == -1 ) {
01409             iAxis = 0;
01410             T zValue = vAABBMinPt[iAxis];
01411             int numberOfSlices = gridResolution[iAxis] * iSubdivision;
01412             if ( numberOfSlices > iMaxNumberOfSlices ) numberOfSlices = iMaxNumberOfSlices;
01413             T zStep  = (vAABBMaxPt[iAxis] - vAABBMinPt[iAxis] ) / (numberOfSlices-1);
01414             T tValue = 0;
01415             T tStep  = 1.0 / (numberOfSlices-1);
01416             for ( int i = 0; i < numberOfSlices; ++i )
01417             {
01418                 glTexCoord3f( tValue, 0, 1 );
01419                 glNormal3f( 1, 0, 0 );
01420                 glVertex3f( zValue, vAABBMinPt[1], vAABBMaxPt[2] );
01421                 //
01422                 glTexCoord3f( tValue, 0, 0 );
01423                 glNormal3f( 1, 0, 0 );
01424                 glVertex3f( zValue, vAABBMinPt[1], vAABBMinPt[2] );
01425                 //
01426                 glTexCoord3f( tValue, 1, 0 );
01427                 glNormal3f( 1, 0, 0 );
01428                 glVertex3f( zValue, vAABBMaxPt[1], vAABBMinPt[2] );
01429                 //
01430                 glTexCoord3f( tValue, 1, 1 );
01431                 glNormal3f( 1, 0, 0 );
01432                 glVertex3f( zValue, vAABBMaxPt[1], vAABBMaxPt[2] );
01433                 //
01434                 zValue += zStep;
01435                 tValue += tStep;
01436             }
01437         }
01438         //-----------------------------------------------------------
01439         // DrawSliceStack_NegativeY();  // back-to-front is from -y to +y
01440         else if ( iDrawDir == -2 ) {
01441             iAxis = 1;
01442             T zValue = vAABBMinPt[iAxis];
01443             int numberOfSlices = gridResolution[iAxis] * iSubdivision;
01444             if ( numberOfSlices > iMaxNumberOfSlices ) numberOfSlices = iMaxNumberOfSlices;
01445             T zStep  = (vAABBMaxPt[iAxis] - vAABBMinPt[iAxis] ) / (numberOfSlices-1);
01446             T tValue = 0;
01447             T tStep  = 1.0 / (numberOfSlices-1);
01448             for ( int i = 0; i < numberOfSlices; ++i )
01449             {
01450                 glTexCoord3f( 0, tValue, 1 );
01451                 glNormal3f( 0, 1, 0 );
01452                 glVertex3f( vAABBMinPt[0], zValue, vAABBMaxPt[2] );
01453                 //
01454                 glTexCoord3f( 1, tValue, 1 );
01455                 glNormal3f( 0, 1, 0 );
01456                 glVertex3f( vAABBMaxPt[0], zValue, vAABBMaxPt[2] );
01457                 //
01458                 glTexCoord3f( 1, tValue, 0 );
01459                 glNormal3f( 0, 1, 0 );
01460                 glVertex3f( vAABBMaxPt[0], zValue, vAABBMinPt[2] );
01461                 //
01462                 glTexCoord3f( 0, tValue, 0 );
01463                 glNormal3f( 0, 1, 0 );
01464                 glVertex3f( vAABBMinPt[0], zValue, vAABBMinPt[2] );
01465                 //
01466                 zValue += zStep;
01467                 tValue += tStep;
01468             }
01469         }
01470         //-----------------------------------------------------------
01471         // DrawSliceStack_PositiveZ();  // back-to-front is from +z to -z
01472         else if ( iDrawDir == 3 ) {
01473             iAxis = 2;
01474             T zValue = vAABBMaxPt[iAxis];
01475             int numberOfSlices = gridResolution[iAxis] * iSubdivision;
01476             if ( numberOfSlices > iMaxNumberOfSlices ) numberOfSlices = iMaxNumberOfSlices;
01477             T zStep  = (vAABBMinPt[iAxis] - vAABBMaxPt[iAxis] ) / (numberOfSlices-1);
01478             T tValue = 1.0;
01479             T tStep  = -1.0 / (numberOfSlices-1);
01480             for ( int i = 0; i < numberOfSlices; ++i )
01481             {
01482                 glTexCoord3f( 0, 0, tValue );
01483                 glNormal3f( 0, 0, -1 );
01484                 glVertex3f( vAABBMinPt[0], vAABBMinPt[1], zValue );
01485                 //
01486                 glTexCoord3f( 0, 1, tValue );
01487                 glNormal3f( 0, 0, -1 );
01488                 glVertex3f( vAABBMinPt[0], vAABBMaxPt[1], zValue );
01489                 //
01490                 glTexCoord3f( 1, 1, tValue );
01491                 glNormal3f( 0, 0, -1 );
01492                 glVertex3f( vAABBMaxPt[0], vAABBMaxPt[1], zValue );
01493                 //
01494                 glTexCoord3f( 1, 0, tValue );
01495                 glNormal3f( 0, 0, -1 );
01496                 glVertex3f( vAABBMaxPt[0], vAABBMinPt[1], zValue );
01497                 //
01498                 zValue += zStep;
01499                 tValue += tStep;
01500             }
01501         }
01502         //-----------------------------------------------------------
01503         // DrawSliceStack_PositiveX();  // back-to-front is from +x to -x
01504         else if ( iDrawDir == 1 ) {
01505             iAxis = 0;
01506             T zValue = vAABBMaxPt[iAxis];
01507             int numberOfSlices = gridResolution[iAxis] * iSubdivision;
01508             if ( numberOfSlices > iMaxNumberOfSlices ) numberOfSlices = iMaxNumberOfSlices;
01509             T zStep  = (vAABBMinPt[iAxis] - vAABBMaxPt[iAxis] ) / (numberOfSlices-1);
01510             T tValue = 1.0;
01511             T tStep  = -1.0 / (numberOfSlices-1);
01512             for ( int i = 0; i < numberOfSlices; ++i )
01513             {
01514                 glTexCoord3f( tValue, 0, 0 );
01515                 glNormal3f( -1, 0, 0 );
01516                 glVertex3f( zValue, vAABBMinPt[1], vAABBMinPt[2] );
01517                 //
01518                 glTexCoord3f( tValue, 0, 1 );
01519                 glNormal3f( -1, 0, 0 );
01520                 glVertex3f( zValue, vAABBMinPt[1], vAABBMaxPt[2] );
01521                 //
01522                 glTexCoord3f( tValue, 1, 1 );
01523                 glNormal3f( -1, 0, 0 );
01524                 glVertex3f( zValue, vAABBMaxPt[1], vAABBMaxPt[2] );
01525                 //
01526                 glTexCoord3f( tValue, 1, 0 );
01527                 glNormal3f( -1, 0, 0 );
01528                 glVertex3f( zValue, vAABBMaxPt[1], vAABBMinPt[2] );
01529                 //
01530                 zValue += zStep;
01531                 tValue += tStep;
01532             }
01533         }
01534         //-----------------------------------------------------------
01535         // DrawSliceStack_PositiveY();  // back-to-front is from +y to -y
01536         else if ( iDrawDir == 2 ) {
01537             iAxis = 1;
01538             T zValue = vAABBMaxPt[iAxis];
01539             int numberOfSlices = gridResolution[iAxis] * iSubdivision;
01540             if ( numberOfSlices > iMaxNumberOfSlices ) numberOfSlices = iMaxNumberOfSlices;
01541             T zStep  = (vAABBMinPt[iAxis] - vAABBMaxPt[iAxis] ) / (numberOfSlices-1);
01542             T tValue = 1.0;
01543             T tStep  = -1.0 / (numberOfSlices-1);
01544             for ( int i = 0; i < numberOfSlices; ++i )
01545             {
01546                 glTexCoord3f( 0, tValue, 1 );
01547                 glNormal3f( 0, -1, 0 );
01548                 glVertex3f( vAABBMinPt[0], zValue, vAABBMaxPt[2] );
01549                 //
01550                 glTexCoord3f( 0, tValue, 0 );
01551                 glNormal3f( 0, -1, 0 );
01552                 glVertex3f( vAABBMinPt[0], zValue, vAABBMinPt[2] );
01553                 //
01554                 glTexCoord3f( 1, tValue, 0 );
01555                 glNormal3f( 0, -1, 0 );
01556                 glVertex3f( vAABBMaxPt[0], zValue, vAABBMinPt[2] );
01557                 //
01558                 glTexCoord3f( 1, tValue, 1 );
01559                 glNormal3f( 0, -1, 0 );
01560                 glVertex3f( vAABBMaxPt[0], zValue, vAABBMaxPt[2] );
01561                 //
01562                 zValue += zStep;
01563                 tValue += tStep;
01564             }
01565         }
01566         //-----------------------------------------------------------
01567         glEnd();
01568         glBindTexture( GL_TEXTURE_3D, 0 );
01569         glPopAttrib();
01570     }
01571     //---------------------------------------------------------------
01572     //===============================================================
01573     // Delete temp data read from GPU
01574     delete [] dataForPosition;
01575     //delete [] dataForConnectivity;
01576 }
01577 //-----------------------------------------------------------------------------
01578 // END: DrawByGL (Using 3D Texture)
01579 //=============================================================================
01580 
01581 //*****************************************************************************
01582 // BEGIN: DrawByGLSL (Using 3D Texture)
01583 //-----------------------------------------------------------------------------
01584 template <typename T>
01585 void ModelDeformableGLSL_Visualization_3DTexture<T>::
01586 DrawByGLSL ( int iSubdivision )
01587 {
01588     static int iMaxNumberOfSlices = 1024;
01589     //---------------------------------------------------------------
01590 #ifdef TAPs_DEBUG_MODE
01591     if ( !m_SimDomain ) return;
01592     //---------------------------------------------------------------
01593     if ( !m_SimDomain->RetObject3DTexturePosition() )   return;
01594     //---------------------------------------------------------------
01595     if ( !m_glslProgramObjectFor3DTextureDrawDirection )    return;
01596     //---------------------------------------------------------------
01597     if ( !m_glslProgramObjectFor3DTextureDrawing )  return;
01598     //---------------------------------------------------------------
01599     if ( !m_3DTextureVisualization )    return;
01600 #endif
01601     //---------------------------------------------------------------
01602     //if ( !Get3DTextureVisualization() )   return;
01603     //---------------------------------------------------------------
01604     //===============================================================
01605     // Read data from GPU
01606     //---------------------------------------------------------------
01607     Texture * texture3DPosition = m_SimDomain->RetObject3DTexturePosition();
01608     //---------------------------------------------------------------
01609     glPushAttrib( GL_ALL_ATTRIB_BITS );
01610     //-----------------------------------------------------
01611     GLsizei sizeForPosition = 
01612             texture3DPosition->GetWidth() *
01613             texture3DPosition->GetHeight() *
01614             texture3DPosition->GetDepth() *
01615             texture3DPosition->GetInternalFormatNumberOfComponents();
01616     GLfloat * dataForPosition = new GLfloat[ sizeForPosition ];
01617     //
01618     //GLsizei sizeForConnectivity = 
01619     //      m_3DTextureConnectivityParameters.width * 
01620     //      m_3DTextureConnectivityParameters.height * 
01621     //      m_3DTextureConnectivityParameters.depth * 
01622     //      m_3DTextureConnectivityParameters.internalFormatNumOfComponents;
01623     //GLfloat * dataForConnectivity = new GLfloat[ sizeForConnectivity ];
01624     //
01625     {
01626         int i = 0;
01627         //=================================================
01628         // Read data for positions from a texture to memory
01629         glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
01630         texture3DPosition->BindTexture(0);
01631         glGetTexImage( 
01632             texture3DPosition->GetTarget(),
01633             texture3DPosition->GetLevel(),
01634             texture3DPosition->GetPixelFormat(),
01635             texture3DPosition->GetDataType(),
01636             dataForPosition
01637         );
01638         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01639         //=================================================
01640         //=================================================
01641         // Read data for connectivities from a texture to memory
01642         //glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
01643         //glBindTexture( m_3DTextureConnectivityParameters.target, m_gluipNameTextureConnectivity[i] );
01644         //glGetTexImage( 
01645         //  m_3DTextureConnectivityParameters.target,
01646         //  0,
01647         //  m_3DTextureConnectivityParameters.pixelFormat,
01648         //  m_3DTextureConnectivityParameters.dataType,
01649         //  dataForConnectivity
01650         //);
01651         //TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01652         //=================================================
01653     }
01654     //---------------------------------------------------------------
01655     glPopAttrib();
01656     //---------------------------------------------------------------
01657     //===============================================================
01658 
01659     //===============================================================
01660     // Find direction to the eye view
01661     //---------------------------------------------------------------
01662     glPushAttrib( GL_ALL_ATTRIB_BITS );
01663     glEnable( GL_TEXTURE_2D );
01664     m_glslProgramObjectFor3DTextureDrawDirection->BeginGLSL();
01665     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_glslFBO_v3DObjAlignedSliceDrawDirection );
01666     TAPs::OpenGL::Fn::CHECK_FRAMEBUFFER_STATUS();
01667     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01668     {
01669         //-------------------------------------------------
01670         glMatrixMode( GL_PROJECTION );
01671         glPushMatrix();
01672         glLoadIdentity();
01673         glOrtho( -1000000, 1000000, -1000000, 1000000, -1000000, 1000000 );
01674         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01675         //
01676         // Load Model View Matrix
01677         glMatrixMode( GL_MODELVIEW );
01678         glPushMatrix();
01679         //glGetFloatv( GL_MODELVIEW_MATRIX, modelViewMatrix );
01680         //glLoadmatrixf( modelViewMatrix );
01681         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01682         //
01683         // Only one pixel is needed to get the inverse transpose of modelview matrix
01684         glViewport( 0, 0, 1, 1 );
01685         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01686         {
01687             //glEnable( GL_TEXTURE_2D );
01688             glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_glslFBO_v3DObjAlignedSliceDrawDirection );
01689             glFramebufferTexture2DEXT(
01690                 GL_FRAMEBUFFER_EXT,                     // target
01691                 GL_COLOR_ATTACHMENT0_EXT,               // attached frame
01692                 GL_TEXTURE_2D,                          // texture target
01693                 m_glui2DTextureFor3DObjAlignedSliceDrawDirection,   // texture id
01694                 0                                       // level
01695             );
01696             glDrawBuffer( GL_COLOR_ATTACHMENT0_EXT );
01697             TAPs::OpenGL::Fn::CHECK_FRAMEBUFFER_STATUS();
01698             TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01699             glClearColor( 0, 0, 0, 0 );
01700             glDisable( GL_DEPTH_TEST );
01701             glClear( GL_COLOR_BUFFER_BIT );
01702             glPolygonMode( GL_FRONT, GL_FILL );
01703             glBegin( GL_POINTS );
01704                 glVertex3f( 0, 0, 0 );
01705             glEnd();
01706         }
01707         glPopMatrix();
01708         glMatrixMode( GL_PROJECTION );
01709         glPopMatrix();
01710         glMatrixMode( GL_MODELVIEW );
01711     }
01712     glDisable( GL_TEXTURE_2D );
01713     glBindTexture( GL_TEXTURE_2D, 0 );
01714     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
01715     m_glslProgramObjectFor3DTextureDrawDirection->EndGLSL();
01716     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01717     glPopAttrib();
01718     //
01719     //===========================================
01720     // Read drawing direction from the texture
01721     const int width = 1, height = 1;
01722     GLfloat drawDir[width*height*4];
01723     glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
01724     glBindTexture( GL_TEXTURE_2D, m_glui2DTextureFor3DObjAlignedSliceDrawDirection );
01725     glGetTexImage( 
01726         GL_TEXTURE_2D,
01727         0,
01728         GL_RGBA,
01729         GL_FLOAT,
01730         drawDir
01731     );
01732     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01733     //-------------------------------------------
01734     // Find the draw direction
01735     //   The draw direction is the data with maximum absolute value.
01736     GLfloat absDrawDir[3] = { fabs( drawDir[0] ), fabs( drawDir[1] ), fabs( drawDir[2] ) };
01737     int iDrawDir = 0;       // X: -1,1; Y: -2,2; Z: -3,3
01738     if ( absDrawDir[0] > absDrawDir[1] ) {
01739         if ( absDrawDir[0] > absDrawDir[2] ) {
01740             if ( drawDir[0] > 0.0f )    iDrawDir =  1;
01741             else                        iDrawDir = -1;
01742         }
01743         else {
01744             if ( drawDir[2] > 0.0f )    iDrawDir =  3;
01745             else                        iDrawDir = -3;
01746         }
01747     }
01748     else {
01749         if ( absDrawDir[1] > absDrawDir[2] ) {
01750             if ( drawDir[1] > 0.0f )    iDrawDir =  2;
01751             else                        iDrawDir = -2;
01752         }
01753         else {
01754             if ( drawDir[2] > 0.0f )    iDrawDir =  3;
01755             else                        iDrawDir = -3;
01756         }
01757     }
01758     //---------------------------------------------------------------
01759     //===============================================================
01760 
01761     //===============================================================
01762     //---------------------------------------------------------------
01763     //=====================================================
01764     // Draw The 3D Texture
01765     //-----------------------------------------------------
01766     //---------------------------------
01767     glPushAttrib( GL_ALL_ATTRIB_BITS );
01768     //-----------------------
01769     // Set Color Material
01770     //GLfloat vAmbient[4]   = { 0.2, 0.2, 0.2, 1.0 };
01771     //GLfloat vDiffuse[4]   = { 0.8, 0.8, 0.8, 1.0 };
01772     //GLfloat vSpecular[4]  = { 0.2, 0.2, 0.2, 1.0 };
01773     //GLfloat vEmission[4]  = { 0.0, 0.0, 0.0, 1.0 };
01774     //GLfloat vShininess[1] = { 128 };
01775     //glMaterialfv( GL_FRONT, GL_AMBIENT,   vAmbient );
01776     //glMaterialfv( GL_FRONT, GL_DIFFUSE,   vDiffuse );
01777     //glMaterialfv( GL_FRONT, GL_SPECULAR,  vSpecular );
01778     //glMaterialfv( GL_FRONT, GL_EMISSION,  vEmission );
01779     //glMaterialfv( GL_FRONT, GL_SHININESS, vShininess );
01780     //-----------------------
01781     glEnable( GL_LIGHTING );
01782     glEnable( GL_BLEND );
01783     glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
01784     //---------------------------------
01785     Vector3<T> vAABBMinPt = m_SimDomain->GetBoundingAABBLowPoint();
01786     Vector3<T> vAABBMaxPt = m_SimDomain->GetBoundingAABBHighPoint();
01787     //---------------------------------
01788     Vector3<int> gridResolution = m_SimDomain->GetGridResolution();
01789     Vector3<T>   gridDimension  = m_SimDomain->GetGridDimension();
01790     //=====================================================
01791     m_glslProgramObjectFor3DTextureDrawing->BeginGLSL();
01792     //-----------------------------------------------------
01793     m_glslProgramObjectFor3DTextureDrawing->SetUniform1f( "threshold", 0.5f );
01794     //---------------------------------
01795     //m_glslProgramObject->SetUniform3f( "LightPosition", 0.0f, 2.0f, 4.0f );
01796     m_glslProgramObjectFor3DTextureDrawing->SetUniform3f( 
01797         "VoxelSize", gridDimension[0], gridDimension[1], gridDimension[2] );
01798     //---------------------------------
01799     // Set 3D texture to Texture Unit 0
01800     m_3DTextureVisualization->EnableTextureTarget();
01801     glActiveTexture( GL_TEXTURE0 );
01802     m_3DTextureVisualization->BindTexture(0);
01803     m_glslProgramObjectFor3DTextureDrawing->SetUniform1i( "ModelVolumeTexture", 0 );
01804     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01805     //---------------------------------
01806 
01807     //---------------------------------
01808     // Set 1D texture to Texture Unit 1
01809     m_1DTextureTransferFnLUT->EnableTextureTarget();
01810     glActiveTexture( GL_TEXTURE1 );
01811     m_1DTextureTransferFnLUT->BindTexture(0);
01812     m_glslProgramObjectFor3DTextureDrawing->SetUniform1i( "ModelTransferFnLUT", 1 );
01813     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01814     //---------------------------------
01815 
01816     // DEBUG
01817     #ifdef  TAPs_DEBUG_MODE
01818     //=================================================
01819     //GLubyte data[256][4];
01820     {
01821         static boolean firstRun = true;
01822         if ( firstRun ) {
01823             GLfloat data[256][4];
01824             //---------------------------------
01825             glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
01826             m_1DTextureTransferFnLUT->BindTexture(0);
01827 //          glBindTexture( m_1DTextureTransferFnLUTParameters.target, m_glui1DTextureTransferFnLUT );
01828             glGetTexImage( 
01829 //              m_1DTextureTransferFnLUTParameters.target,
01830 //              0,
01831 //              m_1DTextureTransferFnLUTParameters.pixelFormat,
01832 //              m_1DTextureTransferFnLUTParameters.dataType,
01833                 m_1DTextureTransferFnLUT->GetTarget(),
01834                 m_1DTextureTransferFnLUT->GetLevel(),
01835                 m_1DTextureTransferFnLUT->GetPixelFormat(),
01836                 m_1DTextureTransferFnLUT->GetDataType(),
01837                 data
01838             );
01839             for ( int i = 0; i < 256; ++i ) {
01840 //              if      ( m_1DTextureTransferFnLUTParameters.dataType == GL_UNSIGNED_BYTE ) {
01841                 if      ( m_1DTextureTransferFnLUT->GetDataType() == GL_UNSIGNED_BYTE ) {
01842                     std::cout << "data: " << (int)data[i][0] << " " 
01843                                         << (int)data[i][1] << " "  
01844                                         << (int)data[i][2] << " " 
01845                                         << (int)data[i][3] << "\n";
01846                 }
01847 //              else if ( m_1DTextureTransferFnLUTParameters.dataType == GL_FLOAT ) {
01848                 else if ( m_1DTextureTransferFnLUT->GetDataType() == GL_FLOAT ) {
01849                     std::cout << "data: " << data[i][0] << " " 
01850                                         << data[i][1] << " "  
01851                                         << data[i][2] << " " 
01852                                         << data[i][3] << "\n";
01853                 }
01854             }
01855             TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01856             //-----------------------------------
01857             firstRun = false;
01858         }
01859     }
01860     //=================================================
01861     #endif//TAPs_DEBUG_MODE
01862 
01863 
01864 
01865     //---------------------------------
01866     glBegin( GL_QUADS );
01867     //---------------------------------
01868     int iAxis = 0;
01869     //-----------------------------------------------------
01870     //=====================================================
01871 
01872     //===============================================================
01873     // DrawSliceStack_NegativeZ();  // back-to-front is from -z to +z
01874     //---------------------------------------------------------------
01875     if ( true ) {
01876     //if ( iDrawDir == -3 ) {
01877         iAxis = 2;
01878         //---------------------------------
01879         T texCoordStep[3] = { 
01880             1.0 / (gridResolution[0]-1), 
01881             1.0 / (gridResolution[1]-1), 
01882             1.0 / (gridResolution[2]-1) 
01883         };
01884         T texCoordStart[3] = { 0, 0, 0 };
01885         T normalizeScale[3] = {
01886             vAABBMaxPt[0] - vAABBMinPt[0], 
01887             vAABBMaxPt[1] - vAABBMinPt[1], 
01888             vAABBMaxPt[2] - vAABBMinPt[2] 
01889         };
01890         T posStep[3] = { 
01891             normalizeScale[0] / (gridResolution[0]-1), 
01892             normalizeScale[1] / (gridResolution[1]-1), 
01893             normalizeScale[2] / (gridResolution[2]-1) 
01894         };
01895         T posStart[3] = { 
01896             vAABBMinPt[0], 
01897             vAABBMinPt[1], 
01898             vAABBMinPt[2] 
01899         };
01900         T zValue = vAABBMinPt[iAxis];
01901         int numberOfSlices = gridResolution[iAxis] * iSubdivision;
01902         if ( numberOfSlices > iMaxNumberOfSlices ) numberOfSlices = iMaxNumberOfSlices;
01903 
01904         // DEBUG for making one-to-one mapping on z-direction
01905         numberOfSlices = gridResolution[iAxis];
01906 
01907         //T zStep  = (vAABBMaxPt[iAxis] - vAABBMinPt[iAxis] ) / numberOfSlices;
01908         //T tValue = 0;
01909         //T tStep  = 1.0 / numberOfSlices;
01910 
01911         //
01912         T texCoordBegin[3];
01913         T texCoordEnd[3];
01914         T posBegin[3];
01915         T posEnd[3];
01916         //
01917         // Offsets for inverse mapping
01918         //   3---2
01919         //   |   |  and [][3] for x,y,z
01920         //   0---1
01921         int index[4];   // for dataPosition
01922         T offsetInvMapping[4][3];
01923         T texCoordAfterInvMapping[4][3];
01924         //
01925         texCoordBegin[2] = texCoordStart[2];
01926         texCoordEnd[2] = texCoordStart[2] + texCoordStep[2];
01927         posBegin[2] = posStart[2];
01928         posEnd[2] = posStart[2] + posStep[2];
01929         //
01930         for ( int i = 0; i < numberOfSlices; ++i ) {
01931         //for ( int i = 0+1; i < numberOfSlices-1; ++i ) {
01932             //
01933             //index = (m_vGridResolution[1] * m_vGridResolution[0] * 4) * i;
01934             //
01935             texCoordBegin[1] = texCoordStart[1];
01936             texCoordEnd[1] = texCoordStart[1] + texCoordStep[1];
01937             posBegin[1] = posStart[1];
01938             posEnd[1] = posStart[1] + posStep[1];
01939             //
01940             for ( int y = 0; y < gridResolution[1]-1; ++y ) {
01941                 //
01942                 texCoordBegin[0] = texCoordStart[0];
01943                 texCoordEnd[0] = texCoordStart[0] + texCoordStep[0];
01944                 posBegin[0] = posStart[0];
01945                 posEnd[0] = posStart[0] + posStep[0];
01946                 //
01947                 for ( int x = 0; x < gridResolution[0]-1; ++x ) {
01948                     //
01949                     {
01950                         //
01951                         int nextX = 4;
01952                         int idxX  = nextX * x;
01953                         int nextY = gridResolution[0] * nextX;
01954                         int idxY  = nextY * y;
01955                         int nextZ = gridResolution[1] * nextY;
01956                         int idxZ  = nextZ * i;
01957                         index[0] = idxZ + idxY + idxX;
01958                         index[1] = index[0] + nextX;
01959                         index[2] = index[1] + nextY;
01960                         index[3] = index[0] + nextY;
01961 
01962                         //
01963                         // begin, begin, begin
01964                         offsetInvMapping[0][0] = posBegin[0] - dataForPosition[index[0]  ];
01965                         offsetInvMapping[0][1] = posBegin[1] - dataForPosition[index[0]+1];
01966                         offsetInvMapping[0][2] = posBegin[2] - dataForPosition[index[0]+2];
01967                         // end, begin, begin
01968                         offsetInvMapping[1][0] = posEnd[0]   - dataForPosition[index[1]  ];
01969                         offsetInvMapping[1][1] = posBegin[1] - dataForPosition[index[1]+1];
01970                         offsetInvMapping[1][2] = posBegin[2] - dataForPosition[index[1]+2];
01971                         // end, end, begin
01972                         offsetInvMapping[2][0] = posEnd[0]   - dataForPosition[index[2]  ];
01973                         offsetInvMapping[2][1] = posEnd[1]   - dataForPosition[index[2]+1];
01974                         offsetInvMapping[2][2] = posBegin[2] - dataForPosition[index[2]+2];
01975                         // begin, end, begin
01976                         offsetInvMapping[3][0] = posBegin[0] - dataForPosition[index[3]  ];
01977                         offsetInvMapping[3][1] = posEnd[1]   - dataForPosition[index[3]+1];
01978                         offsetInvMapping[3][2] = posBegin[2] - dataForPosition[index[3]+2];
01979                         //
01980                         for ( int dim = 0; dim < 4; ++dim ) {
01981                             offsetInvMapping[dim][0] /= normalizeScale[0];
01982                             offsetInvMapping[dim][1] /= normalizeScale[1];
01983                             offsetInvMapping[dim][2] /= normalizeScale[2];
01984 
01985                             //offsetInvMapping[dim][0] = 0;
01986                             //offsetInvMapping[dim][1] = 0;
01987                             //offsetInvMapping[dim][2] = 0;
01988                         }
01989                         //
01990                         // begin, begin, begin
01991                         texCoordAfterInvMapping[0][0] = texCoordBegin[0] + offsetInvMapping[0][0];
01992                         texCoordAfterInvMapping[0][1] = texCoordBegin[1] + offsetInvMapping[0][1];
01993                         texCoordAfterInvMapping[0][2] = texCoordBegin[2] + offsetInvMapping[0][2];
01994                         // end, begin, begin
01995                         texCoordAfterInvMapping[1][0] = texCoordEnd[0]   + offsetInvMapping[1][0];
01996                         texCoordAfterInvMapping[1][1] = texCoordBegin[1] + offsetInvMapping[1][1];
01997                         texCoordAfterInvMapping[1][2] = texCoordBegin[2] + offsetInvMapping[1][2];
01998                         // end, end, begin
01999                         texCoordAfterInvMapping[2][0] = texCoordEnd[0]   + offsetInvMapping[2][0];
02000                         texCoordAfterInvMapping[2][1] = texCoordEnd[1]   + offsetInvMapping[2][1];
02001                         texCoordAfterInvMapping[2][2] = texCoordBegin[2] + offsetInvMapping[2][2];
02002                         // begin, end, begin
02003                         texCoordAfterInvMapping[3][0] = texCoordBegin[0] + offsetInvMapping[3][0];
02004                         texCoordAfterInvMapping[3][1] = texCoordEnd[1]   + offsetInvMapping[3][1];
02005                         texCoordAfterInvMapping[3][2] = texCoordBegin[2] + offsetInvMapping[3][2];
02006                         //
02007                     }
02008                     //
02009                     //glTexCoord3f( texCoordBegin[0], texCoordBegin[1], texCoordBegin[2] );
02010                     glTexCoord3f( texCoordAfterInvMapping[0][0], texCoordAfterInvMapping[0][1], texCoordAfterInvMapping[0][2] );
02011                     glVertex3f( posBegin[0], posBegin[1], posBegin[2] );
02012                     //
02013                     //glTexCoord3f( texCoordEnd[0], texCoordBegin[1], texCoordBegin[2] );
02014                     glTexCoord3f( texCoordAfterInvMapping[1][0], texCoordAfterInvMapping[1][1], texCoordAfterInvMapping[1][2] );
02015                     glVertex3f( posEnd[0], posBegin[1], posBegin[2] );
02016                     //
02017                     //glTexCoord3f( texCoordEnd[0], texCoordEnd[1], texCoordBegin[2] );
02018                     glTexCoord3f( texCoordAfterInvMapping[2][0], texCoordAfterInvMapping[2][1], texCoordAfterInvMapping[2][2] );
02019                     glVertex3f( posEnd[0], posEnd[1], posBegin[2] );
02020                     //
02021                     //glTexCoord3f( texCoordBegin[0], texCoordEnd[1], texCoordBegin[2] );
02022                     glTexCoord3f( texCoordAfterInvMapping[3][0], texCoordAfterInvMapping[3][1], texCoordAfterInvMapping[3][2] );
02023                     glVertex3f( posBegin[0], posEnd[1], posBegin[2] );
02024                     //
02025                     texCoordBegin[0] = texCoordEnd[0];
02026                     texCoordEnd[0] += texCoordStep[0];
02027                     posBegin[0] = posEnd[0];
02028                     posEnd[0] += posStep[0];
02029                 }
02030                 texCoordBegin[1] = texCoordEnd[1];
02031                 texCoordEnd[1] += texCoordStep[1];
02032                 posBegin[1] = posEnd[1];
02033                 posEnd[1] += posStep[1];
02034             }
02035             texCoordBegin[2] = texCoordEnd[2];
02036             texCoordEnd[2] += texCoordStep[2];
02037             posBegin[2] = posEnd[2];
02038             posEnd[2] += posStep[2];
02039         }
02040     }
02041     //---------------------------------------------------------------
02042     //===============================================================
02043 
02047     //if ( iDrawDir == -3 ) {
02048     //  iAxis = 2;
02049     //  //---------------------------------
02050     //  int numberOfSlices = m_vGridResolution[iAxis] * iSubdivision;
02051     //  if ( numberOfSlices > iMaxNumberOfSlices )  numberOfSlices = iMaxNumberOfSlices;
02052     //  T zValue = vAABBMinPt[iAxis];
02053     //  T zStep  = ( vAABBMaxPt[iAxis] - vAABBMinPt[iAxis] ) / ( numberOfSlices-1 );
02054     //  T tValue = 0;
02055     //  T tStep  = 1.0 / ( numberOfSlices-1 );
02056     //  //-----------------------
02057     //  for ( int s = 0; s < numberOfSlices; ++s ) {
02058     //      //
02059     //      glTexCoord3f( 0.4, 0.4, tValue );
02060     //      glNormal3f( 0, 0, 1 );
02061     //      glVertex3f( vAABBMinPt[0], vAABBMinPt[1], zValue );
02062     //      //
02063     //      glTexCoord3f( 0.6, 0.4, tValue );
02064     //      glNormal3f( 0, 0, 1 );
02065     //      glVertex3f( vAABBMaxPt[0], vAABBMinPt[1], zValue );
02066     //      //
02067     //      glTexCoord3f( 0.6, 0.6, tValue );
02068     //      glNormal3f( 0, 0, 1 );
02069     //      glVertex3f( vAABBMaxPt[0], vAABBMaxPt[1], zValue );
02070     //      //
02071     //      glTexCoord3f( 0.4, 0.6, tValue );
02072     //      glNormal3f( 0, 0, 1 );
02073     //      glVertex3f( vAABBMinPt[0], vAABBMaxPt[1], zValue );
02074     //      //
02075     //      zValue += zStep;
02076     //      tValue += tStep;
02077     //  }
02078     //}
02081 
02082     //===============================================================
02083     // DrawSliceStack_NegativeX();  // back-to-front is from -x to +x
02084     //---------------------------------------------------------------
02085     else if ( iDrawDir == -1 ) {
02086         iAxis = 0;
02087         //---------------------------------
02088         int numberOfSlices = gridResolution[iAxis] * iSubdivision;
02089         if ( numberOfSlices > iMaxNumberOfSlices )  numberOfSlices = iMaxNumberOfSlices;
02090         T zValue = vAABBMinPt[iAxis];
02091         T zStep  = ( vAABBMaxPt[iAxis] - vAABBMinPt[iAxis] ) / ( numberOfSlices-1 );
02092         T tValue = 0;
02093         T tStep  = 1.0 / ( numberOfSlices-1 );
02094         //-----------------------
02095         for ( int s = 0; s < numberOfSlices; ++s ) {
02096             //
02097             glTexCoord3f( tValue, 0, 1 );
02098             glNormal3f( 1, 0, 0 );
02099             glVertex3f( zValue, vAABBMinPt[1], vAABBMaxPt[2] );
02100             //
02101             glTexCoord3f( tValue, 0, 0 );
02102             glNormal3f( 1, 0, 0 );
02103             glVertex3f( zValue, vAABBMinPt[1], vAABBMinPt[2] );
02104             //
02105             glTexCoord3f( tValue, 1, 0 );
02106             glNormal3f( 1, 0, 0 );
02107             glVertex3f( zValue, vAABBMaxPt[1], vAABBMinPt[2] );
02108             //
02109             glTexCoord3f( tValue, 1, 1 );
02110             glNormal3f( 1, 0, 0 );
02111             glVertex3f( zValue, vAABBMaxPt[1], vAABBMaxPt[2] );
02112             //
02113             zValue += zStep;
02114             tValue += tStep;
02115         }
02116     }
02117     //---------------------------------------------------------------
02118     //===============================================================
02119 
02120     //===============================================================
02121     // DrawSliceStack_NegativeY();  // back-to-front is from -y to +y
02122     //---------------------------------------------------------------
02123     else if ( iDrawDir == -2 ) {
02124         iAxis = 1;
02125         //---------------------------------
02126         int numberOfSlices = gridResolution[iAxis] * iSubdivision;
02127         if ( numberOfSlices > iMaxNumberOfSlices )  numberOfSlices = iMaxNumberOfSlices;
02128         T zValue = vAABBMinPt[iAxis];
02129         T zStep  = ( vAABBMaxPt[iAxis] - vAABBMinPt[iAxis] ) / ( numberOfSlices-1 );
02130         T tValue = 0;
02131         T tStep  = 1.0 / ( numberOfSlices-1 );
02132         //-----------------------
02133         for ( int s = 0; s < numberOfSlices; ++s ) {
02134             //
02135             glTexCoord3f( 0, tValue, 1 );
02136             glNormal3f( 0, 1, 0 );
02137             glVertex3f( vAABBMinPt[0], zValue, vAABBMaxPt[2] );
02138             //
02139             glTexCoord3f( 1, tValue, 1 );
02140             glNormal3f( 0, 1, 0 );
02141             glVertex3f( vAABBMaxPt[0], zValue, vAABBMaxPt[2] );
02142             //
02143             glTexCoord3f( 1, tValue, 0 );
02144             glNormal3f( 0, 1, 0 );
02145             glVertex3f( vAABBMaxPt[0], zValue, vAABBMinPt[2] );
02146             //
02147             glTexCoord3f( 0, tValue, 0 );
02148             glNormal3f( 0, 1, 0 );
02149             glVertex3f( vAABBMinPt[0], zValue, vAABBMinPt[2] );
02150             //
02151             zValue += zStep;
02152             tValue += tStep;
02153         }
02154     }
02155     //---------------------------------------------------------------
02156     //===============================================================
02157 
02158     //===============================================================
02159     // DrawSliceStack_PositiveZ();  // back-to-front is from +z to -z
02160     //---------------------------------------------------------------
02161     else if ( iDrawDir == 3 ) {
02162         iAxis = 2;
02163         //---------------------------------
02164         int numberOfSlices = gridResolution[iAxis] * iSubdivision;
02165         if ( numberOfSlices > iMaxNumberOfSlices )  numberOfSlices = iMaxNumberOfSlices;
02166         T zValue = vAABBMaxPt[iAxis];
02167         T zStep  = ( vAABBMinPt[iAxis] - vAABBMaxPt[iAxis] ) / ( numberOfSlices-1 );
02168         T tValue = 1.0;
02169         T tStep  = -1.0 / ( numberOfSlices-1 );
02170         //-----------------------
02171         for ( int s = 0; s < numberOfSlices; ++s ) {
02172             //
02173             glTexCoord3f( 0, 0, tValue );
02174             glNormal3f( 0, 0, -1 );
02175             glVertex3f( vAABBMinPt[0], vAABBMinPt[1], zValue );
02176             //
02177             glTexCoord3f( 0, 1, tValue );
02178             glNormal3f( 0, 0, -1 );
02179             glVertex3f( vAABBMinPt[0], vAABBMaxPt[1], zValue );
02180             //
02181             glTexCoord3f( 1, 1, tValue );
02182             glNormal3f( 0, 0, -1 );
02183             glVertex3f( vAABBMaxPt[0], vAABBMaxPt[1], zValue );
02184             //
02185             glTexCoord3f( 1, 0, tValue );
02186             glNormal3f( 0, 0, -1 );
02187             glVertex3f( vAABBMaxPt[0], vAABBMinPt[1], zValue );
02188             //
02189             zValue += zStep;
02190             tValue += tStep;
02191         }
02192     }
02193     //---------------------------------------------------------------
02194     //===============================================================
02195 
02196     //===============================================================
02197     // DrawSliceStack_PositiveX();  // back-to-front is from +x to -x
02198     //---------------------------------------------------------------
02199     else if ( iDrawDir == 1 ) {
02200         iAxis = 0;
02201         //---------------------------------
02202         int numberOfSlices = gridResolution[iAxis] * iSubdivision;
02203         if ( numberOfSlices > iMaxNumberOfSlices )  numberOfSlices = iMaxNumberOfSlices;
02204         T zValue = vAABBMaxPt[iAxis];
02205         T zStep  = ( vAABBMinPt[iAxis] - vAABBMaxPt[iAxis] ) / ( numberOfSlices-1 );
02206         T tValue = 1.0;
02207         T tStep  = -1.0 / ( numberOfSlices-1 );
02208         //-----------------------
02209         for ( int s = 0; s < numberOfSlices; ++s ) {
02210             //
02211             glTexCoord3f( tValue, 0, 0 );
02212             glNormal3f( -1, 0, 0 );
02213             glVertex3f( zValue, vAABBMinPt[1], vAABBMinPt[2] );
02214             //
02215             glTexCoord3f( tValue, 0, 1 );
02216             glNormal3f( -1, 0, 0 );
02217             glVertex3f( zValue, vAABBMinPt[1], vAABBMaxPt[2] );
02218             //
02219             glTexCoord3f( tValue, 1, 1 );
02220             glNormal3f( -1, 0, 0 );
02221             glVertex3f( zValue, vAABBMaxPt[1], vAABBMaxPt[2] );
02222             //
02223             glTexCoord3f( tValue, 1, 0 );
02224             glNormal3f( -1, 0, 0 );
02225             glVertex3f( zValue, vAABBMaxPt[1], vAABBMinPt[2] );
02226             //
02227             zValue += zStep;
02228             tValue += tStep;
02229         }
02230     }
02231     //---------------------------------------------------------------
02232     //===============================================================
02233 
02234     //===============================================================
02235     // DrawSliceStack_PositiveY();  // back-to-front is from +y to -y
02236     //---------------------------------------------------------------
02237     else if ( iDrawDir == 2 ) {
02238         iAxis = 1;
02239         //---------------------------------
02240         int numberOfSlices = gridResolution[iAxis] * iSubdivision;
02241         if ( numberOfSlices > iMaxNumberOfSlices )  numberOfSlices = iMaxNumberOfSlices;
02242         T zValue = vAABBMaxPt[iAxis];
02243         T zStep  = ( vAABBMinPt[iAxis] - vAABBMaxPt[iAxis] ) / ( numberOfSlices-1 );
02244         T tValue = 1.0;
02245         T tStep  = -1.0 / ( numberOfSlices-1 );
02246         //-----------------------
02247         for ( int s = 0; s < numberOfSlices; ++s ) {
02248             //
02249             glTexCoord3f( 0, tValue, 1 );
02250             glNormal3f( 0, -1, 0 );
02251             glVertex3f( vAABBMinPt[0], zValue, vAABBMaxPt[2] );
02252             //
02253             glTexCoord3f( 0, tValue, 0 );
02254             glNormal3f( 0, -1, 0 );
02255             glVertex3f( vAABBMinPt[0], zValue, vAABBMinPt[2] );
02256             //
02257             glTexCoord3f( 1, tValue, 0 );
02258             glNormal3f( 0, -1, 0 );
02259             glVertex3f( vAABBMaxPt[0], zValue, vAABBMinPt[2] );
02260             //
02261             glTexCoord3f( 1, tValue, 1 );
02262             glNormal3f( 0, -1, 0 );
02263             glVertex3f( vAABBMaxPt[0], zValue, vAABBMaxPt[2] );
02264             //
02265             zValue += zStep;
02266             tValue += tStep;
02267         }
02268     }
02269     //---------------------------------------------------------------
02270     //===============================================================
02271 
02272     //=====================================================
02273     //-----------------------------------------------------
02274     glEnd();    // of glBegin( GL_QUADS );
02275     //---------------------------------
02276     //-----------------------------------------------------
02277     m_glslProgramObjectFor3DTextureDrawing->EndGLSL();
02278     //=====================================================
02279     //-----------------------------------------------------
02280     glPopAttrib();
02281 //  glDisable( m_3DTextureVisualizationParameters.target );
02282 //  glBindTexture( m_3DTextureVisualizationParameters.target, 0 );
02283     m_3DTextureVisualization->DisableTextureTarget();
02284     m_3DTextureVisualization->UnbindTexture();
02285 //  glDisable( m_1DTextureTransferFnLUTParameters.target );
02286 //  glBindTexture( m_1DTextureTransferFnLUTParameters.target, 0 );
02287     m_1DTextureTransferFnLUT->DisableTextureTarget();
02288     m_1DTextureTransferFnLUT->UnbindTexture();
02289     //-----------------------------------------------------
02290     //=====================================================
02291     //---------------------------------------------------------------
02292     //===============================================================
02293     // Delete temp data read from GPU
02294     delete [] dataForPosition;
02295     //delete [] dataForConnectivity;
02296 }
02297 //-----------------------------------------------------------------------------
02298 // END: DrawByGLSL (Using 3D Texture)
02299 //*****************************************************************************
02300 
02301 //-----------------------------------------------------------------------------
02302 //=============================================================================
02303 END_NAMESPACE_TAPs__OpenGL
02304 //-----------------------------------------------------------------------------
02305 //34567890123456789012345678901234567890123456789012345678901234567890123456789
02306 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines