TAPs 0.7.7.3
TAPsModelDeformableGLSL_Visualization_RTGenMesh_OLD_VERSION.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsModelDeformableGLSL_Visualization_RTGenMesh.cpp
00003 ******************************************************************************/
00008 /******************************************************************************
00009 SUKITTI PUNAK   (05/28/2007)
00010 UPDATE          (09/29/2007)
00011 ******************************************************************************/
00012 #include "TAPsModelDeformableGLSL_Visualization_RTGenMesh.hpp"
00013 // Using Inclusion Model (i.e. definitions are included in declarations)
00014 //                       (this name.cpp is included in name.hpp)
00015 // Each friend is defined directly inside its declaration.
00016 
00017 BEGIN_NAMESPACE_TAPs__OpenGL
00018 //=============================================================================
00019 // START: STATIC MEMBER(S) AND FUNCTION(S)
00020 //===============================================================
00021 template <typename T> unsigned short
00022 ModelDeformableGLSL_Visualization_RTGenMesh<T>::g_iNumOfCreatedVisualDefModel = 0;
00023 //---------------------------------------------------------------
00024 // Marching Cube Tables (EdgeTable & TriTable)
00025 //---------------------------------------------------------------
00026 // Adapted from "Polygonising a scalar field" by Paul Bourke (1994)
00027 // http://local.wasp.uwa.edu.au/~pbourke/geometry/polygonise/
00028 //---------------------------------------------------------------
00029 //-----------------------------------------------------------------------------
00030 // Vertex & Edge Index
00031 //          3------2------2
00032 //         /|            /|
00033 //       11 |          10 |
00034 //       /  3          /  1
00035 //      7------6------6   |
00036 //      |   |         |   |
00037 //      |   0------0--|---1
00038 //      7  /          5  /
00039 //      | 8           | 9 
00040 //      |/            |/
00041 //      4------4------5
00042 //---------------------------------
00043 // Edge Table [256] & Triangle Table [256][16]
00051 // V = V1 + (isovalue - s1) (V2 - V1) / (s2 - s1)
00052 //-----------------------------------------------------------------------------
00053 template <typename T> unsigned short
00054 ModelDeformableGLSL_Visualization_RTGenMesh<T>::EdgeTable[256] = {
00055     // 0-127
00056     0x0  , 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c,
00057     0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00,
00058     0x190, 0x99 , 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c,
00059     0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90,
00060     0x230, 0x339, 0x33 , 0x13a, 0x636, 0x73f, 0x435, 0x53c,
00061     0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30,
00062     0x3a0, 0x2a9, 0x1a3, 0xaa , 0x7a6, 0x6af, 0x5a5, 0x4ac,
00063     0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0,
00064     0x460, 0x569, 0x663, 0x76a, 0x66 , 0x16f, 0x265, 0x36c,
00065     0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60,
00066     0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0xff , 0x3f5, 0x2fc,
00067     0xdfc, 0xcf5, 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0,
00068     0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x55 , 0x15c,
00069     0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950,
00070     0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0xcc ,
00071     0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0,
00072 
00073     // 128-255
00074     0x8c0, 0x9c9, 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc,
00075     0xcc , 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, 0x7c0,
00076     0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c,
00077     0x15c, 0x55 , 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650,
00078     0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc,
00079     0x2fc, 0x3f5, 0xff , 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0,
00080     0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, 0xd65, 0xc6c,
00081     0x36c, 0x265, 0x16f, 0x66 , 0x76a, 0x663, 0x569, 0x460,
00082     0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac,
00083     0x4ac, 0x5a5, 0x6af, 0x7a6, 0xaa , 0x1a3, 0x2a9, 0x3a0,
00084     0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c,
00085     0x53c, 0x435, 0x73f, 0x636, 0x13a, 0x33 , 0x339, 0x230,
00086     0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c,
00087     0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99 , 0x190,
00088     0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c,
00089     0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x0
00090 };
00091 //-------------------------------------------------------------------
00092 template <typename T> char
00093 ModelDeformableGLSL_Visualization_RTGenMesh<T>::TriTable[256][16] = {
00094     // 0-9
00095     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00096     {0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00097     {0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00098     {1, 8, 3, 9, 8, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00099     {1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00100     {0, 8, 3, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00101     {9, 2, 10, 0, 2, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00102     {2, 8, 3, 2, 10, 8, 10, 9, 8, -1, -1, -1, -1, -1, -1, -1},
00103     {3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00104     {0, 11, 2, 8, 11, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00105     // 10-19
00106     {1, 9, 0, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00107     {1, 11, 2, 1, 9, 11, 9, 8, 11, -1, -1, -1, -1, -1, -1, -1},
00108     {3, 10, 1, 11, 10, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00109     {0, 10, 1, 0, 8, 10, 8, 11, 10, -1, -1, -1, -1, -1, -1, -1},
00110     {3, 9, 0, 3, 11, 9, 11, 10, 9, -1, -1, -1, -1, -1, -1, -1},
00111     {9, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00112     {4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00113     {4, 3, 0, 7, 3, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00114     {0, 1, 9, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00115     {4, 1, 9, 4, 7, 1, 7, 3, 1, -1, -1, -1, -1, -1, -1, -1},
00116     // 20-29
00117     {1, 2, 10, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00118     {3, 4, 7, 3, 0, 4, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1},
00119     {9, 2, 10, 9, 0, 2, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1},
00120     {2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4, -1, -1, -1, -1},
00121     {8, 4, 7, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00122     {11, 4, 7, 11, 2, 4, 2, 0, 4, -1, -1, -1, -1, -1, -1, -1},
00123     {9, 0, 1, 8, 4, 7, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1},
00124     {4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1, -1, -1, -1, -1},
00125     {3, 10, 1, 3, 11, 10, 7, 8, 4, -1, -1, -1, -1, -1, -1, -1},
00126     {1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4, -1, -1, -1, -1},
00127     // 30-39
00128     {4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3, -1, -1, -1, -1},
00129     {4, 7, 11, 4, 11, 9, 9, 11, 10, -1, -1, -1, -1, -1, -1, -1},
00130     {9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00131     {9, 5, 4, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00132     {0, 5, 4, 1, 5, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00133     {8, 5, 4, 8, 3, 5, 3, 1, 5, -1, -1, -1, -1, -1, -1, -1},
00134     {1, 2, 10, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00135     {3, 0, 8, 1, 2, 10, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1},
00136     {5, 2, 10, 5, 4, 2, 4, 0, 2, -1, -1, -1, -1, -1, -1, -1},
00137     {2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8, -1, -1, -1, -1},
00138     // 40-49
00139     {9, 5, 4, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00140     {0, 11, 2, 0, 8, 11, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1},
00141     {0, 5, 4, 0, 1, 5, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1},
00142     {2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5, -1, -1, -1, -1},
00143     {10, 3, 11, 10, 1, 3, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1},
00144     {4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10, -1, -1, -1, -1},
00145     {5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3, -1, -1, -1, -1},
00146     {5, 4, 8, 5, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1},
00147     {9, 7, 8, 5, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00148     {9, 3, 0, 9, 5, 3, 5, 7, 3, -1, -1, -1, -1, -1, -1, -1},
00149     // 50-59
00150     {0, 7, 8, 0, 1, 7, 1, 5, 7, -1, -1, -1, -1, -1, -1, -1},
00151     {1, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00152     {9, 7, 8, 9, 5, 7, 10, 1, 2, -1, -1, -1, -1, -1, -1, -1},
00153     {10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3, -1, -1, -1, -1},
00154     {8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2, -1, -1, -1, -1},
00155     {2, 10, 5, 2, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1},
00156     {7, 9, 5, 7, 8, 9, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1},
00157     {9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11, -1, -1, -1, -1},
00158     {2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7, -1, -1, -1, -1},
00159     {11, 2, 1, 11, 1, 7, 7, 1, 5, -1, -1, -1, -1, -1, -1, -1},
00160     // 60-69
00161     {9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11, -1, -1, -1, -1},
00162     {5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0, -1},
00163     {11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0, -1},
00164     {11, 10, 5, 7, 11, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00165     {10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00166     {0, 8, 3, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00167     {9, 0, 1, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00168     {1, 8, 3, 1, 9, 8, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1},
00169     {1, 6, 5, 2, 6, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00170     {1, 6, 5, 1, 2, 6, 3, 0, 8, -1, -1, -1, -1, -1, -1, -1},
00171     // 70-79
00172     {9, 6, 5, 9, 0, 6, 0, 2, 6, -1, -1, -1, -1, -1, -1, -1},
00173     {5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8, -1, -1, -1, -1},
00174     {2, 3, 11, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00175     {11, 0, 8, 11, 2, 0, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1},
00176     {0, 1, 9, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1},
00177     {5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11, -1, -1, -1, -1},
00178     {6, 3, 11, 6, 5, 3, 5, 1, 3, -1, -1, -1, -1, -1, -1, -1},
00179     {0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6, -1, -1, -1, -1},
00180     {3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9, -1, -1, -1, -1},
00181     {6, 5, 9, 6, 9, 11, 11, 9, 8, -1, -1, -1, -1, -1, -1, -1},
00182     // 80-89
00183     {5, 10, 6, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00184     {4, 3, 0, 4, 7, 3, 6, 5, 10, -1, -1, -1, -1, -1, -1, -1},
00185     {1, 9, 0, 5, 10, 6, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1},
00186     {10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4, -1, -1, -1, -1},
00187     {6, 1, 2, 6, 5, 1, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1},
00188     {1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7, -1, -1, -1, -1},
00189     {8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6, -1, -1, -1, -1},
00190     {7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9, -1},
00191     {3, 11, 2, 7, 8, 4, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1},
00192     {5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11, -1, -1, -1, -1},
00193     // 90-99
00194     {0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1},
00195     {9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6, -1},
00196     {8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6, -1, -1, -1, -1},
00197     {5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11, -1},
00198     {0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7, -1},
00199     {6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9, -1, -1, -1, -1},
00200     {10, 4, 9, 6, 4, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00201     {4, 10, 6, 4, 9, 10, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1},
00202     {10, 0, 1, 10, 6, 0, 6, 4, 0, -1, -1, -1, -1, -1, -1, -1},
00203     {8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10, -1, -1, -1, -1},
00204     // 100-109
00205     {1, 4, 9, 1, 2, 4, 2, 6, 4, -1, -1, -1, -1, -1, -1, -1},
00206     {3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4, -1, -1, -1, -1},
00207     {0, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00208     {8, 3, 2, 8, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1},
00209     {10, 4, 9, 10, 6, 4, 11, 2, 3, -1, -1, -1, -1, -1, -1, -1},
00210     {0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6, -1, -1, -1, -1},
00211     {3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10, -1, -1, -1, -1},
00212     {6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1, -1},
00213     {9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3, -1, -1, -1, -1},
00214     {8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1, -1},
00215     // 110-119
00216     {3, 11, 6, 3, 6, 0, 0, 6, 4, -1, -1, -1, -1, -1, -1, -1},
00217     {6, 4, 8, 11, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00218     {7, 10, 6, 7, 8, 10, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1},
00219     {0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10, -1, -1, -1, -1},
00220     {10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0, -1, -1, -1, -1},
00221     {10, 6, 7, 10, 7, 1, 1, 7, 3, -1, -1, -1, -1, -1, -1, -1},
00222     {1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7, -1, -1, -1, -1},
00223     {2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9, -1},
00224     {7, 8, 0, 7, 0, 6, 6, 0, 2, -1, -1, -1, -1, -1, -1, -1},
00225     {7, 3, 2, 6, 7, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00226     // 120-129
00227     {2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7, -1, -1, -1, -1},
00228     {2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7, -1},
00229     {1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11, -1},
00230     {11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1, -1, -1, -1, -1},
00231     {8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6, -1},
00232     {0, 9, 1, 11, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00233     {7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0, -1, -1, -1, -1},
00234     {7, 11, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00235     {7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00236     {3, 0, 8, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00237     // 130-139
00238     {0, 1, 9, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00239     {8, 1, 9, 8, 3, 1, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1},
00240     {10, 1, 2, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00241     {1, 2, 10, 3, 0, 8, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1},
00242     {2, 9, 0, 2, 10, 9, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1},
00243     {6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8, -1, -1, -1, -1},
00244     {7, 2, 3, 6, 2, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00245     {7, 0, 8, 7, 6, 0, 6, 2, 0, -1, -1, -1, -1, -1, -1, -1},
00246     {2, 7, 6, 2, 3, 7, 0, 1, 9, -1, -1, -1, -1, -1, -1, -1},
00247     {1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6, -1, -1, -1, -1},
00248     // 140-149
00249     {10, 7, 6, 10, 1, 7, 1, 3, 7, -1, -1, -1, -1, -1, -1, -1},
00250     {10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8, -1, -1, -1, -1},
00251     {0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7, -1, -1, -1, -1},
00252     {7, 6, 10, 7, 10, 8, 8, 10, 9, -1, -1, -1, -1, -1, -1, -1},
00253     {6, 8, 4, 11, 8, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00254     {3, 6, 11, 3, 0, 6, 0, 4, 6, -1, -1, -1, -1, -1, -1, -1},
00255     {8, 6, 11, 8, 4, 6, 9, 0, 1, -1, -1, -1, -1, -1, -1, -1},
00256     {9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6, -1, -1, -1, -1},
00257     {6, 8, 4, 6, 11, 8, 2, 10, 1, -1, -1, -1, -1, -1, -1, -1},
00258     {1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6, -1, -1, -1, -1},
00259     // 150-159
00260     {4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9, -1, -1, -1, -1},
00261     {10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3, -1},
00262     {8, 2, 3, 8, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1},
00263     {0, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00264     {1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8, -1, -1, -1, -1},
00265     {1, 9, 4, 1, 4, 2, 2, 4, 6, -1, -1, -1, -1, -1, -1, -1},
00266     {8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1, -1, -1, -1, -1},
00267     {10, 1, 0, 10, 0, 6, 6, 0, 4, -1, -1, -1, -1, -1, -1, -1},
00268     {4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3, -1},
00269     {10, 9, 4, 6, 10, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00270     // 160-169
00271     {4, 9, 5, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00272     {0, 8, 3, 4, 9, 5, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1},
00273     {5, 0, 1, 5, 4, 0, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1},
00274     {11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5, -1, -1, -1, -1},
00275     {9, 5, 4, 10, 1, 2, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1},
00276     {6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5, -1, -1, -1, -1},
00277     {7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2, -1, -1, -1, -1},
00278     {3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6, -1},
00279     {7, 2, 3, 7, 6, 2, 5, 4, 9, -1, -1, -1, -1, -1, -1, -1},
00280     {9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7, -1, -1, -1, -1},
00281     // 170-179
00282     {3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0, -1, -1, -1, -1},
00283     {6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8, -1},
00284     {9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7, -1, -1, -1, -1},
00285     {1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4, -1},
00286     {4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10, -1},
00287     {7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10, -1, -1, -1, -1},
00288     {6, 9, 5, 6, 11, 9, 11, 8, 9, -1, -1, -1, -1, -1, -1, -1},
00289     {3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5, -1, -1, -1, -1},
00290     {0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11, -1, -1, -1, -1},
00291     {6, 11, 3, 6, 3, 5, 5, 3, 1, -1, -1, -1, -1, -1, -1, -1},
00292     // 180-189
00293     {1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6, -1, -1, -1, -1},
00294     {0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10, -1},
00295     {11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5, -1},
00296     {6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3, -1, -1, -1, -1},
00297     {5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2, -1, -1, -1, -1},
00298     {9, 5, 6, 9, 6, 0, 0, 6, 2, -1, -1, -1, -1, -1, -1, -1},
00299     {1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8, -1},
00300     {1, 5, 6, 2, 1, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00301     {1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6, -1},
00302     {10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0, -1, -1, -1, -1},
00303     // 190-199
00304     {0, 3, 8, 5, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00305     {10, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00306     {11, 5, 10, 7, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00307     {11, 5, 10, 11, 7, 5, 8, 3, 0, -1, -1, -1, -1, -1, -1, -1},
00308     {5, 11, 7, 5, 10, 11, 1, 9, 0, -1, -1, -1, -1, -1, -1, -1},
00309     {10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1, -1, -1, -1, -1},
00310     {11, 1, 2, 11, 7, 1, 7, 5, 1, -1, -1, -1, -1, -1, -1, -1},
00311     {0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11, -1, -1, -1, -1},
00312     {9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7, -1, -1, -1, -1},
00313     {7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2, -1},
00314     // 200-209
00315     {2, 5, 10, 2, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1},
00316     {8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5, -1, -1, -1, -1},
00317     {9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2, -1, -1, -1, -1},
00318     {9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2, -1},
00319     {1, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00320     {0, 8, 7, 0, 7, 1, 1, 7, 5, -1, -1, -1, -1, -1, -1, -1},
00321     {9, 0, 3, 9, 3, 5, 5, 3, 7, -1, -1, -1, -1, -1, -1, -1},
00322     {9, 8, 7, 5, 9, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00323     {5, 8, 4, 5, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1},
00324     {5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0, -1, -1, -1, -1},
00325     // 210-219
00326     {0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5, -1, -1, -1, -1},
00327     {10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4, -1},
00328     {2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8, -1, -1, -1, -1},
00329     {0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11, -1},
00330     {0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5, -1},
00331     {9, 4, 5, 2, 11, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00332     {2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4, -1, -1, -1, -1},
00333     {5, 10, 2, 5, 2, 4, 4, 2, 0, -1, -1, -1, -1, -1, -1, -1},
00334     {3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9, -1},
00335     {5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2, -1, -1, -1, -1},
00336     // 220-229
00337     {8, 4, 5, 8, 5, 3, 3, 5, 1, -1, -1, -1, -1, -1, -1, -1},
00338     {0, 4, 5, 1, 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00339     {8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5, -1, -1, -1, -1},
00340     {9, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00341     {4, 11, 7, 4, 9, 11, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1},
00342     {0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11, -1, -1, -1, -1},
00343     {1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11, -1, -1, -1, -1},
00344     {3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4, -1},
00345     {4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2, -1, -1, -1, -1},
00346     {9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3, -1},
00347     // 230-239
00348     {11, 7, 4, 11, 4, 2, 2, 4, 0, -1, -1, -1, -1, -1, -1, -1},
00349     {11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4, -1, -1, -1, -1},
00350     {2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9, -1, -1, -1, -1},
00351     {9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7, -1},
00352     {3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10, -1},
00353     {1, 10, 2, 8, 7, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00354     {4, 9, 1, 4, 1, 7, 7, 1, 3, -1, -1, -1, -1, -1, -1, -1},
00355     {4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1, -1, -1, -1, -1},
00356     {4, 0, 3, 7, 4, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00357     {4, 8, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00358     // 240-249
00359     {9, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00360     {3, 0, 9, 3, 9, 11, 11, 9, 10, -1, -1, -1, -1, -1, -1, -1},
00361     {0, 1, 10, 0, 10, 8, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1},
00362     {3, 1, 10, 11, 3, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00363     {1, 2, 11, 1, 11, 9, 9, 11, 8, -1, -1, -1, -1, -1, -1, -1},
00364     {3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9, -1, -1, -1, -1},
00365     {0, 2, 11, 8, 0, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00366     {3, 2, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00367     {2, 3, 8, 2, 8, 10, 10, 8, 9, -1, -1, -1, -1, -1, -1, -1},
00368     {9, 10, 2, 0, 9, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00369     // 250-255
00370     {2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8, -1, -1, -1, -1},
00371     {1, 10, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00372     {1, 3, 8, 9, 1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00373     {0, 9, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00374     {0, 3, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00375     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
00376 };
00377 //-------------------------------------------------------------------
00378 template <typename T> GLuint
00379 ModelDeformableGLSL_Visualization_RTGenMesh<T>::EdgeTableTexture = 0;
00380 template <typename T> GLuint
00381 ModelDeformableGLSL_Visualization_RTGenMesh<T>::TriTableTexture = 0;
00382 //template <typename T> GLuint
00383 //ModelDeformableGLSL_Visualization_RTGenMesh<T>::TriAndEdgeTableTexture = 0;
00384 //-------------------------------------------------------------------
00385 //template <typename T>
00386 //bool ModelDeformableGLSL_Visualization_RTGenMesh<T>::SetupEdgeAndTriTablesOnGPUToo ()
00387 //{
00388 //  //-----------------------------------------------------
00389 //  /**
00390 //   * Setup Tri and Edge Tables together on one 2D Texture
00391 //   *
00392 //   * Triangle Table Texture
00393 //   *
00394 //   * The value from the edge table is mapped to the triangle table 
00395 //   * for generating triangles.  Each generated triangle has its vertices 
00396 //   * intersect an edge of the cube.
00397 //   *
00398 //   * Edge Table Texture
00399 //   *
00400 //   * An integer 2D texture of size 16x256 (width and height of GPU texture) 
00401 //   * for TriTable[256][16] -- height and width of CPU memory
00402 //   * This texture stores the 256 configurations of the marching cube.
00403 //   * Its 12-bit are set/unset by the eight cube vertices using a bitfield operation.
00404 //   */
00405 //  // An integer 2D texture of size 16x(256+16)
00406 //  glGenTextures( 1, &TriAndEdgeTableTexture );
00407 //  glEnable( GL_TEXTURE_2D );
00408 //  glBindTexture( GL_TEXTURE_2D, TriAndEdgeTableTexture );
00409 //  // Integer textures must use nearest filter
00410 //  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
00411 //  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00412 //  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
00413 //  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
00414 //  {
00415 //      int idx = 0;
00416 //      GLint TriAndEdgeTableData[(256+16)*16];
00417 //      for ( int i = 0; i < 256; ++i ) {
00418 //          for ( int j = 0; j < 16; ++j ) {
00419 //              TriAndEdgeTableData[idx] = static_cast<GLint>( TriTable[i][j] );
00420 //              ++idx;
00421 //          }
00422 //      }
00423 //      for ( int i = 0; i < 256; ++i ) {
00424 //          TriAndEdgeTableData[idx] = static_cast<GLint>( EdgeTable[i] );
00425 //          ++idx;
00426 //      }
00427 //      glTexImage2D( 
00428 //          GL_TEXTURE_2D, 0, GL_ALPHA16I_EXT, 16, 256+16, 
00429 //          0, GL_ALPHA_INTEGER_EXT, GL_INT, &TriAndEdgeTableData );
00430 //  }
00431 //  //---------------------------------
00432 //  // DEBUG: CHECK THE CREATED TEXTURE
00433 //  //#ifdef TAPs_DEBUG_MODE
00434 //  {
00435 //      GLint data[(256+16)*16];
00436 //      glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
00437 //      glBindTexture( GL_TEXTURE_2D, TriAndEdgeTableTexture );
00438 //      glGetTexImage( GL_TEXTURE_2D, 0, GL_ALPHA_INTEGER_EXT, GL_INT, data );
00439 //      TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00440 //
00441 //      std::cout << "---------\n";
00442 //      std::cout << "EdgeTable\n";
00443 //      std::cout << "---------\n";
00444 //      for ( int i = 0; i < 256; ++i ) {
00445 //          std::cout /*<< std::setbase( 16 )*/ << "(" << EdgeTable[i] << ") ";
00446 //          std::cout /*<< std::setbase( 16 )*/ << data[256*16+i] << "\t";
00447 //          if ( (i+1) % 8 == 0 ) {
00448 //              std::cout << "\n";
00449 //          }
00450 //      }
00451 //      std::cout << "---------\n";
00452 //
00453 //      std::cout << "---------\n";
00454 //      std::cout << "TriTable\n";
00455 //      std::cout << "---------\n";
00456 //      int count = 0;
00457 //      for ( int i = 0; i < 256; ++i ) {
00458 //          for ( int j = 0; j < 16; ++j ) {
00459 //              std::cout << "(" << static_cast<GLint>( TriTable[i][j] ) << ") ";
00460 //              std::cout << data[i*16+j] << "\t";
00461 //              if ( ++count % 16 == 0 ) {
00462 //                  std::cout << "\n";
00463 //              }
00464 //          }
00465 //      }
00466 //      std::cout << "---------\n";
00467 //  }
00468 //  //#endif // #ifdef TAPs_DEBUG_MODE
00469 //  //---------------------------------
00470 //  //-----------------------------------------------------
00471 //  glDisable( GL_TEXTURE_2D );
00472 //  glBindTexture( GL_TEXTURE_2D, 0 );
00473 //  //-----------------------------------------------------
00474 //  return true;
00475 //}
00476 //-------------------------------------------------------------------
00477 template <typename T>
00478 bool ModelDeformableGLSL_Visualization_RTGenMesh<T>::SetupEdgeAndTriTablesOnGPU ()
00479 {
00480     // if already generated
00481     if ( EdgeTableTexture && TriTableTexture )  return true;
00482     //-----------------------------------------------------
00489     // An integer 2D texture of size 256x1
00490     glGenTextures( 1, &EdgeTableTexture );
00491     glEnable( GL_TEXTURE_2D );
00492     glBindTexture( GL_TEXTURE_2D, EdgeTableTexture );
00493     // Integer textures must use nearest filter
00494     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
00495     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00496     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
00497     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
00498     {
00499         GLint EdgeTableData[256];
00500         for ( int i = 0; i < 256; ++i ) {
00501             EdgeTableData[i] = static_cast<GLint>( EdgeTable[i] );
00502         }
00503         glTexImage2D( 
00504             GL_TEXTURE_2D, 0, GL_ALPHA16I_EXT, 256, 1, 
00505             0, GL_ALPHA_INTEGER_EXT, GL_INT, &EdgeTableData );
00506     }
00507     //---------------------------------
00508     // DEBUG: CHECK THE CREATED TEXTURE
00509     #ifdef TAPs_DEBUG_MODE
00510     {
00511         GLint data[256];
00512         glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
00513         glBindTexture( GL_TEXTURE_2D, EdgeTableTexture );
00514         glGetTexImage( GL_TEXTURE_2D, 0, GL_ALPHA_INTEGER_EXT, GL_INT, data );
00515 
00516         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00517         std::cout << "---------\n";
00518         std::cout << "EdgeTable\n";
00519         std::cout << "---------\n";
00520         for ( int i = 0; i < 256; ++i ) {
00521             std::cout /*<< std::setbase( 16 )*/ << "(" << EdgeTable[i] << ") ";
00522             std::cout /*<< std::setbase( 16 )*/ << data[i] << "\t";
00523             if ( (i+1) % 8 == 0 ) {
00524                 std::cout << "\n";
00525             }
00526         }
00527         std::cout << "---------\n";
00528     }
00529     #endif // #ifdef TAPs_DEBUG_MODE
00530     //---------------------------------
00531     //-----------------------------------------------------
00539     // An integer 2D texture of size 16x256 (width and height of GPU texture) 
00540     // for TriTable[256][16] -- height and width of CPU memory
00541     glGenTextures( 1, &TriTableTexture );
00542     glEnable( GL_TEXTURE_2D );
00543     glBindTexture( GL_TEXTURE_2D, TriTableTexture );
00544     // Integer textures must use nearest filter
00545     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
00546     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00547     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
00548     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
00549     {
00550         GLint TriTableData[256][16];
00551         for ( int i = 0; i < 256; ++i ) {
00552             for ( int j = 0; j < 16; ++j ) {
00553                 TriTableData[i][j] = static_cast<GLint>( TriTable[i][j] );
00554             }
00555         }
00556         glTexImage2D( 
00557             GL_TEXTURE_2D, 0, GL_ALPHA16I_EXT, 16, 256, 
00558             0, GL_ALPHA_INTEGER_EXT, GL_INT, &TriTableData );
00559     }
00560     //---------------------------------
00561     // DEBUG: CHECK THE CREATED TEXTURE
00562     #ifdef TAPs_DEBUG_MODE
00563     {
00564         GLint data[256][16];
00565         glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
00566         glBindTexture( GL_TEXTURE_2D, TriTableTexture );
00567         glGetTexImage( GL_TEXTURE_2D, 0, GL_ALPHA_INTEGER_EXT, GL_INT, data );
00568 
00569         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00570         std::cout << "---------\n";
00571         std::cout << "TriTable\n";
00572         std::cout << "---------\n";
00573         int count = 0;
00574         for ( int i = 0; i < 256; ++i ) {
00575             for ( int j = 0; j < 16; ++j ) {
00576                 std::cout << "(" << static_cast<GLint>( TriTable[i][j] ) << ") ";
00577                 std::cout << data[i][j] << "\t";
00578                 if ( ++count % 16 == 0 ) {
00579                     std::cout << "\n";
00580                 }
00581             }
00582         }
00583         std::cout << "---------\n";
00584     }
00585     #endif // #ifdef TAPs_DEBUG_MODE
00586     //---------------------------------
00587     glDisable( GL_TEXTURE_2D );
00588     glBindTexture( GL_TEXTURE_2D, 0 );
00589     //-----------------------------------------------------
00590     return true;
00591 }
00592 
00593 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00594 //-------------------------------------------------------------------
00595 template <typename T>
00596 bool ModelDeformableGLSL_Visualization_RTGenMesh<T>::
00597 SetupCubeTrackingTexture ( Vector3<int> const & vGridSize )
00598 {
00599     //-----------------------------------------------------
00606     // An integer 3D texture
00607     glGenTextures( 1, &CubeTrackingTexture );
00608     glEnable( GL_TEXTURE_3D );
00609     glBindTexture( GL_TEXTURE_3D, CubeTrackingTexture );
00610     // Integer textures must use nearest filter
00611     glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
00612     glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00613     glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP );
00614     glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP );
00615     glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP );
00616     //---------------------------------
00617     Vector3<int> vCubes( vGridSize[0]-1, vGridSize[1]-1, vGridSize[2]-1 );
00618     int iTotalCubes = vCubes[0] * vCubes[1] * vCubes[2];
00619 
00620     // Use GLubyte for GL_ALPHA8UI_EXT gives incorrect result.
00621     // It has to be GLuint applied for either GL_ALPHA8UI_EXT or GL_ALPHA16UI_EXT.
00622     // Also the format has to be GL_ALPHA_INTEGER_EXT.
00623     // But the type has to be GL_UNSIGNED_INT.
00624 
00625     // NOT WORKING!
00626     //GLubyte * pCubeValData = new GLubyte[ iTotalCubes ];
00627 
00628     //GLuint * pCubeValData = new GLuint[ iTotalCubes ];
00629     GLint * pCubeValData = new GLint[ iTotalCubes ];
00630     //GLubyte * pCubeValData = new GLubyte[ iTotalCubes ];
00631     {
00632         int i = 0;
00633         for ( int z = 0; z < vCubes[2]; ++z ) {
00634             for ( int y = 0; y < vCubes[1]; ++y ) {
00635                 for ( int x = 0; x < vCubes[0]; ++x ) {
00636                     pCubeValData[i] = i % 256;
00637                     //if ( x >= vCubes[0]/2 && y >= vCubes[1]/2 && z >= vCubes[1]/2 ) {
00638                     //  pCubeValData[i] = 55;
00639                     //}
00640                     //else {
00641                     //  pCubeValData[i] = 0;
00642                     //}
00643                     ++i;
00644                 }
00645             }
00646         }
00647 
00648         //glTexImage3D( 
00649         //  GL_TEXTURE_3D, 0, GL_ALPHA8UI_EXT,      // This makes its  8-bit unsigned integer
00650         //  //GL_TEXTURE_3D, 0, GL_ALPHA16UI_EXT,   // This makes its 16-bit unsigned integer
00651         //  vCubes[0], vCubes[1], vCubes[2], 
00652         //  //0, GL_ALPHA_INTEGER_EXT, GL_UNSIGNED_BYTE, pCubeValData );
00653         //  0, GL_ALPHA_INTEGER_EXT, GL_UNSIGNED_INT, pCubeValData );
00654         // CURRENTLY
00655         // GL_ALPHA8UI_EXT is not working correctly.
00656         // Due to bugs with unsigned int in geometry shader compiler/driver.
00657         // So GL_ALPHA16I_EXT is used instead!
00658         //glTexImage3D( 
00659         //  GL_TEXTURE_3D, 0, GL_ALPHA16I_EXT,  // This makes its 16-bit integer
00660         //  vCubes[0], vCubes[1], vCubes[2], 
00661         //  0, GL_ALPHA_INTEGER_EXT, GL_INT, pCubeValData );
00662         // CURRENTLY
00663         // The GLSL's texture3D( isampler3D sampler, vec3 coords ) 
00664         // doesn't return the correct value for integer
00665         // The GLSL's texture3D( usampler3D sampler, vec3 coords ) 
00666         // doesn't return the correct value for unsinged integer
00667         // So GL_ALPHA8 is used instead!
00668         //glTexImage3D( 
00669         //  GL_TEXTURE_3D, 0, GL_ALPHA8,        // This makes its 8-bit unsigned byte
00670         //  vCubes[0], vCubes[1], vCubes[2], 
00671         //  0, GL_ALPHA, GL_UNSIGNED_BYTE, pCubeValData );
00672         // CURRENTLY
00673         // GL_ALPHA8 DOESN'T WORK EITHER SO BACK TO GL_ALPHA16UI_EXT
00674         glTexImage3D( 
00675             GL_TEXTURE_3D, 0, GL_ALPHA16I_EXT,  // This makes its 16-bit integer
00676             vCubes[0], vCubes[1], vCubes[2], 
00677             0, GL_ALPHA_INTEGER_EXT, GL_INT, pCubeValData );
00678     }
00679 
00680     /*
00681     //---------------------------------
00682     // DEBUG: CHECK THE CREATED TEXTURE
00683     {
00684         std::cout << "-----------------------------------\n";
00685         std::cout << "INIT DATA for Cube Tracking Texture\n";
00686         std::cout << "-----------------------------------\n";
00687         int i = 0;
00688         for ( int z = 0; z < vCubes[2]; ++z ) {
00689             for ( int y = 0; y < vCubes[1]; ++y ) {
00690                 for ( int x = 0; x < vCubes[0]; ++x ) {
00691                     std::cout << "  ";
00692                     if ( pCubeValData[i] < 100 )    std::cout << " ";
00693                     if ( pCubeValData[i] <  10 )    std::cout << " ";
00694                     std::cout << static_cast<int>( pCubeValData[i] );
00695                     ++i;
00696                 }
00697                 std::cout << "\n";
00698             }
00699             std::cout << "\n";
00700         }
00701         std::cout << "---------------------\n";
00702     }
00703     {
00704         glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
00705         glBindTexture( GL_TEXTURE_3D, CubeTrackingTexture );
00706 
00707         // NOT WORKING!
00708         //glGetTexImage( GL_TEXTURE_3D, 0, GL_ALPHA_INTEGER_EXT, GL_UNSIGNED_BYTE, pCubeValData );
00709 
00710         // CURRENTLY, GL_ALPHA8UI_EXT is not working correctly.
00711         // Due to bugs with unsigned int in geometry shader compiler/driver.
00712         // So GL_ALPHA16I_EXT is used instead!
00713         //glGetTexImage( GL_TEXTURE_3D, 0, GL_ALPHA_INTEGER_EXT, GL_UNSIGNED_INT, pCubeValData );
00714         //glGetTexImage( GL_TEXTURE_3D, 0, GL_ALPHA_INTEGER_EXT, GL_INT, pCubeValData );
00715         // CURRENTLY
00716         // The GLSL's texture3D( isampler3D sampler, vec3 coords ) 
00717         // doesn't return the correct value for integer
00718         // The GLSL's texture3D( usampler3D sampler, vec3 coords ) 
00719         // doesn't return the correct value for unsinged integer
00720         // So GL_ALPHA8 is used instead!
00721         //glGetTexImage( GL_TEXTURE_3D, 0, GL_ALPHA_INTEGER_EXT, GL_INT, pCubeValData );
00722         // CURRENTLY
00723         // The GLSL's texture3D( isampler3D sampler, vec3 coords ) 
00724         // doesn't return the correct value for integer
00725         // The GLSL's texture3D( usampler3D sampler, vec3 coords ) 
00726         // doesn't return the correct value for unsinged integer
00727         // So GL_ALPHA8 is used instead!
00728         //glGetTexImage( GL_TEXTURE_3D, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pCubeValData );
00729         // CURRENTLY
00730         // GL_ALPHA8 DOESN'T WORK EITHER SO BACK TO GL_ALPHA16UI_EXT
00731         glGetTexImage( GL_TEXTURE_3D, 0, GL_ALPHA_INTEGER_EXT, GL_INT, pCubeValData );
00732 
00733 
00734 
00735         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
00736         std::cout << "---------------------\n";
00737         std::cout << "Cube Tracking Texture\n";
00738         std::cout << "---------------------\n";
00739         int i = 0;
00740         for ( int z = 0; z < vCubes[2]; ++z ) {
00741             for ( int y = 0; y < vCubes[1]; ++y ) {
00742                 for ( int x = 0; x < vCubes[0]; ++x ) {
00743                     std::cout << "  ";
00744                     if ( pCubeValData[i] < 100 )    std::cout << " ";
00745                     if ( pCubeValData[i] <  10 )    std::cout << " ";
00746                     std::cout << static_cast<int>( pCubeValData[i] );
00747                     ++i;
00748                 }
00749                 std::cout << "\n";
00750             }
00751             std::cout << "\n";
00752         }
00753         std::cout << "-----------------------------------\n";
00754     }
00755     //*/
00756 
00757     //---------------------------------
00758     glDisable( GL_TEXTURE_3D );
00759     glBindTexture( GL_TEXTURE_3D, 0 );
00760     //-----------------------------------------------------
00761     delete [] pCubeValData;
00762     return true;
00763 }
00764 //-------------------------------------------------------------------
00765 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00766 
00767 //-----------------------------------------------------------------------------
00768 // END: STATIC MEMBER(S) AND FUNCTION(S)
00769 //=============================================================================
00770 //=============================================================================
00771 //-----------------------------------------------------------------------------
00772 // Nondefault Constructor
00773 template <typename T>
00774 ModelDeformableGLSL_Visualization_RTGenMesh<T>::
00775 ModelDeformableGLSL_Visualization_RTGenMesh ( 
00776     ModelDeformableGLSL<T> * simDomain
00777 )
00778 {
00779     m_SimDomain = simDomain;
00780     //---------------------------------------------------------------
00781     // Since Microsoft gl.h is version 1.1,
00782     // So this fn calls glewInit() for latest OpenGL version 
00783     // and initialize GLSL.
00784     GFnInitGLSL();
00785 
00786     ++g_iNumOfCreatedVisualDefModel;
00787 
00788     Default();
00789     //---------------------------------------------------------------
00790 #ifdef  TAPs_DEBUG_MODE
00791     std::cout << "ModelDeformableGLSL_Visualization_RTGenMesh<" 
00792                 << typeid(T).name() << "> Constructor\n";
00793 #endif
00794 }
00795 //-----------------------------------------------------------------------------
00796 // Destructor
00797 template <typename T>
00798 ModelDeformableGLSL_Visualization_RTGenMesh<T>::
00799 ~ModelDeformableGLSL_Visualization_RTGenMesh ()
00800 {
00801     Clear();
00802     //---------------------------------------------------------------
00803     // Static Data
00804     --g_iNumOfCreatedVisualDefModel;
00805     if ( g_iNumOfCreatedVisualDefModel == 0 ) {
00806         if ( EdgeTableTexture > 0 ) {
00807             glDeleteTextures( 1, &EdgeTableTexture );
00808             EdgeTableTexture = 0;
00809         }
00810         if ( TriTableTexture > 0 ) {
00811             glDeleteTextures( 1, &TriTableTexture );
00812             TriTableTexture = 0;
00813         }
00814         //if ( TriAndEdgeTableTexture > 0 ) {
00815         //  glDeleteTextures( 1, &TriAndEdgeTableTexture );
00816         //  TriAndEdgeTableTexture = 0;
00817         //}
00818     }
00819     //---------------------------------------------------------------
00820 #ifdef  TAPs_DEBUG_MODE
00821     std::cout << "ModelDeformableGLSL_Visualization_RTGenMesh<" 
00822                 << typeid(T).name() << "> Destructor\n";
00823 #endif
00824 }
00825 //-----------------------------------------------------------------------------
00826 // Default
00827 template <typename T>
00828 void ModelDeformableGLSL_Visualization_RTGenMesh<T>::Default ()
00829 {
00830 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
00831     //-------------------------------------------
00832     // For Rendering
00833     m_uiTotalCubes = 0;
00834     m_vboVerticesAsTexCoords = 0;
00835 
00836     //-------------------------------------------
00837     // For Cube Tracking
00838     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00839     m_vboTexCoordForCubeTracking = 0;
00840     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00841 
00842 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
00843 
00844     //-------------------------------------------
00845     // For GLSL shaders
00846 
00847     // Selectable Gradient Computations
00848     //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
00849     m_bComputeGradientFaces     = true;
00850     m_bComputeGradientVertices  = true;
00851     m_bComputeGradientEdges     = true;
00852     //#endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
00853 
00854     // Interpolation Parameters
00855     m_glslProgramObjectWithGlobalIntpValue  = NULL;
00856     m_glslProgramObjectWithTextureIntpValue = NULL;
00857 
00858     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
00859     m_glslProgramObjectForShaderSurfaceFromSW = NULL;
00860     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
00861 
00862     // Cube Tracking
00863     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00864     m_glslFBO_CubeTracking = 0;
00865     m_glslProgramObjectUpdateCubeTracking = NULL;
00866     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00867 
00868     //-------------------------------------------
00869     // For Textures
00870     m_3DTexture = 0;
00871     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00872     CubeTrackingTexture = 0;
00873     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00874     //-------------------------------------------
00875     // Adjustable Parameters
00876     m_bUseGlobalInterpolationVal    =   false;
00877     m_tGlobalInterpolationVal       =   0.1;
00878     m_bDrawPNTriangle               =   false;
00879     m_PNTriangleSmoothness          =   4;
00880     //-------------------------------------------
00881 }
00882 //-----------------------------------------------------------------------------
00883 // Clear
00884 template <typename T>
00885 void ModelDeformableGLSL_Visualization_RTGenMesh<T>::Clear ()
00886 {
00887 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
00888     //---------------------------------------------------------------
00889     // Clear Vertex Buffer for Rendering
00890     if ( m_vboVerticesAsTexCoords > 0 ) {
00891         glDeleteBuffers( 1, &m_vboVerticesAsTexCoords );
00892     }
00893 
00894     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00895     //---------------------------------------------------------------
00896     // Clear Vertex Buffer for Cube Tracking
00897     if ( m_vboTexCoordForCubeTracking > 0 ) {
00898         glDeleteBuffers( 1, &m_vboTexCoordForCubeTracking );
00899     }
00900     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00901 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
00902     //---------------------------------------------------------------
00903     // Clear GLSL Shaders
00904     if ( m_glslProgramObjectWithGlobalIntpValue ) {
00905         m_glslShaderManager.Delete( m_glslProgramObjectWithGlobalIntpValue );
00906     }
00907     if ( m_glslProgramObjectWithTextureIntpValue ) {
00908         m_glslShaderManager.Delete( m_glslProgramObjectWithTextureIntpValue );
00909     }
00910 
00911     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
00912     if ( m_glslProgramObjectForShaderSurfaceFromSW ) {
00913         m_glslShaderManager.Delete( m_glslProgramObjectForShaderSurfaceFromSW );
00914     }
00915     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
00916 
00917     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00918     // Delete Frame Buffer Object(s)
00919     if ( m_glslFBO_CubeTracking > 0 ) {
00920         glDeleteFramebuffersEXT( 1, &m_glslFBO_CubeTracking );
00921     }
00922     if ( m_glslProgramObjectUpdateCubeTracking ) {
00923         m_glslShaderManager.Delete( m_glslProgramObjectUpdateCubeTracking );
00924     }
00925     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00926     //---------------------------------------------------------------
00927     // Clear Textures
00928     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00929     if ( CubeTrackingTexture > 0 ) {
00930         glDeleteTextures( 1, &CubeTrackingTexture );
00931     }
00932     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00933     //---------------------------------------------------------------
00934     Default();
00935 }
00936 //-----------------------------------------------------------------------------
00937 //=============================================================================
00938 
00939 
00940 //=============================================================================
00941 // SetupVisualization
00942 //-----------------------------------------------------------------------------
00943 template <typename T>
00944 bool ModelDeformableGLSL_Visualization_RTGenMesh<T>::
00945 SetupVisualization ( 
00946     int resolutionX, int resolutionY, int resolutionZ, 
00947     enum GridGenerator<T>::VertexFlag *** pFlagDataForXYZVoxels 
00948 )
00949 {
00950     //---------------------------------------------------------------
00951     // Setup (static) Edge and Triangle Tables
00952     if ( !SetupEdgeAndTriTablesOnGPU() ) {
00953         std::cout << "SetupEdgeAndTriTablesOnGPU returns false!\n";
00954         return false;
00955     }
00956     //---------------------------------------------------------------
00957 
00958     //---------------------------------------------------------------
00959     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
00960     if ( !GenerateTheVBOForTextureCoordinates( 
00961             Vector3<int>( resolutionX, resolutionY, resolutionZ ) ) 
00962     ) {
00963         std::cout << "GenerateTheVBOForTextureCoordinates(...) returns false!\n";
00964         return false;
00965     }
00966     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
00967     //---------------------------------------------------------------
00968 
00969     //---------------------------------------------------------------
00970     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
00971     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00972     if ( !GenerateTheVBOForTexCoordForCubeTracking( 
00973             Vector3<int>( resolutionX, resolutionY, resolutionZ ) ) 
00974     ) {
00975         std::cout << "GenerateTheVBOForTexCoordForCubeTracking(...) returns false!\n";
00976         return false;
00977     }
00978     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00979     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
00980     //---------------------------------------------------------------
00981 
00982     //---------------------------------------------------------------
00983     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_RENDERING
00984     if ( !SetupShadersForDrawing() ) {
00985         std::cout << "SetupShadersForDrawing() returns false!\n";
00986         return false;
00987     }
00988     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_RENDERING
00989     //---------------------------------------------------------------
00990 
00991     //---------------------------------------------------------------
00992     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
00993     //-----------------------
00994     if ( !GenerateBufferObjects() ) {
00995         std::cout << "GenerateBufferObjects() returns false!\n";
00996         return false;
00997     }
00998     //-----------------------
00999     if ( !SetupCubeTrackingTexture( 
01000             Vector3<int>( resolutionX, resolutionY, resolutionZ ) ) 
01001     ) {
01002         std::cout << "SetupCubeTrackingTexture(...) returns false!\n";
01003         return false;
01004     }
01005     //-----------------------
01006     if ( !SetupShadersForUpdateCubeTrackingTexture() ) {
01007         std::cout << "SetupShadersForUpdateCubeTrackingTexture() returns false!\n";
01008         return false;
01009     }
01010     //-----------------------
01011     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
01012     //---------------------------------------------------------------
01013 
01014     return true;
01015 }
01016 //-----------------------------------------------------------------------------
01017 // END: SetupVisualization
01018 //=============================================================================
01019 
01020 
01021 //=============================================================================
01022 // Setup a 3D Texture for Visualization
01023 //-----------------------------------------------------------------------------
01024 /*
01025 template <typename T>
01026 bool ModelDeformableGLSL_Visualization_RTGenMesh<T>::
01027 Setup3DTexture ( 
01028 //  int resolutionX, int resolutionY, int resolutionZ, 
01029 //  enum GridGenerator<T>::VertexFlag *** pFlagDataForXYZVoxels
01030 )
01031 {
01032     //---------------------------------------------------------------
01033     return true;
01034 }
01035 //*/
01036 //-----------------------------------------------------------------------------
01037 // END: Setup3DTexture
01038 //=============================================================================
01039 
01040 
01041 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_RENDERING
01042 //=============================================================================
01043 // Setup Shaders for Drawing by Marching Cubes Algorithm
01044 //-----------------------------------------------------------------------------
01045 template <typename T>
01046 bool ModelDeformableGLSL_Visualization_RTGenMesh<T>::SetupShadersForDrawing ()
01047 {
01048     //*
01049     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
01050     if ( !m_glslShaderManager.CreateGLSLProgramObject( m_glslProgramObjectForShaderSurfaceFromSW ) ) {
01051         std::cout << "Cannot Create Program Object: m_glslProgramObjectForShaderSurfaceFromSW" << std::endl;
01052         Clear();
01053         return false;
01054     }
01055     if ( !m_glslShaderManager.AttachShaderFromFile( m_glslProgramObjectForShaderSurfaceFromSW, 
01056         "Resource/Shader/DefModelShaderForDrawBySW.vert", GL_VERTEX_SHADER ) ) {
01057         std::cout << "Cannot Load Shader: m_glslProgramObjectForShaderSurfaceFromSW --- Resource/Shader/DefModelShaderForDrawBySW.vert" << std::endl;
01058         Clear();
01059         return false;
01060     }
01061     if ( !m_glslShaderManager.AttachShaderFromFile( m_glslProgramObjectForShaderSurfaceFromSW, 
01062         "Resource/Shader/DefModelShaderForDrawBySW.frag", GL_FRAGMENT_SHADER ) ) {
01063         std::cout << "Cannot Load Shader: m_glslProgramObjectForShaderSurfaceFromSW --- Resource/Shader/DefModelShaderForDrawBySW.frag" << std::endl;
01064         Clear();
01065         return false;
01066     }
01067     if ( !m_glslShaderManager.Link( m_glslProgramObjectForShaderSurfaceFromSW ) ) {
01068         Clear();
01069         return false;
01070     }
01071     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
01072     //*/
01073 
01074     //===============================================================
01075     // START: m_glslProgramObjectWithGlobalIntpValue
01076     //---------------------------------------------------------------
01077     if ( !m_glslShaderManager.CreateGLSLProgramObject( m_glslProgramObjectWithGlobalIntpValue ) ) {
01078         Clear();
01079         return false;
01080     }
01081     //---------------------------------------------------------------
01082     // Vertex Shader
01083     if ( !m_glslShaderManager.AttachShaderFromFile( m_glslProgramObjectWithGlobalIntpValue, 
01084         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_PNTRI
01085 
01086             // WITH 1X1X1 CUBE
01087             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01088                 // WITH CUBE TRACKING
01089                 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
01090                     // WITH GRADIENT TEXTURE
01091                     #ifdef TAPs_MODEL_DEFORMABLE_USE_GRADIENT_TEXTURE
01092                         //"Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes.vert", GL_VERTEX_SHADER ) ) {
01093                         "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_RdCubeVerticesOnVertShader.vert", GL_VERTEX_SHADER ) ) {
01094                     #endif
01095                     // WITHOUT GRADIENT TEXTURE
01096                     #ifdef TAPs_MODEL_DEFORMABLE_NOT_USE_GRADIENT_TEXTURE
01097                         // WITH SELECTABLE GRADIENT COMPUTATION OPTION
01098                         #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
01099                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes.vert", GL_VERTEX_SHADER ) ) {
01100                         // WITHOUT SELECTABLE GRADIENT COMPUTATION OPTION
01101                         #else
01102                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes.vert", GL_VERTEX_SHADER ) ) {
01103                         #endif
01104                     #endif
01105                 #endif
01106                 // WITHOUT CUBE TRACKING
01107                 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_TRACKING_CUBES
01108                     // WITH GRADIENT TEXTURE
01109                     #ifdef TAPs_MODEL_DEFORMABLE_USE_GRADIENT_TEXTURE
01110                         "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes.vert", GL_VERTEX_SHADER ) ) {
01111                     #endif
01112                     // WITHOUT GRADIENT TEXTURE
01113                     #ifdef TAPs_MODEL_DEFORMABLE_NOT_USE_GRADIENT_TEXTURE
01114                         // WITH SELECTABLE GRADIENT COMPUTATION OPTION
01115                         #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
01116                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes.vert", GL_VERTEX_SHADER ) ) {
01117                         // WITHOUT SELECTABLE GRADIENT COMPUTATION OPTION
01118                         #else
01119                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes.vert", GL_VERTEX_SHADER ) ) {
01120                         #endif
01121                     #endif
01122                 #endif
01123             #endif
01124 
01125             // WITH 2X2X2 CUBE
01126             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_2X2X2_CUBES
01127                 "Resource/Shader/DefModelDrawByGenMesh_2x2x2cubes.vert", GL_VERTEX_SHADER ) ) {
01128             #endif
01129 
01130             // WITH 4X4X4 CUBE
01131             //#ifdef    TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_4X4X4_CUBES
01132             //  "Resource/Shader/DefModelDrawByGenMesh_4x4x4cubes.vert", GL_VERTEX_SHADER ) ) {
01133             //#endif
01134         #endif
01135         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_PNTRI
01136             "Resource/Shader/DefModelDrawByGenMeshWithPNTri.vert", GL_VERTEX_SHADER ) ) {
01137         #endif
01138         Clear();
01139         return false;
01140     }
01141 
01142     //*
01144     // DEBUG
01145     {
01146         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_PNTRI
01147             // WITH 1X1X1 CUBE
01148             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01149                 // WITH CUBE TRACKING
01150                 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
01151                     // WITH GRADIENT TEXTURE
01152                     #ifdef TAPs_MODEL_DEFORMABLE_USE_GRADIENT_TEXTURE
01153                             //std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wGlobalIntp_wCubeTracking_wGradTex.geom" << std::endl;
01154                             std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wGlobalIntp_wCubeTracking_wGradTex_RdCubeVerticesOnVertShader.geom" << std::endl;
01155                     #endif
01156                     // WITHOUT GRADIENT TEXTURE
01157                     #ifdef TAPs_MODEL_DEFORMABLE_NOT_USE_GRADIENT_TEXTURE
01158                         // WITH SELECTABLE GRADIENT COMPUTATION OPTION
01159                         #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
01160                             std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wGlobalIntp_wCubeTracking_wSelectableGradients.geom" << std::endl;
01161                         // WITHOUT SELECTABLE GRADIENT COMPUTATION OPTION
01162                         #else
01163                             std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wGlobalIntp_wCubeTracking.geom" << std::endl;
01164                         #endif
01165                     #endif
01166                 #endif
01167                 // WITHOUT CUBE TRACKING
01168                 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_TRACKING_CUBES
01169                     // WITH GRADIENT TEXTURE
01170                     #ifdef TAPs_MODEL_DEFORMABLE_USE_GRADIENT_TEXTURE
01171                         std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wGlobalIntp_wGradTex.geom" << std::endl;
01172                     #endif
01173                     // WITHOUT GRADIENT TEXTURE
01174                     #ifdef TAPs_MODEL_DEFORMABLE_NOT_USE_GRADIENT_TEXTURE
01175                         // WITH SELECTABLE GRADIENT COMPUTATION OPTION
01176                         #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
01177                             std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wGlobalIntp_wSelectableGradients.geom" << std::endl;
01178                         // WITHOUT SELECTABLE GRADIENT COMPUTATION OPTION
01179                         #else
01180                             std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wGlobalIntp.geom" << std::endl;
01181                         #endif
01182                     #endif
01183                 #endif
01184             #endif
01185             // WITH 2X2X2 CUBES
01186             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_2X2X2_CUBES
01187                 std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_2x2x2cubes.geom" << std::endl;
01188             #endif
01189         #endif
01190         // WITH PN-TRIANGLES
01191         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_PNTRI
01192             std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMeshWithPNTri.geom" << std::endl;
01193         #endif
01194     }
01196     //*/
01197 
01198     //---------------------------------------------------------------
01199     // Geometry Shader -- WITH GLOBAL INTERPOLATION VALUE
01200     if ( !m_glslShaderManager.AttachShaderFromFile( m_glslProgramObjectWithGlobalIntpValue, 
01201         // WITHOUT PN-TRIANGLES
01202         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_PNTRI
01203             // WITH 1X1X1 CUBE
01204             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01205                 // WITH CUBE TRACKING
01206                 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
01207                     // WITH GRADIENT TEXTURE
01208                     #ifdef TAPs_MODEL_DEFORMABLE_USE_GRADIENT_TEXTURE
01209                         //"Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wGlobalIntp_wCubeTracking_wGradTex.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01210                         "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wGlobalIntp_wCubeTracking_wGradTex_RdCubeVerticesOnVertShader.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01211                     #endif
01212                     // WITHOUT GRADIENT TEXTURE
01213                     #ifdef TAPs_MODEL_DEFORMABLE_NOT_USE_GRADIENT_TEXTURE
01214                         // WITH SELECTABLE GRADIENT COMPUTATION OPTION
01215                         #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
01216                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wGlobalIntp_wCubeTracking_wSelectableGradients.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01217                         // WITHOUT SELECTABLE GRADIENT COMPUTATION OPTION
01218                         #else
01219                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wGlobalIntp_wCubeTracking.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01220                         #endif
01221                     #endif
01222                 #endif
01223                 // WITHOUT CUBE TRACKING
01224                 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_TRACKING_CUBES
01225                     // WITH GRADIENT TEXTURE
01226                     #ifdef TAPs_MODEL_DEFORMABLE_USE_GRADIENT_TEXTURE
01227                         "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wGlobalIntp_wGradTex.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01228                     #endif
01229                     // WITHOUT GRADIENT TEXTURE
01230                     #ifdef TAPs_MODEL_DEFORMABLE_NOT_USE_GRADIENT_TEXTURE
01231                         // WITH SELECTABLE GRADIENT COMPUTATION OPTION
01232                         #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
01233                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wGlobalIntp_wSelectableGradients.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01234                         // WITHOUT SELECTABLE GRADIENT COMPUTATION OPTION
01235                         #else
01236                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wGlobalIntp.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01237                         #endif
01238                     #endif
01239                 #endif
01240             #endif
01241             // WITH 2X2X2 CUBES
01242             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_2X2X2_CUBES
01243                 "Resource/Shader/DefModelDrawByGenMesh_2x2x2cubes.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01244             #endif
01245         #endif
01246         // WITH PN-TRIANGLES
01247         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_PNTRI
01248             "Resource/Shader/DefModelDrawByGenMeshWithPNTri.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01249         #endif
01250         Clear();
01251         return false;
01252     }
01253     //---------------------------------------------------------------
01254     // Fragment Shader
01255     if ( !m_glslShaderManager.AttachShaderFromFile( m_glslProgramObjectWithGlobalIntpValue, 
01256         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_PNTRI
01257             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01258                 "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes.frag", GL_FRAGMENT_SHADER ) ) {
01259             #endif
01260             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_2X2X2_CUBES
01261                 "Resource/Shader/DefModelDrawByGenMesh_2x2x2cubes.frag", GL_FRAGMENT_SHADER ) ) {
01262             #endif
01263         #endif
01264         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_PNTRI
01265             "Resource/Shader/DefModelDrawByGenMeshWithPNTri.frag", GL_FRAGMENT_SHADER ) ) {
01266         #endif
01267         Clear();
01268         return false;
01269     }
01270     //---------------------------------------------------------------
01271     // Setup Geometry Shader for Marching Cube
01272     // Since Geometry Shader needs to specify input and output types 
01273     // and the number of maximum out/emitted vertices.
01274     {
01275         GLuint progObj = m_glslProgramObjectWithGlobalIntpValue->GetProgramObject();
01276         glProgramParameteriEXT( progObj, GL_GEOMETRY_INPUT_TYPE_EXT,    GL_POINTS );
01277         glProgramParameteriEXT( progObj, GL_GEOMETRY_OUTPUT_TYPE_EXT,   GL_TRIANGLE_STRIP );
01278         // (At most 5 triangles from Marching Cube) * (PNTriangle Smoothness)
01279         //                                       5  *  10 (smoothness of 4 = 10 vertices per triangle)
01280         // Setting the smoothness to five (# out vertices = 5*15 = 75) seems to be too much 
01281         //   for 320MB 8800 GTS to handle -- i.e. too slow.
01282         // In addition, the rendered surface contains several holes.
01283         // Also setting the # out vertices to 50 (or 55 or 60), 
01284         //   the GLSL shader doesn't render the correct surface.
01285         // It renders a surface with several holes.
01286         // Due to this weird logic error from geometry shader, the # out vertices is set to 64.
01287 
01288         // Check max number of vertices that the geometry shader can output
01289 
01290         #ifdef TAPs_DEBUG_MODE
01291         GLint maxOutNumVertices;
01292         glGetIntegerv( GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT, &maxOutNumVertices );
01293         std::cout << "The maximum number of output vertices that the graphics card's geometry shader can output is " << maxOutNumVertices << ".\n";
01294         // GeForce 8800 GTS's GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT is 1024.
01295         #endif // #ifdef TAPs_DEBUG_MODE
01296 
01297 
01298         // Without PNTriangles
01299         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_PNTRI
01300             #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01301             glProgramParameteriEXT( progObj, GL_GEOMETRY_VERTICES_OUT_EXT, 16 /*15*/ );
01302             #endif
01303             #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_2X2X2_CUBES
01304             glProgramParameteriEXT( progObj, GL_GEOMETRY_VERTICES_OUT_EXT, 16*4 /*15x8*/ );
01305             //glProgramParameteriEXT( progObj, GL_GEOMETRY_VERTICES_OUT_EXT, 256 /*15x8*/ );
01306             #endif
01307             //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_4X4X4_CUBES
01308             //glProgramParameteriEXT( progObj, GL_GEOMETRY_VERTICES_OUT_EXT, 16 /*15x64*/ );
01309             //#endif
01310         #endif
01311 
01312         // With PNTriangles
01313         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_PNTRI
01314         glProgramParameteriEXT( progObj, GL_GEOMETRY_VERTICES_OUT_EXT,  64 /*(15/3)x10*/ );
01315         #endif
01316     }
01317     //---------------------------------------------------------------
01318     if ( !m_glslShaderManager.Link( m_glslProgramObjectWithGlobalIntpValue ) ) {
01319         Clear();
01320         return false;
01321     }
01322     //---------------------------------------------------------------
01323     // END: m_glslProgramObjectWithGlobalIntpValue
01324     //===============================================================
01325 
01326 
01327     //===============================================================
01328     // START: m_glslProgramObjectWithTextureIntpValue
01329     //---------------------------------------------------------------
01330     if ( !m_glslShaderManager.CreateGLSLProgramObject( m_glslProgramObjectWithTextureIntpValue ) ) {
01331         Clear();
01332         return false;
01333     }
01334     //---------------------------------------------------------------
01335     // Vertex Shader
01336     if ( !m_glslShaderManager.AttachShaderFromFile( m_glslProgramObjectWithTextureIntpValue, 
01337         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_PNTRI
01338             // WITH 1X1X1 CUBE
01339             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01340                 // WITH CUBE TRACKING
01341                 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
01342                     // WITH GRADIENT TEXTURE
01343                     #ifdef TAPs_MODEL_DEFORMABLE_USE_GRADIENT_TEXTURE
01344                         //"Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes.vert", GL_VERTEX_SHADER ) ) {
01345                         "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_RdCubeVerticesOnVertShader.vert", GL_VERTEX_SHADER ) ) {
01346                     #endif
01347                     // WITHOUT GRADIENT TEXTURE
01348                     #ifdef TAPs_MODEL_DEFORMABLE_NOT_USE_GRADIENT_TEXTURE
01349                         // WITH SELECTABLE GRADIENT COMPUTATION OPTION
01350                         #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
01351                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes.vert", GL_VERTEX_SHADER ) ) {
01352                         // WITHOUT SELECTABLE GRADIENT COMPUTATION OPTION
01353                         #else
01354                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes.vert", GL_VERTEX_SHADER ) ) {
01355                         #endif
01356                     #endif
01357                 #endif
01358                 // WITHOUT CUBE TRACKING
01359                 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_TRACKING_CUBES
01360                     // WITH GRADIENT TEXTURE
01361                     #ifdef TAPs_MODEL_DEFORMABLE_USE_GRADIENT_TEXTURE
01362                         "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes.vert", GL_VERTEX_SHADER ) ) {
01363                     #endif
01364                     // WITHOUT GRADIENT TEXTURE
01365                     #ifdef TAPs_MODEL_DEFORMABLE_NOT_USE_GRADIENT_TEXTURE
01366                         // WITH SELECTABLE GRADIENT COMPUTATION OPTION
01367                         #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
01368                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes.vert", GL_VERTEX_SHADER ) ) {
01369                         // WITHOUT SELECTABLE GRADIENT COMPUTATION OPTION
01370                         #else
01371                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes.vert", GL_VERTEX_SHADER ) ) {
01372                         #endif
01373                     #endif
01374                 #endif
01375             #endif
01376 
01377             // WITH 2X2X2 CUBE
01378             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_2X2X2_CUBES
01379                 "Resource/Shader/DefModelDrawByGenMesh_2x2x2cubes.vert", GL_VERTEX_SHADER ) ) {
01380             #endif
01381 
01382             // WITH 4X4X4 CUBE
01383             //#ifdef    TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_4X4X4_CUBES
01384             //  "Resource/Shader/DefModelDrawByGenMesh_4x4x4cubes.vert", GL_VERTEX_SHADER ) ) {
01385             //#endif
01386         #endif
01387         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_PNTRI
01388             "Resource/Shader/DefModelDrawByGenMeshWithPNTri.vert", GL_VERTEX_SHADER ) ) {
01389         #endif
01390         Clear();
01391         return false;
01392     }
01393 
01394     //*
01396     // DEBUG
01397     {
01398         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_PNTRI
01399             // WITH 1X1X1 CUBE
01400             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01401                 // WITH CUBE TRACKING
01402                 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
01403                     // WITH GRADIENT TEXTURE
01404                     #ifdef TAPs_MODEL_DEFORMABLE_USE_GRADIENT_TEXTURE
01405                             //std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wTextureIntp_wCubeTracking_wGradTex.geom" << std::endl;
01406                             std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wTextureIntp_wCubeTracking_wGradTex_RdCubeVerticesOnVertShader.geom" << std::endl;
01407                     #endif
01408                     // WITHOUT GRADIENT TEXTURE
01409                     #ifdef TAPs_MODEL_DEFORMABLE_NOT_USE_GRADIENT_TEXTURE
01410                         // WITH SELECTABLE GRADIENT COMPUTATION OPTION
01411                         #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
01412                             std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wTextureIntp_wCubeTracking_wSelectableGradients.geom" << std::endl;
01413                         // WITHOUT SELECTABLE GRADIENT COMPUTATION OPTION
01414                         #else
01415                             std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wTextureIntp_wCubeTracking.geom" << std::endl;
01416                         #endif
01417                     #endif
01418                 #endif
01419                 // WITHOUT CUBE TRACKING
01420                 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_TRACKING_CUBES
01421                     // WITH GRADIENT TEXTURE
01422                     #ifdef TAPs_MODEL_DEFORMABLE_USE_GRADIENT_TEXTURE
01423                         std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wTextureIntp_wGradTex.geom" << std::endl;
01424                     #endif
01425                     // WITHOUT GRADIENT TEXTURE
01426                     #ifdef TAPs_MODEL_DEFORMABLE_NOT_USE_GRADIENT_TEXTURE
01427                         // WITH SELECTABLE GRADIENT COMPUTATION OPTION
01428                         #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
01429                             std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wTextureIntp_wSelectableGradients.geom" << std::endl;
01430                         // WITHOUT SELECTABLE GRADIENT COMPUTATION OPTION
01431                         #else
01432                             std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wTextureIntp.geom" << std::endl;
01433                         #endif
01434                     #endif
01435                 #endif
01436             #endif
01437             // WITH 2X2X2 CUBES
01438             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_2X2X2_CUBES
01439                 std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMesh_2x2x2cubes.geom" << std::endl;
01440             #endif
01441         #endif
01442         // WITH PN-TRIANGLES
01443         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_PNTRI
01444             std::cout << "LOAD: Resource/Shader/DefModelDrawByGenMeshWithPNTri.geom" << std::endl;
01445         #endif
01446     }
01448     //*/
01449 
01450     //---------------------------------------------------------------
01451     // Geometry Shader -- WITH INTERPOLATION VALUE TEXTURE
01452     if ( !m_glslShaderManager.AttachShaderFromFile( m_glslProgramObjectWithTextureIntpValue, 
01453         // WITHOUT PN-TRIANGLES
01454         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_PNTRI
01455             // 1X1X1 CUBE
01456             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01457                 // WITH CUBE TRACKING OPTION
01458                 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
01459                     // WITH GRADIENT TEXTURE
01460                     #ifdef TAPs_MODEL_DEFORMABLE_USE_GRADIENT_TEXTURE
01461                         //"Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wTextureIntp_wCubeTracking_wGradTex.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01462                         "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wTextureIntp_wCubeTracking_wGradTex_RdCubeVerticesOnVertShader.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01463                     #endif
01464                     // WITHOUT GRADIENT TEXTURE
01465                     #ifdef TAPs_MODEL_DEFORMABLE_NOT_USE_GRADIENT_TEXTURE
01466                         // WITH SELECTABLE GRADIENT COMPUTATION OPTION
01467                         #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
01468                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wTextureIntp_wCubeTracking_wSelectableGradients.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01469                         // WITHOUT SELECTABLE GRADIENT COMPUTATION OPTION
01470                         #else
01471                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wTextureIntp_wCubeTracking.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01472                         #endif
01473                     #endif
01474                 #endif
01475                 // WITHOUT CUBE TRACKING OPTION
01476                 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_TRACKING_CUBES
01477                     // WITH GRADIENT TEXTURE
01478                     #ifdef TAPs_MODEL_DEFORMABLE_USE_GRADIENT_TEXTURE
01479                         "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wTextureIntp_wGradTex.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01480                     #endif
01481                     // WITHOUT GRADIENT TEXTURE
01482                     #ifdef TAPs_MODEL_DEFORMABLE_NOT_USE_GRADIENT_TEXTURE
01483                         // WITH SELECTABLE GRADIENT COMPUTATION OPTION
01484                         #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
01485                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wTextureIntp_wSelectableGradients.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01486                         // WITHOUT SELECTABLE GRADIENT COMPUTATION OPTION
01487                         #else
01488                             "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes_wTextureIntp.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01489                         #endif
01490                     #endif
01491                 #endif
01492             #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01493             // 2X2X2 CUBES
01494             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_2X2X2_CUBES
01495                 "Resource/Shader/DefModelDrawByGenMesh_2x2x2cubes.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01496             #endif
01497         #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_PNTRI
01498         // WITH PN-TRIANGLES
01499         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_PNTRI
01500             "Resource/Shader/DefModelDrawByGenMeshWithPNTri.geom", GL_GEOMETRY_SHADER_EXT ) ) {
01501         #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_PNTRI
01502         Clear();
01503         return false;
01504     }
01505     //---------------------------------------------------------------
01506     // Fragment Shader
01507     if ( !m_glslShaderManager.AttachShaderFromFile( m_glslProgramObjectWithTextureIntpValue, 
01508         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_PNTRI
01509             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01510                 "Resource/Shader/DefModelDrawByGenMesh_1x1x1cubes.frag", GL_FRAGMENT_SHADER ) ) {
01511             #endif
01512             #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_2X2X2_CUBES
01513                 "Resource/Shader/DefModelDrawByGenMesh_2x2x2cubes.frag", GL_FRAGMENT_SHADER ) ) {
01514             #endif
01515             //#ifdef    TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_4X4X4_CUBES
01516             //  "Resource/Shader/DefModelDrawByGenMesh_4x4x4cubes.frag", GL_FRAGMENT_SHADER ) ) {
01517             //#endif
01518         #endif
01519         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_PNTRI
01520             "Resource/Shader/DefModelDrawByGenMeshWithPNTri.frag", GL_FRAGMENT_SHADER ) ) {
01521         #endif
01522         Clear();
01523         return false;
01524     }
01525     //---------------------------------------------------------------
01526     // Setup Geometry Shader for Marching Cube
01527     // Since Geometry Shader needs to specify input and output types 
01528     // and the number of maximum out/emitted vertices.
01529     {
01530         GLuint progObj = m_glslProgramObjectWithTextureIntpValue->GetProgramObject();
01531         glProgramParameteriEXT( progObj, GL_GEOMETRY_INPUT_TYPE_EXT,    GL_POINTS );
01532         glProgramParameteriEXT( progObj, GL_GEOMETRY_OUTPUT_TYPE_EXT,   GL_TRIANGLE_STRIP );
01533         // (At most 5 triangles from Marching Cube) * (PNTriangle Smoothness)
01534         //                                       5  *  10 (smoothness of 4 = 10 vertices per triangle)
01535         // Setting the smoothness to five (# out vertices = 5*15 = 75) seems to be too much 
01536         //   for 320MB 8800 GTS to handle -- i.e. too slow.
01537         // In addition, the rendered surface contains several holes.
01538         // Also setting the # out vertices to 50 (or 55 or 60), 
01539         //   the GLSL shader doesn't render the correct surface.
01540         // It renders a surface with several holes.
01541         // Due to this weird logic error from geometry shader, the # out vertices is set to 64.
01542 
01543         // Check max number of vertices that the geometry shader can output
01544 
01545         #ifdef TAPs_DEBUG_MODE
01546         GLint maxOutNumVertices;
01547         glGetIntegerv( GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT, &maxOutNumVertices );
01548         std::cout << "The maximum number of output vertices that the graphics card's geometry shader can output is " << maxOutNumVertices << ".\n";
01549         // GeForce 8800 GTS's GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT is 1024.
01550         #endif // #ifdef TAPs_DEBUG_MODE
01551 
01552 
01553         // Without PNTriangles
01554         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITHOUT_PNTRI
01555             #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01556             glProgramParameteriEXT( progObj, GL_GEOMETRY_VERTICES_OUT_EXT, 16 /*15*/ );
01557             #endif
01558             #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_2X2X2_CUBES
01559             glProgramParameteriEXT( progObj, GL_GEOMETRY_VERTICES_OUT_EXT, 16*4 /*15x8*/ );
01560             //glProgramParameteriEXT( progObj, GL_GEOMETRY_VERTICES_OUT_EXT, 256 /*15x8*/ );
01561             #endif
01562             //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_4X4X4_CUBES
01563             //glProgramParameteriEXT( progObj, GL_GEOMETRY_VERTICES_OUT_EXT, 16 /*15x64*/ );
01564             //#endif
01565         #endif
01566 
01567         // With PNTriangles
01568         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_PNTRI
01569         glProgramParameteriEXT( progObj, GL_GEOMETRY_VERTICES_OUT_EXT,  64 /*(15/3)x10*/ );
01570         #endif
01571     }
01572     //---------------------------------------------------------------
01573     if ( !m_glslShaderManager.Link( m_glslProgramObjectWithTextureIntpValue ) ) {
01574         Clear();
01575         return false;
01576     }
01577     //---------------------------------------------------------------
01578     // END: m_glslProgramObjectWithTextureIntpValue
01579     //===============================================================
01580 
01581     return true;
01582 }
01583 //-----------------------------------------------------------------------------
01584 // END: SetupShadersForDrawing
01585 //=============================================================================
01586 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_RENDERING
01587 
01588 
01589 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
01590 //=============================================================================
01591 // Setup Shaders for Update Cube Tracking Texture
01592 //-----------------------------------------------------------------------------
01593 template <typename T>
01594 bool ModelDeformableGLSL_Visualization_RTGenMesh<T>::
01595 SetupShadersForUpdateCubeTrackingTexture ()
01596 {
01597     //---------------------------------------------------------------
01598     if ( !m_glslShaderManager.CreateGLSLProgramObject( m_glslProgramObjectUpdateCubeTracking ) ) {
01599         Clear();
01600         return false;
01601     }
01602     //---------------------------------------------------------------
01603     // Fragment Shader
01604     if ( !m_glslShaderManager.AttachShaderFromFile( m_glslProgramObjectUpdateCubeTracking, 
01605         #ifdef  TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01606             "Resource/Shader/DefModelDrawByGenMesh_UpdateCubeTracking_1x1x1cubes.frag", GL_FRAGMENT_SHADER ) ) {
01607         #endif
01608         Clear();
01609         return false;
01610     }
01611     //---------------------------------------------------------------
01612     if ( !m_glslShaderManager.Link( m_glslProgramObjectUpdateCubeTracking ) ) {
01613         Clear();
01614         return false;
01615     }
01616     //---------------------------------------------------------------
01617     return true;
01618 }
01619 //-----------------------------------------------------------------------------
01620 // END: SetupShadersForUpdateCubeTrackingTexture
01621 //=============================================================================
01622 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
01623 
01624 
01625 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
01626 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01627 #ifdef TAPs_DEFORMABLE_MODEL_VISUALIZATION_GPU_MARCHING_CUBES_SUBCUBE_BY_TWO
01628 //=============================================================================
01629 // Vertex Buffer for Rendering
01630 //-----------------------------------------------------------------------------
01631 template <typename T>
01632 bool ModelDeformableGLSL_Visualization_RTGenMesh<T>::
01633 GenerateTheVBOForTextureCoordinates ( Vector3<int> const & vGridSize )
01634 {
01635     std::cout << "TAPs_DEFORMABLE_MODEL_VISUALIZATION_GPU_MARCHING_CUBES_SUBCUBE_BY_TWO" << std::endl;
01636 
01637     assert( m_vboVerticesAsTexCoords == 0 );
01638     //-------------------------------------------
01639     // Reduce the grid size by one, 
01640     // since needed only the # of cubes for marching cube.
01641     //-------------------------------------------
01642     // Process 1x1x1 Cubes as a group
01643     Vector3<int> newGridSize( vGridSize[0]*2, vGridSize[1]*2, vGridSize[2]*2 );
01644     Vector3<int> v1x1x1NumBlocks( newGridSize[0]*2 - 1, newGridSize[1]*2 - 1, newGridSize[2]*2 - 1 );
01645     m_uiTotalCubes = v1x1x1NumBlocks[0] * v1x1x1NumBlocks[1] * v1x1x1NumBlocks[2];
01646     glGenBuffers( 1, &m_vboVerticesAsTexCoords );
01647     if ( m_vboVerticesAsTexCoords == 0 ) return false;
01648     //-----------------------------------------------------
01649     glBindBuffer( GL_ARRAY_BUFFER, m_vboVerticesAsTexCoords );
01650     glBufferData( GL_ARRAY_BUFFER, sizeof( GLfloat)*m_uiTotalCubes*3, NULL, GL_STATIC_DRAW );
01651     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01652     float * vertices = static_cast<float *>( glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY ) );
01653     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01654     //-----------------------------------------------------
01655     // Grid size is #ofBlocks + 1
01656     // Divided by vGridSize so that the texture coordinates is of 
01657     // the first vertex (#0) of the cube
01658     Vector3<float> texCoordStep( 
01659         1.0 / newGridSize[0], 1.0 / newGridSize[1], 1.0 / newGridSize[2] );
01660     Vector3<float> minTexCoord( 
01661         texCoordStep[0] / 2.0, texCoordStep[1] / 2.0, texCoordStep[2] / 2.0 );
01662     int idx = 0;
01663     float Z = minTexCoord[2];
01664 
01665     //*
01666     //-------------------------------------------
01667     for ( int z = 0; z < v1x1x1NumBlocks[2]; ++z ) {
01668         float Y = minTexCoord[1];
01669         for ( int y = 0; y < v1x1x1NumBlocks[1]; ++y ) {
01670             float X = minTexCoord[0];
01671             for ( int x = 0; x < v1x1x1NumBlocks[0]; ++x ) {
01672                 vertices[ idx++ ] = X;
01673                 vertices[ idx++ ] = Y;
01674                 vertices[ idx++ ] = Z;
01675                 //std::cout << "VBO for Rendering: " 
01676                 //  << X << ", " 
01677                 //  << Y << ", " 
01678                 //  << Z << "\n";
01679                 X += texCoordStep[0];
01680             }
01681             Y += texCoordStep[1];
01682         }
01683         Z += texCoordStep[2];
01684     }
01685     //-----------------------------------------------------
01686     //*/
01687 
01688     /*
01689     //-----------------------------------------------------
01690     // Swizzled Walk
01691     //
01692     //    +---+---+---+---+---+
01693     //    | 0 | 0 | 0 | 0 | 0 |
01694     //  --+---+---+---+---+---+
01695     //    | 1 | 2 | 1 | 2 | 0 |
01696     //    +---+---+---+---+---+
01697     //    | 0 | 3 | 0 | 3 | 0 |
01698     //  --+---+---+---+---+---+
01699     //    | 1 | 2 | 1 | 2 | 0 |
01700     //    +---+---+---+---+---+
01701     //    | 0 | 3 | 0 | 3 | 0 |
01702     //  --+---+---+---+---+---+
01703     //    |       |       |
01704     //
01705     for ( int z = 0; z < v1x1x1NumBlocks[2]; ++z ) {
01706         float Y = minTexCoord[1];
01707         for ( int y = 0; y < v1x1x1NumBlocks[1]; y+=2 ) {
01708             float X = minTexCoord[0];
01709             for ( int x = 0; x < v1x1x1NumBlocks[0]; x+=2 ) {
01710                 if ( x < v1x1x1NumBlocks[0]-1 && y < v1x1x1NumBlocks[1]-1 )
01711                 {
01712                     T X1 = X + texCoordStep[0];
01713                     T Y1 = Y + texCoordStep[1];
01714                     // 0
01715                     vertices[ idx++ ] = X;
01716                     vertices[ idx++ ] = Y;
01717                     vertices[ idx++ ] = Z;
01718                     // 1
01719                     vertices[ idx++ ] = X;
01720                     vertices[ idx++ ] = Y1;
01721                     vertices[ idx++ ] = Z;
01722                     // 2
01723                     vertices[ idx++ ] = X1;
01724                     vertices[ idx++ ] = Y1;
01725                     vertices[ idx++ ] = Z;
01726                     // 3
01727                     vertices[ idx++ ] = X1;
01728                     vertices[ idx++ ] = Y;
01729                     vertices[ idx++ ] = Z;
01730                 }
01731                 else {
01732                     vertices[ idx++ ] = X;
01733                     vertices[ idx++ ] = Y;
01734                     vertices[ idx++ ] = Z;
01735                 }
01736                 X += texCoordStep[0] + texCoordStep[0];
01737             }
01738             Y += texCoordStep[1] * texCoordStep[1];
01739         }
01740         Z += texCoordStep[2];
01741     }
01742     //-----------------------------------------------------
01743     //*/
01744 
01745     //-----------------------------------------------------
01746     glUnmapBuffer( GL_ARRAY_BUFFER );
01747     glBindBuffer( GL_ARRAY_BUFFER, 0 );
01748     //-----------------------------------------------------
01749     return true;
01750 }
01751 //-----------------------------------------------------------------------------
01752 // END: GenerateTheVBOForTextureCoordinates
01753 //=============================================================================
01754 
01755 #else // TAPs_DEFORMABLE_MODEL_VISUALIZATION_GPU_MARCHING_CUBES_SUBCUBE_BY_TWO is not defined
01756 
01757 //=============================================================================
01758 // Vertex Buffer for Rendering
01759 //-----------------------------------------------------------------------------
01760 template <typename T>
01761 bool ModelDeformableGLSL_Visualization_RTGenMesh<T>::
01762 GenerateTheVBOForTextureCoordinates ( Vector3<int> const & vGridSize )
01763 {
01764     assert( m_vboVerticesAsTexCoords == 0 );
01765 
01766     const int iNumComps = 4;
01767     //-------------------------------------------
01768     // Reduce the grid size by one, 
01769     // since needed only the # of cubes for marching cube.
01770     //-------------------------------------------
01771     // Process 1x1x1 Cubes as a group
01772     Vector3<int> v1x1x1NumBlocks( 
01773         vGridSize[0]-1, 
01774         vGridSize[1]-1, 
01775         vGridSize[2]-1 
01776     );
01777     m_uiTotalCubes = v1x1x1NumBlocks[0] * v1x1x1NumBlocks[1] * v1x1x1NumBlocks[2];
01778     glGenBuffers( 1, &m_vboVerticesAsTexCoords );
01779     if ( m_vboVerticesAsTexCoords == 0 ) return false;
01780     //-----------------------------------------------------
01781     glBindBuffer( GL_ARRAY_BUFFER, m_vboVerticesAsTexCoords );
01782     glBufferData( GL_ARRAY_BUFFER, sizeof( GLfloat)*m_uiTotalCubes*iNumComps, NULL, GL_STATIC_DRAW );
01783     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01784     float * vertices = static_cast<float *>( glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY ) );
01785     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01786     //-----------------------------------------------------
01787     // Grid size is #ofBlocks + 1
01788     // Divided by vGridSize so that the texture coordinates is of 
01789     // the first vertex (#0) of the cube
01790     Vector3<float> texCoordStep( 
01791         1.0 / vGridSize[0], 1.0 / vGridSize[1], 1.0 / vGridSize[2] );
01792     Vector3<float> minTexCoord( 
01793         texCoordStep[0] / 2.0, texCoordStep[1] / 2.0, texCoordStep[2] / 2.0 );
01794     int idx = 0;
01795     float Z = minTexCoord[2];
01796 
01797     //*
01798     //-------------------------------------------
01799     for ( int z = 0; z < v1x1x1NumBlocks[2]; ++z ) {
01800         float Y = minTexCoord[1];
01801         for ( int y = 0; y < v1x1x1NumBlocks[1]; ++y ) {
01802             float X = minTexCoord[0];
01803             for ( int x = 0; x < v1x1x1NumBlocks[0]; ++x ) {
01804                 vertices[ idx++ ] = X;
01805                 vertices[ idx++ ] = Y;
01806                 vertices[ idx++ ] = Z;
01807                 if ( iNumComps == 4 ) {
01808                     if ( z > v1x1x1NumBlocks[2] / 2 ) {
01809                         vertices[ idx++ ] = 0.0;
01810                     }
01811                     else {
01812                         vertices[ idx++ ] = 1.0;
01813                     }
01814                 }
01815                 //std::cout << "VBO for Rendering: " 
01816                 //  << X << ", " 
01817                 //  << Y << ", " 
01818                 //  << Z << "\n";
01819                 X += texCoordStep[0];
01820             }
01821             Y += texCoordStep[1];
01822         }
01823         Z += texCoordStep[2];
01824     }
01825     //-----------------------------------------------------
01826     //*/
01827 
01828     /*
01829     //-----------------------------------------------------
01830     // Swizzled Walk
01831     //
01832     //    +---+---+---+---+---+
01833     //    | 0 | 0 | 0 | 0 | 0 |
01834     //  --+---+---+---+---+---+
01835     //    | 1 | 2 | 1 | 2 | 0 |
01836     //    +---+---+---+---+---+
01837     //    | 0 | 3 | 0 | 3 | 0 |
01838     //  --+---+---+---+---+---+
01839     //    | 1 | 2 | 1 | 2 | 0 |
01840     //    +---+---+---+---+---+
01841     //    | 0 | 3 | 0 | 3 | 0 |
01842     //  --+---+---+---+---+---+
01843     //    |       |       |
01844     //
01845     for ( int z = 0; z < v1x1x1NumBlocks[2]; ++z ) {
01846         float Y = minTexCoord[1];
01847         for ( int y = 0; y < v1x1x1NumBlocks[1]; y+=2 ) {
01848             float X = minTexCoord[0];
01849             for ( int x = 0; x < v1x1x1NumBlocks[0]; x+=2 ) {
01850                 if ( x < v1x1x1NumBlocks[0]-1 && y < v1x1x1NumBlocks[1]-1 )
01851                 {
01852                     T X1 = X + texCoordStep[0];
01853                     T Y1 = Y + texCoordStep[1];
01854                     // 0
01855                     vertices[ idx++ ] = X;
01856                     vertices[ idx++ ] = Y;
01857                     vertices[ idx++ ] = Z;
01858                     // 1
01859                     vertices[ idx++ ] = X;
01860                     vertices[ idx++ ] = Y1;
01861                     vertices[ idx++ ] = Z;
01862                     // 2
01863                     vertices[ idx++ ] = X1;
01864                     vertices[ idx++ ] = Y1;
01865                     vertices[ idx++ ] = Z;
01866                     // 3
01867                     vertices[ idx++ ] = X1;
01868                     vertices[ idx++ ] = Y;
01869                     vertices[ idx++ ] = Z;
01870                 }
01871                 else {
01872                     vertices[ idx++ ] = X;
01873                     vertices[ idx++ ] = Y;
01874                     vertices[ idx++ ] = Z;
01875                 }
01876                 X += texCoordStep[0] * texCoordStep[0];
01877             }
01878             Y += texCoordStep[1] * texCoordStep[1];
01879         }
01880         Z += texCoordStep[2];
01881     }
01882     //-----------------------------------------------------
01883     //*/
01884 
01885     glUnmapBuffer( GL_ARRAY_BUFFER );
01886     glBindBuffer( GL_ARRAY_BUFFER, 0 );
01887     //-----------------------------------------------------
01888     return true;
01889 }
01890 //-----------------------------------------------------------------------------
01891 // END: GenerateTheVBOForTextureCoordinates
01892 //=============================================================================
01893 #endif // TAPs_DEFORMABLE_MODEL_VISUALIZATION_GPU_MARCHING_CUBES_SUBCUBE_BY_TWO
01894 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01895 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
01896 
01897 
01898 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
01899 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01900 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
01901 //=============================================================================
01902 // Vertex Buffer for Cube Tracking
01903 //-----------------------------------------------------------------------------
01904 template <typename T>
01905 bool ModelDeformableGLSL_Visualization_RTGenMesh<T>::
01906 GenerateTheVBOForTexCoordForCubeTracking ( Vector3<int> const & vGridSize )
01907 {
01908     assert( m_vboTexCoordForCubeTracking == 0 );
01909     //-------------------------------------------
01910     // Reduce the grid size by one, 
01911     // since needed only the # of cubes for marching cube.
01912     //-------------------------------------------
01913     // Process 1x1x1 Cubes as a group
01914     Vector3<int> v1x1x1NumBlocks( 
01915         vGridSize[0]-1, 
01916         vGridSize[1]-1, 
01917         vGridSize[2]-1 
01918     );
01919     m_uiTotalCubes = v1x1x1NumBlocks[0] * v1x1x1NumBlocks[1] * v1x1x1NumBlocks[2];
01920     glGenBuffers( 1, &m_vboTexCoordForCubeTracking );
01921     if ( m_vboTexCoordForCubeTracking == 0 ) return false;
01922     //-----------------------------------------------------
01923     glBindBuffer( GL_ARRAY_BUFFER, m_vboTexCoordForCubeTracking );
01924     glBufferData( GL_ARRAY_BUFFER, sizeof( GLfloat)*m_uiTotalCubes*3, NULL, GL_STATIC_DRAW );
01925     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01926     float * vertices = static_cast<float *>( glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY ) );
01927     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01928     //-----------------------------------------------------
01929     // Grid size is #ofBlocks + 1
01930     // Divided by vGridSize so that the texture coordinates is of 
01931     // the first vertex (#0) of the cube
01932     Vector3<float> texCoordStep( 
01933         1.0 / v1x1x1NumBlocks[0], 1.0 / v1x1x1NumBlocks[1], 1.0 / v1x1x1NumBlocks[2] );
01934     Vector3<float> minTexCoord( 
01935         texCoordStep[0] / 2.0, texCoordStep[1] / 2.0, texCoordStep[2] / 2.0 );
01936     int idx = 0;
01937     float Z = minTexCoord[2];
01938     //-------------------------------------------
01939     for ( int z = 0; z < v1x1x1NumBlocks[2]; ++z ) {
01940         float Y = minTexCoord[1];
01941         for ( int y = 0; y < v1x1x1NumBlocks[1]; ++y ) {
01942             float X = minTexCoord[0];
01943             for ( int x = 0; x < v1x1x1NumBlocks[0]; ++x ) {
01944                 vertices[ idx++ ] = X;
01945                 vertices[ idx++ ] = Y;
01946                 vertices[ idx++ ] = Z;
01947                 /*/
01948                 std::cout << "VBO for Rendering: " 
01949                     << X << ", " 
01950                     << Y << ", " 
01951                     << Z << "\n";
01952                 //*/
01953                 X += texCoordStep[0];
01954             }
01955             Y += texCoordStep[1];
01956         }
01957         Z += texCoordStep[2];
01958     }
01959     //-----------------------------------------------------
01960     glUnmapBuffer( GL_ARRAY_BUFFER );
01961     glBindBuffer( GL_ARRAY_BUFFER, 0 );
01962     //-----------------------------------------------------
01963     return true;
01964 }
01965 //-----------------------------------------------------------------------------
01966 // END: GenerateTheVBOForTexCoordForCubeTracking
01967 //=============================================================================
01968 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
01969 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
01970 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
01971 
01972 
01973 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
01974 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_2X2X2_CUBES
01975 //=============================================================================
01976 // Vertex Buffer for Rendering
01977 //-----------------------------------------------------------------------------
01978 template <typename T>
01979 bool ModelDeformableGLSL_Visualization_RTGenMesh<T>::
01980 GenerateTheVBOForTextureCoordinates ( Vector3<int> const & vGridSize )
01981 {
01982     assert( m_vboVerticesAsTexCoords == 0 );
01983     //-------------------------------------------
01984     // Reduce the grid size by one, 
01985     // since needed only the # of cubes for marching cube.
01986     // Process 2x2x2 Cubes as a group
01987     Vector3<int> v2x2x2NumBlocks( 
01988         ceil( (vGridSize[0]-1)/2.0 ), 
01989         ceil( (vGridSize[1]-1)/2.0 ), 
01990         ceil( (vGridSize[2]-1)/2.0 ) 
01991     );
01992 
01993     //wxLogError( wxT("#Blocks: %i %i %i"), v2x2x2NumBlocks[0], v2x2x2NumBlocks[1], v2x2x2NumBlocks[2] );
01994 
01995     m_uiTotalCubes = v2x2x2NumBlocks[0] * v2x2x2NumBlocks[1] * v2x2x2NumBlocks[2];
01996     glGenBuffers( 1, &m_vboVerticesAsTexCoords );
01997     if ( m_vboVerticesAsTexCoords == 0 ) return false;
01998     //-----------------------------------------------------
01999     glBindBuffer( GL_ARRAY_BUFFER, m_vboVerticesAsTexCoords );
02000     glBufferData( GL_ARRAY_BUFFER, sizeof( GLfloat)*m_uiTotalCubes*3, NULL, GL_STATIC_DRAW );
02001     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02002     float * vertices = static_cast<float *>( glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY ) );
02003     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02004     //-----------------------------------------------------
02005     // Grid size is #ofBlocks + 1
02006     // Divided by (v2x2x2NumBlocks[0]+1) so that the texture coordinates is of 
02007     // the first vertex (#0) of the cube
02008 
02009     // The texture is from 0 to 1 (0 - gridsize)
02010     Vector3<float> texCoordStep( 
02011         2.0 / vGridSize[0], 2.0 / vGridSize[1], 2.0 / vGridSize[2] );
02012     Vector3<float> minTexCoord( 
02013         0.5 / vGridSize[0], 0.5 / vGridSize[1], 0.5 / vGridSize[2] );
02014     int idx = 0;
02015     float Z = minTexCoord[2];
02016     //-------------------------------------------
02017     // Reduce the grid size by one, 
02018     // since needed only the # of cubes for marching cube.
02019     for ( int z = 0; z < v2x2x2NumBlocks[2]; ++z ) {
02020         float Y = minTexCoord[1];
02021         for ( int y = 0; y < v2x2x2NumBlocks[1]; ++y ) {
02022             float X = minTexCoord[0];
02023             for ( int x = 0; x < v2x2x2NumBlocks[0]; ++x ) {
02024                 vertices[ idx++ ] = X;
02025                 vertices[ idx++ ] = Y;
02026                 vertices[ idx++ ] = Z;
02027                 /*/
02028                 std::cout << "VBO for Rendering: " 
02029                     << X << ", " 
02030                     << Y << ", " 
02031                     << Z << "\n";
02032                 //*/
02033                 X += texCoordStep[0];
02034             }
02035             Y += texCoordStep[1];
02036         }
02037         Z += texCoordStep[2];
02038     }
02039     //-----------------------------------------------------
02040     glUnmapBuffer( GL_ARRAY_BUFFER );
02041     glBindBuffer( GL_ARRAY_BUFFER, 0 );
02042     //-----------------------------------------------------
02043     return true;
02044 }
02045 //-----------------------------------------------------------------------------
02046 // END: GenerateTheVBOForTextureCoordinates
02047 //=============================================================================
02048 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_2X2X2_CUBES
02049 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
02050 
02051 
02052 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_RENDERING
02053 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
02054 //*****************************************************************************
02055 // BEGIN: GenerateBufferObjects
02056 //-----------------------------------------------------------------------------
02057 template <typename T>
02058 bool ModelDeformableGLSL_Visualization_RTGenMesh<T>::GenerateBufferObjects ()
02059 {
02060     //===============================================================
02061     // Framebuffer Object(s)
02062     //---------------------------------------------------------------
02063     //-----------------------
02064     // For Cube Tracking
02065     //-----------------------
02066     glGenFramebuffersEXT( 1, &m_glslFBO_CubeTracking );
02067     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_glslFBO_CubeTracking );
02068     glFramebufferTexture3DEXT(
02069         GL_FRAMEBUFFER_EXT,         // target
02070         GL_COLOR_ATTACHMENT0_EXT,   // attached frame
02071         GL_TEXTURE_3D,              // texture target
02072         CubeTrackingTexture,        // texture id
02073         0,                          // level
02074         0                           // zoffset (for 3D texture)
02075     );
02076     //glDrawBuffer( GL_COLOR_ATTACHMENT0_EXT );
02077     TAPs::OpenGL::Fn::CHECK_FRAMEBUFFER_STATUS();
02078     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
02079     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02080     //-----------------------
02081     //---------------------------------------------------------------
02082     //===============================================================
02083     return true;
02084 }
02085 //-----------------------------------------------------------------------------
02086 // END: GenerateBufferObjects
02087 //*****************************************************************************
02088 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
02089 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_RENDERING
02090 
02091 
02092 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_RENDERING
02093 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
02094 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
02095 //*****************************************************************************
02096 // BEGIN: UpdateCubeTrackingTexture
02097 //-----------------------------------------------------------------------------
02098 template <typename T>
02099 void ModelDeformableGLSL_Visualization_RTGenMesh<T>::UpdateCubeTrackingTexture ()
02100 {
02106     //---------------------------------------------------------------
02107 #ifdef TAPs_DEBUG_MODE
02108     if ( !m_SimDomain ) return;
02109     //---------------------------------------------------------------
02110     if ( !m_SimDomain->RetObject3DTexturePosition() )   return;
02111     //---------------------------------------------------------------
02112 #endif
02113 
02114     //UpdateCollisionDetectionParameters();
02115     //---------------------------------------------------------------
02116     //===============================================================
02117     // Get 3D texture for positions
02118     //---------------------------------------------------------------
02119     Texture * texture3DPosition = m_SimDomain->RetObject3DTexturePosition();
02120     //---------------------------------------------------------------
02121     //===============================================================
02122     texture3DPosition->EnableTextureTarget();
02123     m_glslProgramObjectUpdateCubeTracking->BeginGLSL();
02124     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_glslFBO_CubeTracking );
02125     TAPs::OpenGL::Fn::CHECK_FRAMEBUFFER_STATUS();
02126     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02127     glPushAttrib( GL_ALL_ATTRIB_BITS );
02128     {
02129         //-------------------------------------------------
02130         // Send Stream of Data (one 2D texture at a time in a for loop)
02131         glMatrixMode( GL_PROJECTION );
02132         glPushMatrix();
02133         glLoadIdentity();
02134         //
02135         Vector3<T> offsetCoords = m_SimDomain->GetTexCoordOffsetForPosition();
02136         T offsetRCoord = offsetCoords[2] / 2.0;
02137         T maxCoordS = 1.0 - offsetCoords[0];
02138         T maxCoordT = 1.0 - offsetCoords[1];
02139         //
02140         // Set texture coordinates -- xMin, xMax, yMin and yMax
02141         // In this case, they are      0,    1,    0,  and  1.
02142         //gluOrtho2D( 0, texture3DPosition->GetMaxCoordS(), 
02143         //          0, texture3DPosition->GetMaxCoordT() );
02144         gluOrtho2D( 0, maxCoordS, 0, maxCoordT );
02145         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02146         //
02147         glMatrixMode( GL_MODELVIEW );
02148         glPushMatrix();
02149         glLoadIdentity();
02150         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02151         //
02152         // Generate pixels -- xMin, xMax, yMin, and yMax
02153         // One less due to the cube size.
02154         glViewport( 0, 0, texture3DPosition->GetWidth()-1, 
02155                           texture3DPosition->GetHeight()-1 );
02156         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02157         //
02158         //=================================================
02159         //-------------------------------------------------
02160         // Uniform Variables
02161         //-------------------------------------------------
02162         // Threshold
02163         m_glslProgramObjectUpdateCubeTracking->SetUniform1f( "threshold", m_SimDomain->IC_Thresholds::ON_BOUNDARY );
02164         //---------------------------------
02165         // Coordinate Offsets
02166         m_glslProgramObjectUpdateCubeTracking->SetUniform3f( 
02167             "offsetCoordsPosition", 
02168             offsetCoords[0], offsetCoords[1], offsetCoords[2] 
02169         );
02170         //-------------------------------------------------
02171         texture3DPosition->EnableTextureTarget();
02172         //-------------------------------------------------
02173         // Set PositionTexture to Texture Unit 0
02174         glActiveTexture( GL_TEXTURE0 );
02175         texture3DPosition->BindTexture(0);
02176         m_glslProgramObjectUpdateCubeTracking->SetUniform1i( "PositionTexture", 0 );
02177         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02178         //-------------------------------------------------
02179         //=================================================
02180         // The size is m_vGridResolution[2]-1, due to the cube size is one less.
02181         for ( int i = 0; i < texture3DPosition->GetDepth()-1; ++i ) {
02182             //-------------------------------------------------
02183             glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_glslFBO_CubeTracking );
02184             glFramebufferTexture3DEXT(
02185                 GL_FRAMEBUFFER_EXT,         // target
02186                 GL_COLOR_ATTACHMENT0_EXT,   // attached frame
02187                 GL_TEXTURE_3D,              // texture target
02188                 CubeTrackingTexture,        // texture id -- next position
02189                 0,                          // level
02190                 i                           // zoffset
02191             );
02192             glDrawBuffer( GL_COLOR_ATTACHMENT0_EXT );
02193 
02194             // CURRENTLY
02195             // The GLSL's texture3D( isampler3D sampler, vec3 coords ) 
02196             // doesn't return the correct value for integer
02197             // The GLSL's texture3D( usampler3D sampler, vec3 coords ) 
02198             // doesn't return the correct value for unsinged integer
02199             // So GL_ALPHA8 is used instead!
02200             // So glBindFragDataLocationEXT is not used!
02201             // CURRENTLY
02202             // GL_ALPHA8 DOESN'T WORK EITHER SO BACK TO GL_ALPHA16UI_EXT
02203             // So use the below statement for writing to integer texture
02204 
02205             // New fn for writing unsigned int from fragment shader to GL_COLOR_ATTACHMENT location
02206             //void BindFragDataLocationEXT(uint program, uint colorNumber, const char *name);
02207             glBindFragDataLocationEXT( m_glslProgramObjectUpdateCubeTracking->GetProgramObject(), 0, "fragData0" );
02208 
02209             TAPs::OpenGL::Fn::CHECK_FRAMEBUFFER_STATUS();
02210             //-------------------------------------------------
02211             // Send data stream to GPU
02212             TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02213             //
02214             glClearColor( 0, 0, 0, 0 );
02215             //glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
02216             glClear( GL_COLOR_BUFFER_BIT );
02217             //
02218             T coordR = offsetCoords[2]*i + offsetRCoord;
02219             glPolygonMode( GL_FRONT, GL_FILL );
02220             glBegin( GL_QUADS );
02221                 glTexCoord3f( 0,            0,          coordR );
02222                 glVertex3f  ( 0,            0,          0 );
02223                 glTexCoord3f( maxCoordS,    0,          coordR );
02224                 glVertex3f  ( maxCoordS,    0,          0 );
02225                 glTexCoord3f( maxCoordS,    maxCoordT,  coordR );
02226                 glVertex3f  ( maxCoordS,    maxCoordT,  0 );
02227                 glTexCoord3f( 0,            maxCoordT,  coordR );
02228                 glVertex3f  ( 0,            maxCoordT,  0 );
02229             glEnd();
02230         }
02231         glPopMatrix();
02232         glMatrixMode( GL_PROJECTION );
02233         glPopMatrix();
02234         glMatrixMode( GL_MODELVIEW );
02235     }
02236     glPopAttrib();
02237     texture3DPosition->DisableTextureTarget();
02238     texture3DPosition->UnbindTexture();
02239     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
02240     //
02241     m_glslProgramObjectUpdateCubeTracking->EndGLSL();
02242     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02243     //---------------------------------------------------------------
02244 
02245 
02246     /*
02247     // DEBUG
02248     {
02249         Vector3<int> vCubes( 
02250             texture3DPosition->GetWidth()-1, 
02251             texture3DPosition->GetHeight()-1, 
02252             texture3DPosition->GetDepth()-1 
02253         );
02254         int iTotalCubes = vCubes[0] * vCubes[1] * vCubes[2];
02255         //GLubyte * pCubeValData = new GLubyte[ iTotalCubes * 2 ];  // there's one more read element per row!  Why? How to fix it?
02256         GLint * pCubeValData = new GLint[ iTotalCubes ];
02257         glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
02258         glBindTexture( GL_TEXTURE_3D, CubeTrackingTexture );
02259 
02260         // CURRENTLY, GL_ALPHA8UI_EXT is not working correctly.
02261         // Due to bugs with unsigned int in geometry shader compiler/driver.
02262         // So GL_ALPHA16I_EXT is used instead!
02263         //glGetTexImage( GL_TEXTURE_3D, 0, GL_ALPHA_INTEGER_EXT, GL_UNSIGNED_INT, pCubeValData );
02264         //glGetTexImage( GL_TEXTURE_3D, 0, GL_ALPHA_INTEGER_EXT, GL_INT, pCubeValData );
02265         // CURRENTLY
02266         // The GLSL's texture3D( isampler3D sampler, vec3 coords ) 
02267         // doesn't return the correct value for integer
02268         // The GLSL's texture3D( usampler3D sampler, vec3 coords ) 
02269         // doesn't return the correct value for unsinged integer
02270         // So GL_ALPHA8 is used instead!
02271         //glGetTexImage( GL_TEXTURE_3D, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pCubeValData );
02272         // CURRENTLY
02273         // GL_ALPHA8 DOESN'T WORK EITHER SO BACK TO GL_ALPHA16UI_EXT
02274         glGetTexImage( GL_TEXTURE_3D, 0, GL_ALPHA_INTEGER_EXT, GL_INT, pCubeValData );
02275 
02276         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02277         std::cout << "-------------------------------\n";
02278         std::cout << "Cube Tracking Texture -- UPDATE\n";
02279         std::cout << "-------------------------------\n";
02280         int i = 0;
02281         for ( int z = 0; z < vCubes[2]; ++z ) {
02282             for ( int y = 0; y < vCubes[1]; ++y ) {
02283                 for ( int x = 0; x < vCubes[0]; ++x ) {
02284                     std::cout << "  ";
02285                     if ( pCubeValData[i] < 100 )    std::cout << " ";
02286                     if ( pCubeValData[i] <  10 )    std::cout << " ";
02287                     std::cout << static_cast<int>( pCubeValData[i] );
02288                     ++i;
02289                 }
02290                 std::cout << "\n";
02291             }
02292             std::cout << "\n";
02293         }
02294         std::cout << "---------------------\n";
02295         //---------------------------------
02296         glDisable( GL_TEXTURE_3D );
02297         glBindTexture( GL_TEXTURE_3D, 0 );
02298         //-----------------------------------------------------
02299         delete [] pCubeValData;
02300     }
02301     //*/
02302 
02303 
02304     /*
02305     //===============================================================
02306     // Get 3D texture for positions
02307     //---------------------------------------------------------------
02308     Texture * texture3DPosition = m_SimDomain->RetObject3DTexturePosition();
02309     //---------------------------------------------------------------
02310     //===============================================================
02311     // 
02312     //---------------------------------------------------------------
02313     glPushAttrib( GL_ALL_ATTRIB_BITS );
02314     //-----------------------------------------------------
02315     //-----------------------------------------------------
02316     m_glslProgramObjectUpdateCubeTracking->BeginGLSL();
02317     //-----------------------------------------------------
02318 
02319     //+++++++++++++++++++++++++++++++++++++++++++
02320     //===========================================
02321     // START: Parameter Setup
02322     //-------------------------------------------
02323     // Texture Parameter Setup
02324     //---------------------------------
02325     // Set PositionTexture to Texture Unit 0
02326     texture3DPosition->EnableTextureTarget();
02327     glActiveTexture( GL_TEXTURE0 );
02328     texture3DPosition->BindTexture(0);
02329     m_glslProgramObjectUpdateCubeTracking->SetUniform1i( "PositionTexture", 0 );
02330     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02331     //---------------------------------
02332     // Threshold Value
02333     m_glslProgramObjectUpdateCubeTracking->SetUniform1f( "threshold", m_SimDomain->IC_Thresholds::ON_BOUNDARY );
02334     //---------------------------------
02335     // Coordinate Offsets
02336     Vector3<T> coordOffsets = m_SimDomain->GetTexCoordOffsetForPosition();
02337     m_glslProgramObjectUpdateCubeTracking->SetUniform3f( 
02338         "offsetCoordsPosition", 
02339         coordOffsets[0], coordOffsets[1], coordOffsets[2] 
02340     );
02341     //-------------------------------------------
02342     // END: Parameter Setup
02343     //===========================================
02344     //+++++++++++++++++++++++++++++++++++++++++++
02345 
02346     //-----------------------------------------------------
02347     // Bind the buffer and then draw
02348     glBindBuffer( GL_ARRAY_BUFFER, m_vboVerticesAsTexCoords );
02349     glVertexPointer( 3, GL_FLOAT, 0, (GLubyte *)((char *)NULL + (0)) );
02350     glEnableClientState( GL_VERTEX_ARRAY );
02351     glDrawArrays( GL_POINTS, 0, m_uiTotalCubes );
02352     glDisableClientState( GL_VERTEX_ARRAY );
02353     glBindBuffer( GL_ARRAY_BUFFER, 0 );
02354     //-----------------------------------------------------
02355     m_glslProgramObjectUpdateCubeTracking->EndGLSL();
02356     //-----------------------------------------------------
02357     texture3DPosition->DisableTextureTarget();
02358     texture3DPosition->UnbindTexture();
02359     //-----------------------------------------------------
02360     glPopAttrib();
02361     //---------------------------------------------------------------
02362     //===============================================================
02363     //*/
02364 }
02365 //-----------------------------------------------------------------------------
02366 // END: UpdateCubeTrackingTexture
02367 //*****************************************************************************
02368 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
02369 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
02370 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_RENDERING
02371 
02372 
02373 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_RENDERING
02374 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
02375 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
02376 //*****************************************************************************
02377 // BEGIN: DrawByGLSL
02378 //-----------------------------------------------------------------------------
02379 template <typename T>
02380 void ModelDeformableGLSL_Visualization_RTGenMesh<T>::DrawByGLSL ()
02381 {
02392     //---------------------------------------------------------------
02393 #ifdef TAPs_DEBUG_MODE
02394     if ( !m_SimDomain ) return;
02395     //---------------------------------------------------------------
02396     if ( !m_SimDomain->RetObject3DTexturePosition() )   return;
02397     //---------------------------------------------------------------
02398 #endif
02399     //*
02400     //===============================================================
02401     // Get 3D texture for positions
02402     //---------------------------------------------------------------
02403     Texture * texture3DPosition = m_SimDomain->RetObject3DTexturePosition();
02404     //GLfloat modelviewMatrix[16];
02405     //glGetFloatv( GL_MODELVIEW_MATRIX, modelviewMatrix );
02406     //---------------------------------------------------------------
02407     //===============================================================
02408     // Draw the deformable model
02409     //---------------------------------------------------------------
02410     glPushAttrib( GL_ALL_ATTRIB_BITS );
02411     //-----------------------------------------------------
02412     glColor3ub( 80, 80, 80 );
02413     //glEnable( GL_COLOR_MATERIAL );
02414     glEnable( GL_LIGHTING );
02415     glEnable( GL_BLEND );
02416     glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
02417     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );
02418     glEnable( GL_TEXTURE_3D );
02419     //glEnable( GL_CULL_FACE ); // Doesn't really make the drawing run faster!
02420     //glShadeModel( GL_SMOOTH );
02421     //glShadeModel( GL_FLAT );
02422     //-----------------------------------------------------
02423     //texture3DPosition->EnableTextureTarget();
02424     //glActiveTexture( GL_TEXTURE0 );
02425     //texture3DPosition->BindTexture(0);
02426     //-----------------------------------------------------
02427     GLSLProgramObject * pShader = NULL;
02428     if ( m_bUseGlobalInterpolationVal ) {
02429         pShader = m_glslProgramObjectWithGlobalIntpValue;
02430     }
02431     else {
02432         pShader = m_glslProgramObjectWithTextureIntpValue;
02433     }
02434     pShader->BeginGLSL();
02435     //-----------------------------------------------------
02436 
02437     //+++++++++++++++++++++++++++++++++++++++++++
02438     //===========================================
02439     // START: Parameter Setup
02440     //-------------------------------------------
02441     // Texture Parameter Setup
02442     // Set Surface 3D Texture to Texture Unit 0
02443     //glEnable( GL_BLEND );
02444     if ( m_3DTexture ) {
02445         glActiveTexture( GL_TEXTURE0 );
02446         glBindTexture( GL_TEXTURE_3D, m_3DTexture );
02447         pShader->SetUniform1i( "SurfaceTexture", 0 );
02448         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02449     }
02450     //---------------------------------
02451     // Set PositionTexture to Texture Unit 1
02452     texture3DPosition->EnableTextureTarget();
02453     glActiveTexture( GL_TEXTURE1 );
02454     texture3DPosition->BindTexture(0);
02455     pShader->SetUniform1i( "PositionTexture", 1 );
02456     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02457     //---------------------------------
02458     // Set Edge Table to Texture Unit 2
02459     glEnable( GL_TEXTURE_2D );
02460     glActiveTexture( GL_TEXTURE2 );
02461     glBindTexture( GL_TEXTURE_2D, EdgeTableTexture );
02462     pShader->SetUniform1i( "EdgeTableTexture", 2 );
02463     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02464     //---------------------------------
02465     // Set Triangle Table to Texture Unit 3
02466     //glEnable( GL_TEXTURE_2D );
02467     glActiveTexture( GL_TEXTURE3 );
02468     glBindTexture( GL_TEXTURE_2D, TriTableTexture );
02469     pShader->SetUniform1i( "TriTableTexture", 3 );
02470     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02471     //---------------------------------
02472     // A Combined Texture for Triangle and Edge Tables
02473     // Set Triangle and Edge Tables to Texture Unit 2
02474     //glEnable( GL_TEXTURE_2D );
02475     //glActiveTexture( GL_TEXTURE2 );
02476     //glBindTexture( GL_TEXTURE_2D, TriAndEdgeTableTexture );
02477     //m_glslProgramObject->SetUniform1i( "TriAndEdgeTableTexture", 2 );
02478     //TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02479     //-------------------------------------------------------------------------
02480 
02481     // WITH GRADIENT TEXTURE
02482     #ifdef TAPs_MODEL_DEFORMABLE_USE_GRADIENT_TEXTURE
02483     //---------------------------------
02484     // Set Gradient to Texture Unit 6
02485     glActiveTexture( GL_TEXTURE6 );
02486     glBindTexture( GL_TEXTURE_3D, m_SimDomain->Get3DTextureGradient() );
02487     pShader->SetUniform1i( "Gradient", 6 );
02488     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02489     //---------------------------------
02490     #endif // TAPs_MODEL_DEFORMABLE_USE_GRADIENT_TEXTURE
02491 
02492     //int maximum;
02493     //glGetIntegerv( GL_MAX_TEXTURE_COORDS, &maximum );
02494     //std::cout << "GL_MAX_TEXTURE_COORDS: " << maximum << "\n";
02495     //glGetIntegerv( GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maximum );
02496     //std::cout << "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: " << maximum << "\n";
02497 
02498     //=========================================================================
02499     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
02500     //-------------------------------------------------------------------------
02501     // Can't set to Texture Unit 0, 1, 2, or 3 -- they are in used
02502     // If set to Texture Unit 0 (1, 2, or 3), the surface will not be drawn.
02503     // Set Cube Tracking Texture to Texture Unit 4
02504     //glEnable( GL_TEXTURE_3D );
02505     glActiveTexture( GL_TEXTURE4 );
02506     glBindTexture( GL_TEXTURE_3D, CubeTrackingTexture );
02507 
02508     //m_glslProgramObject->SetUniform1ui( "CubeTrackingTexture", 4 );
02509     // CURRENTLY, GL_ALPHA8UI_EXT is not working correctly.
02510     // Due to bugs with unsigned int in geometry shader compiler/driver.
02511     // So GL_ALPHA16I_EXT is used instead!
02512     //m_glslProgramObject->SetUniform1i( "CubeTrackingTexture", 4 );
02513     // CURRENTLY
02514     // The GLSL's texture3D( isampler3D sampler, vec3 coords ) 
02515     // doesn't return the correct value for integer
02516     // The GLSL's texture3D( usampler3D sampler, vec3 coords ) 
02517     // doesn't return the correct value for unsinged integer
02518     // So GL_ALPHA8 is used instead!
02519     pShader->SetUniform1i( "CubeTrackingTexture", 4 );
02520     //-------------------------------------------------------------------------
02521     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
02522     //=========================================================================
02523     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02524 
02525     //---------------------------------
02526     // Selectable Gradient Computations
02527     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
02528     pShader->SetUniform1i( "bGradFaces",    m_bComputeGradientFaces );
02529     pShader->SetUniform1i( "bGradVertices", m_bComputeGradientVertices );
02530     pShader->SetUniform1i( "bGradEdges",    m_bComputeGradientEdges );
02531     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_GRADIENT_SELECTABLE
02532 
02533     //---------------------------------
02534     // Interpolation Value
02535     //   use global value
02536     if ( m_bUseGlobalInterpolationVal ) {
02537         pShader->SetUniform1f( "interpolationValue", m_tGlobalInterpolationVal );
02538     }
02539     //   use values store in texture
02540     else {
02541         glActiveTexture( GL_TEXTURE5 );
02542         glBindTexture( GL_TEXTURE_3D, m_SimDomain->Get3DTextureEllipsoidInfluence() );
02543         pShader->SetUniform1i( "IntpValTexture", 5 );
02544         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02545     }
02546 
02547     //---------------------------------
02548     // Threshold Value
02549     pShader->SetUniform1f( "threshold", m_SimDomain->IC_Thresholds::ON_BOUNDARY );
02550     //---------------------------------
02551     // Threshold Value
02552     // For NVIDIA 320MB 8800 GTS, the smoothness should be at most 4.
02553     // The smotthness cannot be less than 2, due to the essential nature of PNTriangle algorithm.
02554     // Therefore, the smoothness must be between 2 (no PNTriangle, i.e. original triangle) to 4.
02555     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_PNTRI
02556         int Smoothness = static_cast<int>( m_PNTriangleSmoothness );
02557         if      ( Smoothness < 2 ) Smoothness = 2;
02558         else if ( Smoothness > 4 ) Smoothness = 4;
02559         //std::cout << "Smoothness: " << Smoothness << std::endl;
02560         m_glslProgramObject->SetUniform1i( "Smoothness", Smoothness );
02561     #endif // #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_PNTRI
02562     //---------------------------------
02563     // Coordinate Offsets
02564     Vector3<T> coordOffsets = m_SimDomain->GetTexCoordOffsetForPosition();
02565     pShader->SetUniform3f( 
02566         "offsetCoordsPosition", 
02567 #ifdef TAPs_DEFORMABLE_MODEL_VISUALIZATION_GPU_MARCHING_CUBES_SUBCUBE_BY_TWO
02568         coordOffsets[0]*0.5, coordOffsets[1]*0.5, coordOffsets[2]*0.5 
02569 #else
02570         coordOffsets[0], coordOffsets[1], coordOffsets[2] 
02571 #endif // TAPs_DEFORMABLE_MODEL_VISUALIZATION_GPU_MARCHING_CUBES_SUBCUBE_BY_TWO
02572     );
02573     //-------------------------------------------
02574     // END: Parameter Setup
02575     //===========================================
02576     //+++++++++++++++++++++++++++++++++++++++++++
02577 
02578     glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
02579     //-----------------------------------------------------
02580 
02581     glEnableClientState( GL_VERTEX_ARRAY );
02582 
02583     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
02584     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
02585     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
02586 
02587     //-------------------------------------------
02588     // Bind the buffer for vertices as texture coordinate for vertex positions
02589     glBindBuffer( GL_ARRAY_BUFFER, m_vboVerticesAsTexCoords );
02590     //glVertexPointer( 3, GL_FLOAT, 0, (GLubyte *)((char *)NULL + (0)) );
02591     glVertexPointer( 4, GL_FLOAT, 0, (GLubyte *)((char *)NULL + (0)) );
02592 
02593     //-------------------------------------------
02594     // Bind the buffer for vertices as texture coordinate for vertex positions
02595     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
02596     glBindBuffer( GL_ARRAY_BUFFER, m_vboTexCoordForCubeTracking );
02597     glTexCoordPointer( 3, GL_FLOAT, 0, (GLubyte *)((char *)NULL + (0)) );
02598     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
02599 
02600     //-------------------------------------------
02601     // Draw
02602     //glBeginTransformFeedbackNV( GL_TRIANGLES );
02603     glDrawArrays( GL_POINTS, 0, m_uiTotalCubes );
02604     //glEndTransformFeedbackNV();
02605 
02606     glBindBuffer( GL_ARRAY_BUFFER, 0 );
02607     glDisableClientState( GL_VERTEX_ARRAY );
02608     //-----------------------------------------------------
02609     glPopClientAttrib();
02610 
02611     //-----------------------------------------------------
02612     pShader->EndGLSL();
02613     //-----------------------------------------------------
02614     texture3DPosition->DisableTextureTarget();
02615     texture3DPosition->UnbindTexture();
02616     //-----------------------------------------------------
02617     glPopAttrib();
02618     //---------------------------------------------------------------
02619     //===============================================================
02620     //*/
02621 
02622     /*
02623     //=========================================================================
02624     // TEST DRAWING
02625     glPushAttrib( GL_ALL_ATTRIB_BITS );
02626     glColor3ub( 175, 80, 80 );
02627     glEnable( GL_COLOR_MATERIAL );
02628     glDisable( GL_LIGHTING );
02629     glPointSize( 5 );
02630     glBindBuffer( GL_ARRAY_BUFFER, m_vboVerticesAsTexCoords );
02631     glVertexPointer( 3, GL_FLOAT, 0, (GLubyte *)((char *)NULL + (0)) );
02632     glEnableClientState( GL_VERTEX_ARRAY );
02633     //glDrawArrays( GL_POINTS, 0, m_SimDomain->GetTotalGridElements() );
02634 
02635     //glBeginTransformFeedbackNV( GL_TRIANGLES );
02636     glDrawArrays( GL_POINTS, 0, m_uiTotalCubes );
02637     //glEndTransformFeedbackNV();
02638 
02639     glDisableClientState( GL_VERTEX_ARRAY );
02640     glBindBuffer( GL_ARRAY_BUFFER, 0 );
02641     glPopAttrib();
02642     //=========================================================================
02643     //*/
02644 }
02645 //-----------------------------------------------------------------------------
02646 // END: DrawByGLSL
02647 //*****************************************************************************
02648 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_1X1X1_CUBES
02649 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
02650 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_RENDERING
02651 
02652 
02653 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_RENDERING
02654 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
02655 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_2X2X2_CUBES
02656 //*****************************************************************************
02657 // BEGIN: DrawByGLSL
02658 //-----------------------------------------------------------------------------
02659 template <typename T>
02660 void ModelDeformableGLSL_Visualization_RTGenMesh<T>::DrawByGLSL ()
02661 {
02672     //---------------------------------------------------------------
02673 #ifdef TAPs_DEBUG_MODE
02674     if ( !m_SimDomain ) return;
02675     //---------------------------------------------------------------
02676     if ( !m_SimDomain->RetObject3DTexturePosition() )   return;
02677     //---------------------------------------------------------------
02678 #endif
02679     //*
02680     //===============================================================
02681     // Get 3D texture for positions
02682     //---------------------------------------------------------------
02683     Texture * texture3DPosition = m_SimDomain->RetObject3DTexturePosition();
02684     //GLfloat modelviewMatrix[16];
02685     //glGetFloatv( GL_MODELVIEW_MATRIX, modelviewMatrix );
02686     //---------------------------------------------------------------
02687     //===============================================================
02688     // Draw the deformable model
02689     //---------------------------------------------------------------
02690     glPushAttrib( GL_ALL_ATTRIB_BITS );
02691     //-----------------------------------------------------
02692     glColor3ub( 80, 80, 80 );
02693     glEnable( GL_COLOR_MATERIAL );
02694     glEnable( GL_LIGHTING );
02695     glEnable( GL_BLEND );
02696     glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
02697     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );
02698     //glShadeModel( GL_SMOOTH );
02699     //glShadeModel( GL_FLAT );
02700     //-----------------------------------------------------
02701     //texture3DPosition->EnableTextureTarget();
02702     //glActiveTexture( GL_TEXTURE0 );
02703     //texture3DPosition->BindTexture(0);
02704     //-----------------------------------------------------
02705     GLSLProgramObject * pShader = NULL;
02706     if ( m_bUseGlobalInterpolationVal ) {
02707         pShader = m_glslProgramObjectWithGlobalIntpValue;
02708     }
02709     else {
02710         pShader = m_glslProgramObjectWithTextureIntpValue;
02711     }
02712     pShader->BeginGLSL();
02713     //-----------------------------------------------------
02714 
02715     //+++++++++++++++++++++++++++++++++++++++++++
02716     //===========================================
02717     // START: Parameter Setup
02718     //-------------------------------------------
02719     // Texture Parameter Setup
02720     // Set Surface 3D Texture to Texture Unit 0
02721     //glEnable( GL_BLEND );
02722     glEnable( GL_TEXTURE_3D );
02723     glActiveTexture( GL_TEXTURE0 );
02724     glBindTexture( GL_TEXTURE_3D, m_3DTexture );
02725     pShader->SetUniform1i( "SurfaceTexture", 0 );
02726     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02727     //---------------------------------
02728     // Set PositionTexture to Texture Unit 1
02729     texture3DPosition->EnableTextureTarget();
02730     glActiveTexture( GL_TEXTURE1 );
02731     texture3DPosition->BindTexture(0);
02732     pShader->SetUniform1i( "PositionTexture", 1 );
02733     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02734     //---------------------------------
02735     // Set Edge Table to Texture Unit 2
02736     glEnable( GL_TEXTURE_2D );
02737     glActiveTexture( GL_TEXTURE2 );
02738     glBindTexture( GL_TEXTURE_2D, EdgeTableTexture );
02739     pShader->SetUniform1i( "EdgeTableTexture", 2 );
02740     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02741     //---------------------------------
02742     // Set Triangle Table to Texture Unit 3
02743     //glEnable( GL_TEXTURE_2D );
02744     glActiveTexture( GL_TEXTURE3 );
02745     glBindTexture( GL_TEXTURE_2D, TriTableTexture );
02746     pShader->SetUniform1i( "TriTableTexture", 3 );
02747     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02748     //---------------------------------
02749     // Set Triangle and Edge Tables to Texture Unit 2
02750     //glEnable( GL_TEXTURE_2D );
02751     //glActiveTexture( GL_TEXTURE2 );
02752     //glBindTexture( GL_TEXTURE_2D, TriAndEdgeTableTexture );
02753     //pShader->SetUniform1i( "TriAndEdgeTableTexture", 2 );
02754     //TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02755     //---------------------------------
02756     // Threshold Value
02757     pShader->SetUniform1f( "threshold", m_SimDomain->IC_Thresholds::ON_BOUNDARY );
02758     //---------------------------------
02759     // Interpolation Value
02760     pShader->SetUniform1f( "interpolationValue", m_tGlobalInterpolationVal );
02761     //---------------------------------
02762     // Threshold Value
02763     // For NVIDIA 320MB 8800 GTS, the smoothness should be at most 4.
02764     // The smotthness cannot be less than 2, due to the essential nature of PNTriangle algorithm.
02765     // Therefore, the smoothness must be between 2 (no PNTriangle, i.e. original triangle) to 4.
02766     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_PNTRI
02767         int Smoothness = static_cast<int>( m_PNTriangleSmoothness );
02768         if      ( Smoothness < 2 ) Smoothness = 2;
02769         else if ( Smoothness > 4 ) Smoothness = 4;
02770         //std::cout << "Smoothness: " << Smoothness << std::endl;
02771         pShader->SetUniform1i( "Smoothness", Smoothness );
02772     #endif // #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_PNTRI
02773     //---------------------------------
02774     // Coordinate Offsets
02775     Vector3<T> coordOffsets = m_SimDomain->GetTexCoordOffsetForPosition();
02776     pShader->SetUniform3f( 
02777         "offsetCoordsPosition", 
02778         coordOffsets[0], coordOffsets[1], coordOffsets[2] 
02779     );
02780     //-------------------------------------------
02781     // END: Parameter Setup
02782     //===========================================
02783     //+++++++++++++++++++++++++++++++++++++++++++
02784 
02785     //-----------------------------------------------------
02786     // Bind the buffer and then draw
02787     glBindBuffer( GL_ARRAY_BUFFER, m_vboVerticesAsTexCoords );
02788     glVertexPointer( 3, GL_FLOAT, 0, (GLubyte *)((char *)NULL + (0)) );
02789     glEnableClientState( GL_VERTEX_ARRAY );
02790     //glDrawArrays( GL_POINTS, 0, m_SimDomain->GetTotalGridElements() );
02791 
02792     //glBeginTransformFeedbackNV( GL_TRIANGLES );
02793     glDrawArrays( GL_POINTS, 0, m_uiTotalCubes );
02794     //glEndTransformFeedbackNV();
02795 
02796     glDisableClientState( GL_VERTEX_ARRAY );
02797     glBindBuffer( GL_ARRAY_BUFFER, 0 );
02798     //-----------------------------------------------------
02799     pShader->EndGLSL();
02800     //-----------------------------------------------------
02801     texture3DPosition->DisableTextureTarget();
02802     texture3DPosition->UnbindTexture();
02803     //-----------------------------------------------------
02804     glPopAttrib();
02805     //---------------------------------------------------------------
02806     //===============================================================
02807     //*/
02808 
02809     /*
02810     //=========================================================================
02811     // TEST DRAWING
02812     glPushAttrib( GL_ALL_ATTRIB_BITS );
02813     glColor3ub( 175, 80, 80 );
02814     glEnable( GL_COLOR_MATERIAL );
02815     glDisable( GL_LIGHTING );
02816     glPointSize( 5 );
02817     glBindBuffer( GL_ARRAY_BUFFER, m_vboVerticesAsTexCoords );
02818     glVertexPointer( 3, GL_FLOAT, 0, (GLubyte *)((char *)NULL + (0)) );
02819     glEnableClientState( GL_VERTEX_ARRAY );
02820     //glDrawArrays( GL_POINTS, 0, m_SimDomain->GetTotalGridElements() );
02821 
02822     //glBeginTransformFeedbackNV( GL_TRIANGLES );
02823     glDrawArrays( GL_POINTS, 0, m_uiTotalCubes );
02824     //glEndTransformFeedbackNV();
02825 
02826     glDisableClientState( GL_VERTEX_ARRAY );
02827     glBindBuffer( GL_ARRAY_BUFFER, 0 );
02828     glPopAttrib();
02829     //=========================================================================
02830     //*/
02831 }
02832 //-----------------------------------------------------------------------------
02833 // END: DrawByGLSL
02834 //*****************************************************************************
02835 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_2X2X2_CUBES
02836 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_BUFFERS
02837 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_RENDERING
02838 
02839 
02840 #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_CPU_RENDERING
02841 //*****************************************************************************
02842 // BEGIN: DrawByGLSL
02843 //-----------------------------------------------------------------------------
02844 template <typename T>
02845 void ModelDeformableGLSL_Visualization_RTGenMesh<T>::DrawByGLSL ()
02846 {
02847     DrawByGL();
02848 }
02849 //-----------------------------------------------------------------------------
02850 // END: DrawByGLSL
02851 //*****************************************************************************
02852 #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_CPU_RENDERING
02853 
02854 
02855 //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_RENDERING
02856 //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_NOT_USE_BUFFERS
02860 //template <typename T>
02861 //void ModelDeformableGLSL_Visualization_RTGenMesh<T>::DrawByGLSL ()
02862 //{
02863 //          //*
02864 //          glPushAttrib( GL_ALL_ATTRIB_BITS );
02865 //          glDisable( GL_LIGHTING );
02866 //          glPointSize( 17 );
02867 //          glBegin( GL_POINTS );
02868 //          glColor3f( 1, 1, 1 );
02869 //          glVertex3f( 0, 0, 0 );
02870 //          glEnd();
02871 //          glPopAttrib();
02872 //          //*/
02873 //
02874 //  //---------------------------------------------------------------
02875 //#ifdef TAPs_DEBUG_MODE
02876 //  if ( !m_SimDomain ) return;
02877 //  //---------------------------------------------------------------
02878 //  if ( !m_SimDomain->RetObject3DTexturePosition() )   return;
02879 //  //---------------------------------------------------------------
02880 //#endif
02881 //  //===============================================================
02882 //  // Get 3D texture for positions
02883 //  //---------------------------------------------------------------
02884 //  Texture * texture3DPosition = m_SimDomain->RetObject3DTexturePosition();
02885 //  //GLfloat modelviewMatrix[16];
02886 //  //glGetFloatv( GL_MODELVIEW_MATRIX, modelviewMatrix );
02887 //  //---------------------------------------------------------------
02888 //  //===============================================================
02889 //  // Draw the deformable model
02890 //  //---------------------------------------------------------------
02891 //  glPushAttrib( GL_ALL_ATTRIB_BITS );
02892 //  //-----------------------------------------------------
02893 //  texture3DPosition->EnableTextureTarget();
02894 //  //glActiveTexture( GL_TEXTURE0 );
02895 //  texture3DPosition->BindTexture(0);
02896 //  //-----------------------------------------------------
02897 //  m_glslProgramObject->BeginGLSL();
02898 //  //-----------------------------------------------------
02899 //  // Set PositionTexture to Texture Unit 0
02900 //  texture3DPosition->EnableTextureTarget();
02901 //  glActiveTexture( GL_TEXTURE0 );
02902 //  texture3DPosition->BindTexture(0);
02903 //  m_glslProgramObject->SetUniform1i( "PositionTexture", 0 );
02904 //  TAPs::OpenGL::Fn::CHECK_GL_ERROR();
02905 //  //-------------------------------------------
02906 //                                         // location name, count, isTranspose, *data
02907 //  //////m_glslProgramObject->SetUniformMatrix4fv( "ModelviewMatrix", 1, GL_FALSE, modelviewMatrix );
02908 //  //-------------------------------------------
02909 //  Vector3<int>    gridResolution = m_SimDomain->GetGridResolution();
02910 //  Vector3<T>      texCoordOffset = m_SimDomain->GetTexCoordOffsetForPosition();
02911 //  T texCoordStart[3] = { 
02912 //      texCoordOffset[0] / 2.0, 
02913 //      texCoordOffset[1] / 2.0, 
02914 //      texCoordOffset[2] / 2.0 
02915 //  };
02916 //  T texCoord[3] = { texCoordStart[0], texCoordStart[1], texCoordStart[2] };
02917 //  int idx = 0;
02918 //  glPointSize( 11 );
02919 //
02920 //
02921 //  /*/
02922 //  static bool isFirstRun = true;
02923 //  if ( isFirstRun ) {
02924 //      isFirstRun = false;
02925 //      std::cout << "m_SimDomain->GetTotalGridElements(): " << m_SimDomain->GetTotalGridElements() << "\n";
02926 //      glBindBuffer( GL_ARRAY_BUFFER, m_vboVerticesAsTexCoords );
02927 //      float * vertices = static_cast<float *>( glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY ) );
02928 //      int idx = 0;
02929 //      for ( int z = 0; z < gridResolution[2]; ++z ) {
02930 //          for ( int y = 0; y < gridResolution[1]; ++y ) {
02931 //              for ( int x = 0; x < gridResolution[0]; ++x ) {
02932 //                  std::cout << "VBO READ BACK: " 
02933 //                      << x << " " 
02934 //                      << y << " " 
02935 //                      << z << ": "
02936 //                      << vertices[idx++] << " " 
02937 //                      << vertices[idx++] << " " 
02938 //                      << vertices[idx++] << "\n";
02939 //              }
02940 //          }
02941 //      }
02942 //      glUnmapBuffer( GL_ARRAY_BUFFER );
02943 //      glBindBuffer( GL_ARRAY_BUFFER, 0 );
02944 //  }
02945 //  //*/
02946 //
02947 //  //*
02948 //  glBegin( GL_POINTS );
02949 //      //texCoord[2] = texCoordStart[2];
02950 //      for ( int Z = 0; Z < gridResolution[2]; ++Z ) {
02951 //          texCoord[1] = texCoordStart[1];
02952 //          for ( int Y = 0; Y < gridResolution[1]; ++Y ) {
02953 //              texCoord[0] = texCoordStart[0];
02954 //              for ( int X = 0; X < gridResolution[0]; ++X ) {
02955 //                  //glTexCoord3f( texCoord[0], texCoord[1], texCoord[2] );
02956 //                  glVertex3f( texCoord[0], texCoord[1], texCoord[2] );
02957 //                  idx += 4;
02958 //                  texCoord[0] += texCoordOffset[0];
02959 //              }
02960 //              texCoord[1] += texCoordOffset[1];
02961 //          }
02962 //          texCoord[2] += texCoordOffset[2];
02963 //      }
02964 //  glEnd();
02965 //  //*/
02966 //  //-----------------------------------------------------
02967 //  m_glslProgramObject->EndGLSL();
02968 //  //-----------------------------------------------------
02969 //  texture3DPosition->DisableTextureTarget();
02970 //  texture3DPosition->UnbindTexture();
02971 //  //-----------------------------------------------------
02972 //  glPopAttrib();
02973 //  //---------------------------------------------------------------
02974 //  //===============================================================
02975 //}
02979 //#endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_NOT_USE_BUFFERS
02980 //#endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_RENDERING
02981 
02982 
02983 //*****************************************************************************
02984 // BEGIN: DrawByGL_StandardMC
02985 //-----------------------------------------------------------------------------
02986 template <typename T>
02987 void ModelDeformableGLSL_Visualization_RTGenMesh<T>::DrawByGL_StandardMC ()
02988 {
02999 //*
03000     //---------------------------------------------------------------
03001 #ifdef TAPs_DEBUG_MODE
03002     if ( !m_SimDomain ) return;
03003     //---------------------------------------------------------------
03004     if ( !m_SimDomain->RetObject3DTexturePosition() )   return;
03005     //---------------------------------------------------------------
03006 #endif
03007     //===============================================================
03008     // Read data from GPU -- Positions
03009     //---------------------------------------------------------------
03010     Texture * texture3DPosition = m_SimDomain->RetObject3DTexturePosition();
03011     //---------------------------------------------------------------
03012     glPushAttrib( GL_ALL_ATTRIB_BITS );
03013     //-----------------------------------------------------
03014     GLsizei sizeForPosition = 
03015             texture3DPosition->GetWidth() *
03016             texture3DPosition->GetHeight() *
03017             texture3DPosition->GetDepth() *
03018             texture3DPosition->GetInternalFormatNumberOfComponents();
03019     GLfloat * dataForPosition = new GLfloat[ sizeForPosition ];
03020     {
03021         int i = 0;
03022         //=================================================
03023         // Read data for positions from a texture to memory
03024         glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
03025         texture3DPosition->BindTexture(0);
03026         glGetTexImage( 
03027             texture3DPosition->GetTarget(),
03028             texture3DPosition->GetLevel(),
03029             texture3DPosition->GetPixelFormat(),
03030             texture3DPosition->GetDataType(),
03031             dataForPosition
03032         );
03033         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
03034         //=================================================
03035     }
03036     //---------------------------------------------------------------
03037     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
03038     //-----------------------------------------------------
03039     glPopAttrib();
03040     //---------------------------------------------------------------
03041     //===============================================================
03042 
03043     //===============================================================
03044     // Read data from GPU -- Ellipsoid Influence
03045     //---------------------------------------------------------------
03046     Texture * texture3DEllipsoidInfluence = m_SimDomain->RetObject3DTextureEllipsoidInfluence();
03047     //---------------------------------------------------------------
03048     glPushAttrib( GL_ALL_ATTRIB_BITS );
03049     //-----------------------------------------------------
03050     GLsizei sizeForEllipsoidInfluence = 
03051             texture3DEllipsoidInfluence->GetWidth() *
03052             texture3DEllipsoidInfluence->GetHeight() *
03053             texture3DEllipsoidInfluence->GetDepth() *
03054             texture3DEllipsoidInfluence->GetInternalFormatNumberOfComponents();
03055     //std::cout << "sizeForEllipsoidInfluence: " << sizeForEllipsoidInfluence << "\n";
03056     GLfloat * dataForEllipsoidInfluence = new GLfloat[ sizeForEllipsoidInfluence ];
03057     {
03058         int i = 0;
03059         //=================================================
03060         // Read data for ellipsoid influences from a texture to memory
03061         glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
03062         texture3DEllipsoidInfluence->BindTexture(0);
03063         glGetTexImage( 
03064             texture3DEllipsoidInfluence->GetTarget(),
03065             texture3DEllipsoidInfluence->GetLevel(),
03066             texture3DEllipsoidInfluence->GetPixelFormat(),
03067             texture3DEllipsoidInfluence->GetDataType(),
03068             dataForEllipsoidInfluence
03069         );
03070         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
03071         //=================================================
03072     }
03073     //---------------------------------------------------------------
03074     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
03075     //-----------------------------------------------------
03076     glPopAttrib();
03077     //---------------------------------------------------------------
03078     //===============================================================
03079 
03080     //---------------------------------
03081     Vector3<int> gridResolution = m_SimDomain->GetGridResolution();
03082     Vector3<T>   gridDimension  = m_SimDomain->GetGridDimension();
03083     //---------------------------------
03084 
03085     //*
03086     //===============================================================
03087     // Draw by Marching Cube
03088     //---------------------------------------------------------------
03089     // Adapted from "Polygonising a scalar field" by Paul Bourke (1994)
03090     // http://local.wasp.uwa.edu.au/~pbourke/geometry/polygonise/
03091     //---------------------------------------------------------------
03092     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
03093     m_glslProgramObjectForShaderSurfaceFromSW->BeginGLSL();
03094     if ( m_3DTexture ) {
03095         glActiveTexture( GL_TEXTURE0 );
03096         glBindTexture( GL_TEXTURE_3D, m_3DTexture );
03097         m_glslProgramObjectForShaderSurfaceFromSW->SetUniform1i( "SurfaceTexture", 0 );
03098         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
03099     }
03100     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
03101 
03102     glPushAttrib( GL_ALL_ATTRIB_BITS );
03103     //glLineWidth( 2 );
03104     glPointSize( 5 );
03105 
03106     //glColor3ub( 240/2, 220/2, 130/2 );
03107     //glColor3ub( 240/8, 220/8, 130/8 );
03108     //glColor3ub( 65, 65, 65 );
03109 
03110     glColor3ub( 75, 75, 75 );
03111 
03112     //glColor3ub( 150, 150, 150 );
03113     //glColor3ub( 255, 255, 255 );
03114     
03115     //glEnable( GL_COLOR_MATERIAL );
03116 
03117     /*
03118     GLfloat diffuseMaterial[4]  = { 0.5f, 0.5f, 0.5f, 1.0f };
03119     GLfloat specularMaterial[4] = { 0.8f, 0.8f, 0.8f, 1.0f };
03120     GLfloat ambientMaterial[4]  = { 0.1f, 0.1f, 0.1f, 1.0f };
03121     GLfloat shininessMaterial[] = { 128.0 };
03122     glMaterialfv( GL_FRONT, GL_AMBIENT,  ambientMaterial );
03123     glMaterialfv( GL_FRONT, GL_SPECULAR, specularMaterial );
03124     glMaterialfv( GL_FRONT, GL_DIFFUSE,  diffuseMaterial );
03125     glMaterialfv( GL_FRONT, GL_SHININESS,  diffuseMaterial );
03126     //*/
03127 
03128     glEnable( GL_LIGHTING );
03129     //glDisable( GL_LIGHTING );
03130 
03131     // DEBUG for two-sided lighting
03132     glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
03133 
03134     //---------------------------------------------------------------
03135     // Texture Parameter Setup
03136 
03137     /*
03138     // 3D Texture for surface
03139     glEnable( GL_TEXTURE_3D );
03140     glBindTexture( GL_TEXTURE_3D, m_3DTexture );
03141     //*/
03142 
03143     // Composition
03144     glEnable( GL_BLEND );
03145     glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
03146     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
03147     //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );
03148     //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
03149     //glTexEnvf( GL_TEXTURE_ENV, GL_RGB_SCALE, 0.5 );
03150     //glTexEnvf( GL_TEXTURE_ENV, GL_ALPHA_SCALE, 0.5 );
03151     //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD );
03152 
03153     /*
03154     static GLfloat constColor[4] = { 0.0, 0.0, 0.0, 0.0 };
03155     constColor[3] = 0.2;
03156     glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, constColor );
03157     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
03158     glTexEnvf( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE );
03159     glTexEnvf( GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE );
03160     glTexEnvf( GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR );
03161     glTexEnvf( GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PREVIOUS );
03162     glTexEnvf( GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR );
03163     glTexEnvf( GL_TEXTURE_ENV, GL_SRC2_RGB, GL_PREVIOUS );
03164     glTexEnvf( GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA );
03165     //*/
03166 
03167     //---------------------------------------------------------------
03168     int iOffsetSlice = gridResolution[1] * gridResolution[0] * 4;
03169     int iOffsetRow = gridResolution[0] * 4;
03170     int iOffsetCol = 4;
03171     //---------------------------------
03172     // Vertex & Edge Index
03173     //          3------2------2
03174     //         /|            /|
03175     //       11 |          10 |
03176     //       /  3          /  1
03177     //      7------6------6   |
03178     //      |   |         |   |
03179     //      |   0------0--|---1
03180     //      7  /          5  /
03181     //      | 8           | 9 
03182     //      |/            |/
03183     //      4------4------5
03184     //---------------------------------
03185     // Edge Table [256] & Triangle Table [256][16]
03193     // V = V1 + (isovalue - s1) (V2 - V1) / (s2 - s1)
03194     //---------------------------------
03195     //unsigned char cubeClass = 0;
03196     //if ( grid.val[0] < isolevel ) cubeClass |= 1;
03197     //if ( grid.val[1] < isolevel ) cubeClass |= 2;
03198     //if ( grid.val[2] < isolevel ) cubeClass |= 4;
03199     //if ( grid.val[3] < isolevel ) cubeClass |= 8;
03200     //if ( grid.val[4] < isolevel ) cubeClass |= 16;
03201     //if ( grid.val[5] < isolevel ) cubeClass |= 32;
03202     //if ( grid.val[6] < isolevel ) cubeClass |= 64;
03203     //if ( grid.val[7] < isolevel ) cubeClass |= 128;
03204     //---------------------------------
03205     int numOfTrisPerCell = 0;   // number of triangles in the cell
03206     int triIndices[15];         // at most 5 triangles
03207     Vector3<T>  V[8];           // vertices of the cell
03208     Vector3<T>  VTC[8];         // texture coordinates of the vertices
03209     Vector3<T>  texCoordList[12];   // since 12 edges
03210     Vector3<T>  vertexList[12]; // since 12 edges
03211     int         bV[12];         // >=0 if the vertex is counted (e.g. on boundary, existing, etc.)
03212     Vector3<T>  grad[12];       // gradients of vertices
03213     //Vector3<T> texCoords[12]; // 
03214     Vector3<T> Center, nE, nW, nN, nS, nF, nB;  // center and its neighbors for gradient computation
03215     //---------------------------------
03216     int idx[8], d, i;
03217     unsigned char cubeClass = 0;
03218     T threshold = 250;
03219     //
03220     T tc[3];
03221     T tcStep[3] = {
03222         1.0 / static_cast<T>( gridResolution[0]-1 ), 
03223         1.0 / static_cast<T>( gridResolution[1]-1 ), 
03224         1.0 / static_cast<T>( gridResolution[2]-1 ) 
03225     };
03226     //
03227 
03228     //-----------------------------------------------------
03229     // For Interpolation Data used by Marching Cube
03230     int iOffsetSliceIntp = gridResolution[1] * gridResolution[0] * 3;
03231     int iOffsetRowIntp = gridResolution[0] * 3;
03232     int iOffsetColIntp = 3;
03233     int intpIdx[8];
03234     Vector3<T> VIntp[8];    // Interpolation Data (XYZ)
03235     //-----------------------------------------------------
03236 
03237     //---------------------------------
03238     //for ( int Z = gridResolution[2]*3/4; Z < gridResolution[2]-1; ++Z ) {
03239     for ( int Z = 0; Z < gridResolution[2]-1; ++Z ) {
03240         //
03241         // For Interpolation Data
03242         int offsetZIntp = Z * iOffsetSliceIntp;
03243         //
03244         int offsetZ = Z * iOffsetSlice;
03245         //
03246         // Texture Coordinates -- Z
03247         tc[2] = static_cast<T>( Z ) / static_cast<T>( gridResolution[2]-1 );
03248         VTC[0].SetZ( tc[2] );
03249         VTC[1].SetZ( tc[2] );
03250         VTC[2].SetZ( tc[2] );
03251         VTC[3].SetZ( tc[2] );
03252         tc[2] += tcStep[2];
03253         VTC[4].SetZ( tc[2] );
03254         VTC[5].SetZ( tc[2] );
03255         VTC[6].SetZ( tc[2] );
03256         VTC[7].SetZ( tc[2] );
03257         //
03258         //for ( int Y = gridResolution[1]; Y < gridResolution[1]-1; ++Y ) {
03259         for ( int Y = 0; Y < gridResolution[1]-1; ++Y ) {
03260             //
03261             // Set Indices for Interpolation Data
03262             int offsetYZIntp = Y * iOffsetRowIntp + offsetZIntp;
03263             intpIdx[0] = offsetYZIntp;;
03264             intpIdx[1] = intpIdx[0] + iOffsetColIntp;
03265             intpIdx[2] = intpIdx[1] + iOffsetRowIntp;
03266             intpIdx[3] = intpIdx[0] + iOffsetRowIntp;
03267             intpIdx[4] = intpIdx[0] + iOffsetSliceIntp;
03268             intpIdx[5] = intpIdx[4] + iOffsetColIntp;
03269             intpIdx[6] = intpIdx[5] + iOffsetRowIntp;
03270             intpIdx[7] = intpIdx[4] + iOffsetRowIntp;
03271             //
03272             // Set Indices for Cube Vertices
03273             int offsetYZ = Y * iOffsetRow + offsetZ;
03274             idx[0] = offsetYZ;
03275             idx[1] = idx[0] + iOffsetCol;
03276             idx[2] = idx[1] + iOffsetRow;
03277             idx[3] = idx[0] + iOffsetRow;
03278             idx[4] = idx[0] + iOffsetSlice;
03279             idx[5] = idx[4] + iOffsetCol;
03280             idx[6] = idx[5] + iOffsetRow;
03281             idx[7] = idx[4] + iOffsetRow;
03282             //
03283             // Texture Coordinates -- Y
03284             tc[1] = static_cast<T>( Y ) / static_cast<T>( gridResolution[1]-1 );
03285             VTC[0].SetY( tc[1] );
03286             VTC[1].SetY( tc[1] );
03287             VTC[4].SetY( tc[1] );
03288             VTC[5].SetY( tc[1] );
03289             tc[1] += tcStep[1];
03290             VTC[2].SetY( tc[1] );
03291             VTC[3].SetY( tc[1] );
03292             VTC[6].SetY( tc[1] );
03293             VTC[7].SetY( tc[1] );
03294             //
03295             for ( int X = 0; X < gridResolution[0]-1; ++X ) {
03296                 //
03297                 // Texture Coordinates -- X
03298                 tc[0] = static_cast<T>( X ) / static_cast<T>( gridResolution[0]-1 );
03299                 VTC[0].SetX( tc[0] );
03300                 VTC[3].SetX( tc[0] );
03301                 VTC[4].SetX( tc[0] );
03302                 VTC[7].SetX( tc[0] );
03303                 tc[0] += tcStep[0];
03304                 VTC[1].SetX( tc[0] );
03305                 VTC[2].SetX( tc[0] );
03306                 VTC[5].SetX( tc[0] );
03307                 VTC[6].SetX( tc[0] );
03308                 //
03309 
03310 
03311                 bool bLeftPresent  = false;
03312                 bool bRightPresent = false;
03313                 {
03314                     for ( int i = 0; i < 8; ++i ) {
03315                         int flagVal = static_cast<int>( dataForPosition[idx[i] + 3] );
03316                         if ( flagVal > threshold ) {
03317                             flagVal %= int( m_SimDomain->IC_Thresholds::FLAG_STEP );
03318                             if      ( 1 < flagVal && flagVal < m_SimDomain->IC_Thresholds::MC_LEFT_ADD+1 ) {
03319                                 bLeftPresent = true;
03320                             }
03321                             else if ( flagVal > m_SimDomain->IC_Thresholds::MC_LEFT_ADD+1 ) {
03322                                 bRightPresent = true;
03323                             }
03324                         }
03325                     }
03326                 }
03327 
03328                 // STANDARD CUBE CONFIGURATION
03329                 //-------------------------------
03330                 // Determine the threshold of each vertex of the cube
03331                 cubeClass = 0;
03332                 if ( dataForPosition[idx[0] + 3] > threshold )  cubeClass |= 1;
03333                 if ( dataForPosition[idx[1] + 3] > threshold )  cubeClass |= 2;
03334                 if ( dataForPosition[idx[2] + 3] > threshold )  cubeClass |= 4;
03335                 if ( dataForPosition[idx[3] + 3] > threshold )  cubeClass |= 8;
03336                 if ( dataForPosition[idx[4] + 3] > threshold )  cubeClass |= 16;
03337                 if ( dataForPosition[idx[5] + 3] > threshold )  cubeClass |= 32;
03338                 if ( dataForPosition[idx[6] + 3] > threshold )  cubeClass |= 64;
03339                 if ( dataForPosition[idx[7] + 3] > threshold )  cubeClass |= 128;
03340                 //-------------------------------
03341 
03342 
03343                 // ONE ITERATION FOR EACH CUBE
03344                 {
03345                     //-------------------------------
03346                     // Cell is entirely in/out of the surface
03347                     if ( EdgeTable[cubeClass] == 0 ) {
03348                         numOfTrisPerCell = 0;
03349                     }
03350                     else {
03351                     //-------------------------------
03352                     // Cell contains part of the surface
03353                         //-----------------
03354                         for ( i = 0; i < 8; ++i ) {
03355                             V[i].SetXYZ( 
03356                                 dataForPosition[idx[i]    ], 
03357                                 dataForPosition[idx[i] + 1], 
03358                                 dataForPosition[idx[i] + 2] 
03359                             );
03360                         }
03361                         //-----------------
03362                         // Reset list of existing vertices
03363                         for ( i = 0; i < 12; ++i ) {
03364                             bV[i] = -1;
03365                         }
03366 
03367                         //-----------------
03368                         // Interpolation Data
03369                         for ( i = 0; i < 8; ++i ) {
03370                             VIntp[i].SetXYZ( 
03371                                 dataForEllipsoidInfluence[intpIdx[i]    ], 
03372                                 dataForEllipsoidInfluence[intpIdx[i] + 1], 
03373                                 dataForEllipsoidInfluence[intpIdx[i] + 2] 
03374                             );
03375                         }
03376 
03377                         //===============================================
03378                         // Calculate Gradients
03379                         //-----------------------------------------------
03380                         Vector3<T> G[8];
03381                         int index;
03382                         for ( int vID = 0; vID < 8; ++vID ) {
03383                             Center.SetXYZ(  dataForPosition[idx[vID]  ], 
03384                                             dataForPosition[idx[vID]+1], 
03385                                             dataForPosition[idx[vID]+2] );
03386                             G[vID].SetXYZ( 0, 0, 0 );
03387 
03388                             //*
03389                             //=================================
03390                             // START: 6 Face Gradients
03391                             //---------------------------------
03392                             if ( GetGradientComputationByFacesStatus() ) {
03393                             //--- East --------------
03394                             if ( X < gridResolution[0]-2 ) {
03395                                 index = idx[vID] + iOffsetCol;
03396                                 if ( dataForPosition[index+3] > threshold ) {
03397                                     nE.SetXYZ( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03398                                     G[vID] += Center - nE;
03399                                 }
03400                             }
03401                             //--- West --------------
03402                             if ( X > 0 ) {
03403                                 index = idx[vID] - iOffsetCol;
03404                                 if ( dataForPosition[index+3] > threshold ) {
03405                                     nW.SetXYZ( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03406                                     G[vID] += Center - nW;
03407                                 }
03408                             }
03409                             //--- North -------------
03410                             if ( Y < gridResolution[1]-2 ) {
03411                                 index = idx[vID] + iOffsetRow;
03412                                 if ( dataForPosition[index+3] > threshold ) {
03413                                     nN.SetXYZ( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03414                                     G[vID] += Center - nN;
03415                                 }
03416                             }
03417                             //--- South -------------
03418                             if ( Y > 0 ) {
03419                                 index = idx[vID] - iOffsetRow;
03420                                 if ( dataForPosition[index+3] > threshold ) {
03421                                     nS.SetXYZ( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03422                                     G[vID] += Center - nS;
03423                                 }
03424                             }
03425                             //--- Front -------------
03426                             if ( Z < gridResolution[2]-2 ) {
03427                                 index = idx[vID] + iOffsetSlice;
03428                                 if ( dataForPosition[index+3] > threshold ) {
03429                                     nF.SetXYZ( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03430                                     G[vID] += Center - nF;
03431                                 }
03432                             }
03433                             //--- Back --------------
03434                             if ( Z > 0 ) {
03435                                 index = idx[vID] - iOffsetSlice;
03436                                 if ( dataForPosition[index+3] > threshold ) {
03437                                     nB.SetXYZ( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03438                                     G[vID] += Center - nB;
03439                                 }
03440                             }
03441                             //-----------------------
03442                             } // END: if ( GetGradientComputationByFacesStatus() ) {
03443                             //---------------------------------
03444                             // END: 6 Face Gradients
03445                             //=================================
03446                             //*/
03447 
03448                             //*
03449                             //=================================
03450                             // START: 8 Vertex Gradients
03451                             //---------------------------------
03452                             if ( GetGradientComputationByVerticesStatus() ) {
03453                             //--- Front -------------
03454                             if ( Z < gridResolution[2]-2 ) {
03455                                 int idxF = idx[vID] + iOffsetSlice;
03456                                 //-- North -----
03457                                 if ( Y < gridResolution[1]-2 ) {
03458                                     int idxFN = idxF + iOffsetRow;
03459                                     //- West
03460                                     if ( X > 0 ) {
03461                                         index = idxFN - iOffsetCol;
03462                                         if ( dataForPosition[index+3] > threshold ) {
03463                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03464                                             G[vID] += Center - vGrad;
03465                                         }
03466                                     }
03467                                     //- East
03468                                     if ( X < gridResolution[0]-2 ) {
03469                                         index = idxFN + iOffsetCol;
03470                                         if ( dataForPosition[index+3] > threshold ) {
03471                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03472                                             G[vID] += Center - vGrad;
03473                                         }
03474                                     }
03475                                 }
03476                                 //-- South -----
03477                                 if ( Y > 0 ) {
03478                                     int idxFS = idxF - iOffsetRow;
03479                                     //- West
03480                                     if ( X > 0 ) {
03481                                         index = idxFS - iOffsetCol;
03482                                         if ( dataForPosition[index+3] > threshold ) {
03483                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03484                                             G[vID] += Center - vGrad;
03485                                         }
03486                                     }
03487                                     //- East
03488                                     if ( X < gridResolution[0]-2 ) {
03489                                         index = idxFS + iOffsetCol;
03490                                         if ( dataForPosition[index+3] > threshold ) {
03491                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03492                                             G[vID] += Center - vGrad;
03493                                         }
03494                                     }
03495                                 }
03496                             }
03497                             //--- Back --------------
03498                             if ( Z > 0 ) {
03499                                 int idxF = idx[vID] - iOffsetSlice;
03500                                 //-- North -----
03501                                 if ( Y < gridResolution[1]-2 ) {
03502                                     int idxFN = idxF + iOffsetRow;
03503                                     //- West
03504                                     if ( X > 0 ) {
03505                                         index = idxFN - iOffsetCol;
03506                                         if ( dataForPosition[index+3] > threshold ) {
03507                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03508                                             G[vID] += Center - vGrad;
03509                                         }
03510                                     }
03511                                     //- East
03512                                     if ( X < gridResolution[0]-2 ) {
03513                                         index = idxFN + iOffsetCol;
03514                                         if ( dataForPosition[index+3] > threshold ) {
03515                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03516                                             G[vID] += Center - vGrad;
03517                                         }
03518                                     }
03519                                 }
03520                                 //-- South -----
03521                                 if ( Y > 0 ) {
03522                                     int idxFS = idxF - iOffsetRow;
03523                                     //- West
03524                                     if ( X > 0 ) {
03525                                         index = idxFS - iOffsetCol;
03526                                         if ( dataForPosition[index+3] > threshold ) {
03527                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03528                                             G[vID] += Center - vGrad;
03529                                         }
03530                                     }
03531                                     //- East
03532                                     if ( X < gridResolution[0]-2 ) {
03533                                         index = idxFS + iOffsetCol;
03534                                         if ( dataForPosition[index+3] > threshold ) {
03535                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03536                                             G[vID] += Center - vGrad;
03537                                         }
03538                                     }
03539                                 }
03540                             }
03541                             //-----------------------
03542                             } // END: if ( GetGradientComputationByVerticesStatus() ) {
03543                             //---------------------------------
03544                             // END: 8 Vertex Gradients
03545                             //=================================
03546                             //*/
03547 
03548                             //*
03549                             //=================================
03550                             // START: 12 Edge Gradients
03551                             //---------------------------------
03552                             if ( GetGradientComputationByEdgesStatus() ) {
03553                             //--- Front -------------
03554                             if ( Z < gridResolution[2]-2 ) {
03555                                 int idxF = idx[vID] + iOffsetSlice;
03556                                 //-- East ------
03557                                 if ( X < gridResolution[0]-2 ) {
03558                                     index = idxF + iOffsetCol;
03559                                     if ( dataForPosition[index+3] > threshold ) {
03560                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03561                                         G[vID] += Center - vGrad;
03562                                     }
03563                                 }
03564                                 //-- West ------
03565                                 if ( X > 0 ) {
03566                                     index = idxF - iOffsetCol;
03567                                     if ( dataForPosition[index+3] > threshold ) {
03568                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03569                                         G[vID] += Center - vGrad;
03570                                     }
03571                                 }
03572                                 //-- North -----
03573                                 if ( Y < gridResolution[1]-2 ) {
03574                                     index = idxF + iOffsetRow;
03575                                     if ( dataForPosition[index+3] > threshold ) {
03576                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03577                                         G[vID] += Center - vGrad;
03578                                     }
03579                                 }
03580                                 //-- South -----
03581                                 if ( Y > 0 ) {
03582                                     index = idxF - iOffsetRow;
03583                                     if ( dataForPosition[index+3] > threshold ) {
03584                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03585                                         G[vID] += Center - vGrad;
03586                                     }
03587                                 }
03588                             }
03589                             //--- Center ------------
03590                             if ( true ) {
03591                                 int idxC = idx[vID];
03592                                 //-- East ------
03593                                 if ( X < gridResolution[0]-2 ) {
03594                                     index = idxC + iOffsetCol;
03595                                     if ( dataForPosition[index+3] > threshold ) {
03596                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03597                                         G[vID] += Center - vGrad;
03598                                     }
03599                                 }
03600                                 //-- West ------
03601                                 if ( X > 0 ) {
03602                                     index = idxC - iOffsetCol;
03603                                     if ( dataForPosition[index+3] > threshold ) {
03604                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03605                                         G[vID] += Center - vGrad;
03606                                     }
03607                                 }
03608                                 //-- North -----
03609                                 if ( Y < gridResolution[1]-2 ) {
03610                                     index = idxC + iOffsetRow;
03611                                     if ( dataForPosition[index+3] > threshold ) {
03612                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03613                                         G[vID] += Center - vGrad;
03614                                     }
03615                                 }
03616                                 //-- South -----
03617                                 if ( Y > 0 ) {
03618                                     index = idxC - iOffsetRow;
03619                                     if ( dataForPosition[index+3] > threshold ) {
03620                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03621                                         G[vID] += Center - vGrad;
03622                                     }
03623                                 }
03624                             }
03625                             //--- Back --------------
03626                             if ( Z > 0 ) {
03627                                 int idxB = idx[vID] - iOffsetSlice;
03628                                 //-- East ------
03629                                 if ( X < gridResolution[0]-2 ) {
03630                                     index = idxB + iOffsetCol;
03631                                     if ( dataForPosition[index+3] > threshold ) {
03632                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03633                                         G[vID] += Center - vGrad;
03634                                     }
03635                                 }
03636                                 //-- West ------
03637                                 if ( X > 0 ) {
03638                                     index = idxB - iOffsetCol;
03639                                     if ( dataForPosition[index+3] > threshold ) {
03640                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03641                                         G[vID] += Center - vGrad;
03642                                     }
03643                                 }
03644                                 //-- North -----
03645                                 if ( Y < gridResolution[1]-2 ) {
03646                                     index = idxB + iOffsetRow;
03647                                     if ( dataForPosition[index+3] > threshold ) {
03648                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03649                                         G[vID] += Center - vGrad;
03650                                     }
03651                                 }
03652                                 //-- South -----
03653                                 if ( Y > 0 ) {
03654                                     index = idxB - iOffsetRow;
03655                                     if ( dataForPosition[index+3] > threshold ) {
03656                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
03657                                         G[vID] += Center - vGrad;
03658                                     }
03659                                 }
03660                             }
03661                             //-----------------------
03662                             } // END: if ( GetGradientComputationByEdgesStatus() ) {
03663                             //---------------------------------
03664                             // END: 12 Edge Gradients
03665                             //=================================
03666                             //*/
03667                         }
03668                         //-----------------------------------------------
03669                         // END: Calculate Gradients
03670                         //===============================================
03671 
03672 
03673                         //===============================================
03674                         // Marching Cube Interpolation
03675                         // V = V1 + (isovalue - s1) (V2 - V1) / (s2 - s1)
03676                         //-----------------------------------------------
03677                         // RUNTIME ERROR CAN OCCURS -- DEPENDING ON THE DATA
03678                         // SINCE IT DOES NOT HAVE BOUNDARY PROTECTION -- ARRAY INDEX OUT OF BOUND ERROR
03679                         //-----------------------------------------------
03680                         // Not Use Global Interpolation Value -- each element has its own xyz interpolation values
03681                         if ( !m_bUseGlobalInterpolationVal ) {
03682                             //-------------------------------------
03683                             // Marching Cube Interpolation
03684                             T interpolationValPlus;// = m_tGlobalInterpolationVal;
03685                             T interpolationValNeg;//  = 1.0 - m_tGlobalInterpolationVal;
03686                             //-------------------------------------
03687                             // Edge# 0 to 11 <==> [ 1, 2, 4, ... , 2048 ]
03688                             if ( EdgeTable[cubeClass] & 1 ) {
03689                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
03690                                     //
03691                                     // Interpotation Data
03692                                     interpolationValPlus = VIntp[0].GetX();
03693                                     //
03694                                     vertexList[ 0] = V[0] + interpolationValPlus*(V[1] - V[0]);
03695                                     grad[0]        = G[0] + interpolationValPlus*(G[1] - G[0]);
03696                                     texCoordList[0] = VTC[0] + interpolationValPlus*(VTC[1] - VTC[0]);
03697                                 }
03698                                 else {
03699                                     //
03700                                     // Interpotation Data
03701                                     interpolationValNeg = 1.0 - VIntp[1].GetX();
03702                                     //
03703                                     vertexList[ 0] = V[0] + interpolationValNeg*(V[1] - V[0]);
03704                                     grad[0]        = G[0] + interpolationValNeg*(G[1] - G[0]);
03705                                     texCoordList[0] = VTC[0] + interpolationValNeg*(VTC[1] - VTC[0]);
03706                                     //grad[0] *= -1.0;
03707                                 }
03708                                 grad[0].Normalized();
03709                             }
03710                             if ( EdgeTable[cubeClass] & 2 ) {
03711                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
03712                                     //
03713                                     // Interpotation Data
03714                                     interpolationValPlus = VIntp[1].GetY();
03715                                     //
03716                                     vertexList[ 1] = V[1] + interpolationValPlus*(V[2] - V[1]);
03717                                     grad[1]        = G[1] + interpolationValPlus*(G[2] - G[1]);
03718                                     texCoordList[1] = VTC[1] + interpolationValPlus*(VTC[2] - VTC[1]);
03719                                 }
03720                                 else {
03721                                     //
03722                                     // Interpotation Data
03723                                     interpolationValNeg = 1.0 - VIntp[2].GetY();
03724                                     //
03725                                     vertexList[ 1] = V[1] + interpolationValNeg*(V[2] - V[1]);
03726                                     grad[1]        = G[1] + interpolationValNeg*(G[2] - G[1]);
03727                                     texCoordList[1] = VTC[1] + interpolationValNeg*(VTC[2] - VTC[1]);
03728                                     //grad[1] *= -1.0;
03729                                 }
03730                                 grad[1].Normalized();
03731                             }
03732                             if ( EdgeTable[cubeClass] & 4 ) {
03733                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
03734                                     //
03735                                     // Interpotation Data
03736                                     interpolationValPlus = VIntp[2].GetX();
03737                                     //
03738                                     vertexList[ 2] = V[2] + interpolationValPlus*(V[3] - V[2]);
03739                                     grad[2]        = G[2] + interpolationValPlus*(G[3] - G[2]);
03740                                     texCoordList[2] = VTC[2] + interpolationValPlus*(VTC[3] - VTC[2]);
03741                                     //grad[2] *= -1.0;
03742                                 }
03743                                 else {
03744                                     //
03745                                     // Interpotation Data
03746                                     interpolationValNeg = 1.0 - VIntp[3].GetX();
03747                                     //
03748                                     vertexList[ 2] = V[2] + interpolationValNeg*(V[3] - V[2]);
03749                                     grad[2]        = G[2] + interpolationValNeg*(G[3] - G[2]);
03750                                     texCoordList[2] = VTC[2] + interpolationValNeg*(VTC[3] - VTC[2]);
03751                                 }
03752                                 grad[2].Normalized();
03753                             }
03754                             if ( EdgeTable[cubeClass] & 8 ) {
03755                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
03756                                     //
03757                                     // Interpotation Data
03758                                     interpolationValPlus = VIntp[0].GetY();
03759                                     //
03760                                     vertexList[ 3] = V[0] + interpolationValPlus*(V[3] - V[0]);
03761                                     grad[3]        = G[0] + interpolationValPlus*(G[3] - G[0]);
03762                                     texCoordList[3] = VTC[0] + interpolationValPlus*(VTC[3] - VTC[0]);
03763                                     //grad[3][1] *= -1.0;
03764                                 }
03765                                 else {
03766                                     //
03767                                     // Interpotation Data
03768                                     interpolationValNeg = 1.0 - VIntp[3].GetY();
03769                                     //
03770                                     vertexList[ 3] = V[0] + interpolationValNeg*(V[3] - V[0]);
03771                                     grad[3]        = G[0] + interpolationValNeg*(G[3] - G[0]);
03772                                     texCoordList[3] = VTC[0] + interpolationValNeg*(VTC[3] - VTC[0]);
03773                                 }
03774                                 grad[3].Normalized();
03775                             }
03776                             if ( EdgeTable[cubeClass] & 16 ) {
03777                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
03778                                     //
03779                                     // Interpotation Data
03780                                     interpolationValPlus = VIntp[4].GetX();
03781                                     //
03782                                     vertexList[ 4] = V[4] + interpolationValPlus*(V[5] - V[4]);
03783                                     grad[4]        = G[4] + interpolationValPlus*(G[5] - G[4]);
03784                                     texCoordList[4] = VTC[4] + interpolationValPlus*(VTC[5] - VTC[4]);
03785                                 }
03786                                 else {
03787                                     //
03788                                     // Interpotation Data
03789                                     interpolationValNeg = 1.0 - VIntp[5].GetX();
03790                                     //
03791                                     vertexList[ 4] = V[4] + interpolationValNeg*(V[5] - V[4]);
03792                                     grad[4]        = G[4] + interpolationValNeg*(G[5] - G[4]);
03793                                     texCoordList[4] = VTC[4] + interpolationValNeg*(VTC[5] - VTC[4]);
03794                                     //grad[4] *= -1.0;
03795                                 }
03796                                 grad[4].Normalized();
03797                             }
03798                             if ( EdgeTable[cubeClass] & 32 ) {
03799                                 if ( dataForPosition[idx[5] + 3] > threshold ) {
03800                                     //
03801                                     // Interpotation Data
03802                                     interpolationValPlus = VIntp[5].GetY();
03803                                     //
03804                                     vertexList[ 5] = V[5] + interpolationValPlus*(V[6] - V[5]);
03805                                     grad[5]        = G[5] + interpolationValPlus*(G[6] - G[5]);
03806                                     texCoordList[5] = VTC[5] + interpolationValPlus*(VTC[6] - VTC[5]);
03807                                 }
03808                                 else {
03809                                     //
03810                                     // Interpotation Data
03811                                     interpolationValNeg = 1.0 - VIntp[6].GetY();
03812                                     //
03813                                     vertexList[ 5] = V[5] + interpolationValNeg*(V[6] - V[5]);
03814                                     grad[5]        = G[5] + interpolationValNeg*(G[6] - G[5]);
03815                                     texCoordList[5] = VTC[5] + interpolationValNeg*(VTC[6] - VTC[5]);
03816                                     //grad[5][1] *= -1.0;
03817                                 }
03818                                 grad[5].Normalized();
03819                             }
03820                             if ( EdgeTable[cubeClass] & 64 ) {
03821                                 if ( dataForPosition[idx[6] + 3] > threshold ) {
03822                                     //
03823                                     // Interpotation Data
03824                                     interpolationValPlus = VIntp[6].GetX();
03825                                     //
03826                                     vertexList[ 6] = V[6] + interpolationValPlus*(V[7] - V[6]);
03827                                     grad[6]        = G[6] + interpolationValPlus*(G[7] - G[6]);
03828                                     texCoordList[6] = VTC[6] + interpolationValPlus*(VTC[7] - VTC[6]);
03829                                 }
03830                                 else {
03831                                     //
03832                                     // Interpotation Data
03833                                     interpolationValNeg = 1.0 - VIntp[7].GetX();
03834                                     //
03835                                     vertexList[ 6] = V[6] + interpolationValNeg*(V[7] - V[6]);
03836                                     grad[6]        = G[6] + interpolationValNeg*(G[7] - G[6]);
03837                                     texCoordList[6] = VTC[6] + interpolationValNeg*(VTC[7] - VTC[6]);
03838                                     //grad[6] *= -1.0;
03839                                 }
03840                                 grad[6].Normalized();
03841                             }
03842                             if ( EdgeTable[cubeClass] & 128 ) {
03843                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
03844                                     //
03845                                     // Interpotation Data
03846                                     interpolationValPlus = VIntp[4].GetY();
03847                                     //
03848                                     vertexList[ 7] = V[4] + interpolationValPlus*(V[7] - V[4]);
03849                                     grad[7]        = G[4] + interpolationValPlus*(G[7] - G[4]);
03850                                     texCoordList[7] = VTC[4] + interpolationValPlus*(VTC[7] - VTC[4]);
03851                                     //grad[7][1] *= -1.0;
03852                                 }
03853                                 else {
03854                                     //
03855                                     // Interpotation Data
03856                                     interpolationValNeg = 1.0 - VIntp[7].GetY();
03857                                     //
03858                                     vertexList[ 7] = V[4] + interpolationValNeg*(V[7] - V[4]);
03859                                     grad[7]        = G[4] + interpolationValNeg*(G[7] - G[4]);
03860                                     texCoordList[7] = VTC[4] + interpolationValNeg*(VTC[7] - VTC[4]);
03861                                 }
03862                                 grad[7].Normalized();
03863                             }
03864                             if ( EdgeTable[cubeClass] & 256 ) {
03865                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
03866                                     //
03867                                     // Interpotation Data
03868                                     interpolationValPlus = VIntp[0].GetZ();
03869                                     //
03870                                     vertexList[ 8] = V[0] + interpolationValPlus*(V[4] - V[0]);
03871                                     grad[8]        = G[0] + interpolationValPlus*(G[4] - G[0]);
03872                                     texCoordList[8] = VTC[0] + interpolationValPlus*(VTC[4] - VTC[0]);
03873                                 }
03874                                 else {
03875                                     //
03876                                     // Interpotation Data
03877                                     interpolationValNeg = 1.0 - VIntp[4].GetZ();
03878                                     //
03879                                     vertexList[ 8] = V[0] + interpolationValNeg*(V[4] - V[0]);
03880                                     grad[8]        = G[0] + interpolationValNeg*(G[4] - G[0]);
03881                                     texCoordList[8] = VTC[0] + interpolationValNeg*(VTC[4] - VTC[0]);
03882                                     //grad[8][2] *= -1.0;
03883                                 }
03884                                 grad[8].Normalized();
03885                             }
03886                             if ( EdgeTable[cubeClass] & 512 ) {
03887                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
03888                                     //
03889                                     // Interpotation Data
03890                                     interpolationValPlus = VIntp[1].GetZ();
03891                                     //
03892                                     vertexList[ 9] = V[1] + interpolationValPlus*(V[5] - V[1]);
03893                                     grad[9]        = G[1] + interpolationValPlus*(G[5] - G[1]);
03894                                     texCoordList[9] = VTC[1] + interpolationValPlus*(VTC[5] - VTC[1]);
03895                                 }
03896                                 else {
03897                                     //
03898                                     // Interpotation Data
03899                                     interpolationValNeg = 1.0 - VIntp[5].GetZ();
03900                                     //
03901                                     vertexList[ 9] = V[1] + interpolationValNeg*(V[5] - V[1]);
03902                                     grad[9]        = G[1] + interpolationValNeg*(G[5] - G[1]);
03903                                     texCoordList[9] = VTC[1] + interpolationValNeg*(VTC[5] - VTC[1]);
03904                                     //grad[9][2] *= -1.0;
03905                                 }
03906                                 grad[9].Normalized();
03907                             }
03908                             if ( EdgeTable[cubeClass] & 1024 ) {
03909                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
03910                                     //
03911                                     // Interpotation Data
03912                                     interpolationValPlus = VIntp[2].GetZ();
03913                                     //
03914                                     vertexList[10] = V[2] + interpolationValPlus*(V[6] - V[2]);
03915                                     grad[10]       = G[2] + interpolationValPlus*(G[6] - G[2]);
03916                                     texCoordList[10] = VTC[2] + interpolationValPlus*(VTC[6] - VTC[2]);
03917                                 }
03918                                 else {
03919                                     //
03920                                     // Interpotation Data
03921                                     interpolationValNeg = 1.0 - VIntp[6].GetZ();
03922                                     //
03923                                     vertexList[10] = V[2] + interpolationValNeg*(V[6] - V[2]);
03924                                     grad[10]       = G[2] + interpolationValNeg*(G[6] - G[2]);
03925                                     texCoordList[10] = VTC[2] + interpolationValNeg*(VTC[6] - VTC[2]);
03926                                     //grad[10][2] *= -1.0;
03927                                 }
03928                                 grad[10].Normalized();
03929                             }
03930                             if ( EdgeTable[cubeClass] & 2048 ) {
03931                                 if ( dataForPosition[idx[3] + 3] > threshold ) {
03932                                     //
03933                                     // Interpotation Data
03934                                     interpolationValPlus = VIntp[3].GetZ();
03935                                     //
03936                                     vertexList[11] = V[3] + interpolationValPlus*(V[7] - V[3]);
03937                                     grad[11]       = G[3] + interpolationValPlus*(G[7] - G[3]);
03938                                     texCoordList[11] = VTC[3] + interpolationValPlus*(VTC[7] - VTC[3]);
03939                                 }
03940                                 else {
03941                                     //
03942                                     // Interpotation Data
03943                                     interpolationValNeg = 1.0 - VIntp[7].GetZ();
03944                                     //
03945                                     vertexList[11] = V[3] + interpolationValNeg*(V[7] - V[3]);
03946                                     grad[11]       = G[3] + interpolationValNeg*(G[7] - G[3]);
03947                                     texCoordList[11] = VTC[3] + interpolationValNeg*(VTC[7] - VTC[3]);
03948                                     //grad[11][2] *= -1.0;
03949                                 }
03950                                 grad[11].Normalized();
03951                             }
03952                         } // END: Not Use Global Interpolation Value
03953                         //-----------------------------------------------
03954                         // Use Global Interpolation Value -- all elements have the same interpolation value
03955                         else {
03956                             //-------------------------------------
03957                             // Marching Cube Interpolation
03958                             T interpolationValPlus = m_tGlobalInterpolationVal;
03959                             T interpolationValNeg  = 1.0 - m_tGlobalInterpolationVal;
03960                             //-------------------------------------
03961                             // Edge# 0 to 11 <==> [ 1, 2, 4, ... , 2048 ]
03962                             if ( EdgeTable[cubeClass] & 1 ) {
03963                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
03964                                     //
03965                                     // Interpotation Data
03966                                     //interpolationValPlus = VIntp[0].GetX();
03967                                     //
03968                                     vertexList[ 0] = V[0] + interpolationValPlus*(V[1] - V[0]);
03969                                     grad[0]        = G[0] + interpolationValPlus*(G[1] - G[0]);
03970                                     texCoordList[0] = VTC[0] + interpolationValPlus*(VTC[1] - VTC[0]);
03971                                 }
03972                                 else {
03973                                     //
03974                                     // Interpotation Data
03975                                     //interpolationValNeg = 1.0 - VIntp[1].GetX();
03976                                     //
03977                                     vertexList[ 0] = V[0] + interpolationValNeg*(V[1] - V[0]);
03978                                     grad[0]        = G[0] + interpolationValNeg*(G[1] - G[0]);
03979                                     texCoordList[0] = VTC[0] + interpolationValNeg*(VTC[1] - VTC[0]);
03980                                     //grad[0] *= -1.0;
03981                                 }
03982                                 grad[0].Normalized();
03983                             }
03984                             if ( EdgeTable[cubeClass] & 2 ) {
03985                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
03986                                     //
03987                                     // Interpotation Data
03988                                     //interpolationValPlus = VIntp[1].GetY();
03989                                     //
03990                                     vertexList[ 1] = V[1] + interpolationValPlus*(V[2] - V[1]);
03991                                     grad[1]        = G[1] + interpolationValPlus*(G[2] - G[1]);
03992                                     texCoordList[1] = VTC[1] + interpolationValPlus*(VTC[2] - VTC[1]);
03993                                 }
03994                                 else {
03995                                     //
03996                                     // Interpotation Data
03997                                     //interpolationValNeg = 1.0 - VIntp[2].GetY();
03998                                     //
03999                                     vertexList[ 1] = V[1] + interpolationValNeg*(V[2] - V[1]);
04000                                     grad[1]        = G[1] + interpolationValNeg*(G[2] - G[1]);
04001                                     texCoordList[1] = VTC[1] + interpolationValNeg*(VTC[2] - VTC[1]);
04002                                     //grad[1] *= -1.0;
04003                                 }
04004                                 grad[1].Normalized();
04005                             }
04006                             if ( EdgeTable[cubeClass] & 4 ) {
04007                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
04008                                     //
04009                                     // Interpotation Data
04010                                     //interpolationValPlus = VIntp[2].GetX();
04011                                     //
04012                                     vertexList[ 2] = V[2] + interpolationValPlus*(V[3] - V[2]);
04013                                     grad[2]        = G[2] + interpolationValPlus*(G[3] - G[2]);
04014                                     texCoordList[2] = VTC[2] + interpolationValPlus*(VTC[3] - VTC[2]);
04015                                     //grad[2] *= -1.0;
04016                                 }
04017                                 else {
04018                                     //
04019                                     // Interpotation Data
04020                                     //interpolationValNeg = 1.0 - VIntp[3].GetX();
04021                                     //
04022                                     vertexList[ 2] = V[2] + interpolationValNeg*(V[3] - V[2]);
04023                                     grad[2]        = G[2] + interpolationValNeg*(G[3] - G[2]);
04024                                     texCoordList[2] = VTC[2] + interpolationValNeg*(VTC[3] - VTC[2]);
04025                                 }
04026                                 grad[2].Normalized();
04027                             }
04028                             if ( EdgeTable[cubeClass] & 8 ) {
04029                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
04030                                     //
04031                                     // Interpotation Data
04032                                     //interpolationValPlus = VIntp[0].GetY();
04033                                     //
04034                                     vertexList[ 3] = V[0] + interpolationValPlus*(V[3] - V[0]);
04035                                     grad[3]        = G[0] + interpolationValPlus*(G[3] - G[0]);
04036                                     texCoordList[3] = VTC[0] + interpolationValPlus*(VTC[3] - VTC[0]);
04037                                     //grad[3][1] *= -1.0;
04038                                 }
04039                                 else {
04040                                     //
04041                                     // Interpotation Data
04042                                     //interpolationValNeg = 1.0 - VIntp[3].GetY();
04043                                     //
04044                                     vertexList[ 3] = V[0] + interpolationValNeg*(V[3] - V[0]);
04045                                     grad[3]        = G[0] + interpolationValNeg*(G[3] - G[0]);
04046                                     texCoordList[3] = VTC[0] + interpolationValNeg*(VTC[3] - VTC[0]);
04047                                 }
04048                                 grad[3].Normalized();
04049                             }
04050                             if ( EdgeTable[cubeClass] & 16 ) {
04051                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
04052                                     //
04053                                     // Interpotation Data
04054                                     //interpolationValPlus = VIntp[4].GetX();
04055                                     //
04056                                     vertexList[ 4] = V[4] + interpolationValPlus*(V[5] - V[4]);
04057                                     grad[4]        = G[4] + interpolationValPlus*(G[5] - G[4]);
04058                                     texCoordList[4] = VTC[4] + interpolationValPlus*(VTC[5] - VTC[4]);
04059                                 }
04060                                 else {
04061                                     //
04062                                     // Interpotation Data
04063                                     //interpolationValNeg = 1.0 - VIntp[5].GetX();
04064                                     //
04065                                     vertexList[ 4] = V[4] + interpolationValNeg*(V[5] - V[4]);
04066                                     grad[4]        = G[4] + interpolationValNeg*(G[5] - G[4]);
04067                                     texCoordList[4] = VTC[4] + interpolationValNeg*(VTC[5] - VTC[4]);
04068                                     //grad[4] *= -1.0;
04069                                 }
04070                                 grad[4].Normalized();
04071                             }
04072                             if ( EdgeTable[cubeClass] & 32 ) {
04073                                 if ( dataForPosition[idx[5] + 3] > threshold ) {
04074                                     //
04075                                     // Interpotation Data
04076                                     //interpolationValPlus = VIntp[5].GetY();
04077                                     //
04078                                     vertexList[ 5] = V[5] + interpolationValPlus*(V[6] - V[5]);
04079                                     grad[5]        = G[5] + interpolationValPlus*(G[6] - G[5]);
04080                                     texCoordList[5] = VTC[5] + interpolationValPlus*(VTC[6] - VTC[5]);
04081                                 }
04082                                 else {
04083                                     //
04084                                     // Interpotation Data
04085                                     //interpolationValNeg = 1.0 - VIntp[6].GetY();
04086                                     //
04087                                     vertexList[ 5] = V[5] + interpolationValNeg*(V[6] - V[5]);
04088                                     grad[5]        = G[5] + interpolationValNeg*(G[6] - G[5]);
04089                                     texCoordList[5] = VTC[5] + interpolationValNeg*(VTC[6] - VTC[5]);
04090                                     //grad[5][1] *= -1.0;
04091                                 }
04092                                 grad[5].Normalized();
04093                             }
04094                             if ( EdgeTable[cubeClass] & 64 ) {
04095                                 if ( dataForPosition[idx[6] + 3] > threshold ) {
04096                                     //
04097                                     // Interpotation Data
04098                                     //interpolationValPlus = VIntp[6].GetX();
04099                                     //
04100                                     vertexList[ 6] = V[6] + interpolationValPlus*(V[7] - V[6]);
04101                                     grad[6]        = G[6] + interpolationValPlus*(G[7] - G[6]);
04102                                     texCoordList[6] = VTC[6] + interpolationValPlus*(VTC[7] - VTC[6]);
04103                                 }
04104                                 else {
04105                                     //
04106                                     // Interpotation Data
04107                                     //interpolationValNeg = 1.0 - VIntp[7].GetX();
04108                                     //
04109                                     vertexList[ 6] = V[6] + interpolationValNeg*(V[7] - V[6]);
04110                                     grad[6]        = G[6] + interpolationValNeg*(G[7] - G[6]);
04111                                     texCoordList[6] = VTC[6] + interpolationValNeg*(VTC[7] - VTC[6]);
04112                                     //grad[6] *= -1.0;
04113                                 }
04114                                 grad[6].Normalized();
04115                             }
04116                             if ( EdgeTable[cubeClass] & 128 ) {
04117                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
04118                                     //
04119                                     // Interpotation Data
04120                                     //interpolationValPlus = VIntp[4].GetY();
04121                                     //
04122                                     vertexList[ 7] = V[4] + interpolationValPlus*(V[7] - V[4]);
04123                                     grad[7]        = G[4] + interpolationValPlus*(G[7] - G[4]);
04124                                     texCoordList[7] = VTC[4] + interpolationValPlus*(VTC[7] - VTC[4]);
04125                                     //grad[7][1] *= -1.0;
04126                                 }
04127                                 else {
04128                                     //
04129                                     // Interpotation Data
04130                                     //interpolationValNeg = 1.0 - VIntp[7].GetY();
04131                                     //
04132                                     vertexList[ 7] = V[4] + interpolationValNeg*(V[7] - V[4]);
04133                                     grad[7]        = G[4] + interpolationValNeg*(G[7] - G[4]);
04134                                     texCoordList[7] = VTC[4] + interpolationValNeg*(VTC[7] - VTC[4]);
04135                                 }
04136                                 grad[7].Normalized();
04137                             }
04138                             if ( EdgeTable[cubeClass] & 256 ) {
04139                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
04140                                     //
04141                                     // Interpotation Data
04142                                     //interpolationValPlus = VIntp[0].GetZ();
04143                                     //
04144                                     vertexList[ 8] = V[0] + interpolationValPlus*(V[4] - V[0]);
04145                                     grad[8]        = G[0] + interpolationValPlus*(G[4] - G[0]);
04146                                     texCoordList[8] = VTC[0] + interpolationValPlus*(VTC[4] - VTC[0]);
04147                                 }
04148                                 else {
04149                                     //
04150                                     // Interpotation Data
04151                                     //interpolationValNeg = 1.0 - VIntp[4].GetZ();
04152                                     //
04153                                     vertexList[ 8] = V[0] + interpolationValNeg*(V[4] - V[0]);
04154                                     grad[8]        = G[0] + interpolationValNeg*(G[4] - G[0]);
04155                                     texCoordList[8] = VTC[0] + interpolationValNeg*(VTC[4] - VTC[0]);
04156                                     //grad[8][2] *= -1.0;
04157                                 }
04158                                 grad[8].Normalized();
04159                             }
04160                             if ( EdgeTable[cubeClass] & 512 ) {
04161                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
04162                                     //
04163                                     // Interpotation Data
04164                                     //interpolationValPlus = VIntp[1].GetZ();
04165                                     //
04166                                     vertexList[ 9] = V[1] + interpolationValPlus*(V[5] - V[1]);
04167                                     grad[9]        = G[1] + interpolationValPlus*(G[5] - G[1]);
04168                                     texCoordList[9] = VTC[1] + interpolationValPlus*(VTC[5] - VTC[1]);
04169                                 }
04170                                 else {
04171                                     //
04172                                     // Interpotation Data
04173                                     //interpolationValNeg = 1.0 - VIntp[5].GetZ();
04174                                     //
04175                                     vertexList[ 9] = V[1] + interpolationValNeg*(V[5] - V[1]);
04176                                     grad[9]        = G[1] + interpolationValNeg*(G[5] - G[1]);
04177                                     texCoordList[9] = VTC[1] + interpolationValNeg*(VTC[5] - VTC[1]);
04178                                     //grad[9][2] *= -1.0;
04179                                 }
04180                                 grad[9].Normalized();
04181                             }
04182                             if ( EdgeTable[cubeClass] & 1024 ) {
04183                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
04184                                     //
04185                                     // Interpotation Data
04186                                     //interpolationValPlus = VIntp[2].GetZ();
04187                                     //
04188                                     vertexList[10] = V[2] + interpolationValPlus*(V[6] - V[2]);
04189                                     grad[10]       = G[2] + interpolationValPlus*(G[6] - G[2]);
04190                                     texCoordList[10] = VTC[2] + interpolationValPlus*(VTC[6] - VTC[2]);
04191                                 }
04192                                 else {
04193                                     //
04194                                     // Interpotation Data
04195                                     //interpolationValNeg = 1.0 - VIntp[6].GetZ();
04196                                     //
04197                                     vertexList[10] = V[2] + interpolationValNeg*(V[6] - V[2]);
04198                                     grad[10]       = G[2] + interpolationValNeg*(G[6] - G[2]);
04199                                     texCoordList[10] = VTC[2] + interpolationValNeg*(VTC[6] - VTC[2]);
04200                                     //grad[10][2] *= -1.0;
04201                                 }
04202                                 grad[10].Normalized();
04203                             }
04204                             if ( EdgeTable[cubeClass] & 2048 ) {
04205                                 if ( dataForPosition[idx[3] + 3] > threshold ) {
04206                                     //
04207                                     // Interpotation Data
04208                                     //interpolationValPlus = VIntp[3].GetZ();
04209                                     //
04210                                     vertexList[11] = V[3] + interpolationValPlus*(V[7] - V[3]);
04211                                     grad[11]       = G[3] + interpolationValPlus*(G[7] - G[3]);
04212                                     texCoordList[11] = VTC[3] + interpolationValPlus*(VTC[7] - VTC[3]);
04213                                 }
04214                                 else {
04215                                     //
04216                                     // Interpotation Data
04217                                     //interpolationValNeg = 1.0 - VIntp[7].GetZ();
04218                                     //
04219                                     vertexList[11] = V[3] + interpolationValNeg*(V[7] - V[3]);
04220                                     grad[11]       = G[3] + interpolationValNeg*(G[7] - G[3]);
04221                                     texCoordList[11] = VTC[3] + interpolationValNeg*(VTC[7] - VTC[3]);
04222                                     //grad[11][2] *= -1.0;
04223                                 }
04224                                 grad[11].Normalized();
04225                             }
04226                         } // END: Use Global Interpolation Value
04227                         //-----------------------------------------------
04228                         // END: if/else statement for MC Interpolation
04229                         //===============================================
04230 
04231                         //===============================================
04232                         //-----------------------------------------------
04233                         // Generate Triangle Index
04234                         numOfTrisPerCell = 0;
04235                         for ( d = 0; TriTable[cubeClass][d] != -1; ++d ) {
04236                             triIndices[d] = TriTable[cubeClass][d];
04237                         }
04238                         numOfTrisPerCell = d / 3;
04239                         //-----------------------------------------------
04240                         //===============================================
04241                     }
04242 
04243                     /*
04244                     //-------------------------------
04245                     // Draw (Gradient) Normals
04246                     {
04247                         glPushAttrib( GL_ALL_ATTRIB_BITS );
04248                         glBegin( GL_LINES );
04249                         glDisable( GL_TEXTURE_3D );
04250                         glDisable( GL_BLEND );
04251                         glDisable( GL_LIGHTING );
04252                         for ( int i = 0; i < numOfTrisPerCell*3; i+=3 ) {
04253                             glColor3f( 1, 0, 0 );
04254                             glVertex3fv( vertexList[triIndices[i+2]].GetDataFloat() );
04255                             glVertex3fv( ( vertexList[triIndices[i+2]] + grad[triIndices[i+2]]*0.15 ).GetDataFloat() );
04256                             glColor3f( 0, 1, 0 );
04257                             glVertex3fv( vertexList[triIndices[i+1]].GetDataFloat() );
04258                             glVertex3fv( ( vertexList[triIndices[i+1]] + grad[triIndices[i+1]]*0.15 ).GetDataFloat() );
04259                             glColor3f( 0, 0, 1 );
04260                             glVertex3fv( vertexList[triIndices[i+0]].GetDataFloat() );
04261                             glVertex3fv( ( vertexList[triIndices[i+0]] + grad[triIndices[i+0]]*0.15 ).GetDataFloat() );
04262                         }
04263                         glEnd();
04264                         glPopAttrib();
04265                     }
04266                     //-------------------------------
04267                     //*/
04268 
04269                     //-------------------------------
04270                     // Draw PN-Triangles generated 
04271                     // from Marching Cube Triangles
04272                     if ( m_bDrawPNTriangle ) {
04273                         int i = 0;
04274                         while ( numOfTrisPerCell > 0 ) {
04275                             PNTriangle<T>::DrawByOpenGL( 
04276                                 vertexList[triIndices[i+2]], 
04277                                 vertexList[triIndices[i+1]], 
04278                                 vertexList[triIndices[i+0]], 
04279                                 grad[triIndices[i+2]], 
04280                                 grad[triIndices[i+1]], 
04281                                 grad[triIndices[i+0]], 
04282                                 texCoordList[triIndices[i+2]], 
04283                                 texCoordList[triIndices[i+1]], 
04284                                 texCoordList[triIndices[i+0]], 
04285                                 m_PNTriangleSmoothness 
04286                             );
04287                             --numOfTrisPerCell;
04288                             i += 3;
04289                         }
04290                     }
04291                     // DEBUG for PNTri Drawing
04292                     //glUseProgramObjectARB( g_PNTriProg );
04293                     //
04294                     // DEBUG for PNTri Drawing
04295                     //glUseProgramObjectARB( NULL );
04296 
04297                     //-------------------------------
04298                     // Draw Marching Cube Triangles
04299                     else {
04300 
04301                         /*
04302                         //*************************************
04303                         // DEBUG
04304                         {
04305                             glPushAttrib( GL_ALL_ATTRIB_BITS );
04306                             glDisable( GL_LIGHTING );
04307                             glDisable( GL_BLEND );
04308                             glDisable( GL_TEXTURE_3D );
04309                             glColor3ub( 200, 50, 100 );
04310                             //glColor3f( 0.0, 0.0, 1.0 );
04311                             glBegin( GL_POINTS );
04312                             int count = numOfTrisPerCell;
04313                             int i = 0;
04314                             while ( count > 0 ) {
04315                                 //glTexCoord3fv( texCoordList[triIndices[i+2]].GetDataFloat() );
04316                                 glColor3f( 1, 0, 0 );
04317                                 glNormal3fv( grad[triIndices[i+2]].GetDataFloat() );
04318                                 glVertex3fv( vertexList[triIndices[i+2]].GetDataFloat() );
04319                                 //
04320                                 //glTexCoord3fv( texCoordList[triIndices[i+1]].GetDataFloat() );
04321                                 glColor3f( 0, 1, 0 );
04322                                 glNormal3fv( grad[triIndices[i+1]].GetDataFloat() );
04323                                 glVertex3fv( vertexList[triIndices[i+1]].GetDataFloat() );
04324                                 //
04325                                 //glTexCoord3fv( texCoordList[triIndices[i+0]].GetDataFloat() );
04326                                 glColor3f( 0, 0, 1 );
04327                                 glNormal3fv( grad[triIndices[i+0]].GetDataFloat() );
04328                                 glVertex3fv( vertexList[triIndices[i+0]].GetDataFloat() );
04329                                 //
04330                                 --count;
04331                                 i += 3;
04332                             }
04333                             glEnd();
04334                             glPopAttrib();
04335                         }
04336                         //*************************************
04337                         //*/
04338 
04339                         //glBegin( GL_LINE_LOOP );
04340                         glBegin( GL_TRIANGLES );
04341                         int i = 0;
04342                         while ( numOfTrisPerCell > 0 ) {
04343                             glTexCoord3fv( texCoordList[triIndices[i+2]].GetDataFloat() );
04344                             glNormal3fv( grad[triIndices[i+2]].GetDataFloat() );
04345                             glVertex3fv( vertexList[triIndices[i+2]].GetDataFloat() );
04346                             //
04347                             glTexCoord3fv( texCoordList[triIndices[i+1]].GetDataFloat() );
04348                             glNormal3fv( grad[triIndices[i+1]].GetDataFloat() );
04349                             glVertex3fv( vertexList[triIndices[i+1]].GetDataFloat() );
04350                             //
04351                             glTexCoord3fv( texCoordList[triIndices[i+0]].GetDataFloat() );
04352                             glNormal3fv( grad[triIndices[i+0]].GetDataFloat() );
04353                             glVertex3fv( vertexList[triIndices[i+0]].GetDataFloat() );
04354                             //
04355                             --numOfTrisPerCell;
04356                             i += 3;
04357                         }
04358                         glEnd();
04359                     }
04360 
04361                     //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
04363                     //if ( isFirstRun ) {
04364                     //  if ( cubeClass < 100 )  std::cout << " ";
04365                     //  if ( cubeClass < 10 )   std::cout << " ";
04366                     //  std::cout << "  " << static_cast<int>( cubeClass );
04367                     //}
04368                     //#endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
04369 
04370                 } // END: // TWO ITERATIONS FOR LEFT AND RIGHT CUBE CONFIGURATIONS
04371 
04372                 //-------------------------------
04373                 // Next Cell
04374                 for ( i = 0; i < 8; ++i ) {
04375                     idx[i] += 4;
04376                     //
04377                     intpIdx[i] += 3;
04378                 }
04379                 //-------------------------------
04380 
04381             } // END: for loop for X
04382 
04383             //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
04385             //if ( isFirstRun ) std::cout << "\n";
04386             //#endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
04387 
04388         } // END: for loop for Y
04389 
04390         //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
04392         //if ( isFirstRun ) std::cout << "\n";
04393         //#endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
04394 
04395     } // END: for loop for Z
04396 
04397     //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
04399     //if ( isFirstRun ) {
04400     //  std::cout << "-------------------------\n";
04401     //}
04403     //#endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
04404 
04405     glPopAttrib();
04406 
04407     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
04408     m_glslProgramObjectForShaderSurfaceFromSW->EndGLSL();
04409     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
04410     //---------------------------------------------------------------
04411     //===============================================================
04412     //*/
04413 
04414     //---------------------------------------------------------------
04415     //===============================================================
04416     delete [] dataForPosition;
04417     delete [] dataForEllipsoidInfluence;
04418 }
04419 //-----------------------------------------------------------------------------
04420 // END: DrawByGL_StandardMC
04421 //*****************************************************************************
04422 
04423 
04427 //*****************************************************************************
04428 // BEGIN: DrawByGL
04429 //-----------------------------------------------------------------------------
04430 template <typename T>
04431 void ModelDeformableGLSL_Visualization_RTGenMesh<T>::DrawByGL ()
04432 {
04444     //---------------------------------------------------------------
04445     // For fixed length (by interpolation) after cutting
04446     const Vector3<T> CUBE_DIMENSIONS = m_SimDomain->RetGridDimension();
04447     //---------------------------------------------------------------
04448 
04449 //*
04450     //---------------------------------------------------------------
04451 #ifdef TAPs_DEBUG_MODE
04452     if ( !m_SimDomain ) return;
04453     //---------------------------------------------------------------
04454     if ( !m_SimDomain->RetObject3DTexturePosition() )   return;
04455     //---------------------------------------------------------------
04456 #endif
04457     //===============================================================
04458     // Read data from GPU -- Positions
04459     //---------------------------------------------------------------
04460     Texture * texture3DPosition = m_SimDomain->RetObject3DTexturePosition();
04461     //---------------------------------------------------------------
04462     glPushAttrib( GL_ALL_ATTRIB_BITS );
04463     //-----------------------------------------------------
04464     GLsizei sizeForPosition = 
04465             texture3DPosition->GetWidth() *
04466             texture3DPosition->GetHeight() *
04467             texture3DPosition->GetDepth() *
04468             texture3DPosition->GetInternalFormatNumberOfComponents();
04469     GLfloat * dataForPosition = new GLfloat[ sizeForPosition ];
04470     {
04471         int i = 0;
04472         //=================================================
04473         // Read data for positions from a texture to memory
04474         glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
04475         texture3DPosition->BindTexture(0);
04476         glGetTexImage( 
04477             texture3DPosition->GetTarget(),
04478             texture3DPosition->GetLevel(),
04479             texture3DPosition->GetPixelFormat(),
04480             texture3DPosition->GetDataType(),
04481             dataForPosition
04482         );
04483         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
04484         //=================================================
04485     }
04486     //---------------------------------------------------------------
04487     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
04488     //-----------------------------------------------------
04489     glPopAttrib();
04490     //---------------------------------------------------------------
04491     //===============================================================
04492 
04493     //===============================================================
04494     // Read data from GPU -- Ellipsoid Influence
04495     //---------------------------------------------------------------
04496     Texture * texture3DEllipsoidInfluence = m_SimDomain->RetObject3DTextureEllipsoidInfluence();
04497     //---------------------------------------------------------------
04498     glPushAttrib( GL_ALL_ATTRIB_BITS );
04499     //-----------------------------------------------------
04500     GLsizei sizeForEllipsoidInfluence = 
04501             texture3DEllipsoidInfluence->GetWidth() *
04502             texture3DEllipsoidInfluence->GetHeight() *
04503             texture3DEllipsoidInfluence->GetDepth() *
04504             texture3DEllipsoidInfluence->GetInternalFormatNumberOfComponents();
04505     //std::cout << "sizeForEllipsoidInfluence: " << sizeForEllipsoidInfluence << "\n";
04506     GLfloat * dataForEllipsoidInfluence = new GLfloat[ sizeForEllipsoidInfluence ];
04507     {
04508         int i = 0;
04509         //=================================================
04510         // Read data for ellipsoid influences from a texture to memory
04511         glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
04512         texture3DEllipsoidInfluence->BindTexture(0);
04513         glGetTexImage( 
04514             texture3DEllipsoidInfluence->GetTarget(),
04515             texture3DEllipsoidInfluence->GetLevel(),
04516             texture3DEllipsoidInfluence->GetPixelFormat(),
04517             texture3DEllipsoidInfluence->GetDataType(),
04518             dataForEllipsoidInfluence
04519         );
04520         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
04521         //=================================================
04522     }
04523     //---------------------------------------------------------------
04524     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
04525     //-----------------------------------------------------
04526     glPopAttrib();
04527     //---------------------------------------------------------------
04528     //===============================================================
04529 
04530     //---------------------------------
04531     Vector3<int> gridResolution = m_SimDomain->GetGridResolution();
04532     Vector3<T>   gridDimension  = m_SimDomain->GetGridDimension();
04533     //---------------------------------
04534 
04535     //*
04536     //===============================================================
04537     // Draw by Marching Cube
04538     //---------------------------------------------------------------
04539     // Adapted from "Polygonising a scalar field" by Paul Bourke (1994)
04540     // http://local.wasp.uwa.edu.au/~pbourke/geometry/polygonise/
04541     //---------------------------------------------------------------
04542     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
04543     m_glslProgramObjectForShaderSurfaceFromSW->BeginGLSL();
04544     if ( m_3DTexture ) {
04545         glActiveTexture( GL_TEXTURE0 );
04546         glBindTexture( GL_TEXTURE_3D, m_3DTexture );
04547         m_glslProgramObjectForShaderSurfaceFromSW->SetUniform1i( "SurfaceTexture", 0 );
04548         TAPs::OpenGL::Fn::CHECK_GL_ERROR();
04549     }
04550     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
04551 
04552     glPushAttrib( GL_ALL_ATTRIB_BITS );
04553     //glLineWidth( 2 );
04554     glPointSize( 5 );
04555 
04556     //glColor3ub( 240/2, 220/2, 130/2 );
04557     //glColor3ub( 240/8, 220/8, 130/8 );
04558     //glColor3ub( 65, 65, 65 );
04559 
04560     glColor3ub( 75, 75, 75 );
04561 
04562     //glColor3ub( 150, 150, 150 );
04563     //glColor3ub( 255, 255, 255 );
04564     
04565     //glEnable( GL_COLOR_MATERIAL );
04566 
04567     /*
04568     GLfloat diffuseMaterial[4]  = { 0.5f, 0.5f, 0.5f, 1.0f };
04569     GLfloat specularMaterial[4] = { 0.8f, 0.8f, 0.8f, 1.0f };
04570     GLfloat ambientMaterial[4]  = { 0.1f, 0.1f, 0.1f, 1.0f };
04571     GLfloat shininessMaterial[] = { 128.0 };
04572     glMaterialfv( GL_FRONT, GL_AMBIENT,  ambientMaterial );
04573     glMaterialfv( GL_FRONT, GL_SPECULAR, specularMaterial );
04574     glMaterialfv( GL_FRONT, GL_DIFFUSE,  diffuseMaterial );
04575     glMaterialfv( GL_FRONT, GL_SHININESS,  diffuseMaterial );
04576     //*/
04577 
04578     glEnable( GL_LIGHTING );
04579     //glDisable( GL_LIGHTING );
04580 
04581     // DEBUG for two-sided lighting
04582     glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
04583 
04584     //---------------------------------------------------------------
04585     // Texture Parameter Setup
04586 
04587     /*
04588     // 3D Texture for surface
04589     glEnable( GL_TEXTURE_3D );
04590     glBindTexture( GL_TEXTURE_3D, m_3DTexture );
04591     //*/
04592 
04593     // Composition
04594     glEnable( GL_BLEND );
04595     glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
04596     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
04597     //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );
04598     //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
04599     //glTexEnvf( GL_TEXTURE_ENV, GL_RGB_SCALE, 0.5 );
04600     //glTexEnvf( GL_TEXTURE_ENV, GL_ALPHA_SCALE, 0.5 );
04601     //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD );
04602 
04603     /*
04604     static GLfloat constColor[4] = { 0.0, 0.0, 0.0, 0.0 };
04605     constColor[3] = 0.2;
04606     glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, constColor );
04607     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
04608     glTexEnvf( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE );
04609     glTexEnvf( GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE );
04610     glTexEnvf( GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR );
04611     glTexEnvf( GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PREVIOUS );
04612     glTexEnvf( GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR );
04613     glTexEnvf( GL_TEXTURE_ENV, GL_SRC2_RGB, GL_PREVIOUS );
04614     glTexEnvf( GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA );
04615     //*/
04616 
04617     //---------------------------------------------------------------
04618     int iOffsetSlice = gridResolution[1] * gridResolution[0] * 4;
04619     int iOffsetRow = gridResolution[0] * 4;
04620     int iOffsetCol = 4;
04621     //---------------------------------
04622     // Vertex & Edge Index
04623     //          3------2------2
04624     //         /|            /|
04625     //       11 |          10 |
04626     //       /  3          /  1
04627     //      7------6------6   |
04628     //      |   |         |   |
04629     //      |   0------0--|---1
04630     //      7  /          5  /
04631     //      | 8           | 9 
04632     //      |/            |/
04633     //      4------4------5
04634     //---------------------------------
04635     // Edge Table [256] & Triangle Table [256][16]
04643     // V = V1 + (isovalue - s1) (V2 - V1) / (s2 - s1)
04644     //---------------------------------
04645     //unsigned char cubeClass = 0;
04646     //if ( grid.val[0] < isolevel ) cubeClass |= 1;
04647     //if ( grid.val[1] < isolevel ) cubeClass |= 2;
04648     //if ( grid.val[2] < isolevel ) cubeClass |= 4;
04649     //if ( grid.val[3] < isolevel ) cubeClass |= 8;
04650     //if ( grid.val[4] < isolevel ) cubeClass |= 16;
04651     //if ( grid.val[5] < isolevel ) cubeClass |= 32;
04652     //if ( grid.val[6] < isolevel ) cubeClass |= 64;
04653     //if ( grid.val[7] < isolevel ) cubeClass |= 128;
04654     //---------------------------------
04655     int numOfTrisPerCell = 0;   // number of triangles in the cell
04656     int triIndices[15];         // at most 5 triangles
04657     Vector3<T>  V[8];           // vertices of the cell
04658     Vector3<T>  VTC[8];         // texture coordinates of the vertices
04659     Vector3<T>  texCoordList[12];   // since 12 edges
04660     Vector3<T>  vertexList[12]; // since 12 edges
04661     int         bV[12];         // >=0 if the vertex is counted (e.g. on boundary, existing, etc.)
04662     Vector3<T>  grad[12];       // gradients of vertices
04663     //Vector3<T> texCoords[12]; // 
04664     Vector3<T> Center;//, nE, nW, nN, nS, nF, nB;   // center and its neighbors for gradient computation
04665     //---------------------------------
04666     int idx[8], d, i;
04667     unsigned char cubeClass = 0;
04668     T threshold  = 250;
04669     //
04670     T tc[3];
04671     T tcStep[3] = {
04672         1.0 / static_cast<T>( gridResolution[0]-1 ), 
04673         1.0 / static_cast<T>( gridResolution[1]-1 ), 
04674         1.0 / static_cast<T>( gridResolution[2]-1 ) 
04675     };
04676     //
04677 
04678     //-----------------------------------------------------
04679     // For Interpolation Data used by Marching Cube
04680     int iOffsetSliceIntp = gridResolution[1] * gridResolution[0] * 3;
04681     int iOffsetRowIntp = gridResolution[0] * 3;
04682     int iOffsetColIntp = 3;
04683     int intpIdx[8];
04684     Vector3<T> VIntp[8];    // Interpolation Data (XYZ)
04685     //-----------------------------------------------------
04686 
04687     //---------------------------------
04688     //for ( int Z = gridResolution[2]*3/4; Z < gridResolution[2]-1; ++Z ) {
04689     for ( int Z = 0; Z < gridResolution[2]-1; ++Z ) {
04690         //
04691         // For Interpolation Data
04692         int offsetZIntp = Z * iOffsetSliceIntp;
04693         //
04694         int offsetZ = Z * iOffsetSlice;
04695         //
04696         // Texture Coordinates -- Z
04697         tc[2] = static_cast<T>( Z ) / static_cast<T>( gridResolution[2]-1 );
04698         VTC[0].SetZ( tc[2] );
04699         VTC[1].SetZ( tc[2] );
04700         VTC[2].SetZ( tc[2] );
04701         VTC[3].SetZ( tc[2] );
04702         tc[2] += tcStep[2];
04703         VTC[4].SetZ( tc[2] );
04704         VTC[5].SetZ( tc[2] );
04705         VTC[6].SetZ( tc[2] );
04706         VTC[7].SetZ( tc[2] );
04707         //
04708         //for ( int Y = gridResolution[1]; Y < gridResolution[1]-1; ++Y ) {
04709         for ( int Y = 0; Y < gridResolution[1]-1; ++Y ) {
04710             //
04711             // Set Indices for Interpolation Data
04712             int offsetYZIntp = Y * iOffsetRowIntp + offsetZIntp;
04713             intpIdx[0] = offsetYZIntp;;
04714             intpIdx[1] = intpIdx[0] + iOffsetColIntp;
04715             intpIdx[2] = intpIdx[1] + iOffsetRowIntp;
04716             intpIdx[3] = intpIdx[0] + iOffsetRowIntp;
04717             intpIdx[4] = intpIdx[0] + iOffsetSliceIntp;
04718             intpIdx[5] = intpIdx[4] + iOffsetColIntp;
04719             intpIdx[6] = intpIdx[5] + iOffsetRowIntp;
04720             intpIdx[7] = intpIdx[4] + iOffsetRowIntp;
04721             //
04722             // Set Indices for Cube Vertices
04723             int offsetYZ = Y * iOffsetRow + offsetZ;
04724             idx[0] = offsetYZ;
04725             idx[1] = idx[0] + iOffsetCol;
04726             idx[2] = idx[1] + iOffsetRow;
04727             idx[3] = idx[0] + iOffsetRow;
04728             idx[4] = idx[0] + iOffsetSlice;
04729             idx[5] = idx[4] + iOffsetCol;
04730             idx[6] = idx[5] + iOffsetRow;
04731             idx[7] = idx[4] + iOffsetRow;
04732             //
04733             // Texture Coordinates -- Y
04734             tc[1] = static_cast<T>( Y ) / static_cast<T>( gridResolution[1]-1 );
04735             VTC[0].SetY( tc[1] );
04736             VTC[1].SetY( tc[1] );
04737             VTC[4].SetY( tc[1] );
04738             VTC[5].SetY( tc[1] );
04739             tc[1] += tcStep[1];
04740             VTC[2].SetY( tc[1] );
04741             VTC[3].SetY( tc[1] );
04742             VTC[6].SetY( tc[1] );
04743             VTC[7].SetY( tc[1] );
04744             //
04745             for ( int X = 0; X < gridResolution[0]-1; ++X ) {
04746                 //
04747                 // Texture Coordinates -- X
04748                 tc[0] = static_cast<T>( X ) / static_cast<T>( gridResolution[0]-1 );
04749                 VTC[0].SetX( tc[0] );
04750                 VTC[3].SetX( tc[0] );
04751                 VTC[4].SetX( tc[0] );
04752                 VTC[7].SetX( tc[0] );
04753                 tc[0] += tcStep[0];
04754                 VTC[1].SetX( tc[0] );
04755                 VTC[2].SetX( tc[0] );
04756                 VTC[5].SetX( tc[0] );
04757                 VTC[6].SetX( tc[0] );
04758                 //
04759 
04760 
04761                 bool bLeftPresent  = false;
04762                 bool bRightPresent = false;
04763                 {
04764                     for ( int i = 0; i < 8; ++i ) {
04765                         int flagVal = static_cast<int>( dataForPosition[idx[i] + 3] );
04766                         if ( flagVal > threshold ) {
04767                             flagVal %= int( m_SimDomain->IC_Thresholds::FLAG_STEP );
04768                             if      ( 1 < flagVal && flagVal < m_SimDomain->IC_Thresholds::MC_LEFT_ADD+1 ) {
04769                                 bLeftPresent = true;
04770                             }
04771                             else if ( flagVal > m_SimDomain->IC_Thresholds::MC_LEFT_ADD+1 ) {
04772                                 bRightPresent = true;
04773                             }
04774                         }
04775                     }
04776                 }
04777 
04778                 /*
04779                 // STANDARD CUBE CONFIGURATION
04780                 //-------------------------------
04781                 // Determine the threshold of each vertex of the cube
04782                 cubeClass = 0;
04783                 if ( dataForPosition[idx[0] + 3] > threshold )  cubeClass |= 1;
04784                 if ( dataForPosition[idx[1] + 3] > threshold )  cubeClass |= 2;
04785                 if ( dataForPosition[idx[2] + 3] > threshold )  cubeClass |= 4;
04786                 if ( dataForPosition[idx[3] + 3] > threshold )  cubeClass |= 8;
04787                 if ( dataForPosition[idx[4] + 3] > threshold )  cubeClass |= 16;
04788                 if ( dataForPosition[idx[5] + 3] > threshold )  cubeClass |= 32;
04789                 if ( dataForPosition[idx[6] + 3] > threshold )  cubeClass |= 64;
04790                 if ( dataForPosition[idx[7] + 3] > threshold )  cubeClass |= 128;
04791                 //-------------------------------
04792                 //*/
04793 
04794                 // TWO ITERATIONS FOR LEFT AND RIGHT CUBE CONFIGURATIONS
04795                 for ( int iIterate = 0; iIterate < 2; ++iIterate )
04796                 {
04797                     // Store Reloaded Threshold Values
04798                     GLfloat reloadedThresholdValues[8];
04799                     for ( int R = 0; R < 8; ++R ) {
04800                         reloadedThresholdValues[R] = dataForPosition[idx[R] + 3];
04801                     }
04802 
04803                     // STANDARD CUBE CONFIGURATION
04804                     //-------------------------------
04805                     if ( !bLeftPresent || !bRightPresent ) {
04806                         iIterate = 2;
04807                         //-------------------------------
04808                         // Determine the threshold of each vertex of the cube
04809                         cubeClass = 0;
04810                         if ( dataForPosition[idx[0] + 3] > threshold )  cubeClass |= 1;
04811                         if ( dataForPosition[idx[1] + 3] > threshold )  cubeClass |= 2;
04812                         if ( dataForPosition[idx[2] + 3] > threshold )  cubeClass |= 4;
04813                         if ( dataForPosition[idx[3] + 3] > threshold )  cubeClass |= 8;
04814                         if ( dataForPosition[idx[4] + 3] > threshold )  cubeClass |= 16;
04815                         if ( dataForPosition[idx[5] + 3] > threshold )  cubeClass |= 32;
04816                         if ( dataForPosition[idx[6] + 3] > threshold )  cubeClass |= 64;
04817                         if ( dataForPosition[idx[7] + 3] > threshold )  cubeClass |= 128;
04818                         //-------------------------------
04819                     }
04820                     else {
04821 
04822                         //-------------------------------
04823                         if ( iIterate == 0 )
04824                         // LEFT CUBE CONFIGURATION
04825                         //-------------------------------
04826                         // Determine the threshold of each vertex of the cube
04827                         {
04828                             //*
04829                             float flagVal;
04830                             int factor = 1;
04831                             cubeClass = 0;
04832                             for ( int i = 0; i < 8; ++i ) {
04833                                 flagVal = dataForPosition[idx[i] + 3];
04834                                 if ( flagVal > threshold ) {
04835                                     int iFlag = int( flagVal ) % int( m_SimDomain->IC_Thresholds::FLAG_STEP );
04836                                     if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD+1 ) {
04837                                         cubeClass |= factor;
04838                                     }
04839                                     else {
04840                                         dataForPosition[idx[i] + 3] = m_SimDomain->IC_Thresholds::RIGHT_OUTSIDE_BOUNDARY;
04841                                     }
04842                                 }
04843                                 factor *= 2;
04844                             }
04845                             //*/
04846                         }
04847 
04848                         //-------------------------------
04849                         else
04850                         // RIGHT CUBE CONFIGURATION
04851                         //-------------------------------
04852                         // Determine the threshold of each vertex of the cube
04853                         {
04854                             //*
04855                             float flagVal;
04856                             int factor = 1;
04857                             cubeClass = 0;
04858                             for ( int i = 0; i < 8; ++i ) {
04859                                 flagVal = dataForPosition[idx[i] + 3];
04860                                 if ( flagVal > threshold ) {
04861                                     int iFlag = int( flagVal ) % int( m_SimDomain->IC_Thresholds::FLAG_STEP );
04862                                     if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD-1 
04863                                     || iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD+1 ) {
04864                                         cubeClass |= factor;
04865                                     }
04866                                     else {
04867                                         dataForPosition[idx[i] + 3] = m_SimDomain->IC_Thresholds::RIGHT_OUTSIDE_BOUNDARY;
04868                                     }
04869                                 }
04870                                 factor *= 2;
04871                             }
04872                             //*/
04873                         }
04874                         //-------------------------------
04875                     }
04876 
04877 
04878                     //-------------------------------
04879                     // Cell is entirely in/out of the surface
04880                     if ( EdgeTable[cubeClass] == 0 ) {
04881                         numOfTrisPerCell = 0;
04882                     }
04883                     else {
04884                     //-------------------------------
04885                     // Cell contains part of the surface
04886                         //-----------------
04887                         for ( i = 0; i < 8; ++i ) {
04888                             V[i].SetXYZ( 
04889                                 dataForPosition[idx[i]    ], 
04890                                 dataForPosition[idx[i] + 1], 
04891                                 dataForPosition[idx[i] + 2] 
04892                             );
04893                         }
04894                         //-----------------
04895                         // Reset list of existing vertices
04896                         for ( i = 0; i < 12; ++i ) {
04897                             bV[i] = -1;
04898                         }
04899 
04900                         //-----------------
04901                         // Interpolation Data
04902                         for ( i = 0; i < 8; ++i ) {
04903                             VIntp[i].SetXYZ( 
04904                                 dataForEllipsoidInfluence[intpIdx[i]    ], 
04905                                 dataForEllipsoidInfluence[intpIdx[i] + 1], 
04906                                 dataForEllipsoidInfluence[intpIdx[i] + 2] 
04907                             );
04908                         }
04909 
04910                         //===============================================
04911                         // Calculate Gradients
04912                         //-----------------------------------------------
04913                         Vector3<T> G[8];
04914                         int index;
04915                         for ( int vID = 0; vID < 8; ++vID ) {
04916                             Center.SetXYZ(  dataForPosition[idx[vID]  ], 
04917                                             dataForPosition[idx[vID]+1], 
04918                                             dataForPosition[idx[vID]+2] );
04919                             G[vID].SetXYZ( 0, 0, 0 );
04920 
04921                             //*
04922                             //=================================
04923                             // START: 6 Face Gradients
04924                             //---------------------------------
04925                             if ( GetGradientComputationByFacesStatus() ) {
04926                             //--- East --------------
04927                             if ( X < gridResolution[0]-2 ) {
04928                                 index = idx[vID] + iOffsetCol;
04929                                 if ( dataForPosition[index+3] > threshold ) {
04930                                     int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04931                                     if ( iIterate == 2 ) {  // Standard Cube
04932                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04933                                         G[vID] += Center - vGrad;
04934                                     }
04935                                     if ( iIterate == 1 ) {  // Right Cube
04936                                         if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
04937                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04938                                             G[vID] += Center - vGrad;
04939                                         }
04940                                     }
04941                                     else {                  // Left Cube
04942                                         if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
04943                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04944                                             G[vID] += Center - vGrad;
04945                                         }
04946                                     }
04947                                 }
04948                             }
04949                             //--- West --------------
04950                             if ( X > 0 ) {
04951                                 index = idx[vID] - iOffsetCol;
04952                                 if ( dataForPosition[index+3] > threshold ) {
04953                                     int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04954                                     if ( iIterate == 2 ) {  // Standard Cube
04955                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04956                                         G[vID] += Center - vGrad;
04957                                     }
04958                                     if ( iIterate == 1 ) {  // Right Cube
04959                                         if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
04960                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04961                                             G[vID] += Center - vGrad;
04962                                         }
04963                                     }
04964                                     else {                  // Left Cube
04965                                         if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
04966                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04967                                             G[vID] += Center - vGrad;
04968                                         }
04969                                     }
04970                                 }
04971                             }
04972                             //--- North -------------
04973                             if ( Y < gridResolution[1]-2 ) {
04974                                 index = idx[vID] + iOffsetRow;
04975                                 if ( dataForPosition[index+3] > threshold ) {
04976                                     int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04977                                     if ( iIterate == 2 ) {  // Standard Cube
04978                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04979                                         G[vID] += Center - vGrad;
04980                                     }
04981                                     if ( iIterate == 1 ) {  // Right Cube
04982                                         if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
04983                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04984                                             G[vID] += Center - vGrad;
04985                                         }
04986                                     }
04987                                     else {                  // Left Cube
04988                                         if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
04989                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04990                                             G[vID] += Center - vGrad;
04991                                         }
04992                                     }
04993                                 }
04994                             }
04995                             //--- South -------------
04996                             if ( Y > 0 ) {
04997                                 index = idx[vID] - iOffsetRow;
04998                                 if ( dataForPosition[index+3] > threshold ) {
04999                                     int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05000                                     if ( iIterate == 2 ) {  // Standard Cube
05001                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05002                                         G[vID] += Center - vGrad;
05003                                     }
05004                                     if ( iIterate == 1 ) {  // Right Cube
05005                                         if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05006                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05007                                             G[vID] += Center - vGrad;
05008                                         }
05009                                     }
05010                                     else {                  // Left Cube
05011                                         if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05012                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05013                                             G[vID] += Center - vGrad;
05014                                         }
05015                                     }
05016                                 }
05017                             }
05018                             //--- Front -------------
05019                             if ( Z < gridResolution[2]-2 ) {
05020                                 index = idx[vID] + iOffsetSlice;
05021                                 if ( dataForPosition[index+3] > threshold ) {
05022                                     int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05023                                     if ( iIterate == 2 ) {  // Standard Cube
05024                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05025                                         G[vID] += Center - vGrad;
05026                                     }
05027                                     if ( iIterate == 1 ) {  // Right Cube
05028                                         if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05029                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05030                                             G[vID] += Center - vGrad;
05031                                         }
05032                                     }
05033                                     else {                  // Left Cube
05034                                         if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05035                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05036                                             G[vID] += Center - vGrad;
05037                                         }
05038                                     }
05039                                 }
05040                             }
05041                             //--- Back --------------
05042                             if ( Z > 0 ) {
05043                                 index = idx[vID] - iOffsetSlice;
05044                                 if ( dataForPosition[index+3] > threshold ) {
05045                                     int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05046                                     if ( iIterate == 2 ) {  // Standard Cube
05047                                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05048                                         G[vID] += Center - vGrad;
05049                                     }
05050                                     if ( iIterate == 1 ) {  // Right Cube
05051                                         if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05052                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05053                                             G[vID] += Center - vGrad;
05054                                         }
05055                                     }
05056                                     else {                  // Left Cube
05057                                         if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05058                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05059                                             G[vID] += Center - vGrad;
05060                                         }
05061                                     }
05062                                 }
05063                             }
05064                             //-----------------------
05065                             } // END: if ( GetGradientComputationByFacesStatus() ) {
05066                             //---------------------------------
05067                             // END: 6 Face Gradients
05068                             //=================================
05069                             //*/
05070 
05071                             //*
05072                             //=================================
05073                             // START: 8 Vertex Gradients
05074                             //---------------------------------
05075                             if ( GetGradientComputationByVerticesStatus() ) {
05076                             //--- Front -------------
05077                             if ( Z < gridResolution[2]-2 ) {
05078                                 int idxF = idx[vID] + iOffsetSlice;
05079                                 //-- North -----
05080                                 if ( Y < gridResolution[1]-2 ) {
05081                                     int idxFN = idxF + iOffsetRow;
05082                                     //- West
05083                                     if ( X > 0 ) {
05084                                         index = idxFN - iOffsetCol;
05085                                         if ( dataForPosition[index+3] > threshold ) {
05086                                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05087                                             if ( iIterate == 2 ) {  // Standard Cube
05088                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05089                                                 G[vID] += Center - vGrad;
05090                                             }
05091                                             if ( iIterate == 1 ) {  // Right Cube
05092                                                 if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05093                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05094                                                     G[vID] += Center - vGrad;
05095                                                 }
05096                                             }
05097                                             else {                  // Left Cube
05098                                                 if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05099                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05100                                                     G[vID] += Center - vGrad;
05101                                                 }
05102                                             }
05103                                         }
05104                                     }
05105                                     //- East
05106                                     if ( X < gridResolution[0]-2 ) {
05107                                         index = idxFN + iOffsetCol;
05108                                         if ( dataForPosition[index+3] > threshold ) {
05109                                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05110                                             if ( iIterate == 2 ) {  // Standard Cube
05111                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05112                                                 G[vID] += Center - vGrad;
05113                                             }
05114                                             if ( iIterate == 1 ) {  // Right Cube
05115                                                 if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05116                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05117                                                     G[vID] += Center - vGrad;
05118                                                 }
05119                                             }
05120                                             else {                  // Left Cube
05121                                                 if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05122                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05123                                                     G[vID] += Center - vGrad;
05124                                                 }
05125                                             }
05126                                         }
05127                                     }
05128                                 }
05129                                 //-- South -----
05130                                 if ( Y > 0 ) {
05131                                     int idxFS = idxF - iOffsetRow;
05132                                     //- West
05133                                     if ( X > 0 ) {
05134                                         index = idxFS - iOffsetCol;
05135                                         if ( dataForPosition[index+3] > threshold ) {
05136                                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05137                                             if ( iIterate == 2 ) {  // Standard Cube
05138                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05139                                                 G[vID] += Center - vGrad;
05140                                             }
05141                                             if ( iIterate == 1 ) {  // Right Cube
05142                                                 if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05143                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05144                                                     G[vID] += Center - vGrad;
05145                                                 }
05146                                             }
05147                                             else {                  // Left Cube
05148                                                 if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05149                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05150                                                     G[vID] += Center - vGrad;
05151                                                 }
05152                                             }
05153                                         }
05154                                     }
05155                                     //- East
05156                                     if ( X < gridResolution[0]-2 ) {
05157                                         index = idxFS + iOffsetCol;
05158                                         if ( dataForPosition[index+3] > threshold ) {
05159                                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05160                                             if ( iIterate == 2 ) {  // Standard Cube
05161                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05162                                                 G[vID] += Center - vGrad;
05163                                             }
05164                                             if ( iIterate == 1 ) {  // Right Cube
05165                                                 if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05166                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05167                                                     G[vID] += Center - vGrad;
05168                                                 }
05169                                             }
05170                                             else {                  // Left Cube
05171                                                 if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05172                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05173                                                     G[vID] += Center - vGrad;
05174                                                 }
05175                                             }
05176                                         }
05177                                     }
05178                                 }
05179                             }
05180                             //--- Back --------------
05181                             if ( Z > 0 ) {
05182                                 int idxF = idx[vID] - iOffsetSlice;
05183                                 //-- North -----
05184                                 if ( Y < gridResolution[1]-2 ) {
05185                                     int idxFN = idxF + iOffsetRow;
05186                                     //- West
05187                                     if ( X > 0 ) {
05188                                         index = idxFN - iOffsetCol;
05189                                         if ( dataForPosition[index+3] > threshold ) {
05190                                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05191                                             if ( iIterate == 2 ) {  // Standard Cube
05192                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05193                                                 G[vID] += Center - vGrad;
05194                                             }
05195                                             if ( iIterate == 1 ) {  // Right Cube
05196                                                 if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05197                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05198                                                     G[vID] += Center - vGrad;
05199                                                 }
05200                                             }
05201                                             else {                  // Left Cube
05202                                                 if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05203                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05204                                                     G[vID] += Center - vGrad;
05205                                                 }
05206                                             }
05207                                         }
05208                                     }
05209                                     //- East
05210                                     if ( X < gridResolution[0]-2 ) {
05211                                         index = idxFN + iOffsetCol;
05212                                         if ( dataForPosition[index+3] > threshold ) {
05213                                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05214                                             if ( iIterate == 2 ) {  // Standard Cube
05215                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05216                                                 G[vID] += Center - vGrad;
05217                                             }
05218                                             if ( iIterate == 1 ) {  // Right Cube
05219                                                 if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05220                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05221                                                     G[vID] += Center - vGrad;
05222                                                 }
05223                                             }
05224                                             else {                  // Left Cube
05225                                                 if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05226                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05227                                                     G[vID] += Center - vGrad;
05228                                                 }
05229                                             }
05230                                         }
05231                                     }
05232                                 }
05233                                 //-- South -----
05234                                 if ( Y > 0 ) {
05235                                     int idxFS = idxF - iOffsetRow;
05236                                     //- West
05237                                     if ( X > 0 ) {
05238                                         index = idxFS - iOffsetCol;
05239                                         if ( dataForPosition[index+3] > threshold ) {
05240                                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05241                                             if ( iIterate == 2 ) {  // Standard Cube
05242                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05243                                                 G[vID] += Center - vGrad;
05244                                             }
05245                                             if ( iIterate == 1 ) {  // Right Cube
05246                                                 if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05247                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05248                                                     G[vID] += Center - vGrad;
05249                                                 }
05250                                             }
05251                                             else {                  // Left Cube
05252                                                 if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05253                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05254                                                     G[vID] += Center - vGrad;
05255                                                 }
05256                                             }
05257                                         }
05258                                     }
05259                                     //- East
05260                                     if ( X < gridResolution[0]-2 ) {
05261                                         index = idxFS + iOffsetCol;
05262                                         if ( dataForPosition[index+3] > threshold ) {
05263                                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05264                                             if ( iIterate == 2 ) {  // Standard Cube
05265                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05266                                                 G[vID] += Center - vGrad;
05267                                             }
05268                                             if ( iIterate == 1 ) {  // Right Cube
05269                                                 if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05270                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05271                                                     G[vID] += Center - vGrad;
05272                                                 }
05273                                             }
05274                                             else {                  // Left Cube
05275                                                 if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05276                                                     Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05277                                                     G[vID] += Center - vGrad;
05278                                                 }
05279                                             }
05280                                         }
05281                                     }
05282                                 }
05283                             }
05284                             //-----------------------
05285                             } // END: if ( GetGradientComputationByVerticesStatus() ) {
05286                             //---------------------------------
05287                             // END: 8 Vertex Gradients
05288                             //=================================
05289                             //*/
05290 
05291                             //*
05292                             //=================================
05293                             // START: 12 Edge Gradients
05294                             //---------------------------------
05295                             if ( GetGradientComputationByEdgesStatus() ) {
05296                             //--- Front -------------
05297                             if ( Z < gridResolution[2]-2 ) {
05298                                 int idxF = idx[vID] + iOffsetSlice;
05299                                 //-- East ------
05300                                 if ( X < gridResolution[0]-2 ) {
05301                                     index = idxF + iOffsetCol;
05302                                     if ( dataForPosition[index+3] > threshold ) {
05303                                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05304                                         if ( iIterate == 2 ) {  // Standard Cube
05305                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05306                                             G[vID] += Center - vGrad;
05307                                         }
05308                                         if ( iIterate == 1 ) {  // Right Cube
05309                                             if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05310                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05311                                                 G[vID] += Center - vGrad;
05312                                             }
05313                                         }
05314                                         else {                  // Left Cube
05315                                             if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05316                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05317                                                 G[vID] += Center - vGrad;
05318                                             }
05319                                         }
05320                                     }
05321                                 }
05322                                 //-- West ------
05323                                 if ( X > 0 ) {
05324                                     index = idxF - iOffsetCol;
05325                                     if ( dataForPosition[index+3] > threshold ) {
05326                                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05327                                         if ( iIterate == 2 ) {  // Standard Cube
05328                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05329                                             G[vID] += Center - vGrad;
05330                                         }
05331                                         if ( iIterate == 1 ) {  // Right Cube
05332                                             if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05333                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05334                                                 G[vID] += Center - vGrad;
05335                                             }
05336                                         }
05337                                         else {                  // Left Cube
05338                                             if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05339                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05340                                                 G[vID] += Center - vGrad;
05341                                             }
05342                                         }
05343                                     }
05344                                 }
05345                                 //-- North -----
05346                                 if ( Y < gridResolution[1]-2 ) {
05347                                     index = idxF + iOffsetRow;
05348                                     if ( dataForPosition[index+3] > threshold ) {
05349                                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05350                                         if ( iIterate == 2 ) {  // Standard Cube
05351                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05352                                             G[vID] += Center - vGrad;
05353                                         }
05354                                         if ( iIterate == 1 ) {  // Right Cube
05355                                             if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05356                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05357                                                 G[vID] += Center - vGrad;
05358                                             }
05359                                         }
05360                                         else {                  // Left Cube
05361                                             if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05362                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05363                                                 G[vID] += Center - vGrad;
05364                                             }
05365                                         }
05366                                     }
05367                                 }
05368                                 //-- South -----
05369                                 if ( Y > 0 ) {
05370                                     index = idxF - iOffsetRow;
05371                                     if ( dataForPosition[index+3] > threshold ) {
05372                                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05373                                         if ( iIterate == 2 ) {  // Standard Cube
05374                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05375                                             G[vID] += Center - vGrad;
05376                                         }
05377                                         if ( iIterate == 1 ) {  // Right Cube
05378                                             if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05379                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05380                                                 G[vID] += Center - vGrad;
05381                                             }
05382                                         }
05383                                         else {                  // Left Cube
05384                                             if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05385                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05386                                                 G[vID] += Center - vGrad;
05387                                             }
05388                                         }
05389                                     }
05390                                 }
05391                             }
05392                             //--- Center ------------
05393                             if ( true ) {
05394                                 int idxC = idx[vID];
05395                                 //-- East ------
05396                                 if ( X < gridResolution[0]-2 ) {
05397                                     index = idxC + iOffsetCol;
05398                                     if ( dataForPosition[index+3] > threshold ) {
05399                                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05400                                         if ( iIterate == 2 ) {  // Standard Cube
05401                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05402                                             G[vID] += Center - vGrad;
05403                                         }
05404                                         if ( iIterate == 1 ) {  // Right Cube
05405                                             if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05406                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05407                                                 G[vID] += Center - vGrad;
05408                                             }
05409                                         }
05410                                         else {                  // Left Cube
05411                                             if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05412                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05413                                                 G[vID] += Center - vGrad;
05414                                             }
05415                                         }
05416                                     }
05417                                 }
05418                                 //-- West ------
05419                                 if ( X > 0 ) {
05420                                     index = idxC - iOffsetCol;
05421                                     if ( dataForPosition[index+3] > threshold ) {
05422                                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05423                                         if ( iIterate == 2 ) {  // Standard Cube
05424                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05425                                             G[vID] += Center - vGrad;
05426                                         }
05427                                         if ( iIterate == 1 ) {  // Right Cube
05428                                             if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05429                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05430                                                 G[vID] += Center - vGrad;
05431                                             }
05432                                         }
05433                                         else {                  // Left Cube
05434                                             if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05435                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05436                                                 G[vID] += Center - vGrad;
05437                                             }
05438                                         }
05439                                     }
05440                                 }
05441                                 //-- North -----
05442                                 if ( Y < gridResolution[1]-2 ) {
05443                                     index = idxC + iOffsetRow;
05444                                     if ( dataForPosition[index+3] > threshold ) {
05445                                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05446                                         if ( iIterate == 2 ) {  // Standard Cube
05447                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05448                                             G[vID] += Center - vGrad;
05449                                         }
05450                                         if ( iIterate == 1 ) {  // Right Cube
05451                                             if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05452                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05453                                                 G[vID] += Center - vGrad;
05454                                             }
05455                                         }
05456                                         else {                  // Left Cube
05457                                             if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05458                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05459                                                 G[vID] += Center - vGrad;
05460                                             }
05461                                         }
05462                                     }
05463                                 }
05464                                 //-- South -----
05465                                 if ( Y > 0 ) {
05466                                     index = idxC - iOffsetRow;
05467                                     if ( dataForPosition[index+3] > threshold ) {
05468                                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05469                                         if ( iIterate == 2 ) {  // Standard Cube
05470                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05471                                             G[vID] += Center - vGrad;
05472                                         }
05473                                         if ( iIterate == 1 ) {  // Right Cube
05474                                             if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05475                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05476                                                 G[vID] += Center - vGrad;
05477                                             }
05478                                         }
05479                                         else {                  // Left Cube
05480                                             if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05481                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05482                                                 G[vID] += Center - vGrad;
05483                                             }
05484                                         }
05485                                     }
05486                                 }
05487                             }
05488                             //--- Back --------------
05489                             if ( Z > 0 ) {
05490                                 int idxB = idx[vID] - iOffsetSlice;
05491                                 //-- East ------
05492                                 if ( X < gridResolution[0]-2 ) {
05493                                     index = idxB + iOffsetCol;
05494                                     if ( dataForPosition[index+3] > threshold ) {
05495                                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05496                                         if ( iIterate == 2 ) {  // Standard Cube
05497                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05498                                             G[vID] += Center - vGrad;
05499                                         }
05500                                         if ( iIterate == 1 ) {  // Right Cube
05501                                             if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05502                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05503                                                 G[vID] += Center - vGrad;
05504                                             }
05505                                         }
05506                                         else {                  // Left Cube
05507                                             if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05508                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05509                                                 G[vID] += Center - vGrad;
05510                                             }
05511                                         }
05512                                     }
05513                                 }
05514                                 //-- West ------
05515                                 if ( X > 0 ) {
05516                                     index = idxB - iOffsetCol;
05517                                     if ( dataForPosition[index+3] > threshold ) {
05518                                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05519                                         if ( iIterate == 2 ) {  // Standard Cube
05520                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05521                                             G[vID] += Center - vGrad;
05522                                         }
05523                                         if ( iIterate == 1 ) {  // Right Cube
05524                                             if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05525                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05526                                                 G[vID] += Center - vGrad;
05527                                             }
05528                                         }
05529                                         else {                  // Left Cube
05530                                             if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05531                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05532                                                 G[vID] += Center - vGrad;
05533                                             }
05534                                         }
05535                                     }
05536                                 }
05537                                 //-- North -----
05538                                 if ( Y < gridResolution[1]-2 ) {
05539                                     index = idxB + iOffsetRow;
05540                                     if ( dataForPosition[index+3] > threshold ) {
05541                                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05542                                         if ( iIterate == 2 ) {  // Standard Cube
05543                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05544                                             G[vID] += Center - vGrad;
05545                                         }
05546                                         if ( iIterate == 1 ) {  // Right Cube
05547                                             if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05548                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05549                                                 G[vID] += Center - vGrad;
05550                                             }
05551                                         }
05552                                         else {                  // Left Cube
05553                                             if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05554                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05555                                                 G[vID] += Center - vGrad;
05556                                             }
05557                                         }
05558                                     }
05559                                 }
05560                                 //-- South -----
05561                                 if ( Y > 0 ) {
05562                                     index = idxB - iOffsetRow;
05563                                     if ( dataForPosition[index+3] > threshold ) {
05564                                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05565                                         if ( iIterate == 2 ) {  // Standard Cube
05566                                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05567                                             G[vID] += Center - vGrad;
05568                                         }
05569                                         if ( iIterate == 1 ) {  // Right Cube
05570                                             if ( iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05571                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05572                                                 G[vID] += Center - vGrad;
05573                                             }
05574                                         }
05575                                         else {                  // Left Cube
05576                                             if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD + m_SimDomain->IC_Thresholds::HALF_THRESHOLD_ADD ) {
05577                                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05578                                                 G[vID] += Center - vGrad;
05579                                             }
05580                                         }
05581                                     }
05582                                 }
05583                             }
05584                             //-----------------------
05585                             } // END: if ( GetGradientComputationByEdgesStatus() ) {
05586                             //---------------------------------
05587                             // END: 12 Edge Gradients
05588                             //=================================
05589                             //*/
05590                         }
05591                         //-----------------------------------------------
05592                         // END: Calculate Gradients
05593                         //===============================================
05594 
05595 
05596                         //===============================================
05597                         // Marching Cube Interpolation
05598                         // V = V1 + (isovalue - s1) (V2 - V1) / (s2 - s1)
05599                         //-----------------------------------------------
05600                         // RUNTIME ERROR CAN OCCURS -- DEPENDING ON THE DATA
05601                         // SINCE IT DOES NOT HAVE BOUNDARY PROTECTION -- ARRAY INDEX OUT OF BOUND ERROR
05602                         //-----------------------------------------------
05603                         // Not Use Global Interpolation Value -- each element has its own xyz interpolation values
05604                         if ( !m_bUseGlobalInterpolationVal ) {
05605                             //-------------------------------------
05606                             // Marching Cube Interpolation
05607                             T interpolationValPlus;// = m_tGlobalInterpolationVal;
05608                             T interpolationValNeg;//  = 1.0 - m_tGlobalInterpolationVal;
05609 
05610 
05611 
05612                             /*
05613                             //***************************************
05614                             // Normal Interpolation
05615                             //=======================================
05616                             //-------------------------------------
05617                             // Edge# 0 to 11 <==> [ 1, 2, 4, ... , 2048 ]
05618                             if ( EdgeTable[cubeClass] & 1 ) {
05619                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
05620                                     //
05621                                     // Interpotation Data
05622                                     interpolationValPlus = VIntp[0].GetX();
05623                                     //
05624                                     vertexList[ 0] = V[0] + interpolationValPlus*(V[1] - V[0]);
05625                                     grad[0]        = G[0] + interpolationValPlus*(G[1] - G[0]);
05626                                     texCoordList[0] = VTC[0] + interpolationValPlus*(VTC[1] - VTC[0]);
05627                                 }
05628                                 else {
05629                                     //
05630                                     // Interpotation Data
05631                                     interpolationValNeg = 1.0 - VIntp[1].GetX();
05632                                     //
05633                                     vertexList[ 0] = V[0] + interpolationValNeg*(V[1] - V[0]);
05634                                     grad[0]        = G[0] + interpolationValNeg*(G[1] - G[0]);
05635                                     texCoordList[0] = VTC[0] + interpolationValNeg*(VTC[1] - VTC[0]);
05636                                     //grad[0] *= -1.0;
05637                                 }
05638                                 grad[0].Normalized();
05639                             }
05640                             if ( EdgeTable[cubeClass] & 2 ) {
05641                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
05642                                     //
05643                                     // Interpotation Data
05644                                     interpolationValPlus = VIntp[1].GetY();
05645                                     //
05646                                     vertexList[ 1] = V[1] + interpolationValPlus*(V[2] - V[1]);
05647                                     grad[1]        = G[1] + interpolationValPlus*(G[2] - G[1]);
05648                                     texCoordList[1] = VTC[1] + interpolationValPlus*(VTC[2] - VTC[1]);
05649                                 }
05650                                 else {
05651                                     //
05652                                     // Interpotation Data
05653                                     interpolationValNeg = 1.0 - VIntp[2].GetY();
05654                                     //
05655                                     vertexList[ 1] = V[1] + interpolationValNeg*(V[2] - V[1]);
05656                                     grad[1]        = G[1] + interpolationValNeg*(G[2] - G[1]);
05657                                     texCoordList[1] = VTC[1] + interpolationValNeg*(VTC[2] - VTC[1]);
05658                                     //grad[1] *= -1.0;
05659                                 }
05660                                 grad[1].Normalized();
05661                             }
05662                             if ( EdgeTable[cubeClass] & 4 ) {
05663                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
05664                                     //
05665                                     // Interpotation Data
05666                                     interpolationValPlus = VIntp[2].GetX();
05667                                     //
05668                                     vertexList[ 2] = V[2] + interpolationValPlus*(V[3] - V[2]);
05669                                     grad[2]        = G[2] + interpolationValPlus*(G[3] - G[2]);
05670                                     texCoordList[2] = VTC[2] + interpolationValPlus*(VTC[3] - VTC[2]);
05671                                     //grad[2] *= -1.0;
05672                                 }
05673                                 else {
05674                                     //
05675                                     // Interpotation Data
05676                                     interpolationValNeg = 1.0 - VIntp[3].GetX();
05677                                     //
05678                                     vertexList[ 2] = V[2] + interpolationValNeg*(V[3] - V[2]);
05679                                     grad[2]        = G[2] + interpolationValNeg*(G[3] - G[2]);
05680                                     texCoordList[2] = VTC[2] + interpolationValNeg*(VTC[3] - VTC[2]);
05681                                 }
05682                                 grad[2].Normalized();
05683                             }
05684                             if ( EdgeTable[cubeClass] & 8 ) {
05685                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
05686                                     //
05687                                     // Interpotation Data
05688                                     interpolationValPlus = VIntp[0].GetY();
05689                                     //
05690                                     vertexList[ 3] = V[0] + interpolationValPlus*(V[3] - V[0]);
05691                                     grad[3]        = G[0] + interpolationValPlus*(G[3] - G[0]);
05692                                     texCoordList[3] = VTC[0] + interpolationValPlus*(VTC[3] - VTC[0]);
05693                                     //grad[3][1] *= -1.0;
05694                                 }
05695                                 else {
05696                                     //
05697                                     // Interpotation Data
05698                                     interpolationValNeg = 1.0 - VIntp[3].GetY();
05699                                     //
05700                                     vertexList[ 3] = V[0] + interpolationValNeg*(V[3] - V[0]);
05701                                     grad[3]        = G[0] + interpolationValNeg*(G[3] - G[0]);
05702                                     texCoordList[3] = VTC[0] + interpolationValNeg*(VTC[3] - VTC[0]);
05703                                 }
05704                                 grad[3].Normalized();
05705                             }
05706                             if ( EdgeTable[cubeClass] & 16 ) {
05707                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
05708                                     //
05709                                     // Interpotation Data
05710                                     interpolationValPlus = VIntp[4].GetX();
05711                                     //
05712                                     vertexList[ 4] = V[4] + interpolationValPlus*(V[5] - V[4]);
05713                                     grad[4]        = G[4] + interpolationValPlus*(G[5] - G[4]);
05714                                     texCoordList[4] = VTC[4] + interpolationValPlus*(VTC[5] - VTC[4]);
05715                                 }
05716                                 else {
05717                                     //
05718                                     // Interpotation Data
05719                                     interpolationValNeg = 1.0 - VIntp[5].GetX();
05720                                     //
05721                                     vertexList[ 4] = V[4] + interpolationValNeg*(V[5] - V[4]);
05722                                     grad[4]        = G[4] + interpolationValNeg*(G[5] - G[4]);
05723                                     texCoordList[4] = VTC[4] + interpolationValNeg*(VTC[5] - VTC[4]);
05724                                     //grad[4] *= -1.0;
05725                                 }
05726                                 grad[4].Normalized();
05727                             }
05728                             if ( EdgeTable[cubeClass] & 32 ) {
05729                                 if ( dataForPosition[idx[5] + 3] > threshold ) {
05730                                     //
05731                                     // Interpotation Data
05732                                     interpolationValPlus = VIntp[5].GetY();
05733                                     //
05734                                     vertexList[ 5] = V[5] + interpolationValPlus*(V[6] - V[5]);
05735                                     grad[5]        = G[5] + interpolationValPlus*(G[6] - G[5]);
05736                                     texCoordList[5] = VTC[5] + interpolationValPlus*(VTC[6] - VTC[5]);
05737                                 }
05738                                 else {
05739                                     //
05740                                     // Interpotation Data
05741                                     interpolationValNeg = 1.0 - VIntp[6].GetY();
05742                                     //
05743                                     vertexList[ 5] = V[5] + interpolationValNeg*(V[6] - V[5]);
05744                                     grad[5]        = G[5] + interpolationValNeg*(G[6] - G[5]);
05745                                     texCoordList[5] = VTC[5] + interpolationValNeg*(VTC[6] - VTC[5]);
05746                                     //grad[5][1] *= -1.0;
05747                                 }
05748                                 grad[5].Normalized();
05749                             }
05750                             if ( EdgeTable[cubeClass] & 64 ) {
05751                                 if ( dataForPosition[idx[6] + 3] > threshold ) {
05752                                     //
05753                                     // Interpotation Data
05754                                     interpolationValPlus = VIntp[6].GetX();
05755                                     //
05756                                     vertexList[ 6] = V[6] + interpolationValPlus*(V[7] - V[6]);
05757                                     grad[6]        = G[6] + interpolationValPlus*(G[7] - G[6]);
05758                                     texCoordList[6] = VTC[6] + interpolationValPlus*(VTC[7] - VTC[6]);
05759                                 }
05760                                 else {
05761                                     //
05762                                     // Interpotation Data
05763                                     interpolationValNeg = 1.0 - VIntp[7].GetX();
05764                                     //
05765                                     vertexList[ 6] = V[6] + interpolationValNeg*(V[7] - V[6]);
05766                                     grad[6]        = G[6] + interpolationValNeg*(G[7] - G[6]);
05767                                     texCoordList[6] = VTC[6] + interpolationValNeg*(VTC[7] - VTC[6]);
05768                                     //grad[6] *= -1.0;
05769                                 }
05770                                 grad[6].Normalized();
05771                             }
05772                             if ( EdgeTable[cubeClass] & 128 ) {
05773                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
05774                                     //
05775                                     // Interpotation Data
05776                                     interpolationValPlus = VIntp[4].GetY();
05777                                     //
05778                                     vertexList[ 7] = V[4] + interpolationValPlus*(V[7] - V[4]);
05779                                     grad[7]        = G[4] + interpolationValPlus*(G[7] - G[4]);
05780                                     texCoordList[7] = VTC[4] + interpolationValPlus*(VTC[7] - VTC[4]);
05781                                     //grad[7][1] *= -1.0;
05782                                 }
05783                                 else {
05784                                     //
05785                                     // Interpotation Data
05786                                     interpolationValNeg = 1.0 - VIntp[7].GetY();
05787                                     //
05788                                     vertexList[ 7] = V[4] + interpolationValNeg*(V[7] - V[4]);
05789                                     grad[7]        = G[4] + interpolationValNeg*(G[7] - G[4]);
05790                                     texCoordList[7] = VTC[4] + interpolationValNeg*(VTC[7] - VTC[4]);
05791                                 }
05792                                 grad[7].Normalized();
05793                             }
05794                             if ( EdgeTable[cubeClass] & 256 ) {
05795                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
05796                                     //
05797                                     // Interpotation Data
05798                                     interpolationValPlus = VIntp[0].GetZ();
05799                                     //
05800                                     vertexList[ 8] = V[0] + interpolationValPlus*(V[4] - V[0]);
05801                                     grad[8]        = G[0] + interpolationValPlus*(G[4] - G[0]);
05802                                     texCoordList[8] = VTC[0] + interpolationValPlus*(VTC[4] - VTC[0]);
05803                                 }
05804                                 else {
05805                                     //
05806                                     // Interpotation Data
05807                                     interpolationValNeg = 1.0 - VIntp[4].GetZ();
05808                                     //
05809                                     vertexList[ 8] = V[0] + interpolationValNeg*(V[4] - V[0]);
05810                                     grad[8]        = G[0] + interpolationValNeg*(G[4] - G[0]);
05811                                     texCoordList[8] = VTC[0] + interpolationValNeg*(VTC[4] - VTC[0]);
05812                                     //grad[8][2] *= -1.0;
05813                                 }
05814                                 grad[8].Normalized();
05815                             }
05816                             if ( EdgeTable[cubeClass] & 512 ) {
05817                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
05818                                     //
05819                                     // Interpotation Data
05820                                     interpolationValPlus = VIntp[1].GetZ();
05821                                     //
05822                                     vertexList[ 9] = V[1] + interpolationValPlus*(V[5] - V[1]);
05823                                     grad[9]        = G[1] + interpolationValPlus*(G[5] - G[1]);
05824                                     texCoordList[9] = VTC[1] + interpolationValPlus*(VTC[5] - VTC[1]);
05825                                 }
05826                                 else {
05827                                     //
05828                                     // Interpotation Data
05829                                     interpolationValNeg = 1.0 - VIntp[5].GetZ();
05830                                     //
05831                                     vertexList[ 9] = V[1] + interpolationValNeg*(V[5] - V[1]);
05832                                     grad[9]        = G[1] + interpolationValNeg*(G[5] - G[1]);
05833                                     texCoordList[9] = VTC[1] + interpolationValNeg*(VTC[5] - VTC[1]);
05834                                     //grad[9][2] *= -1.0;
05835                                 }
05836                                 grad[9].Normalized();
05837                             }
05838                             if ( EdgeTable[cubeClass] & 1024 ) {
05839                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
05840                                     //
05841                                     // Interpotation Data
05842                                     interpolationValPlus = VIntp[2].GetZ();
05843                                     //
05844                                     vertexList[10] = V[2] + interpolationValPlus*(V[6] - V[2]);
05845                                     grad[10]       = G[2] + interpolationValPlus*(G[6] - G[2]);
05846                                     texCoordList[10] = VTC[2] + interpolationValPlus*(VTC[6] - VTC[2]);
05847                                 }
05848                                 else {
05849                                     //
05850                                     // Interpotation Data
05851                                     interpolationValNeg = 1.0 - VIntp[6].GetZ();
05852                                     //
05853                                     vertexList[10] = V[2] + interpolationValNeg*(V[6] - V[2]);
05854                                     grad[10]       = G[2] + interpolationValNeg*(G[6] - G[2]);
05855                                     texCoordList[10] = VTC[2] + interpolationValNeg*(VTC[6] - VTC[2]);
05856                                     //grad[10][2] *= -1.0;
05857                                 }
05858                                 grad[10].Normalized();
05859                             }
05860                             if ( EdgeTable[cubeClass] & 2048 ) {
05861                                 if ( dataForPosition[idx[3] + 3] > threshold ) {
05862                                     //
05863                                     // Interpotation Data
05864                                     interpolationValPlus = VIntp[3].GetZ();
05865                                     //
05866                                     vertexList[11] = V[3] + interpolationValPlus*(V[7] - V[3]);
05867                                     grad[11]       = G[3] + interpolationValPlus*(G[7] - G[3]);
05868                                     texCoordList[11] = VTC[3] + interpolationValPlus*(VTC[7] - VTC[3]);
05869                                 }
05870                                 else {
05871                                     //
05872                                     // Interpotation Data
05873                                     interpolationValNeg = 1.0 - VIntp[7].GetZ();
05874                                     //
05875                                     vertexList[11] = V[3] + interpolationValNeg*(V[7] - V[3]);
05876                                     grad[11]       = G[3] + interpolationValNeg*(G[7] - G[3]);
05877                                     texCoordList[11] = VTC[3] + interpolationValNeg*(VTC[7] - VTC[3]);
05878                                     //grad[11][2] *= -1.0;
05879                                 }
05880                                 grad[11].Normalized();
05881                             }
05882                             //---------------------------------------
05883                             //=======================================
05884                             //***************************************
05885                             //*/
05886 
05887 
05888 
05889 // START DEBUGGING
05891                             //*
05892                             //***************************************
05893                             // Fixed Distance Interpolation
05894                             //=======================================
05895                             //---------------------------------------
05896                             // Edge# 0 to 11 <==> [ 1, 2, 4, ... , 2048 ]
05897 if ( iIterate >= 2 ) {
05898                             if ( EdgeTable[cubeClass] & 1 ) {
05899                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
05900                                     //
05901                                     // Interpotation Data
05902                                     interpolationValPlus = VIntp[0].GetX();
05903                                     //
05904                                     vertexList[ 0] = V[0] + interpolationValPlus*(V[1] - V[0]);//.Normalized()*CUBE_DIMENSIONS[0];
05905                                     grad[0]        = G[0] + interpolationValPlus*(G[1] - G[0]);//.Normalized()*CUBE_DIMENSIONS[0];
05906                                     texCoordList[0] = VTC[0] + interpolationValPlus*(VTC[1] - VTC[0]);//.Normalized()*CUBE_DIMENSIONS[0];
05907                                 }
05908                                 else {
05909                                     //
05910                                     // Interpotation Data
05911                                     interpolationValNeg = -VIntp[1].GetX();
05912                                     //
05913                                     vertexList[ 0] = V[1] + interpolationValNeg*(V[1] - V[0]);//.Normalized()*CUBE_DIMENSIONS[0];
05914                                     grad[0]        = G[1] + interpolationValNeg*(G[1] - G[0]);//.Normalized()*CUBE_DIMENSIONS[0];
05915                                     texCoordList[0] = VTC[1] + interpolationValNeg*(VTC[1] - VTC[0]);//.Normalized()*CUBE_DIMENSIONS[0];
05916                                 }
05917                                 grad[0].Normalized();
05918                             }
05919                             if ( EdgeTable[cubeClass] & 2 ) {
05920                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
05921                                     //
05922                                     // Interpotation Data
05923                                     interpolationValPlus = VIntp[1].GetY();
05924                                     //
05925                                     vertexList[ 1] = V[1] + interpolationValPlus*(V[2] - V[1]);//.Normalized()*CUBE_DIMENSIONS[1];
05926                                     grad[1]        = G[1] + interpolationValPlus*(G[2] - G[1]);//.Normalized()*CUBE_DIMENSIONS[1];
05927                                     texCoordList[1] = VTC[1] + interpolationValPlus*(VTC[2] - VTC[1]);//.Normalized()*CUBE_DIMENSIONS[1];
05928                                 }
05929                                 else {
05930                                     //
05931                                     // Interpotation Data
05932                                     interpolationValNeg = -VIntp[2].GetY();
05933                                     //
05934                                     vertexList[ 1] = V[2] + interpolationValNeg*(V[2] - V[1]);//.Normalized()*CUBE_DIMENSIONS[1];
05935                                     grad[1]        = G[2] + interpolationValNeg*(G[2] - G[1]);//.Normalized()*CUBE_DIMENSIONS[1];
05936                                     texCoordList[1] = VTC[2] + interpolationValNeg*(VTC[2] - VTC[1]);//.Normalized()*CUBE_DIMENSIONS[1];
05937                                 }
05938                                 grad[1].Normalized();
05939                             }
05940                             if ( EdgeTable[cubeClass] & 4 ) {
05941                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
05942                                     //
05943                                     // Interpotation Data
05944                                     interpolationValPlus = VIntp[2].GetX();
05945                                     //
05946                                     vertexList[ 2] = V[2] + interpolationValPlus*(V[3] - V[2]);//.Normalized()*CUBE_DIMENSIONS[0];
05947                                     grad[2]        = G[2] + interpolationValPlus*(G[3] - G[2]);//.Normalized()*CUBE_DIMENSIONS[0];
05948                                     texCoordList[2] = VTC[2] + interpolationValPlus*(VTC[3] - VTC[2]);//.Normalized()*CUBE_DIMENSIONS[0];
05949                                 }
05950                                 else {
05951                                     //
05952                                     // Interpotation Data
05953                                     interpolationValNeg = -VIntp[3].GetX();
05954                                     //
05955                                     vertexList[ 2] = V[3] + interpolationValNeg*(V[3] - V[2]);//.Normalized()*CUBE_DIMENSIONS[0];
05956                                     grad[2]        = G[3] + interpolationValNeg*(G[3] - G[2]);//.Normalized()*CUBE_DIMENSIONS[0];
05957                                     texCoordList[2] = VTC[3] + interpolationValNeg*(VTC[3] - VTC[2]);//.Normalized()*CUBE_DIMENSIONS[0];
05958                                 }
05959                                 grad[2].Normalized();
05960                             }
05961                             if ( EdgeTable[cubeClass] & 8 ) {
05962                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
05963                                     //
05964                                     // Interpotation Data
05965                                     interpolationValPlus = VIntp[0].GetY();
05966                                     //
05967                                     vertexList[ 3] = V[0] + interpolationValPlus*(V[3] - V[0]);//.Normalized()*CUBE_DIMENSIONS[1];
05968                                     grad[3]        = G[0] + interpolationValPlus*(G[3] - G[0]);//.Normalized()*CUBE_DIMENSIONS[1];
05969                                     texCoordList[3] = VTC[0] + interpolationValPlus*(VTC[3] - VTC[0]);//.Normalized()*CUBE_DIMENSIONS[1];
05970                                 }
05971                                 else {
05972                                     //
05973                                     // Interpotation Data
05974                                     interpolationValNeg = -VIntp[3].GetY();
05975                                     //
05976                                     vertexList[ 3] = V[3] + interpolationValNeg*(V[3] - V[0]);//.Normalized()*CUBE_DIMENSIONS[1];
05977                                     grad[3]        = G[3] + interpolationValNeg*(G[3] - G[0]);//.Normalized()*CUBE_DIMENSIONS[1];
05978                                     texCoordList[3] = VTC[3] + interpolationValNeg*(VTC[3] - VTC[0]);//.Normalized()*CUBE_DIMENSIONS[1];
05979                                 }
05980                                 grad[3].Normalized();
05981                             }
05982                             if ( EdgeTable[cubeClass] & 16 ) {
05983                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
05984                                     //
05985                                     // Interpotation Data
05986                                     interpolationValPlus = VIntp[4].GetX();
05987                                     //
05988                                     vertexList[ 4] = V[4] + interpolationValPlus*(V[5] - V[4]);//.Normalized()*CUBE_DIMENSIONS[0];
05989                                     grad[4]        = G[4] + interpolationValPlus*(G[5] - G[4]);//.Normalized()*CUBE_DIMENSIONS[0];
05990                                     texCoordList[4] = VTC[4] + interpolationValPlus*(VTC[5] - VTC[4]);//.Normalized()*CUBE_DIMENSIONS[0];
05991                                 }
05992                                 else {
05993                                     //
05994                                     // Interpotation Data
05995                                     interpolationValNeg = -VIntp[5].GetX();
05996                                     //
05997                                     vertexList[ 4] = V[5] + interpolationValNeg*(V[5] - V[4]);//.Normalized()*CUBE_DIMENSIONS[0];
05998                                     grad[4]        = G[5] + interpolationValNeg*(G[5] - G[4]);//.Normalized()*CUBE_DIMENSIONS[0];
05999                                     texCoordList[4] = VTC[5] + interpolationValNeg*(VTC[5] - VTC[4]);//.Normalized()*CUBE_DIMENSIONS[0];
06000                                 }
06001                                 grad[4].Normalized();
06002                             }
06003                             if ( EdgeTable[cubeClass] & 32 ) {
06004                                 if ( dataForPosition[idx[5] + 3] > threshold ) {
06005                                     //
06006                                     // Interpotation Data
06007                                     interpolationValPlus = VIntp[5].GetY();
06008                                     //
06009                                     vertexList[ 5] = V[5] + interpolationValPlus*(V[6] - V[5]);//.Normalized()*CUBE_DIMENSIONS[1];
06010                                     grad[5]        = G[5] + interpolationValPlus*(G[6] - G[5]);//.Normalized()*CUBE_DIMENSIONS[1];
06011                                     texCoordList[5] = VTC[5] + interpolationValPlus*(VTC[6] - VTC[5]);//.Normalized()*CUBE_DIMENSIONS[1];
06012                                 }
06013                                 else {
06014                                     //
06015                                     // Interpotation Data
06016                                     interpolationValNeg = -VIntp[6].GetY();
06017                                     //
06018                                     vertexList[ 5] = V[6] + interpolationValNeg*(V[6] - V[5]);//.Normalized()*CUBE_DIMENSIONS[1];
06019                                     grad[5]        = G[6] + interpolationValNeg*(G[6] - G[5]);//.Normalized()*CUBE_DIMENSIONS[1];
06020                                     texCoordList[5] = VTC[6] + interpolationValNeg*(VTC[6] - VTC[5]);//.Normalized()*CUBE_DIMENSIONS[1];
06021                                 }
06022                                 grad[5].Normalized();
06023                             }
06024                             if ( EdgeTable[cubeClass] & 64 ) {
06025                                 if ( dataForPosition[idx[6] + 3] > threshold ) {
06026                                     //
06027                                     // Interpotation Data
06028                                     interpolationValPlus = VIntp[6].GetX();
06029                                     //
06030                                     vertexList[ 6] = V[6] + interpolationValPlus*(V[7] - V[6]);//.Normalized()*CUBE_DIMENSIONS[0];
06031                                     grad[6]        = G[6] + interpolationValPlus*(G[7] - G[6]);//.Normalized()*CUBE_DIMENSIONS[0];
06032                                     texCoordList[6] = VTC[6] + interpolationValPlus*(VTC[7] - VTC[6]);//.Normalized()*CUBE_DIMENSIONS[0];
06033                                 }
06034                                 else {
06035                                     //
06036                                     // Interpotation Data
06037                                     interpolationValNeg = -VIntp[7].GetX();
06038                                     //
06039                                     vertexList[ 6] = V[7] + interpolationValNeg*(V[7] - V[6]);//.Normalized()*CUBE_DIMENSIONS[0];
06040                                     grad[6]        = G[7] + interpolationValNeg*(G[7] - G[6]);//.Normalized()*CUBE_DIMENSIONS[0];
06041                                     texCoordList[6] = VTC[7] + interpolationValNeg*(VTC[7] - VTC[6]);//.Normalized()*CUBE_DIMENSIONS[0];
06042                                 }
06043                                 grad[6].Normalized();
06044                             }
06045                             if ( EdgeTable[cubeClass] & 128 ) {
06046                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
06047                                     //
06048                                     // Interpotation Data
06049                                     interpolationValPlus = VIntp[4].GetY();
06050                                     //
06051                                     vertexList[ 7] = V[4] + interpolationValPlus*(V[7] - V[4]);//.Normalized()*CUBE_DIMENSIONS[1];
06052                                     grad[7]        = G[4] + interpolationValPlus*(G[7] - G[4]);//.Normalized()*CUBE_DIMENSIONS[1];
06053                                     texCoordList[7] = VTC[4] + interpolationValPlus*(VTC[7] - VTC[4]);//.Normalized()*CUBE_DIMENSIONS[1];
06054                                 }
06055                                 else {
06056                                     //
06057                                     // Interpotation Data
06058                                     interpolationValNeg = -VIntp[7].GetY();
06059                                     //
06060                                     vertexList[ 7] = V[7] + interpolationValNeg*(V[7] - V[4]);//.Normalized()*CUBE_DIMENSIONS[1];
06061                                     grad[7]        = G[7] + interpolationValNeg*(G[7] - G[4]);//.Normalized()*CUBE_DIMENSIONS[1];
06062                                     texCoordList[7] = VTC[7] + interpolationValNeg*(VTC[7] - VTC[4]);//.Normalized()*CUBE_DIMENSIONS[1];
06063                                 }
06064                                 grad[7].Normalized();
06065                             }
06066                             if ( EdgeTable[cubeClass] & 256 ) {
06067                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06068                                     //
06069                                     // Interpotation Data
06070                                     interpolationValPlus = VIntp[0].GetZ();
06071                                     //
06072                                     vertexList[ 8] = V[0] + interpolationValPlus*(V[4] - V[0]);//.Normalized()*CUBE_DIMENSIONS[2];
06073                                     grad[8]        = G[0] + interpolationValPlus*(G[4] - G[0]);//.Normalized()*CUBE_DIMENSIONS[2];
06074                                     texCoordList[8] = VTC[0] + interpolationValPlus*(VTC[4] - VTC[0]);//.Normalized()*CUBE_DIMENSIONS[2];
06075                                 }
06076                                 else {
06077                                     //
06078                                     // Interpotation Data
06079                                     interpolationValNeg = -VIntp[4].GetZ();
06080                                     //
06081                                     vertexList[ 8] = V[4] + interpolationValNeg*(V[4] - V[0]);//.Normalized()*CUBE_DIMENSIONS[2];
06082                                     grad[8]        = G[4] + interpolationValNeg*(G[4] - G[0]);//.Normalized()*CUBE_DIMENSIONS[2];
06083                                     texCoordList[8] = VTC[4] + interpolationValNeg*(VTC[4] - VTC[0]);//.Normalized()*CUBE_DIMENSIONS[2];
06084                                 }
06085                                 grad[8].Normalized();
06086                             }
06087                             if ( EdgeTable[cubeClass] & 512 ) {
06088                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
06089                                     //
06090                                     // Interpotation Data
06091                                     interpolationValPlus = VIntp[1].GetZ();
06092                                     //
06093                                     vertexList[ 9] = V[1] + interpolationValPlus*(V[5] - V[1]);//.Normalized()*CUBE_DIMENSIONS[2];
06094                                     grad[9]        = G[1] + interpolationValPlus*(G[5] - G[1]);//.Normalized()*CUBE_DIMENSIONS[2];
06095                                     texCoordList[9] = VTC[1] + interpolationValPlus*(VTC[5] - VTC[1]);//.Normalized()*CUBE_DIMENSIONS[2];
06096                                 }
06097                                 else {
06098                                     //
06099                                     // Interpotation Data
06100                                     interpolationValNeg = -VIntp[5].GetZ();
06101                                     //
06102                                     vertexList[ 9] = V[5] + interpolationValNeg*(V[5] - V[1]);//.Normalized()*CUBE_DIMENSIONS[2];
06103                                     grad[9]        = G[5] + interpolationValNeg*(G[5] - G[1]);//.Normalized()*CUBE_DIMENSIONS[2];
06104                                     texCoordList[9] = VTC[5] + interpolationValNeg*(VTC[5] - VTC[1]);//.Normalized()*CUBE_DIMENSIONS[2];
06105                                 }
06106                                 grad[9].Normalized();
06107                             }
06108                             if ( EdgeTable[cubeClass] & 1024 ) {
06109                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
06110                                     //
06111                                     // Interpotation Data
06112                                     interpolationValPlus = VIntp[2].GetZ();
06113                                     //
06114                                     vertexList[10] = V[2] + interpolationValPlus*(V[6] - V[2]);//.Normalized()*CUBE_DIMENSIONS[2];
06115                                     grad[10]       = G[2] + interpolationValPlus*(G[6] - G[2]);//.Normalized()*CUBE_DIMENSIONS[2];
06116                                     texCoordList[10] = VTC[2] + interpolationValPlus*(VTC[6] - VTC[2]);//.Normalized()*CUBE_DIMENSIONS[2];
06117                                 }
06118                                 else {
06119                                     //
06120                                     // Interpotation Data
06121                                     interpolationValNeg = -VIntp[6].GetZ();
06122                                     //
06123                                     vertexList[10] = V[6] + interpolationValNeg*(V[6] - V[2]);//.Normalized()*CUBE_DIMENSIONS[2];
06124                                     grad[10]       = G[6] + interpolationValNeg*(G[6] - G[2]);//.Normalized()*CUBE_DIMENSIONS[2];
06125                                     texCoordList[10] = VTC[6] + interpolationValNeg*(VTC[6] - VTC[2]);//.Normalized()*CUBE_DIMENSIONS[2];
06126                                 }
06127                                 grad[10].Normalized();
06128                             }
06129                             if ( EdgeTable[cubeClass] & 2048 ) {
06130                                 if ( dataForPosition[idx[3] + 3] > threshold ) {
06131                                     //
06132                                     // Interpotation Data
06133                                     interpolationValPlus = VIntp[3].GetZ();
06134                                     //
06135                                     vertexList[11] = V[3] + interpolationValPlus*(V[7] - V[3]);//.Normalized()*CUBE_DIMENSIONS[2];
06136                                     grad[11]       = G[3] + interpolationValPlus*(G[7] - G[3]);//.Normalized()*CUBE_DIMENSIONS[2];
06137                                     texCoordList[11] = VTC[3] + interpolationValPlus*(VTC[7] - VTC[3]);//.Normalized()*CUBE_DIMENSIONS[2];
06138                                 }
06139                                 else {
06140                                     //
06141                                     // Interpotation Data
06142                                     interpolationValNeg = -VIntp[7].GetZ();
06143                                     //
06144                                     vertexList[11] = V[7] + interpolationValNeg*(V[7] - V[3]);//.Normalized()*CUBE_DIMENSIONS[2];
06145                                     grad[11]       = G[7] + interpolationValNeg*(G[7] - G[3]);//.Normalized()*CUBE_DIMENSIONS[2];
06146                                     texCoordList[11] = VTC[7] + interpolationValNeg*(VTC[7] - VTC[3]);//.Normalized()*CUBE_DIMENSIONS[2];
06147                                 }
06148                                 grad[11].Normalized();
06149                             }
06150 } // End processing normal cube
06151                             //---------------------------------------
06152                             //=======================================
06153                             //***************************************
06154                             //*/
06156 // END DEBUGGING
06157 
06158 
06159 //std::cout << "(V[1] - V[0]).Normalized(): " << (V[1] - V[0]).Normalized() << std::endl;
06160 //std::cout << "(V[1] - V[0]).Normalized().Length(): " << (V[1] - V[0]).Normalized().Length() << std::endl;;
06161 
06162                             //*
06163                             //***************************************
06164                             // Fixed Distance Interpolation
06165                             //=======================================
06166                             //---------------------------------------
06167                             // Edge# 0 to 11 <==> [ 1, 2, 4, ... , 2048 ]
06168 else { //if ( iterate < 2 ) {
06169                             if ( EdgeTable[cubeClass] & 1 ) {
06170                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06171                                     //
06172                                     // Interpotation Data
06173                                     interpolationValPlus = VIntp[0].GetX();
06174                                     //
06175                                     vertexList[ 0] = V[0] + interpolationValPlus*(V[1] - V[0]).Normalized()*CUBE_DIMENSIONS[0];
06176                                     grad[0]        = G[0] + interpolationValPlus*(G[1] - G[0]).Normalized()*CUBE_DIMENSIONS[0];
06177                                     texCoordList[0] = VTC[0] + interpolationValPlus*(VTC[1] - VTC[0]).Normalized()*CUBE_DIMENSIONS[0];
06178                                 }
06179                                 else {
06180                                     //
06181                                     // Interpotation Data
06182                                     interpolationValNeg = -VIntp[1].GetX();
06183                                     //
06184                                     vertexList[ 0] = V[1] + interpolationValNeg*(V[1] - V[0]).Normalized()*CUBE_DIMENSIONS[0];
06185                                     grad[0]        = G[1] + interpolationValNeg*(G[1] - G[0]).Normalized()*CUBE_DIMENSIONS[0];
06186                                     texCoordList[0] = VTC[1] + interpolationValNeg*(VTC[1] - VTC[0]).Normalized()*CUBE_DIMENSIONS[0];
06187                                 }
06188                                 grad[0].Normalized();
06189                             }
06190                             if ( EdgeTable[cubeClass] & 2 ) {
06191                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
06192                                     //
06193                                     // Interpotation Data
06194                                     interpolationValPlus = VIntp[1].GetY();
06195                                     //
06196                                     vertexList[ 1] = V[1] + interpolationValPlus*(V[2] - V[1]).Normalized()*CUBE_DIMENSIONS[1];
06197                                     grad[1]        = G[1] + interpolationValPlus*(G[2] - G[1]).Normalized()*CUBE_DIMENSIONS[1];
06198                                     texCoordList[1] = VTC[1] + interpolationValPlus*(VTC[2] - VTC[1]).Normalized()*CUBE_DIMENSIONS[1];
06199                                 }
06200                                 else {
06201                                     //
06202                                     // Interpotation Data
06203                                     interpolationValNeg = -VIntp[2].GetY();
06204                                     //
06205                                     vertexList[ 1] = V[2] + interpolationValNeg*(V[2] - V[1]).Normalized()*CUBE_DIMENSIONS[1];
06206                                     grad[1]        = G[2] + interpolationValNeg*(G[2] - G[1]).Normalized()*CUBE_DIMENSIONS[1];
06207                                     texCoordList[1] = VTC[2] + interpolationValNeg*(VTC[2] - VTC[1]).Normalized()*CUBE_DIMENSIONS[1];
06208                                 }
06209                                 grad[1].Normalized();
06210                             }
06211                             if ( EdgeTable[cubeClass] & 4 ) {
06212                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
06213                                     //
06214                                     // Interpotation Data
06215                                     interpolationValPlus = VIntp[2].GetX();
06216                                     //
06217                                     vertexList[ 2] = V[2] + interpolationValPlus*(V[3] - V[2]).Normalized()*CUBE_DIMENSIONS[0];
06218                                     grad[2]        = G[2] + interpolationValPlus*(G[3] - G[2]).Normalized()*CUBE_DIMENSIONS[0];
06219                                     texCoordList[2] = VTC[2] + interpolationValPlus*(VTC[3] - VTC[2]).Normalized()*CUBE_DIMENSIONS[0];
06220                                 }
06221                                 else {
06222                                     //
06223                                     // Interpotation Data
06224                                     interpolationValNeg = -VIntp[3].GetX();
06225                                     //
06226                                     vertexList[ 2] = V[3] + interpolationValNeg*(V[3] - V[2]).Normalized()*CUBE_DIMENSIONS[0];
06227                                     grad[2]        = G[3] + interpolationValNeg*(G[3] - G[2]).Normalized()*CUBE_DIMENSIONS[0];
06228                                     texCoordList[2] = VTC[3] + interpolationValNeg*(VTC[3] - VTC[2]).Normalized()*CUBE_DIMENSIONS[0];
06229                                 }
06230                                 grad[2].Normalized();
06231                             }
06232                             if ( EdgeTable[cubeClass] & 8 ) {
06233                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06234                                     //
06235                                     // Interpotation Data
06236                                     interpolationValPlus = VIntp[0].GetY();
06237                                     //
06238                                     vertexList[ 3] = V[0] + interpolationValPlus*(V[3] - V[0]).Normalized()*CUBE_DIMENSIONS[1];
06239                                     grad[3]        = G[0] + interpolationValPlus*(G[3] - G[0]).Normalized()*CUBE_DIMENSIONS[1];
06240                                     texCoordList[3] = VTC[0] + interpolationValPlus*(VTC[3] - VTC[0]).Normalized()*CUBE_DIMENSIONS[1];
06241                                 }
06242                                 else {
06243                                     //
06244                                     // Interpotation Data
06245                                     interpolationValNeg = -VIntp[3].GetY();
06246                                     //
06247                                     vertexList[ 3] = V[3] + interpolationValNeg*(V[3] - V[0]).Normalized()*CUBE_DIMENSIONS[1];
06248                                     grad[3]        = G[3] + interpolationValNeg*(G[3] - G[0]).Normalized()*CUBE_DIMENSIONS[1];
06249                                     texCoordList[3] = VTC[3] + interpolationValNeg*(VTC[3] - VTC[0]).Normalized()*CUBE_DIMENSIONS[1];
06250                                 }
06251                                 grad[3].Normalized();
06252                             }
06253                             if ( EdgeTable[cubeClass] & 16 ) {
06254                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
06255                                     //
06256                                     // Interpotation Data
06257                                     interpolationValPlus = VIntp[4].GetX();
06258                                     //
06259                                     vertexList[ 4] = V[4] + interpolationValPlus*(V[5] - V[4]).Normalized()*CUBE_DIMENSIONS[0];
06260                                     grad[4]        = G[4] + interpolationValPlus*(G[5] - G[4]).Normalized()*CUBE_DIMENSIONS[0];
06261                                     texCoordList[4] = VTC[4] + interpolationValPlus*(VTC[5] - VTC[4]).Normalized()*CUBE_DIMENSIONS[0];
06262                                 }
06263                                 else {
06264                                     //
06265                                     // Interpotation Data
06266                                     interpolationValNeg = -VIntp[5].GetX();
06267                                     //
06268                                     vertexList[ 4] = V[5] + interpolationValNeg*(V[5] - V[4]).Normalized()*CUBE_DIMENSIONS[0];
06269                                     grad[4]        = G[5] + interpolationValNeg*(G[5] - G[4]).Normalized()*CUBE_DIMENSIONS[0];
06270                                     texCoordList[4] = VTC[5] + interpolationValNeg*(VTC[5] - VTC[4]).Normalized()*CUBE_DIMENSIONS[0];
06271                                 }
06272                                 grad[4].Normalized();
06273                             }
06274                             if ( EdgeTable[cubeClass] & 32 ) {
06275                                 if ( dataForPosition[idx[5] + 3] > threshold ) {
06276                                     //
06277                                     // Interpotation Data
06278                                     interpolationValPlus = VIntp[5].GetY();
06279                                     //
06280                                     vertexList[ 5] = V[5] + interpolationValPlus*(V[6] - V[5]).Normalized()*CUBE_DIMENSIONS[1];
06281                                     grad[5]        = G[5] + interpolationValPlus*(G[6] - G[5]).Normalized()*CUBE_DIMENSIONS[1];
06282                                     texCoordList[5] = VTC[5] + interpolationValPlus*(VTC[6] - VTC[5]).Normalized()*CUBE_DIMENSIONS[1];
06283                                 }
06284                                 else {
06285                                     //
06286                                     // Interpotation Data
06287                                     interpolationValNeg = -VIntp[6].GetY();
06288                                     //
06289                                     vertexList[ 5] = V[6] + interpolationValNeg*(V[6] - V[5]).Normalized()*CUBE_DIMENSIONS[1];
06290                                     grad[5]        = G[6] + interpolationValNeg*(G[6] - G[5]).Normalized()*CUBE_DIMENSIONS[1];
06291                                     texCoordList[5] = VTC[6] + interpolationValNeg*(VTC[6] - VTC[5]).Normalized()*CUBE_DIMENSIONS[1];
06292                                 }
06293                                 grad[5].Normalized();
06294                             }
06295                             if ( EdgeTable[cubeClass] & 64 ) {
06296                                 if ( dataForPosition[idx[6] + 3] > threshold ) {
06297                                     //
06298                                     // Interpotation Data
06299                                     interpolationValPlus = VIntp[6].GetX();
06300                                     //
06301                                     vertexList[ 6] = V[6] + interpolationValPlus*(V[7] - V[6]).Normalized()*CUBE_DIMENSIONS[0];
06302                                     grad[6]        = G[6] + interpolationValPlus*(G[7] - G[6]).Normalized()*CUBE_DIMENSIONS[0];
06303                                     texCoordList[6] = VTC[6] + interpolationValPlus*(VTC[7] - VTC[6]).Normalized()*CUBE_DIMENSIONS[0];
06304                                 }
06305                                 else {
06306                                     //
06307                                     // Interpotation Data
06308                                     interpolationValNeg = -VIntp[7].GetX();
06309                                     //
06310                                     vertexList[ 6] = V[7] + interpolationValNeg*(V[7] - V[6]).Normalized()*CUBE_DIMENSIONS[0];
06311                                     grad[6]        = G[7] + interpolationValNeg*(G[7] - G[6]).Normalized()*CUBE_DIMENSIONS[0];
06312                                     texCoordList[6] = VTC[7] + interpolationValNeg*(VTC[7] - VTC[6]).Normalized()*CUBE_DIMENSIONS[0];
06313                                 }
06314                                 grad[6].Normalized();
06315                             }
06316                             if ( EdgeTable[cubeClass] & 128 ) {
06317                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
06318                                     //
06319                                     // Interpotation Data
06320                                     interpolationValPlus = VIntp[4].GetY();
06321                                     //
06322                                     vertexList[ 7] = V[4] + interpolationValPlus*(V[7] - V[4]).Normalized()*CUBE_DIMENSIONS[1];
06323                                     grad[7]        = G[4] + interpolationValPlus*(G[7] - G[4]).Normalized()*CUBE_DIMENSIONS[1];
06324                                     texCoordList[7] = VTC[4] + interpolationValPlus*(VTC[7] - VTC[4]).Normalized()*CUBE_DIMENSIONS[1];
06325                                 }
06326                                 else {
06327                                     //
06328                                     // Interpotation Data
06329                                     interpolationValNeg = -VIntp[7].GetY();
06330                                     //
06331                                     vertexList[ 7] = V[7] + interpolationValNeg*(V[7] - V[4]).Normalized()*CUBE_DIMENSIONS[1];
06332                                     grad[7]        = G[7] + interpolationValNeg*(G[7] - G[4]).Normalized()*CUBE_DIMENSIONS[1];
06333                                     texCoordList[7] = VTC[7] + interpolationValNeg*(VTC[7] - VTC[4]).Normalized()*CUBE_DIMENSIONS[1];
06334                                 }
06335                                 grad[7].Normalized();
06336                             }
06337                             if ( EdgeTable[cubeClass] & 256 ) {
06338                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06339                                     //
06340                                     // Interpotation Data
06341                                     interpolationValPlus = VIntp[0].GetZ();
06342                                     //
06343                                     vertexList[ 8] = V[0] + interpolationValPlus*(V[4] - V[0]).Normalized()*CUBE_DIMENSIONS[2];
06344                                     grad[8]        = G[0] + interpolationValPlus*(G[4] - G[0]).Normalized()*CUBE_DIMENSIONS[2];
06345                                     texCoordList[8] = VTC[0] + interpolationValPlus*(VTC[4] - VTC[0]).Normalized()*CUBE_DIMENSIONS[2];
06346                                 }
06347                                 else {
06348                                     //
06349                                     // Interpotation Data
06350                                     interpolationValNeg = -VIntp[4].GetZ();
06351                                     //
06352                                     vertexList[ 8] = V[4] + interpolationValNeg*(V[4] - V[0]).Normalized()*CUBE_DIMENSIONS[2];
06353                                     grad[8]        = G[4] + interpolationValNeg*(G[4] - G[0]).Normalized()*CUBE_DIMENSIONS[2];
06354                                     texCoordList[8] = VTC[4] + interpolationValNeg*(VTC[4] - VTC[0]).Normalized()*CUBE_DIMENSIONS[2];
06355                                 }
06356                                 grad[8].Normalized();
06357                             }
06358                             if ( EdgeTable[cubeClass] & 512 ) {
06359                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
06360                                     //
06361                                     // Interpotation Data
06362                                     interpolationValPlus = VIntp[1].GetZ();
06363                                     //
06364                                     vertexList[ 9] = V[1] + interpolationValPlus*(V[5] - V[1]).Normalized()*CUBE_DIMENSIONS[2];
06365                                     grad[9]        = G[1] + interpolationValPlus*(G[5] - G[1]).Normalized()*CUBE_DIMENSIONS[2];
06366                                     texCoordList[9] = VTC[1] + interpolationValPlus*(VTC[5] - VTC[1]).Normalized()*CUBE_DIMENSIONS[2];
06367                                 }
06368                                 else {
06369                                     //
06370                                     // Interpotation Data
06371                                     interpolationValNeg = -VIntp[5].GetZ();
06372                                     //
06373                                     vertexList[ 9] = V[5] + interpolationValNeg*(V[5] - V[1]).Normalized()*CUBE_DIMENSIONS[2];
06374                                     grad[9]        = G[5] + interpolationValNeg*(G[5] - G[1]).Normalized()*CUBE_DIMENSIONS[2];
06375                                     texCoordList[9] = VTC[5] + interpolationValNeg*(VTC[5] - VTC[1]).Normalized()*CUBE_DIMENSIONS[2];
06376                                 }
06377                                 grad[9].Normalized();
06378                             }
06379                             if ( EdgeTable[cubeClass] & 1024 ) {
06380                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
06381                                     //
06382                                     // Interpotation Data
06383                                     interpolationValPlus = VIntp[2].GetZ();
06384                                     //
06385                                     vertexList[10] = V[2] + interpolationValPlus*(V[6] - V[2]).Normalized()*CUBE_DIMENSIONS[2];
06386                                     grad[10]       = G[2] + interpolationValPlus*(G[6] - G[2]).Normalized()*CUBE_DIMENSIONS[2];
06387                                     texCoordList[10] = VTC[2] + interpolationValPlus*(VTC[6] - VTC[2]).Normalized()*CUBE_DIMENSIONS[2];
06388                                 }
06389                                 else {
06390                                     //
06391                                     // Interpotation Data
06392                                     interpolationValNeg = -VIntp[6].GetZ();
06393                                     //
06394                                     vertexList[10] = V[6] + interpolationValNeg*(V[6] - V[2]).Normalized()*CUBE_DIMENSIONS[2];
06395                                     grad[10]       = G[6] + interpolationValNeg*(G[6] - G[2]).Normalized()*CUBE_DIMENSIONS[2];
06396                                     texCoordList[10] = VTC[6] + interpolationValNeg*(VTC[6] - VTC[2]).Normalized()*CUBE_DIMENSIONS[2];
06397                                 }
06398                                 grad[10].Normalized();
06399                             }
06400                             if ( EdgeTable[cubeClass] & 2048 ) {
06401                                 if ( dataForPosition[idx[3] + 3] > threshold ) {
06402                                     //
06403                                     // Interpotation Data
06404                                     interpolationValPlus = VIntp[3].GetZ();
06405                                     //
06406                                     vertexList[11] = V[3] + interpolationValPlus*(V[7] - V[3]).Normalized()*CUBE_DIMENSIONS[2];
06407                                     grad[11]       = G[3] + interpolationValPlus*(G[7] - G[3]).Normalized()*CUBE_DIMENSIONS[2];
06408                                     texCoordList[11] = VTC[3] + interpolationValPlus*(VTC[7] - VTC[3]).Normalized()*CUBE_DIMENSIONS[2];
06409                                 }
06410                                 else {
06411                                     //
06412                                     // Interpotation Data
06413                                     interpolationValNeg = -VIntp[7].GetZ();
06414                                     //
06415                                     vertexList[11] = V[7] + interpolationValNeg*(V[7] - V[3]).Normalized()*CUBE_DIMENSIONS[2];
06416                                     grad[11]       = G[7] + interpolationValNeg*(G[7] - G[3]).Normalized()*CUBE_DIMENSIONS[2];
06417                                     texCoordList[11] = VTC[7] + interpolationValNeg*(VTC[7] - VTC[3]).Normalized()*CUBE_DIMENSIONS[2];
06418                                 }
06419                                 grad[11].Normalized();
06420                             }
06421 } // End processing `left' or `right' cube
06422                             //---------------------------------------
06423                             //=======================================
06424                             //***************************************
06425                             //*/
06426 
06427 
06428 
06429                         } // END: Not Use Global Interpolation Value
06430                         //-----------------------------------------------
06431                         // Use Global Interpolation Value -- all elements have the same interpolation value
06432                         else {
06433                             //-------------------------------------
06434                             // Marching Cube Interpolation
06435                             T interpolationValPlus = m_tGlobalInterpolationVal;
06436                             T interpolationValNeg  = 1.0 - m_tGlobalInterpolationVal;
06437                             //T interpolationValPlus = 0.3;
06438                             //T interpolationValNeg  = 1.0 - 0.3;
06439 
06440 
06441                             /*
06442                             //***************************************
06443                             // Normal Interpolation
06444                             //=======================================
06445                             //---------------------------------------
06446                             // Edge# 0 to 11 <==> [ 1, 2, 4, ... , 2048 ]
06447                             if ( EdgeTable[cubeClass] & 1 ) {
06448                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06449                                     //
06450                                     // Interpotation Data
06451                                     //interpolationValPlus = VIntp[0].GetX();
06452                                     //
06453                                     vertexList[ 0] = V[0] + interpolationValPlus*(V[1] - V[0]);
06454                                     grad[0]        = G[0] + interpolationValPlus*(G[1] - G[0]);
06455                                     texCoordList[0] = VTC[0] + interpolationValPlus*(VTC[1] - VTC[0]);
06456                                 }
06457                                 else {
06458                                     //
06459                                     // Interpotation Data
06460                                     //interpolationValNeg = 1.0 - VIntp[1].GetX();
06461                                     //
06462                                     vertexList[ 0] = V[0] + interpolationValNeg*(V[1] - V[0]);
06463                                     grad[0]        = G[0] + interpolationValNeg*(G[1] - G[0]);
06464                                     texCoordList[0] = VTC[0] + interpolationValNeg*(VTC[1] - VTC[0]);
06465                                 }
06466                                 grad[0].Normalized();
06467                             }
06468                             if ( EdgeTable[cubeClass] & 2 ) {
06469                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
06470                                     //
06471                                     // Interpotation Data
06472                                     //interpolationValPlus = VIntp[1].GetY();
06473                                     //
06474                                     vertexList[ 1] = V[1] + interpolationValPlus*(V[2] - V[1]);
06475                                     grad[1]        = G[1] + interpolationValPlus*(G[2] - G[1]);
06476                                     texCoordList[1] = VTC[1] + interpolationValPlus*(VTC[2] - VTC[1]);
06477                                 }
06478                                 else {
06479                                     //
06480                                     // Interpotation Data
06481                                     //interpolationValNeg = 1.0 - VIntp[2].GetY();
06482                                     //
06483                                     vertexList[ 1] = V[1] + interpolationValNeg*(V[2] - V[1]);
06484                                     grad[1]        = G[1] + interpolationValNeg*(G[2] - G[1]);
06485                                     texCoordList[1] = VTC[1] + interpolationValNeg*(VTC[2] - VTC[1]);
06486                                 }
06487                                 grad[1].Normalized();
06488                             }
06489                             if ( EdgeTable[cubeClass] & 4 ) {
06490                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
06491                                     //
06492                                     // Interpotation Data
06493                                     //interpolationValPlus = VIntp[2].GetX();
06494                                     //
06495                                     vertexList[ 2] = V[2] + interpolationValPlus*(V[3] - V[2]);
06496                                     grad[2]        = G[2] + interpolationValPlus*(G[3] - G[2]);
06497                                     texCoordList[2] = VTC[2] + interpolationValPlus*(VTC[3] - VTC[2]);
06498                                 }
06499                                 else {
06500                                     //
06501                                     // Interpotation Data
06502                                     //interpolationValNeg = 1.0 - VIntp[3].GetX();
06503                                     //
06504                                     vertexList[ 2] = V[2] + interpolationValNeg*(V[3] - V[2]);
06505                                     grad[2]        = G[2] + interpolationValNeg*(G[3] - G[2]);
06506                                     texCoordList[2] = VTC[2] + interpolationValNeg*(VTC[3] - VTC[2]);
06507                                 }
06508                                 grad[2].Normalized();
06509                             }
06510                             if ( EdgeTable[cubeClass] & 8 ) {
06511                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06512                                     //
06513                                     // Interpotation Data
06514                                     //interpolationValPlus = VIntp[0].GetY();
06515                                     //
06516                                     vertexList[ 3] = V[0] + interpolationValPlus*(V[3] - V[0]);
06517                                     grad[3]        = G[0] + interpolationValPlus*(G[3] - G[0]);
06518                                     texCoordList[3] = VTC[0] + interpolationValPlus*(VTC[3] - VTC[0]);
06519                                 }
06520                                 else {
06521                                     //
06522                                     // Interpotation Data
06523                                     //interpolationValNeg = 1.0 - VIntp[3].GetY();
06524                                     //
06525                                     vertexList[ 3] = V[0] + interpolationValNeg*(V[3] - V[0]);
06526                                     grad[3]        = G[0] + interpolationValNeg*(G[3] - G[0]);
06527                                     texCoordList[3] = VTC[0] + interpolationValNeg*(VTC[3] - VTC[0]);
06528                                 }
06529                                 grad[3].Normalized();
06530                             }
06531                             if ( EdgeTable[cubeClass] & 16 ) {
06532                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
06533                                     //
06534                                     // Interpotation Data
06535                                     //interpolationValPlus = VIntp[4].GetX();
06536                                     //
06537                                     vertexList[ 4] = V[4] + interpolationValPlus*(V[5] - V[4]);
06538                                     grad[4]        = G[4] + interpolationValPlus*(G[5] - G[4]);
06539                                     texCoordList[4] = VTC[4] + interpolationValPlus*(VTC[5] - VTC[4]);
06540                                 }
06541                                 else {
06542                                     //
06543                                     // Interpotation Data
06544                                     //interpolationValNeg = 1.0 - VIntp[5].GetX();
06545                                     //
06546                                     vertexList[ 4] = V[4] + interpolationValNeg*(V[5] - V[4]);
06547                                     grad[4]        = G[4] + interpolationValNeg*(G[5] - G[4]);
06548                                     texCoordList[4] = VTC[4] + interpolationValNeg*(VTC[5] - VTC[4]);
06549                                 }
06550                                 grad[4].Normalized();
06551                             }
06552                             if ( EdgeTable[cubeClass] & 32 ) {
06553                                 if ( dataForPosition[idx[5] + 3] > threshold ) {
06554                                     //
06555                                     // Interpotation Data
06556                                     //interpolationValPlus = VIntp[5].GetY();
06557                                     //
06558                                     vertexList[ 5] = V[5] + interpolationValPlus*(V[6] - V[5]);
06559                                     grad[5]        = G[5] + interpolationValPlus*(G[6] - G[5]);
06560                                     texCoordList[5] = VTC[5] + interpolationValPlus*(VTC[6] - VTC[5]);
06561                                 }
06562                                 else {
06563                                     //
06564                                     // Interpotation Data
06565                                     //interpolationValNeg = 1.0 - VIntp[6].GetY();
06566                                     //
06567                                     vertexList[ 5] = V[5] + interpolationValNeg*(V[6] - V[5]);
06568                                     grad[5]        = G[5] + interpolationValNeg*(G[6] - G[5]);
06569                                     texCoordList[5] = VTC[5] + interpolationValNeg*(VTC[6] - VTC[5]);
06570                                 }
06571                                 grad[5].Normalized();
06572                             }
06573                             if ( EdgeTable[cubeClass] & 64 ) {
06574                                 if ( dataForPosition[idx[6] + 3] > threshold ) {
06575                                     //
06576                                     // Interpotation Data
06577                                     //interpolationValPlus = VIntp[6].GetX();
06578                                     //
06579                                     vertexList[ 6] = V[6] + interpolationValPlus*(V[7] - V[6]);
06580                                     grad[6]        = G[6] + interpolationValPlus*(G[7] - G[6]);
06581                                     texCoordList[6] = VTC[6] + interpolationValPlus*(VTC[7] - VTC[6]);
06582                                 }
06583                                 else {
06584                                     //
06585                                     // Interpotation Data
06586                                     //interpolationValNeg = 1.0 - VIntp[7].GetX();
06587                                     //
06588                                     vertexList[ 6] = V[6] + interpolationValNeg*(V[7] - V[6]);
06589                                     grad[6]        = G[6] + interpolationValNeg*(G[7] - G[6]);
06590                                     texCoordList[6] = VTC[6] + interpolationValNeg*(VTC[7] - VTC[6]);
06591                                 }
06592                                 grad[6].Normalized();
06593                             }
06594                             if ( EdgeTable[cubeClass] & 128 ) {
06595                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
06596                                     //
06597                                     // Interpotation Data
06598                                     //interpolationValPlus = VIntp[4].GetY();
06599                                     //
06600                                     vertexList[ 7] = V[4] + interpolationValPlus*(V[7] - V[4]);
06601                                     grad[7]        = G[4] + interpolationValPlus*(G[7] - G[4]);
06602                                     texCoordList[7] = VTC[4] + interpolationValPlus*(VTC[7] - VTC[4]);
06603                                 }
06604                                 else {
06605                                     //
06606                                     // Interpotation Data
06607                                     //interpolationValNeg = 1.0 - VIntp[7].GetY();
06608                                     //
06609                                     vertexList[ 7] = V[4] + interpolationValNeg*(V[7] - V[4]);
06610                                     grad[7]        = G[4] + interpolationValNeg*(G[7] - G[4]);
06611                                     texCoordList[7] = VTC[4] + interpolationValNeg*(VTC[7] - VTC[4]);
06612                                 }
06613                                 grad[7].Normalized();
06614                             }
06615                             if ( EdgeTable[cubeClass] & 256 ) {
06616                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06617                                     //
06618                                     // Interpotation Data
06619                                     //interpolationValPlus = VIntp[0].GetZ();
06620                                     //
06621                                     vertexList[ 8] = V[0] + interpolationValPlus*(V[4] - V[0]);
06622                                     grad[8]        = G[0] + interpolationValPlus*(G[4] - G[0]);
06623                                     texCoordList[8] = VTC[0] + interpolationValPlus*(VTC[4] - VTC[0]);
06624                                 }
06625                                 else {
06626                                     //
06627                                     // Interpotation Data
06628                                     //interpolationValNeg = 1.0 - VIntp[4].GetZ();
06629                                     //
06630                                     vertexList[ 8] = V[0] + interpolationValNeg*(V[4] - V[0]);
06631                                     grad[8]        = G[0] + interpolationValNeg*(G[4] - G[0]);
06632                                     texCoordList[8] = VTC[0] + interpolationValNeg*(VTC[4] - VTC[0]);
06633                                 }
06634                                 grad[8].Normalized();
06635                             }
06636                             if ( EdgeTable[cubeClass] & 512 ) {
06637                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
06638                                     //
06639                                     // Interpotation Data
06640                                     //interpolationValPlus = VIntp[1].GetZ();
06641                                     //
06642                                     vertexList[ 9] = V[1] + interpolationValPlus*(V[5] - V[1]);
06643                                     grad[9]        = G[1] + interpolationValPlus*(G[5] - G[1]);
06644                                     texCoordList[9] = VTC[1] + interpolationValPlus*(VTC[5] - VTC[1]);
06645                                 }
06646                                 else {
06647                                     //
06648                                     // Interpotation Data
06649                                     //interpolationValNeg = 1.0 - VIntp[5].GetZ();
06650                                     //
06651                                     vertexList[ 9] = V[1] + interpolationValNeg*(V[5] - V[1]);
06652                                     grad[9]        = G[1] + interpolationValNeg*(G[5] - G[1]);
06653                                     texCoordList[9] = VTC[1] + interpolationValNeg*(VTC[5] - VTC[1]);
06654                                 }
06655                                 grad[9].Normalized();
06656                             }
06657                             if ( EdgeTable[cubeClass] & 1024 ) {
06658                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
06659                                     //
06660                                     // Interpotation Data
06661                                     //interpolationValPlus = VIntp[2].GetZ();
06662                                     //
06663                                     vertexList[10] = V[2] + interpolationValPlus*(V[6] - V[2]);
06664                                     grad[10]       = G[2] + interpolationValPlus*(G[6] - G[2]);
06665                                     texCoordList[10] = VTC[2] + interpolationValPlus*(VTC[6] - VTC[2]);
06666                                 }
06667                                 else {
06668                                     //
06669                                     // Interpotation Data
06670                                     //interpolationValNeg = 1.0 - VIntp[6].GetZ();
06671                                     //
06672                                     vertexList[10] = V[2] + interpolationValNeg*(V[6] - V[2]);
06673                                     grad[10]       = G[2] + interpolationValNeg*(G[6] - G[2]);
06674                                     texCoordList[10] = VTC[2] + interpolationValNeg*(VTC[6] - VTC[2]);
06675                                 }
06676                                 grad[10].Normalized();
06677                             }
06678                             if ( EdgeTable[cubeClass] & 2048 ) {
06679                                 if ( dataForPosition[idx[3] + 3] > threshold ) {
06680                                     //
06681                                     // Interpotation Data
06682                                     //interpolationValPlus = VIntp[3].GetZ();
06683                                     //
06684                                     vertexList[11] = V[3] + interpolationValPlus*(V[7] - V[3]);
06685                                     grad[11]       = G[3] + interpolationValPlus*(G[7] - G[3]);
06686                                     texCoordList[11] = VTC[3] + interpolationValPlus*(VTC[7] - VTC[3]);
06687                                 }
06688                                 else {
06689                                     //
06690                                     // Interpotation Data
06691                                     //interpolationValNeg = 1.0 - VIntp[7].GetZ();
06692                                     //
06693                                     vertexList[11] = V[3] + interpolationValNeg*(V[7] - V[3]);
06694                                     grad[11]       = G[3] + interpolationValNeg*(G[7] - G[3]);
06695                                     texCoordList[11] = VTC[3] + interpolationValNeg*(VTC[7] - VTC[3]);
06696                                 }
06697                                 grad[11].Normalized();
06698                             }
06699                             //---------------------------------------
06700                             //=======================================
06701                             //***************************************
06702                             //*/
06703 
06704 
06705 
06706                             //*
06707                             //***************************************
06708                             // Fixed Distance Interpolation
06709                             //=======================================
06710                             //---------------------------------------
06711                             // Edge# 0 to 11 <==> [ 1, 2, 4, ... , 2048 ]
06712                             if ( EdgeTable[cubeClass] & 1 ) {
06713                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06714                                     //
06715                                     // Interpotation Data
06716                                     //interpolationValPlus = VIntp[0].GetX();
06717                                     //
06718                                     vertexList[ 0] = V[0] + interpolationValPlus*(V[1] - V[0]).Normalized()*CUBE_DIMENSIONS[0];
06719                                     grad[0]        = G[0] + interpolationValPlus*(G[1] - G[0]).Normalized()*CUBE_DIMENSIONS[0];
06720                                     texCoordList[0] = VTC[0] + interpolationValPlus*(VTC[1] - VTC[0]).Normalized()*CUBE_DIMENSIONS[0];
06721                                 }
06722                                 else {
06723                                     //
06724                                     // Interpotation Data
06725                                     //interpolationValNeg = 1.0 - VIntp[1].GetX();
06726                                     //
06727                                     vertexList[ 0] = V[1] - interpolationValPlus*(V[1] - V[0]).Normalized()*CUBE_DIMENSIONS[0];
06728                                     grad[0]        = G[1] - interpolationValPlus*(G[1] - G[0]).Normalized()*CUBE_DIMENSIONS[0];
06729                                     texCoordList[0] = VTC[1] - interpolationValPlus*(VTC[1] - VTC[0]).Normalized()*CUBE_DIMENSIONS[0];
06730                                 }
06731                                 grad[0].Normalized();
06732                             }
06733                             if ( EdgeTable[cubeClass] & 2 ) {
06734                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
06735                                     //
06736                                     // Interpotation Data
06737                                     //interpolationValPlus = VIntp[1].GetY();
06738                                     //
06739                                     vertexList[ 1] = V[1] + interpolationValPlus*(V[2] - V[1]).Normalized()*CUBE_DIMENSIONS[1];
06740                                     grad[1]        = G[1] + interpolationValPlus*(G[2] - G[1]).Normalized()*CUBE_DIMENSIONS[1];
06741                                     texCoordList[1] = VTC[1] + interpolationValPlus*(VTC[2] - VTC[1]).Normalized()*CUBE_DIMENSIONS[1];
06742                                 }
06743                                 else {
06744                                     //
06745                                     // Interpotation Data
06746                                     //interpolationValNeg = 1.0 - VIntp[2].GetY();
06747                                     //
06748                                     vertexList[ 1] = V[2] - interpolationValPlus*(V[2] - V[1]).Normalized()*CUBE_DIMENSIONS[1];
06749                                     grad[1]        = G[2] - interpolationValPlus*(G[2] - G[1]).Normalized()*CUBE_DIMENSIONS[1];
06750                                     texCoordList[1] = VTC[2] - interpolationValPlus*(VTC[2] - VTC[1]).Normalized()*CUBE_DIMENSIONS[1];
06751                                 }
06752                                 grad[1].Normalized();
06753                             }
06754                             if ( EdgeTable[cubeClass] & 4 ) {
06755                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
06756                                     //
06757                                     // Interpotation Data
06758                                     //interpolationValPlus = VIntp[2].GetX();
06759                                     //
06760                                     vertexList[ 2] = V[2] + interpolationValPlus*(V[3] - V[2]).Normalized()*CUBE_DIMENSIONS[0];
06761                                     grad[2]        = G[2] + interpolationValPlus*(G[3] - G[2]).Normalized()*CUBE_DIMENSIONS[0];
06762                                     texCoordList[2] = VTC[2] + interpolationValPlus*(VTC[3] - VTC[2]).Normalized()*CUBE_DIMENSIONS[0];
06763                                 }
06764                                 else {
06765                                     //
06766                                     // Interpotation Data
06767                                     //interpolationValNeg = 1.0 - VIntp[3].GetX();
06768                                     //
06769                                     vertexList[ 2] = V[3] - interpolationValPlus*(V[3] - V[2]).Normalized()*CUBE_DIMENSIONS[0];
06770                                     grad[2]        = G[3] - interpolationValPlus*(G[3] - G[2]).Normalized()*CUBE_DIMENSIONS[0];
06771                                     texCoordList[2] = VTC[3] - interpolationValPlus*(VTC[3] - VTC[2]).Normalized()*CUBE_DIMENSIONS[0];
06772                                 }
06773                                 grad[2].Normalized();
06774                             }
06775                             if ( EdgeTable[cubeClass] & 8 ) {
06776                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06777                                     //
06778                                     // Interpotation Data
06779                                     //interpolationValPlus = VIntp[0].GetY();
06780                                     //
06781                                     vertexList[ 3] = V[0] + interpolationValPlus*(V[3] - V[0]).Normalized()*CUBE_DIMENSIONS[1];
06782                                     grad[3]        = G[0] + interpolationValPlus*(G[3] - G[0]).Normalized()*CUBE_DIMENSIONS[1];
06783                                     texCoordList[3] = VTC[0] + interpolationValPlus*(VTC[3] - VTC[0]).Normalized()*CUBE_DIMENSIONS[1];
06784                                 }
06785                                 else {
06786                                     //
06787                                     // Interpotation Data
06788                                     //interpolationValNeg = 1.0 - VIntp[3].GetY();
06789                                     //
06790                                     vertexList[ 3] = V[3] - interpolationValPlus*(V[3] - V[0]).Normalized()*CUBE_DIMENSIONS[1];
06791                                     grad[3]        = G[3] - interpolationValPlus*(G[3] - G[0]).Normalized()*CUBE_DIMENSIONS[1];
06792                                     texCoordList[3] = VTC[3] - interpolationValPlus*(VTC[3] - VTC[0]).Normalized()*CUBE_DIMENSIONS[1];
06793                                 }
06794                                 grad[3].Normalized();
06795                             }
06796                             if ( EdgeTable[cubeClass] & 16 ) {
06797                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
06798                                     //
06799                                     // Interpotation Data
06800                                     //interpolationValPlus = VIntp[4].GetX();
06801                                     //
06802                                     vertexList[ 4] = V[4] + interpolationValPlus*(V[5] - V[4]).Normalized()*CUBE_DIMENSIONS[0];
06803                                     grad[4]        = G[4] + interpolationValPlus*(G[5] - G[4]).Normalized()*CUBE_DIMENSIONS[0];
06804                                     texCoordList[4] = VTC[4] + interpolationValPlus*(VTC[5] - VTC[4]).Normalized()*CUBE_DIMENSIONS[0];
06805                                 }
06806                                 else {
06807                                     //
06808                                     // Interpotation Data
06809                                     //interpolationValNeg = 1.0 - VIntp[5].GetX();
06810                                     //
06811                                     vertexList[ 4] = V[5] - interpolationValPlus*(V[5] - V[4]).Normalized()*CUBE_DIMENSIONS[0];
06812                                     grad[4]        = G[5] - interpolationValPlus*(G[5] - G[4]).Normalized()*CUBE_DIMENSIONS[0];
06813                                     texCoordList[4] = VTC[5] - interpolationValPlus*(VTC[5] - VTC[4]).Normalized()*CUBE_DIMENSIONS[0];
06814                                 }
06815                                 grad[4].Normalized();
06816                             }
06817                             if ( EdgeTable[cubeClass] & 32 ) {
06818                                 if ( dataForPosition[idx[5] + 3] > threshold ) {
06819                                     //
06820                                     // Interpotation Data
06821                                     //interpolationValPlus = VIntp[5].GetY();
06822                                     //
06823                                     vertexList[ 5] = V[5] + interpolationValPlus*(V[6] - V[5]).Normalized()*CUBE_DIMENSIONS[1];
06824                                     grad[5]        = G[5] + interpolationValPlus*(G[6] - G[5]).Normalized()*CUBE_DIMENSIONS[1];
06825                                     texCoordList[5] = VTC[5] + interpolationValPlus*(VTC[6] - VTC[5]).Normalized()*CUBE_DIMENSIONS[1];
06826                                 }
06827                                 else {
06828                                     //
06829                                     // Interpotation Data
06830                                     //interpolationValNeg = 1.0 - VIntp[6].GetY();
06831                                     //
06832                                     vertexList[ 5] = V[6] - interpolationValPlus*(V[6] - V[5]).Normalized()*CUBE_DIMENSIONS[1];
06833                                     grad[5]        = G[6] - interpolationValPlus*(G[6] - G[5]).Normalized()*CUBE_DIMENSIONS[1];
06834                                     texCoordList[5] = VTC[6] - interpolationValPlus*(VTC[6] - VTC[5]).Normalized()*CUBE_DIMENSIONS[1];
06835                                 }
06836                                 grad[5].Normalized();
06837                             }
06838                             if ( EdgeTable[cubeClass] & 64 ) {
06839                                 if ( dataForPosition[idx[6] + 3] > threshold ) {
06840                                     //
06841                                     // Interpotation Data
06842                                     //interpolationValPlus = VIntp[6].GetX();
06843                                     //
06844                                     vertexList[ 6] = V[6] + interpolationValPlus*(V[7] - V[6]).Normalized()*CUBE_DIMENSIONS[0];
06845                                     grad[6]        = G[6] + interpolationValPlus*(G[7] - G[6]).Normalized()*CUBE_DIMENSIONS[0];
06846                                     texCoordList[6] = VTC[6] + interpolationValPlus*(VTC[7] - VTC[6]).Normalized()*CUBE_DIMENSIONS[0];
06847                                 }
06848                                 else {
06849                                     //
06850                                     // Interpotation Data
06851                                     //interpolationValNeg = 1.0 - VIntp[7].GetX();
06852                                     //
06853                                     vertexList[ 6] = V[7] - interpolationValPlus*(V[7] - V[6]).Normalized()*CUBE_DIMENSIONS[0];
06854                                     grad[6]        = G[7] - interpolationValPlus*(G[7] - G[6]).Normalized()*CUBE_DIMENSIONS[0];
06855                                     texCoordList[6] = VTC[7] - interpolationValPlus*(VTC[7] - VTC[6]).Normalized()*CUBE_DIMENSIONS[0];
06856                                 }
06857                                 grad[6].Normalized();
06858                             }
06859                             if ( EdgeTable[cubeClass] & 128 ) {
06860                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
06861                                     //
06862                                     // Interpotation Data
06863                                     //interpolationValPlus = VIntp[4].GetY();
06864                                     //
06865                                     vertexList[ 7] = V[4] + interpolationValPlus*(V[7] - V[4]).Normalized()*CUBE_DIMENSIONS[1];
06866                                     grad[7]        = G[4] + interpolationValPlus*(G[7] - G[4]).Normalized()*CUBE_DIMENSIONS[1];
06867                                     texCoordList[7] = VTC[4] + interpolationValPlus*(VTC[7] - VTC[4]).Normalized()*CUBE_DIMENSIONS[1];
06868                                 }
06869                                 else {
06870                                     //
06871                                     // Interpotation Data
06872                                     //interpolationValNeg = 1.0 - VIntp[7].GetY();
06873                                     //
06874                                     vertexList[ 7] = V[7] - interpolationValPlus*(V[7] - V[4]).Normalized()*CUBE_DIMENSIONS[1];
06875                                     grad[7]        = G[7] - interpolationValPlus*(G[7] - G[4]).Normalized()*CUBE_DIMENSIONS[1];
06876                                     texCoordList[7] = VTC[7] - interpolationValPlus*(VTC[7] - VTC[4]).Normalized()*CUBE_DIMENSIONS[1];
06877                                 }
06878                                 grad[7].Normalized();
06879                             }
06880                             if ( EdgeTable[cubeClass] & 256 ) {
06881                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06882                                     //
06883                                     // Interpotation Data
06884                                     //interpolationValPlus = VIntp[0].GetZ();
06885                                     //
06886                                     vertexList[ 8] = V[0] + interpolationValPlus*(V[4] - V[0]).Normalized()*CUBE_DIMENSIONS[2];
06887                                     grad[8]        = G[0] + interpolationValPlus*(G[4] - G[0]).Normalized()*CUBE_DIMENSIONS[2];
06888                                     texCoordList[8] = VTC[0] + interpolationValPlus*(VTC[4] - VTC[0]).Normalized()*CUBE_DIMENSIONS[2];
06889                                 }
06890                                 else {
06891                                     //
06892                                     // Interpotation Data
06893                                     //interpolationValNeg = 1.0 - VIntp[4].GetZ();
06894                                     //
06895                                     vertexList[ 8] = V[4] - interpolationValPlus*(V[4] - V[0]).Normalized()*CUBE_DIMENSIONS[2];
06896                                     grad[8]        = G[4] - interpolationValPlus*(G[4] - G[0]).Normalized()*CUBE_DIMENSIONS[2];
06897                                     texCoordList[8] = VTC[4] - interpolationValPlus*(VTC[4] - VTC[0]).Normalized()*CUBE_DIMENSIONS[2];
06898                                 }
06899                                 grad[8].Normalized();
06900                             }
06901                             if ( EdgeTable[cubeClass] & 512 ) {
06902                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
06903                                     //
06904                                     // Interpotation Data
06905                                     //interpolationValPlus = VIntp[1].GetZ();
06906                                     //
06907                                     vertexList[ 9] = V[1] + interpolationValPlus*(V[5] - V[1]).Normalized()*CUBE_DIMENSIONS[2];
06908                                     grad[9]        = G[1] + interpolationValPlus*(G[5] - G[1]).Normalized()*CUBE_DIMENSIONS[2];
06909                                     texCoordList[9] = VTC[1] + interpolationValPlus*(VTC[5] - VTC[1]).Normalized()*CUBE_DIMENSIONS[2];
06910                                 }
06911                                 else {
06912                                     //
06913                                     // Interpotation Data
06914                                     //interpolationValNeg = 1.0 - VIntp[5].GetZ();
06915                                     //
06916                                     vertexList[ 9] = V[5] - interpolationValPlus*(V[5] - V[1]).Normalized()*CUBE_DIMENSIONS[2];
06917                                     grad[9]        = G[5] - interpolationValPlus*(G[5] - G[1]).Normalized()*CUBE_DIMENSIONS[2];
06918                                     texCoordList[9] = VTC[5] - interpolationValPlus*(VTC[5] - VTC[1]).Normalized()*CUBE_DIMENSIONS[2];
06919                                 }
06920                                 grad[9].Normalized();
06921                             }
06922                             if ( EdgeTable[cubeClass] & 1024 ) {
06923                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
06924                                     //
06925                                     // Interpotation Data
06926                                     //interpolationValPlus = VIntp[2].GetZ();
06927                                     //
06928                                     vertexList[10] = V[2] + interpolationValPlus*(V[6] - V[2]).Normalized()*CUBE_DIMENSIONS[2];
06929                                     grad[10]       = G[2] + interpolationValPlus*(G[6] - G[2]).Normalized()*CUBE_DIMENSIONS[2];
06930                                     texCoordList[10] = VTC[2] + interpolationValPlus*(VTC[6] - VTC[2]).Normalized()*CUBE_DIMENSIONS[2];
06931                                 }
06932                                 else {
06933                                     //
06934                                     // Interpotation Data
06935                                     //interpolationValNeg = 1.0 - VIntp[6].GetZ();
06936                                     //
06937                                     vertexList[10] = V[6] - interpolationValPlus*(V[6] - V[2]).Normalized()*CUBE_DIMENSIONS[2];
06938                                     grad[10]       = G[6] - interpolationValPlus*(G[6] - G[2]).Normalized()*CUBE_DIMENSIONS[2];
06939                                     texCoordList[10] = VTC[6] - interpolationValPlus*(VTC[6] - VTC[2]).Normalized()*CUBE_DIMENSIONS[2];
06940                                 }
06941                                 grad[10].Normalized();
06942                             }
06943                             if ( EdgeTable[cubeClass] & 2048 ) {
06944                                 if ( dataForPosition[idx[3] + 3] > threshold ) {
06945                                     //
06946                                     // Interpotation Data
06947                                     //interpolationValPlus = VIntp[3].GetZ();
06948                                     //
06949                                     vertexList[11] = V[3] + interpolationValPlus*(V[7] - V[3]).Normalized()*CUBE_DIMENSIONS[2];
06950                                     grad[11]       = G[3] + interpolationValPlus*(G[7] - G[3]).Normalized()*CUBE_DIMENSIONS[2];
06951                                     texCoordList[11] = VTC[3] + interpolationValPlus*(VTC[7] - VTC[3]).Normalized()*CUBE_DIMENSIONS[2];
06952                                 }
06953                                 else {
06954                                     //
06955                                     // Interpotation Data
06956                                     //interpolationValNeg = 1.0 - VIntp[7].GetZ();
06957                                     //
06958                                     vertexList[11] = V[7] - interpolationValPlus*(V[7] - V[3]).Normalized()*CUBE_DIMENSIONS[2];
06959                                     grad[11]       = G[7] - interpolationValPlus*(G[7] - G[3]).Normalized()*CUBE_DIMENSIONS[2];
06960                                     texCoordList[11] = VTC[7] - interpolationValPlus*(VTC[7] - VTC[3]).Normalized()*CUBE_DIMENSIONS[2];
06961                                 }
06962                                 grad[11].Normalized();
06963                             }
06964                             //---------------------------------------
06965                             //=======================================
06966                             //***************************************
06967                             //*/
06968 
06969 
06970 
06971                         } // END: Use Global Interpolation Value
06972                         //-----------------------------------------------
06973                         // END: if/else statement for MC Interpolation
06974                         //===============================================
06975 
06976                         //===============================================
06977                         //-----------------------------------------------
06978                         // Generate Triangle Index
06979                         numOfTrisPerCell = 0;
06980                         for ( d = 0; TriTable[cubeClass][d] != -1; ++d ) {
06981                             triIndices[d] = TriTable[cubeClass][d];
06982                         }
06983                         numOfTrisPerCell = d / 3;
06984                         //-----------------------------------------------
06985                         //===============================================
06986                     }
06987 
06988                     /*
06989                     //-------------------------------
06990                     // Draw (Gradient) Normals
06991                     {
06992                         glPushAttrib( GL_ALL_ATTRIB_BITS );
06993                         glBegin( GL_LINES );
06994                         glDisable( GL_TEXTURE_3D );
06995                         glDisable( GL_BLEND );
06996                         glDisable( GL_LIGHTING );
06997                         for ( int i = 0; i < numOfTrisPerCell*3; i+=3 ) {
06998                             glColor3f( 1, 0, 0 );
06999                             glVertex3fv( vertexList[triIndices[i+2]].GetDataFloat() );
07000                             glVertex3fv( ( vertexList[triIndices[i+2]] + grad[triIndices[i+2]]*0.15 ).GetDataFloat() );
07001                             glColor3f( 0, 1, 0 );
07002                             glVertex3fv( vertexList[triIndices[i+1]].GetDataFloat() );
07003                             glVertex3fv( ( vertexList[triIndices[i+1]] + grad[triIndices[i+1]]*0.15 ).GetDataFloat() );
07004                             glColor3f( 0, 0, 1 );
07005                             glVertex3fv( vertexList[triIndices[i+0]].GetDataFloat() );
07006                             glVertex3fv( ( vertexList[triIndices[i+0]] + grad[triIndices[i+0]]*0.15 ).GetDataFloat() );
07007                         }
07008                         glEnd();
07009                         glPopAttrib();
07010                     }
07011                     //-------------------------------
07012                     //*/
07013 
07014                     //-------------------------------
07015                     // Draw PN-Triangles generated 
07016                     // from Marching Cube Triangles
07017                     if ( m_bDrawPNTriangle ) {
07018                         int i = 0;
07019                         while ( numOfTrisPerCell > 0 ) {
07020                             PNTriangle<T>::DrawByOpenGL( 
07021                                 vertexList[triIndices[i+2]], 
07022                                 vertexList[triIndices[i+1]], 
07023                                 vertexList[triIndices[i+0]], 
07024                                 grad[triIndices[i+2]], 
07025                                 grad[triIndices[i+1]], 
07026                                 grad[triIndices[i+0]], 
07027                                 texCoordList[triIndices[i+2]], 
07028                                 texCoordList[triIndices[i+1]], 
07029                                 texCoordList[triIndices[i+0]], 
07030                                 m_PNTriangleSmoothness 
07031                             );
07032                             --numOfTrisPerCell;
07033                             i += 3;
07034                         }
07035                     }
07036                     // DEBUG for PNTri Drawing
07037                     //glUseProgramObjectARB( g_PNTriProg );
07038                     //
07039                     // DEBUG for PNTri Drawing
07040                     //glUseProgramObjectARB( NULL );
07041 
07042                     //-------------------------------
07043                     // Draw Marching Cube Triangles
07044                     else {
07045 
07046                         /*
07047                         //*************************************
07048                         // DEBUG
07049                         {
07050                             glPushAttrib( GL_ALL_ATTRIB_BITS );
07051                             glDisable( GL_LIGHTING );
07052                             glDisable( GL_BLEND );
07053                             glDisable( GL_TEXTURE_3D );
07054                             glColor3ub( 200, 50, 100 );
07055                             //glColor3f( 0.0, 0.0, 1.0 );
07056                             glBegin( GL_POINTS );
07057                             int count = numOfTrisPerCell;
07058                             int i = 0;
07059                             while ( count > 0 ) {
07060                                 //glTexCoord3fv( texCoordList[triIndices[i+2]].GetDataFloat() );
07061                                 glColor3f( 1, 0, 0 );
07062                                 glNormal3fv( grad[triIndices[i+2]].GetDataFloat() );
07063                                 glVertex3fv( vertexList[triIndices[i+2]].GetDataFloat() );
07064                                 //
07065                                 //glTexCoord3fv( texCoordList[triIndices[i+1]].GetDataFloat() );
07066                                 glColor3f( 0, 1, 0 );
07067                                 glNormal3fv( grad[triIndices[i+1]].GetDataFloat() );
07068                                 glVertex3fv( vertexList[triIndices[i+1]].GetDataFloat() );
07069                                 //
07070                                 //glTexCoord3fv( texCoordList[triIndices[i+0]].GetDataFloat() );
07071                                 glColor3f( 0, 0, 1 );
07072                                 glNormal3fv( grad[triIndices[i+0]].GetDataFloat() );
07073                                 glVertex3fv( vertexList[triIndices[i+0]].GetDataFloat() );
07074                                 //
07075                                 --count;
07076                                 i += 3;
07077                             }
07078                             glEnd();
07079                             glPopAttrib();
07080                         }
07081                         //*************************************
07082                         //*/
07083 
07084                         //glBegin( GL_LINE_LOOP );
07085                         glBegin( GL_TRIANGLES );
07086                         int i = 0;
07087                         while ( numOfTrisPerCell > 0 ) {
07088                             glTexCoord3fv( texCoordList[triIndices[i+2]].GetDataFloat() );
07089                             glNormal3fv( grad[triIndices[i+2]].GetDataFloat() );
07090                             glVertex3fv( vertexList[triIndices[i+2]].GetDataFloat() );
07091                             //
07092                             glTexCoord3fv( texCoordList[triIndices[i+1]].GetDataFloat() );
07093                             glNormal3fv( grad[triIndices[i+1]].GetDataFloat() );
07094                             glVertex3fv( vertexList[triIndices[i+1]].GetDataFloat() );
07095                             //
07096                             glTexCoord3fv( texCoordList[triIndices[i+0]].GetDataFloat() );
07097                             glNormal3fv( grad[triIndices[i+0]].GetDataFloat() );
07098                             glVertex3fv( vertexList[triIndices[i+0]].GetDataFloat() );
07099                             //
07100                             --numOfTrisPerCell;
07101                             i += 3;
07102                         }
07103                         glEnd();
07104                     }
07105 
07106                     //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
07108                     //if ( isFirstRun ) {
07109                     //  if ( cubeClass < 100 )  std::cout << " ";
07110                     //  if ( cubeClass < 10 )   std::cout << " ";
07111                     //  std::cout << "  " << static_cast<int>( cubeClass );
07112                     //}
07113                     //#endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
07114 
07115                     // Restore Reloaded Threshold Values
07116                     for ( int R = 0; R < 8; ++R ) {
07117                         dataForPosition[idx[R] + 3] = reloadedThresholdValues[R];
07118                     }
07119 
07120                 } // END: // TWO ITERATIONS FOR LEFT AND RIGHT CUBE CONFIGURATIONS
07121 
07122                 //-------------------------------
07123                 // Next Cell
07124                 for ( i = 0; i < 8; ++i ) {
07125                     idx[i] += 4;
07126                     //
07127                     intpIdx[i] += 3;
07128                 }
07129                 //-------------------------------
07130 
07131             } // END: for loop for X
07132 
07133             //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
07135             //if ( isFirstRun ) std::cout << "\n";
07136             //#endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
07137 
07138         } // END: for loop for Y
07139 
07140         //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
07142         //if ( isFirstRun ) std::cout << "\n";
07143         //#endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
07144 
07145     } // END: for loop for Z
07146 
07147     //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
07149     //if ( isFirstRun ) {
07150     //  std::cout << "-------------------------\n";
07151     //}
07153     //#endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
07154 
07155     glPopAttrib();
07156 
07157     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
07158     m_glslProgramObjectForShaderSurfaceFromSW->EndGLSL();
07159     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
07160     //---------------------------------------------------------------
07161     //===============================================================
07162     //*/
07163 
07164     //---------------------------------------------------------------
07165     //===============================================================
07166     delete [] dataForPosition;
07167     delete [] dataForEllipsoidInfluence;
07168 }
07169 //-----------------------------------------------------------------------------
07170 // END: DrawByGL
07171 //*****************************************************************************
07172 
07173 
07174 //-----------------------------------------------------------------------------
07175 //=============================================================================
07176 END_NAMESPACE_TAPs__OpenGL
07177 //-----------------------------------------------------------------------------
07178 //34567890123456789012345678901234567890123456789012345678901234567890123456789
07179 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines