TAPs 0.7.7.3
TAPsModelDeformableGLSL_Visualization_RTGenMesh_EXPERIMENT.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                 //===============================================
04795                 // Calculate Gradients
04796                 //-----------------------------------------------
04797                 Vector3<T> G[8];
04798                 int index;
04799                 for ( int vID = 0; vID < 8; ++vID ) {
04800                     Center.SetXYZ(  dataForPosition[idx[vID]  ], 
04801                                     dataForPosition[idx[vID]+1], 
04802                                     dataForPosition[idx[vID]+2] );
04803                     G[vID].SetXYZ( 0, 0, 0 );
04804 
04805                 //*
04806                 //=================================
04807                 // START: 6 Face Gradients
04808                 //---------------------------------
04809                 if ( GetGradientComputationByFacesStatus() ) {
04810                 //--- East --------------
04811                 if ( X < gridResolution[0]-2 ) {
04812                     index = idx[vID] + iOffsetCol;
04813                     if ( dataForPosition[index+3] > threshold ) {
04814                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04815                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04816                         G[vID] += Center - vGrad;
04817                     }
04818                 }
04819                 //--- West --------------
04820                 if ( X > 0 ) {
04821                     index = idx[vID] - iOffsetCol;
04822                     if ( dataForPosition[index+3] > threshold ) {
04823                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04824                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04825                         G[vID] += Center - vGrad;
04826                     }
04827                 }
04828                 //--- North -------------
04829                 if ( Y < gridResolution[1]-2 ) {
04830                     index = idx[vID] + iOffsetRow;
04831                     if ( dataForPosition[index+3] > threshold ) {
04832                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04833                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04834                         G[vID] += Center - vGrad;
04835                     }
04836                 }
04837                 //--- South -------------
04838                 if ( Y > 0 ) {
04839                     index = idx[vID] - iOffsetRow;
04840                     if ( dataForPosition[index+3] > threshold ) {
04841                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04842                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04843                         G[vID] += Center - vGrad;
04844                     }
04845                 }
04846                 //--- Front -------------
04847                 if ( Z < gridResolution[2]-2 ) {
04848                     index = idx[vID] + iOffsetSlice;
04849                     if ( dataForPosition[index+3] > threshold ) {
04850                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04851                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04852                         G[vID] += Center - vGrad;
04853                     }
04854                 }
04855                 //--- Back --------------
04856                 if ( Z > 0 ) {
04857                     index = idx[vID] - iOffsetSlice;
04858                     if ( dataForPosition[index+3] > threshold ) {
04859                         int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04860                         Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04861                         G[vID] += Center - vGrad;
04862                     }
04863                 }
04864                 //-----------------------
04865                 } // END: if ( GetGradientComputationByFacesStatus() ) {
04866                 //---------------------------------
04867                 // END: 6 Face Gradients
04868                 //=================================
04869                 //*/
04870 
04871                 //*
04872                 //=================================
04873                 // START: 8 Vertex Gradients
04874                 //---------------------------------
04875                 if ( GetGradientComputationByVerticesStatus() ) {
04876                 //--- Front -------------
04877                 if ( Z < gridResolution[2]-2 ) {
04878                     int idxF = idx[vID] + iOffsetSlice;
04879                     //-- North -----
04880                     if ( Y < gridResolution[1]-2 ) {
04881                         int idxFN = idxF + iOffsetRow;
04882                         //- West
04883                         if ( X > 0 ) {
04884                             index = idxFN - iOffsetCol;
04885                             if ( dataForPosition[index+3] > threshold ) {
04886                                 int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04887                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04888                                 G[vID] += Center - vGrad;
04889                             }
04890                         }
04891                         //- East
04892                         if ( X < gridResolution[0]-2 ) {
04893                             index = idxFN + iOffsetCol;
04894                             if ( dataForPosition[index+3] > threshold ) {
04895                                 int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04896                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04897                                 G[vID] += Center - vGrad;
04898                             }
04899                         }
04900                     }
04901                     //-- South -----
04902                     if ( Y > 0 ) {
04903                         int idxFS = idxF - iOffsetRow;
04904                         //- West
04905                         if ( X > 0 ) {
04906                             index = idxFS - iOffsetCol;
04907                             if ( dataForPosition[index+3] > threshold ) {
04908                                 int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04909                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04910                                 G[vID] += Center - vGrad;
04911                             }
04912                         }
04913                         //- East
04914                         if ( X < gridResolution[0]-2 ) {
04915                             index = idxFS + iOffsetCol;
04916                             if ( dataForPosition[index+3] > threshold ) {
04917                                 int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04918                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04919                                 G[vID] += Center - vGrad;
04920                             }
04921                         }
04922                     }
04923                 }
04924                 //--- Back --------------
04925                 if ( Z > 0 ) {
04926                     int idxF = idx[vID] - iOffsetSlice;
04927                     //-- North -----
04928                     if ( Y < gridResolution[1]-2 ) {
04929                         int idxFN = idxF + iOffsetRow;
04930                         //- West
04931                         if ( X > 0 ) {
04932                             index = idxFN - iOffsetCol;
04933                             if ( dataForPosition[index+3] > threshold ) {
04934                                 int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04935                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04936                                 G[vID] += Center - vGrad;
04937                             }
04938                         }
04939                         //- East
04940                         if ( X < gridResolution[0]-2 ) {
04941                             index = idxFN + iOffsetCol;
04942                             if ( dataForPosition[index+3] > threshold ) {
04943                                 int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04944                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04945                                 G[vID] += Center - vGrad;
04946                             }
04947                         }
04948                     }
04949                     //-- South -----
04950                     if ( Y > 0 ) {
04951                         int idxFS = idxF - iOffsetRow;
04952                         //- West
04953                         if ( X > 0 ) {
04954                             index = idxFS - iOffsetCol;
04955                             if ( dataForPosition[index+3] > threshold ) {
04956                                 int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04957                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04958                                 G[vID] += Center - vGrad;
04959                             }
04960                         }
04961                         //- East
04962                         if ( X < gridResolution[0]-2 ) {
04963                             index = idxFS + iOffsetCol;
04964                             if ( dataForPosition[index+3] > threshold ) {
04965                                 int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04966                                 Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04967                                 G[vID] += Center - vGrad;
04968                             }
04969                         }
04970                     }
04971                 }
04972                 //-----------------------
04973                 } // END: if ( GetGradientComputationByVerticesStatus() ) {
04974                 //---------------------------------
04975                 // END: 8 Vertex Gradients
04976                 //=================================
04977                 //*/
04978 
04979                 //*
04980                 //=================================
04981                 // START: 12 Edge Gradients
04982                 //---------------------------------
04983                 if ( GetGradientComputationByEdgesStatus() ) {
04984                 //--- Front -------------
04985                 if ( Z < gridResolution[2]-2 ) {
04986                     int idxF = idx[vID] + iOffsetSlice;
04987                     //-- East ------
04988                     if ( X < gridResolution[0]-2 ) {
04989                         index = idxF + iOffsetCol;
04990                         if ( dataForPosition[index+3] > threshold ) {
04991                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
04992                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
04993                             G[vID] += Center - vGrad;
04994                         }
04995                     }
04996                     //-- West ------
04997                     if ( X > 0 ) {
04998                         index = idxF - iOffsetCol;
04999                         if ( dataForPosition[index+3] > threshold ) {
05000                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05001                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05002                             G[vID] += Center - vGrad;
05003                         }
05004                     }
05005                     //-- North -----
05006                     if ( Y < gridResolution[1]-2 ) {
05007                         index = idxF + iOffsetRow;
05008                         if ( dataForPosition[index+3] > threshold ) {
05009                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05010                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05011                             G[vID] += Center - vGrad;
05012                         }
05013                     }
05014                     //-- South -----
05015                     if ( Y > 0 ) {
05016                         index = idxF - iOffsetRow;
05017                         if ( dataForPosition[index+3] > threshold ) {
05018                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05019                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05020                             G[vID] += Center - vGrad;
05021                         }
05022                     }
05023                 }
05024                 //--- Center ------------
05025                 if ( true ) {
05026                     int idxC = idx[vID];
05027                     //-- East ------
05028                     if ( X < gridResolution[0]-2 ) {
05029                         index = idxC + iOffsetCol;
05030                         if ( dataForPosition[index+3] > threshold ) {
05031                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05032                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05033                             G[vID] += Center - vGrad;
05034                         }
05035                     }
05036                     //-- West ------
05037                     if ( X > 0 ) {
05038                         index = idxC - iOffsetCol;
05039                         if ( dataForPosition[index+3] > threshold ) {
05040                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05041                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05042                             G[vID] += Center - vGrad;
05043                         }
05044                     }
05045                     //-- North -----
05046                     if ( Y < gridResolution[1]-2 ) {
05047                         index = idxC + iOffsetRow;
05048                         if ( dataForPosition[index+3] > threshold ) {
05049                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05050                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05051                             G[vID] += Center - vGrad;
05052                         }
05053                     }
05054                     //-- South -----
05055                     if ( Y > 0 ) {
05056                         index = idxC - iOffsetRow;
05057                         if ( dataForPosition[index+3] > threshold ) {
05058                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05059                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05060                             G[vID] += Center - vGrad;
05061                         }
05062                     }
05063                 }
05064                 //--- Back --------------
05065                 if ( Z > 0 ) {
05066                     int idxB = idx[vID] - iOffsetSlice;
05067                     //-- East ------
05068                     if ( X < gridResolution[0]-2 ) {
05069                         index = idxB + iOffsetCol;
05070                         if ( dataForPosition[index+3] > threshold ) {
05071                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05072                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05073                             G[vID] += Center - vGrad;
05074                         }
05075                     }
05076                     //-- West ------
05077                     if ( X > 0 ) {
05078                         index = idxB - iOffsetCol;
05079                         if ( dataForPosition[index+3] > threshold ) {
05080                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05081                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05082                             G[vID] += Center - vGrad;
05083                         }
05084                     }
05085                     //-- North -----
05086                     if ( Y < gridResolution[1]-2 ) {
05087                         index = idxB + iOffsetRow;
05088                         if ( dataForPosition[index+3] > threshold ) {
05089                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05090                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05091                             G[vID] += Center - vGrad;
05092                         }
05093                     }
05094                     //-- South -----
05095                     if ( Y > 0 ) {
05096                         index = idxB - iOffsetRow;
05097                         if ( dataForPosition[index+3] > threshold ) {
05098                             int iFlag = static_cast<int>( dataForPosition[index+3] ) % m_SimDomain->IC_Thresholds::FLAG_STEP;
05099                             Vector3<T> vGrad( dataForPosition[index], dataForPosition[index+1], dataForPosition[index+2] );
05100                             G[vID] += Center - vGrad;
05101                         }
05102                     }
05103                 }
05104                 //-----------------------
05105                 } // END: if ( GetGradientComputationByEdgesStatus() ) {
05106                 //---------------------------------
05107                 // END: 12 Edge Gradients
05108                 //=================================
05109                 //*/
05110             }
05111             //-----------------------------------------------
05112             // END: Calculate Gradients
05113             //===============================================
05114                 
05115                 // TWO ITERATIONS FOR LEFT AND RIGHT CUBE CONFIGURATIONS
05116                 for ( int iIterate = 0; iIterate < 2; ++iIterate )
05117                 {
05118                     // Store Reloaded Threshold Values
05119                     GLfloat reloadedThresholdValues[8];
05120                     for ( int R = 0; R < 8; ++R ) {
05121                         reloadedThresholdValues[R] = dataForPosition[idx[R] + 3];
05122                     }
05123 
05124                     // STANDARD CUBE CONFIGURATION
05125                     //-------------------------------
05126                     if ( !bLeftPresent || !bRightPresent ) {
05127                         iIterate = 2;
05128                     }
05129                     else {
05130 
05131                         //-------------------------------
05132                         if ( iIterate == 0 )
05133                         // LEFT CUBE CONFIGURATION
05134                         //-------------------------------
05135                         // Determine the threshold of each vertex of the cube
05136                         {
05137                             float flagVal;
05138                             int factor = 1;
05139                             cubeClass = 0;
05140                             for ( int i = 0; i < 8; ++i ) {
05141                                 flagVal = dataForPosition[idx[i] + 3];
05142                                 if ( flagVal > threshold ) {
05143                                     int iFlag = int( flagVal ) % int( m_SimDomain->IC_Thresholds::FLAG_STEP );
05144                                     if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD+1 ) {
05145                                         cubeClass |= factor;
05146                                     }
05147                                     else {
05148                                         dataForPosition[idx[i] + 3] = m_SimDomain->IC_Thresholds::RIGHT_OUTSIDE_BOUNDARY;
05149                                     }
05150                                 }
05151                                 factor *= 2;
05152                             }
05153                         }
05154 
05155                         //-------------------------------
05156                         else
05157                         // RIGHT CUBE CONFIGURATION
05158                         //-------------------------------
05159                         // Determine the threshold of each vertex of the cube
05160                         {
05161                             float flagVal;
05162                             int factor = 1;
05163                             cubeClass = 0;
05164                             for ( int i = 0; i < 8; ++i ) {
05165                                 flagVal = dataForPosition[idx[i] + 3];
05166                                 if ( flagVal > threshold ) {
05167                                     int iFlag = int( flagVal ) % int( m_SimDomain->IC_Thresholds::FLAG_STEP );
05168                                     if ( iFlag < m_SimDomain->IC_Thresholds::MC_LEFT_ADD-1 
05169                                     || iFlag > m_SimDomain->IC_Thresholds::MC_LEFT_ADD+1 ) {
05170                                         cubeClass |= factor;
05171                                     }
05172                                     else {
05173                                         dataForPosition[idx[i] + 3] = m_SimDomain->IC_Thresholds::RIGHT_OUTSIDE_BOUNDARY;
05174                                     }
05175                                 }
05176                                 factor *= 2;
05177                             }
05178                         }
05179                         //-------------------------------
05180                     }
05181 
05182 
05183                     //-------------------------------
05184                     // Cell is entirely in/out of the surface
05185                     if ( EdgeTable[cubeClass] == 0 ) {
05186                         numOfTrisPerCell = 0;
05187                     }
05188                     else {
05189                     //-------------------------------
05190                     // Cell contains part of the surface
05191                         //-----------------
05192                         for ( i = 0; i < 8; ++i ) {
05193                             V[i].SetXYZ( 
05194                                 dataForPosition[idx[i]    ], 
05195                                 dataForPosition[idx[i] + 1], 
05196                                 dataForPosition[idx[i] + 2] 
05197                             );
05198                         }
05199                         //-----------------
05200                         // Reset list of existing vertices
05201                         for ( i = 0; i < 12; ++i ) {
05202                             bV[i] = -1;
05203                         }
05204 
05205                         //-----------------
05206                         // Interpolation Data
05207                         for ( i = 0; i < 8; ++i ) {
05208                             VIntp[i].SetXYZ( 
05209                                 dataForEllipsoidInfluence[intpIdx[i]    ], 
05210                                 dataForEllipsoidInfluence[intpIdx[i] + 1], 
05211                                 dataForEllipsoidInfluence[intpIdx[i] + 2] 
05212                             );
05213                         }
05214 
05215 
05216 
05217                         //===============================================
05218                         // Marching Cube Interpolation
05219                         // V = V1 + (isovalue - s1) (V2 - V1) / (s2 - s1)
05220                         //-----------------------------------------------
05221                         // RUNTIME ERROR CAN OCCURS -- DEPENDING ON THE DATA
05222                         // SINCE IT DOES NOT HAVE BOUNDARY PROTECTION -- ARRAY INDEX OUT OF BOUND ERROR
05223                         //-----------------------------------------------
05224                         // Not Use Global Interpolation Value -- each element has its own xyz interpolation values
05225                         if ( !m_bUseGlobalInterpolationVal ) {
05226                             //-------------------------------------
05227                             // Marching Cube Interpolation
05228                             T interpolationValPlus;// = m_tGlobalInterpolationVal;
05229                             T interpolationValNeg;//  = 1.0 - m_tGlobalInterpolationVal;
05230 
05231 
05232 
05233                             /*
05234                             //***************************************
05235                             // Normal Interpolation
05236                             //=======================================
05237                             //-------------------------------------
05238                             // Edge# 0 to 11 <==> [ 1, 2, 4, ... , 2048 ]
05239                             if ( EdgeTable[cubeClass] & 1 ) {
05240                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
05241                                     //
05242                                     // Interpotation Data
05243                                     interpolationValPlus = VIntp[0].GetX();
05244                                     //
05245                                     vertexList[ 0] = V[0] + interpolationValPlus*(V[1] - V[0]);
05246                                     grad[0]        = G[0] + interpolationValPlus*(G[1] - G[0]);
05247                                     texCoordList[0] = VTC[0] + interpolationValPlus*(VTC[1] - VTC[0]);
05248                                 }
05249                                 else {
05250                                     //
05251                                     // Interpotation Data
05252                                     interpolationValNeg = 1.0 - VIntp[1].GetX();
05253                                     //
05254                                     vertexList[ 0] = V[0] + interpolationValNeg*(V[1] - V[0]);
05255                                     grad[0]        = G[0] + interpolationValNeg*(G[1] - G[0]);
05256                                     texCoordList[0] = VTC[0] + interpolationValNeg*(VTC[1] - VTC[0]);
05257                                     //grad[0] *= -1.0;
05258                                 }
05259                                 grad[0].Normalized();
05260                             }
05261                             if ( EdgeTable[cubeClass] & 2 ) {
05262                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
05263                                     //
05264                                     // Interpotation Data
05265                                     interpolationValPlus = VIntp[1].GetY();
05266                                     //
05267                                     vertexList[ 1] = V[1] + interpolationValPlus*(V[2] - V[1]);
05268                                     grad[1]        = G[1] + interpolationValPlus*(G[2] - G[1]);
05269                                     texCoordList[1] = VTC[1] + interpolationValPlus*(VTC[2] - VTC[1]);
05270                                 }
05271                                 else {
05272                                     //
05273                                     // Interpotation Data
05274                                     interpolationValNeg = 1.0 - VIntp[2].GetY();
05275                                     //
05276                                     vertexList[ 1] = V[1] + interpolationValNeg*(V[2] - V[1]);
05277                                     grad[1]        = G[1] + interpolationValNeg*(G[2] - G[1]);
05278                                     texCoordList[1] = VTC[1] + interpolationValNeg*(VTC[2] - VTC[1]);
05279                                     //grad[1] *= -1.0;
05280                                 }
05281                                 grad[1].Normalized();
05282                             }
05283                             if ( EdgeTable[cubeClass] & 4 ) {
05284                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
05285                                     //
05286                                     // Interpotation Data
05287                                     interpolationValPlus = VIntp[2].GetX();
05288                                     //
05289                                     vertexList[ 2] = V[2] + interpolationValPlus*(V[3] - V[2]);
05290                                     grad[2]        = G[2] + interpolationValPlus*(G[3] - G[2]);
05291                                     texCoordList[2] = VTC[2] + interpolationValPlus*(VTC[3] - VTC[2]);
05292                                     //grad[2] *= -1.0;
05293                                 }
05294                                 else {
05295                                     //
05296                                     // Interpotation Data
05297                                     interpolationValNeg = 1.0 - VIntp[3].GetX();
05298                                     //
05299                                     vertexList[ 2] = V[2] + interpolationValNeg*(V[3] - V[2]);
05300                                     grad[2]        = G[2] + interpolationValNeg*(G[3] - G[2]);
05301                                     texCoordList[2] = VTC[2] + interpolationValNeg*(VTC[3] - VTC[2]);
05302                                 }
05303                                 grad[2].Normalized();
05304                             }
05305                             if ( EdgeTable[cubeClass] & 8 ) {
05306                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
05307                                     //
05308                                     // Interpotation Data
05309                                     interpolationValPlus = VIntp[0].GetY();
05310                                     //
05311                                     vertexList[ 3] = V[0] + interpolationValPlus*(V[3] - V[0]);
05312                                     grad[3]        = G[0] + interpolationValPlus*(G[3] - G[0]);
05313                                     texCoordList[3] = VTC[0] + interpolationValPlus*(VTC[3] - VTC[0]);
05314                                     //grad[3][1] *= -1.0;
05315                                 }
05316                                 else {
05317                                     //
05318                                     // Interpotation Data
05319                                     interpolationValNeg = 1.0 - VIntp[3].GetY();
05320                                     //
05321                                     vertexList[ 3] = V[0] + interpolationValNeg*(V[3] - V[0]);
05322                                     grad[3]        = G[0] + interpolationValNeg*(G[3] - G[0]);
05323                                     texCoordList[3] = VTC[0] + interpolationValNeg*(VTC[3] - VTC[0]);
05324                                 }
05325                                 grad[3].Normalized();
05326                             }
05327                             if ( EdgeTable[cubeClass] & 16 ) {
05328                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
05329                                     //
05330                                     // Interpotation Data
05331                                     interpolationValPlus = VIntp[4].GetX();
05332                                     //
05333                                     vertexList[ 4] = V[4] + interpolationValPlus*(V[5] - V[4]);
05334                                     grad[4]        = G[4] + interpolationValPlus*(G[5] - G[4]);
05335                                     texCoordList[4] = VTC[4] + interpolationValPlus*(VTC[5] - VTC[4]);
05336                                 }
05337                                 else {
05338                                     //
05339                                     // Interpotation Data
05340                                     interpolationValNeg = 1.0 - VIntp[5].GetX();
05341                                     //
05342                                     vertexList[ 4] = V[4] + interpolationValNeg*(V[5] - V[4]);
05343                                     grad[4]        = G[4] + interpolationValNeg*(G[5] - G[4]);
05344                                     texCoordList[4] = VTC[4] + interpolationValNeg*(VTC[5] - VTC[4]);
05345                                     //grad[4] *= -1.0;
05346                                 }
05347                                 grad[4].Normalized();
05348                             }
05349                             if ( EdgeTable[cubeClass] & 32 ) {
05350                                 if ( dataForPosition[idx[5] + 3] > threshold ) {
05351                                     //
05352                                     // Interpotation Data
05353                                     interpolationValPlus = VIntp[5].GetY();
05354                                     //
05355                                     vertexList[ 5] = V[5] + interpolationValPlus*(V[6] - V[5]);
05356                                     grad[5]        = G[5] + interpolationValPlus*(G[6] - G[5]);
05357                                     texCoordList[5] = VTC[5] + interpolationValPlus*(VTC[6] - VTC[5]);
05358                                 }
05359                                 else {
05360                                     //
05361                                     // Interpotation Data
05362                                     interpolationValNeg = 1.0 - VIntp[6].GetY();
05363                                     //
05364                                     vertexList[ 5] = V[5] + interpolationValNeg*(V[6] - V[5]);
05365                                     grad[5]        = G[5] + interpolationValNeg*(G[6] - G[5]);
05366                                     texCoordList[5] = VTC[5] + interpolationValNeg*(VTC[6] - VTC[5]);
05367                                     //grad[5][1] *= -1.0;
05368                                 }
05369                                 grad[5].Normalized();
05370                             }
05371                             if ( EdgeTable[cubeClass] & 64 ) {
05372                                 if ( dataForPosition[idx[6] + 3] > threshold ) {
05373                                     //
05374                                     // Interpotation Data
05375                                     interpolationValPlus = VIntp[6].GetX();
05376                                     //
05377                                     vertexList[ 6] = V[6] + interpolationValPlus*(V[7] - V[6]);
05378                                     grad[6]        = G[6] + interpolationValPlus*(G[7] - G[6]);
05379                                     texCoordList[6] = VTC[6] + interpolationValPlus*(VTC[7] - VTC[6]);
05380                                 }
05381                                 else {
05382                                     //
05383                                     // Interpotation Data
05384                                     interpolationValNeg = 1.0 - VIntp[7].GetX();
05385                                     //
05386                                     vertexList[ 6] = V[6] + interpolationValNeg*(V[7] - V[6]);
05387                                     grad[6]        = G[6] + interpolationValNeg*(G[7] - G[6]);
05388                                     texCoordList[6] = VTC[6] + interpolationValNeg*(VTC[7] - VTC[6]);
05389                                     //grad[6] *= -1.0;
05390                                 }
05391                                 grad[6].Normalized();
05392                             }
05393                             if ( EdgeTable[cubeClass] & 128 ) {
05394                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
05395                                     //
05396                                     // Interpotation Data
05397                                     interpolationValPlus = VIntp[4].GetY();
05398                                     //
05399                                     vertexList[ 7] = V[4] + interpolationValPlus*(V[7] - V[4]);
05400                                     grad[7]        = G[4] + interpolationValPlus*(G[7] - G[4]);
05401                                     texCoordList[7] = VTC[4] + interpolationValPlus*(VTC[7] - VTC[4]);
05402                                     //grad[7][1] *= -1.0;
05403                                 }
05404                                 else {
05405                                     //
05406                                     // Interpotation Data
05407                                     interpolationValNeg = 1.0 - VIntp[7].GetY();
05408                                     //
05409                                     vertexList[ 7] = V[4] + interpolationValNeg*(V[7] - V[4]);
05410                                     grad[7]        = G[4] + interpolationValNeg*(G[7] - G[4]);
05411                                     texCoordList[7] = VTC[4] + interpolationValNeg*(VTC[7] - VTC[4]);
05412                                 }
05413                                 grad[7].Normalized();
05414                             }
05415                             if ( EdgeTable[cubeClass] & 256 ) {
05416                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
05417                                     //
05418                                     // Interpotation Data
05419                                     interpolationValPlus = VIntp[0].GetZ();
05420                                     //
05421                                     vertexList[ 8] = V[0] + interpolationValPlus*(V[4] - V[0]);
05422                                     grad[8]        = G[0] + interpolationValPlus*(G[4] - G[0]);
05423                                     texCoordList[8] = VTC[0] + interpolationValPlus*(VTC[4] - VTC[0]);
05424                                 }
05425                                 else {
05426                                     //
05427                                     // Interpotation Data
05428                                     interpolationValNeg = 1.0 - VIntp[4].GetZ();
05429                                     //
05430                                     vertexList[ 8] = V[0] + interpolationValNeg*(V[4] - V[0]);
05431                                     grad[8]        = G[0] + interpolationValNeg*(G[4] - G[0]);
05432                                     texCoordList[8] = VTC[0] + interpolationValNeg*(VTC[4] - VTC[0]);
05433                                     //grad[8][2] *= -1.0;
05434                                 }
05435                                 grad[8].Normalized();
05436                             }
05437                             if ( EdgeTable[cubeClass] & 512 ) {
05438                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
05439                                     //
05440                                     // Interpotation Data
05441                                     interpolationValPlus = VIntp[1].GetZ();
05442                                     //
05443                                     vertexList[ 9] = V[1] + interpolationValPlus*(V[5] - V[1]);
05444                                     grad[9]        = G[1] + interpolationValPlus*(G[5] - G[1]);
05445                                     texCoordList[9] = VTC[1] + interpolationValPlus*(VTC[5] - VTC[1]);
05446                                 }
05447                                 else {
05448                                     //
05449                                     // Interpotation Data
05450                                     interpolationValNeg = 1.0 - VIntp[5].GetZ();
05451                                     //
05452                                     vertexList[ 9] = V[1] + interpolationValNeg*(V[5] - V[1]);
05453                                     grad[9]        = G[1] + interpolationValNeg*(G[5] - G[1]);
05454                                     texCoordList[9] = VTC[1] + interpolationValNeg*(VTC[5] - VTC[1]);
05455                                     //grad[9][2] *= -1.0;
05456                                 }
05457                                 grad[9].Normalized();
05458                             }
05459                             if ( EdgeTable[cubeClass] & 1024 ) {
05460                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
05461                                     //
05462                                     // Interpotation Data
05463                                     interpolationValPlus = VIntp[2].GetZ();
05464                                     //
05465                                     vertexList[10] = V[2] + interpolationValPlus*(V[6] - V[2]);
05466                                     grad[10]       = G[2] + interpolationValPlus*(G[6] - G[2]);
05467                                     texCoordList[10] = VTC[2] + interpolationValPlus*(VTC[6] - VTC[2]);
05468                                 }
05469                                 else {
05470                                     //
05471                                     // Interpotation Data
05472                                     interpolationValNeg = 1.0 - VIntp[6].GetZ();
05473                                     //
05474                                     vertexList[10] = V[2] + interpolationValNeg*(V[6] - V[2]);
05475                                     grad[10]       = G[2] + interpolationValNeg*(G[6] - G[2]);
05476                                     texCoordList[10] = VTC[2] + interpolationValNeg*(VTC[6] - VTC[2]);
05477                                     //grad[10][2] *= -1.0;
05478                                 }
05479                                 grad[10].Normalized();
05480                             }
05481                             if ( EdgeTable[cubeClass] & 2048 ) {
05482                                 if ( dataForPosition[idx[3] + 3] > threshold ) {
05483                                     //
05484                                     // Interpotation Data
05485                                     interpolationValPlus = VIntp[3].GetZ();
05486                                     //
05487                                     vertexList[11] = V[3] + interpolationValPlus*(V[7] - V[3]);
05488                                     grad[11]       = G[3] + interpolationValPlus*(G[7] - G[3]);
05489                                     texCoordList[11] = VTC[3] + interpolationValPlus*(VTC[7] - VTC[3]);
05490                                 }
05491                                 else {
05492                                     //
05493                                     // Interpotation Data
05494                                     interpolationValNeg = 1.0 - VIntp[7].GetZ();
05495                                     //
05496                                     vertexList[11] = V[3] + interpolationValNeg*(V[7] - V[3]);
05497                                     grad[11]       = G[3] + interpolationValNeg*(G[7] - G[3]);
05498                                     texCoordList[11] = VTC[3] + interpolationValNeg*(VTC[7] - VTC[3]);
05499                                     //grad[11][2] *= -1.0;
05500                                 }
05501                                 grad[11].Normalized();
05502                             }
05503                             //---------------------------------------
05504                             //=======================================
05505                             //***************************************
05506                             //*/
05507 
05508 
05509 
05510 // START DEBUGGING
05512                             //*
05513                             //***************************************
05514                             // Fixed Distance Interpolation
05515                             //=======================================
05516                             //---------------------------------------
05517                             // Edge# 0 to 11 <==> [ 1, 2, 4, ... , 2048 ]
05518 //if ( iIterate >= 2 ) 
05519 {
05520                             if ( EdgeTable[cubeClass] & 1 ) {
05521                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
05522                                     //
05523                                     // Interpotation Data
05524                                     interpolationValPlus = VIntp[0].GetX();
05525                                     //
05526                                     vertexList[ 0] = V[0] + interpolationValPlus*(V[1] - V[0]);//.Normalized()*CUBE_DIMENSIONS[0];
05527                                     grad[0]        = G[0] + interpolationValPlus*(G[1] - G[0]);//.Normalized()*CUBE_DIMENSIONS[0];
05528                                     texCoordList[0] = VTC[0] + interpolationValPlus*(VTC[1] - VTC[0]);//.Normalized()*CUBE_DIMENSIONS[0];
05529                                 }
05530                                 else {
05531                                     //
05532                                     // Interpotation Data
05533                                     interpolationValNeg = -VIntp[1].GetX();
05534                                     //
05535                                     vertexList[ 0] = V[1] + interpolationValNeg*(V[1] - V[0]);//.Normalized()*CUBE_DIMENSIONS[0];
05536                                     grad[0]        = G[1] + interpolationValNeg*(G[1] - G[0]);//.Normalized()*CUBE_DIMENSIONS[0];
05537                                     texCoordList[0] = VTC[1] + interpolationValNeg*(VTC[1] - VTC[0]);//.Normalized()*CUBE_DIMENSIONS[0];
05538                                 }
05539                                 grad[0].Normalized();
05540                             }
05541                             if ( EdgeTable[cubeClass] & 2 ) {
05542                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
05543                                     //
05544                                     // Interpotation Data
05545                                     interpolationValPlus = VIntp[1].GetY();
05546                                     //
05547                                     vertexList[ 1] = V[1] + interpolationValPlus*(V[2] - V[1]);//.Normalized()*CUBE_DIMENSIONS[1];
05548                                     grad[1]        = G[1] + interpolationValPlus*(G[2] - G[1]);//.Normalized()*CUBE_DIMENSIONS[1];
05549                                     texCoordList[1] = VTC[1] + interpolationValPlus*(VTC[2] - VTC[1]);//.Normalized()*CUBE_DIMENSIONS[1];
05550                                 }
05551                                 else {
05552                                     //
05553                                     // Interpotation Data
05554                                     interpolationValNeg = -VIntp[2].GetY();
05555                                     //
05556                                     vertexList[ 1] = V[2] + interpolationValNeg*(V[2] - V[1]);//.Normalized()*CUBE_DIMENSIONS[1];
05557                                     grad[1]        = G[2] + interpolationValNeg*(G[2] - G[1]);//.Normalized()*CUBE_DIMENSIONS[1];
05558                                     texCoordList[1] = VTC[2] + interpolationValNeg*(VTC[2] - VTC[1]);//.Normalized()*CUBE_DIMENSIONS[1];
05559                                 }
05560                                 grad[1].Normalized();
05561                             }
05562                             if ( EdgeTable[cubeClass] & 4 ) {
05563                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
05564                                     //
05565                                     // Interpotation Data
05566                                     interpolationValPlus = VIntp[2].GetX();
05567                                     //
05568                                     vertexList[ 2] = V[2] + interpolationValPlus*(V[3] - V[2]);//.Normalized()*CUBE_DIMENSIONS[0];
05569                                     grad[2]        = G[2] + interpolationValPlus*(G[3] - G[2]);//.Normalized()*CUBE_DIMENSIONS[0];
05570                                     texCoordList[2] = VTC[2] + interpolationValPlus*(VTC[3] - VTC[2]);//.Normalized()*CUBE_DIMENSIONS[0];
05571                                 }
05572                                 else {
05573                                     //
05574                                     // Interpotation Data
05575                                     interpolationValNeg = -VIntp[3].GetX();
05576                                     //
05577                                     vertexList[ 2] = V[3] + interpolationValNeg*(V[3] - V[2]);//.Normalized()*CUBE_DIMENSIONS[0];
05578                                     grad[2]        = G[3] + interpolationValNeg*(G[3] - G[2]);//.Normalized()*CUBE_DIMENSIONS[0];
05579                                     texCoordList[2] = VTC[3] + interpolationValNeg*(VTC[3] - VTC[2]);//.Normalized()*CUBE_DIMENSIONS[0];
05580                                 }
05581                                 grad[2].Normalized();
05582                             }
05583                             if ( EdgeTable[cubeClass] & 8 ) {
05584                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
05585                                     //
05586                                     // Interpotation Data
05587                                     interpolationValPlus = VIntp[0].GetY();
05588                                     //
05589                                     vertexList[ 3] = V[0] + interpolationValPlus*(V[3] - V[0]);//.Normalized()*CUBE_DIMENSIONS[1];
05590                                     grad[3]        = G[0] + interpolationValPlus*(G[3] - G[0]);//.Normalized()*CUBE_DIMENSIONS[1];
05591                                     texCoordList[3] = VTC[0] + interpolationValPlus*(VTC[3] - VTC[0]);//.Normalized()*CUBE_DIMENSIONS[1];
05592                                 }
05593                                 else {
05594                                     //
05595                                     // Interpotation Data
05596                                     interpolationValNeg = -VIntp[3].GetY();
05597                                     //
05598                                     vertexList[ 3] = V[3] + interpolationValNeg*(V[3] - V[0]);//.Normalized()*CUBE_DIMENSIONS[1];
05599                                     grad[3]        = G[3] + interpolationValNeg*(G[3] - G[0]);//.Normalized()*CUBE_DIMENSIONS[1];
05600                                     texCoordList[3] = VTC[3] + interpolationValNeg*(VTC[3] - VTC[0]);//.Normalized()*CUBE_DIMENSIONS[1];
05601                                 }
05602                                 grad[3].Normalized();
05603                             }
05604                             if ( EdgeTable[cubeClass] & 16 ) {
05605                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
05606                                     //
05607                                     // Interpotation Data
05608                                     interpolationValPlus = VIntp[4].GetX();
05609                                     //
05610                                     vertexList[ 4] = V[4] + interpolationValPlus*(V[5] - V[4]);//.Normalized()*CUBE_DIMENSIONS[0];
05611                                     grad[4]        = G[4] + interpolationValPlus*(G[5] - G[4]);//.Normalized()*CUBE_DIMENSIONS[0];
05612                                     texCoordList[4] = VTC[4] + interpolationValPlus*(VTC[5] - VTC[4]);//.Normalized()*CUBE_DIMENSIONS[0];
05613                                 }
05614                                 else {
05615                                     //
05616                                     // Interpotation Data
05617                                     interpolationValNeg = -VIntp[5].GetX();
05618                                     //
05619                                     vertexList[ 4] = V[5] + interpolationValNeg*(V[5] - V[4]);//.Normalized()*CUBE_DIMENSIONS[0];
05620                                     grad[4]        = G[5] + interpolationValNeg*(G[5] - G[4]);//.Normalized()*CUBE_DIMENSIONS[0];
05621                                     texCoordList[4] = VTC[5] + interpolationValNeg*(VTC[5] - VTC[4]);//.Normalized()*CUBE_DIMENSIONS[0];
05622                                 }
05623                                 grad[4].Normalized();
05624                             }
05625                             if ( EdgeTable[cubeClass] & 32 ) {
05626                                 if ( dataForPosition[idx[5] + 3] > threshold ) {
05627                                     //
05628                                     // Interpotation Data
05629                                     interpolationValPlus = VIntp[5].GetY();
05630                                     //
05631                                     vertexList[ 5] = V[5] + interpolationValPlus*(V[6] - V[5]);//.Normalized()*CUBE_DIMENSIONS[1];
05632                                     grad[5]        = G[5] + interpolationValPlus*(G[6] - G[5]);//.Normalized()*CUBE_DIMENSIONS[1];
05633                                     texCoordList[5] = VTC[5] + interpolationValPlus*(VTC[6] - VTC[5]);//.Normalized()*CUBE_DIMENSIONS[1];
05634                                 }
05635                                 else {
05636                                     //
05637                                     // Interpotation Data
05638                                     interpolationValNeg = -VIntp[6].GetY();
05639                                     //
05640                                     vertexList[ 5] = V[6] + interpolationValNeg*(V[6] - V[5]);//.Normalized()*CUBE_DIMENSIONS[1];
05641                                     grad[5]        = G[6] + interpolationValNeg*(G[6] - G[5]);//.Normalized()*CUBE_DIMENSIONS[1];
05642                                     texCoordList[5] = VTC[6] + interpolationValNeg*(VTC[6] - VTC[5]);//.Normalized()*CUBE_DIMENSIONS[1];
05643                                 }
05644                                 grad[5].Normalized();
05645                             }
05646                             if ( EdgeTable[cubeClass] & 64 ) {
05647                                 if ( dataForPosition[idx[6] + 3] > threshold ) {
05648                                     //
05649                                     // Interpotation Data
05650                                     interpolationValPlus = VIntp[6].GetX();
05651                                     //
05652                                     vertexList[ 6] = V[6] + interpolationValPlus*(V[7] - V[6]);//.Normalized()*CUBE_DIMENSIONS[0];
05653                                     grad[6]        = G[6] + interpolationValPlus*(G[7] - G[6]);//.Normalized()*CUBE_DIMENSIONS[0];
05654                                     texCoordList[6] = VTC[6] + interpolationValPlus*(VTC[7] - VTC[6]);//.Normalized()*CUBE_DIMENSIONS[0];
05655                                 }
05656                                 else {
05657                                     //
05658                                     // Interpotation Data
05659                                     interpolationValNeg = -VIntp[7].GetX();
05660                                     //
05661                                     vertexList[ 6] = V[7] + interpolationValNeg*(V[7] - V[6]);//.Normalized()*CUBE_DIMENSIONS[0];
05662                                     grad[6]        = G[7] + interpolationValNeg*(G[7] - G[6]);//.Normalized()*CUBE_DIMENSIONS[0];
05663                                     texCoordList[6] = VTC[7] + interpolationValNeg*(VTC[7] - VTC[6]);//.Normalized()*CUBE_DIMENSIONS[0];
05664                                 }
05665                                 grad[6].Normalized();
05666                             }
05667                             if ( EdgeTable[cubeClass] & 128 ) {
05668                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
05669                                     //
05670                                     // Interpotation Data
05671                                     interpolationValPlus = VIntp[4].GetY();
05672                                     //
05673                                     vertexList[ 7] = V[4] + interpolationValPlus*(V[7] - V[4]);//.Normalized()*CUBE_DIMENSIONS[1];
05674                                     grad[7]        = G[4] + interpolationValPlus*(G[7] - G[4]);//.Normalized()*CUBE_DIMENSIONS[1];
05675                                     texCoordList[7] = VTC[4] + interpolationValPlus*(VTC[7] - VTC[4]);//.Normalized()*CUBE_DIMENSIONS[1];
05676                                 }
05677                                 else {
05678                                     //
05679                                     // Interpotation Data
05680                                     interpolationValNeg = -VIntp[7].GetY();
05681                                     //
05682                                     vertexList[ 7] = V[7] + interpolationValNeg*(V[7] - V[4]);//.Normalized()*CUBE_DIMENSIONS[1];
05683                                     grad[7]        = G[7] + interpolationValNeg*(G[7] - G[4]);//.Normalized()*CUBE_DIMENSIONS[1];
05684                                     texCoordList[7] = VTC[7] + interpolationValNeg*(VTC[7] - VTC[4]);//.Normalized()*CUBE_DIMENSIONS[1];
05685                                 }
05686                                 grad[7].Normalized();
05687                             }
05688                             if ( EdgeTable[cubeClass] & 256 ) {
05689                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
05690                                     //
05691                                     // Interpotation Data
05692                                     interpolationValPlus = VIntp[0].GetZ();
05693                                     //
05694                                     vertexList[ 8] = V[0] + interpolationValPlus*(V[4] - V[0]);//.Normalized()*CUBE_DIMENSIONS[2];
05695                                     grad[8]        = G[0] + interpolationValPlus*(G[4] - G[0]);//.Normalized()*CUBE_DIMENSIONS[2];
05696                                     texCoordList[8] = VTC[0] + interpolationValPlus*(VTC[4] - VTC[0]);//.Normalized()*CUBE_DIMENSIONS[2];
05697                                 }
05698                                 else {
05699                                     //
05700                                     // Interpotation Data
05701                                     interpolationValNeg = -VIntp[4].GetZ();
05702                                     //
05703                                     vertexList[ 8] = V[4] + interpolationValNeg*(V[4] - V[0]);//.Normalized()*CUBE_DIMENSIONS[2];
05704                                     grad[8]        = G[4] + interpolationValNeg*(G[4] - G[0]);//.Normalized()*CUBE_DIMENSIONS[2];
05705                                     texCoordList[8] = VTC[4] + interpolationValNeg*(VTC[4] - VTC[0]);//.Normalized()*CUBE_DIMENSIONS[2];
05706                                 }
05707                                 grad[8].Normalized();
05708                             }
05709                             if ( EdgeTable[cubeClass] & 512 ) {
05710                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
05711                                     //
05712                                     // Interpotation Data
05713                                     interpolationValPlus = VIntp[1].GetZ();
05714                                     //
05715                                     vertexList[ 9] = V[1] + interpolationValPlus*(V[5] - V[1]);//.Normalized()*CUBE_DIMENSIONS[2];
05716                                     grad[9]        = G[1] + interpolationValPlus*(G[5] - G[1]);//.Normalized()*CUBE_DIMENSIONS[2];
05717                                     texCoordList[9] = VTC[1] + interpolationValPlus*(VTC[5] - VTC[1]);//.Normalized()*CUBE_DIMENSIONS[2];
05718                                 }
05719                                 else {
05720                                     //
05721                                     // Interpotation Data
05722                                     interpolationValNeg = -VIntp[5].GetZ();
05723                                     //
05724                                     vertexList[ 9] = V[5] + interpolationValNeg*(V[5] - V[1]);//.Normalized()*CUBE_DIMENSIONS[2];
05725                                     grad[9]        = G[5] + interpolationValNeg*(G[5] - G[1]);//.Normalized()*CUBE_DIMENSIONS[2];
05726                                     texCoordList[9] = VTC[5] + interpolationValNeg*(VTC[5] - VTC[1]);//.Normalized()*CUBE_DIMENSIONS[2];
05727                                 }
05728                                 grad[9].Normalized();
05729                             }
05730                             if ( EdgeTable[cubeClass] & 1024 ) {
05731                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
05732                                     //
05733                                     // Interpotation Data
05734                                     interpolationValPlus = VIntp[2].GetZ();
05735                                     //
05736                                     vertexList[10] = V[2] + interpolationValPlus*(V[6] - V[2]);//.Normalized()*CUBE_DIMENSIONS[2];
05737                                     grad[10]       = G[2] + interpolationValPlus*(G[6] - G[2]);//.Normalized()*CUBE_DIMENSIONS[2];
05738                                     texCoordList[10] = VTC[2] + interpolationValPlus*(VTC[6] - VTC[2]);//.Normalized()*CUBE_DIMENSIONS[2];
05739                                 }
05740                                 else {
05741                                     //
05742                                     // Interpotation Data
05743                                     interpolationValNeg = -VIntp[6].GetZ();
05744                                     //
05745                                     vertexList[10] = V[6] + interpolationValNeg*(V[6] - V[2]);//.Normalized()*CUBE_DIMENSIONS[2];
05746                                     grad[10]       = G[6] + interpolationValNeg*(G[6] - G[2]);//.Normalized()*CUBE_DIMENSIONS[2];
05747                                     texCoordList[10] = VTC[6] + interpolationValNeg*(VTC[6] - VTC[2]);//.Normalized()*CUBE_DIMENSIONS[2];
05748                                 }
05749                                 grad[10].Normalized();
05750                             }
05751                             if ( EdgeTable[cubeClass] & 2048 ) {
05752                                 if ( dataForPosition[idx[3] + 3] > threshold ) {
05753                                     //
05754                                     // Interpotation Data
05755                                     interpolationValPlus = VIntp[3].GetZ();
05756                                     //
05757                                     vertexList[11] = V[3] + interpolationValPlus*(V[7] - V[3]);//.Normalized()*CUBE_DIMENSIONS[2];
05758                                     grad[11]       = G[3] + interpolationValPlus*(G[7] - G[3]);//.Normalized()*CUBE_DIMENSIONS[2];
05759                                     texCoordList[11] = VTC[3] + interpolationValPlus*(VTC[7] - VTC[3]);//.Normalized()*CUBE_DIMENSIONS[2];
05760                                 }
05761                                 else {
05762                                     //
05763                                     // Interpotation Data
05764                                     interpolationValNeg = -VIntp[7].GetZ();
05765                                     //
05766                                     vertexList[11] = V[7] + interpolationValNeg*(V[7] - V[3]);//.Normalized()*CUBE_DIMENSIONS[2];
05767                                     grad[11]       = G[7] + interpolationValNeg*(G[7] - G[3]);//.Normalized()*CUBE_DIMENSIONS[2];
05768                                     texCoordList[11] = VTC[7] + interpolationValNeg*(VTC[7] - VTC[3]);//.Normalized()*CUBE_DIMENSIONS[2];
05769                                 }
05770                                 grad[11].Normalized();
05771                             }
05772 } // End processing normal cube
05773                             //---------------------------------------
05774                             //=======================================
05775                             //***************************************
05776                             //*/
05778 // END DEBUGGING
05779 
05780 
05781 //std::cout << "(V[1] - V[0]).Normalized(): " << (V[1] - V[0]).Normalized() << std::endl;
05782 //std::cout << "(V[1] - V[0]).Normalized().Length(): " << (V[1] - V[0]).Normalized().Length() << std::endl;;
05783 
05784                             //*
05785                             //***************************************
05786                             // Fixed Distance Interpolation
05787                             //=======================================
05788                             //---------------------------------------
05789                             // Edge# 0 to 11 <==> [ 1, 2, 4, ... , 2048 ]
05790 //else {
05791 if ( iIterate < 2 ) {
05792                             if ( EdgeTable[cubeClass] & 1 ) {
05793                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
05794                                     //
05795                                     // Interpotation Data
05796                                     interpolationValPlus = VIntp[0].GetX();
05797                                     //
05798                                     vertexList[ 0] = V[0] + interpolationValPlus*(V[1] - V[0]).Normalized()*CUBE_DIMENSIONS[0];
05799                                     //grad[0]        = G[0] + interpolationValPlus*(G[1] - G[0]).Normalized()*CUBE_DIMENSIONS[0];
05800                                     //texCoordList[0] = VTC[0] + interpolationValPlus*(VTC[1] - VTC[0]).Normalized()*CUBE_DIMENSIONS[0];
05801                                 }
05802                                 else {
05803                                     //
05804                                     // Interpotation Data
05805                                     interpolationValNeg = -VIntp[1].GetX();
05806                                     //
05807                                     vertexList[ 0] = V[1] + interpolationValNeg*(V[1] - V[0]).Normalized()*CUBE_DIMENSIONS[0];
05808                                     //grad[0]        = G[1] + interpolationValNeg*(G[1] - G[0]).Normalized()*CUBE_DIMENSIONS[0];
05809                                     //texCoordList[0] = VTC[1] + interpolationValNeg*(VTC[1] - VTC[0]).Normalized()*CUBE_DIMENSIONS[0];
05810                                 }
05811                                 grad[0].Normalized();
05812                             }
05813                             if ( EdgeTable[cubeClass] & 2 ) {
05814                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
05815                                     //
05816                                     // Interpotation Data
05817                                     interpolationValPlus = VIntp[1].GetY();
05818                                     //
05819                                     vertexList[ 1] = V[1] + interpolationValPlus*(V[2] - V[1]).Normalized()*CUBE_DIMENSIONS[1];
05820                                     //grad[1]        = G[1] + interpolationValPlus*(G[2] - G[1]).Normalized()*CUBE_DIMENSIONS[1];
05821                                     //texCoordList[1] = VTC[1] + interpolationValPlus*(VTC[2] - VTC[1]).Normalized()*CUBE_DIMENSIONS[1];
05822                                 }
05823                                 else {
05824                                     //
05825                                     // Interpotation Data
05826                                     interpolationValNeg = -VIntp[2].GetY();
05827                                     //
05828                                     vertexList[ 1] = V[2] + interpolationValNeg*(V[2] - V[1]).Normalized()*CUBE_DIMENSIONS[1];
05829                                     //grad[1]        = G[2] + interpolationValNeg*(G[2] - G[1]).Normalized()*CUBE_DIMENSIONS[1];
05830                                     //texCoordList[1] = VTC[2] + interpolationValNeg*(VTC[2] - VTC[1]).Normalized()*CUBE_DIMENSIONS[1];
05831                                 }
05832                                 grad[1].Normalized();
05833                             }
05834                             if ( EdgeTable[cubeClass] & 4 ) {
05835                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
05836                                     //
05837                                     // Interpotation Data
05838                                     interpolationValPlus = VIntp[2].GetX();
05839                                     //
05840                                     vertexList[ 2] = V[2] + interpolationValPlus*(V[3] - V[2]).Normalized()*CUBE_DIMENSIONS[0];
05841                                     //grad[2]        = G[2] + interpolationValPlus*(G[3] - G[2]).Normalized()*CUBE_DIMENSIONS[0];
05842                                     //texCoordList[2] = VTC[2] + interpolationValPlus*(VTC[3] - VTC[2]).Normalized()*CUBE_DIMENSIONS[0];
05843                                 }
05844                                 else {
05845                                     //
05846                                     // Interpotation Data
05847                                     interpolationValNeg = -VIntp[3].GetX();
05848                                     //
05849                                     vertexList[ 2] = V[3] + interpolationValNeg*(V[3] - V[2]).Normalized()*CUBE_DIMENSIONS[0];
05850                                     //grad[2]        = G[3] + interpolationValNeg*(G[3] - G[2]).Normalized()*CUBE_DIMENSIONS[0];
05851                                     //texCoordList[2] = VTC[3] + interpolationValNeg*(VTC[3] - VTC[2]).Normalized()*CUBE_DIMENSIONS[0];
05852                                 }
05853                                 grad[2].Normalized();
05854                             }
05855                             if ( EdgeTable[cubeClass] & 8 ) {
05856                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
05857                                     //
05858                                     // Interpotation Data
05859                                     interpolationValPlus = VIntp[0].GetY();
05860                                     //
05861                                     vertexList[ 3] = V[0] + interpolationValPlus*(V[3] - V[0]).Normalized()*CUBE_DIMENSIONS[1];
05862                                     //grad[3]        = G[0] + interpolationValPlus*(G[3] - G[0]).Normalized()*CUBE_DIMENSIONS[1];
05863                                     //texCoordList[3] = VTC[0] + interpolationValPlus*(VTC[3] - VTC[0]).Normalized()*CUBE_DIMENSIONS[1];
05864                                 }
05865                                 else {
05866                                     //
05867                                     // Interpotation Data
05868                                     interpolationValNeg = -VIntp[3].GetY();
05869                                     //
05870                                     vertexList[ 3] = V[3] + interpolationValNeg*(V[3] - V[0]).Normalized()*CUBE_DIMENSIONS[1];
05871                                     //grad[3]        = G[3] + interpolationValNeg*(G[3] - G[0]).Normalized()*CUBE_DIMENSIONS[1];
05872                                     //texCoordList[3] = VTC[3] + interpolationValNeg*(VTC[3] - VTC[0]).Normalized()*CUBE_DIMENSIONS[1];
05873                                 }
05874                                 grad[3].Normalized();
05875                             }
05876                             if ( EdgeTable[cubeClass] & 16 ) {
05877                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
05878                                     //
05879                                     // Interpotation Data
05880                                     interpolationValPlus = VIntp[4].GetX();
05881                                     //
05882                                     vertexList[ 4] = V[4] + interpolationValPlus*(V[5] - V[4]).Normalized()*CUBE_DIMENSIONS[0];
05883                                     //grad[4]        = G[4] + interpolationValPlus*(G[5] - G[4]).Normalized()*CUBE_DIMENSIONS[0];
05884                                     //texCoordList[4] = VTC[4] + interpolationValPlus*(VTC[5] - VTC[4]).Normalized()*CUBE_DIMENSIONS[0];
05885                                 }
05886                                 else {
05887                                     //
05888                                     // Interpotation Data
05889                                     interpolationValNeg = -VIntp[5].GetX();
05890                                     //
05891                                     vertexList[ 4] = V[5] + interpolationValNeg*(V[5] - V[4]).Normalized()*CUBE_DIMENSIONS[0];
05892                                     //grad[4]        = G[5] + interpolationValNeg*(G[5] - G[4]).Normalized()*CUBE_DIMENSIONS[0];
05893                                     //texCoordList[4] = VTC[5] + interpolationValNeg*(VTC[5] - VTC[4]).Normalized()*CUBE_DIMENSIONS[0];
05894                                 }
05895                                 grad[4].Normalized();
05896                             }
05897                             if ( EdgeTable[cubeClass] & 32 ) {
05898                                 if ( dataForPosition[idx[5] + 3] > threshold ) {
05899                                     //
05900                                     // Interpotation Data
05901                                     interpolationValPlus = VIntp[5].GetY();
05902                                     //
05903                                     vertexList[ 5] = V[5] + interpolationValPlus*(V[6] - V[5]).Normalized()*CUBE_DIMENSIONS[1];
05904                                     //grad[5]        = G[5] + interpolationValPlus*(G[6] - G[5]).Normalized()*CUBE_DIMENSIONS[1];
05905                                     //texCoordList[5] = VTC[5] + interpolationValPlus*(VTC[6] - VTC[5]).Normalized()*CUBE_DIMENSIONS[1];
05906                                 }
05907                                 else {
05908                                     //
05909                                     // Interpotation Data
05910                                     interpolationValNeg = -VIntp[6].GetY();
05911                                     //
05912                                     vertexList[ 5] = V[6] + interpolationValNeg*(V[6] - V[5]).Normalized()*CUBE_DIMENSIONS[1];
05913                                     //grad[5]        = G[6] + interpolationValNeg*(G[6] - G[5]).Normalized()*CUBE_DIMENSIONS[1];
05914                                     //texCoordList[5] = VTC[6] + interpolationValNeg*(VTC[6] - VTC[5]).Normalized()*CUBE_DIMENSIONS[1];
05915                                 }
05916                                 grad[5].Normalized();
05917                             }
05918                             if ( EdgeTable[cubeClass] & 64 ) {
05919                                 if ( dataForPosition[idx[6] + 3] > threshold ) {
05920                                     //
05921                                     // Interpotation Data
05922                                     interpolationValPlus = VIntp[6].GetX();
05923                                     //
05924                                     vertexList[ 6] = V[6] + interpolationValPlus*(V[7] - V[6]).Normalized()*CUBE_DIMENSIONS[0];
05925                                     //grad[6]        = G[6] + interpolationValPlus*(G[7] - G[6]).Normalized()*CUBE_DIMENSIONS[0];
05926                                     //texCoordList[6] = VTC[6] + interpolationValPlus*(VTC[7] - VTC[6]).Normalized()*CUBE_DIMENSIONS[0];
05927                                 }
05928                                 else {
05929                                     //
05930                                     // Interpotation Data
05931                                     interpolationValNeg = -VIntp[7].GetX();
05932                                     //
05933                                     vertexList[ 6] = V[7] + interpolationValNeg*(V[7] - V[6]).Normalized()*CUBE_DIMENSIONS[0];
05934                                     //grad[6]        = G[7] + interpolationValNeg*(G[7] - G[6]).Normalized()*CUBE_DIMENSIONS[0];
05935                                     //texCoordList[6] = VTC[7] + interpolationValNeg*(VTC[7] - VTC[6]).Normalized()*CUBE_DIMENSIONS[0];
05936                                 }
05937                                 grad[6].Normalized();
05938                             }
05939                             if ( EdgeTable[cubeClass] & 128 ) {
05940                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
05941                                     //
05942                                     // Interpotation Data
05943                                     interpolationValPlus = VIntp[4].GetY();
05944                                     //
05945                                     vertexList[ 7] = V[4] + interpolationValPlus*(V[7] - V[4]).Normalized()*CUBE_DIMENSIONS[1];
05946                                     //grad[7]        = G[4] + interpolationValPlus*(G[7] - G[4]).Normalized()*CUBE_DIMENSIONS[1];
05947                                     //texCoordList[7] = VTC[4] + interpolationValPlus*(VTC[7] - VTC[4]).Normalized()*CUBE_DIMENSIONS[1];
05948                                 }
05949                                 else {
05950                                     //
05951                                     // Interpotation Data
05952                                     interpolationValNeg = -VIntp[7].GetY();
05953                                     //
05954                                     vertexList[ 7] = V[7] + interpolationValNeg*(V[7] - V[4]).Normalized()*CUBE_DIMENSIONS[1];
05955                                     //grad[7]        = G[7] + interpolationValNeg*(G[7] - G[4]).Normalized()*CUBE_DIMENSIONS[1];
05956                                     //texCoordList[7] = VTC[7] + interpolationValNeg*(VTC[7] - VTC[4]).Normalized()*CUBE_DIMENSIONS[1];
05957                                 }
05958                                 grad[7].Normalized();
05959                             }
05960                             if ( EdgeTable[cubeClass] & 256 ) {
05961                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
05962                                     //
05963                                     // Interpotation Data
05964                                     interpolationValPlus = VIntp[0].GetZ();
05965                                     //
05966                                     vertexList[ 8] = V[0] + interpolationValPlus*(V[4] - V[0]).Normalized()*CUBE_DIMENSIONS[2];
05967                                     //grad[8]        = G[0] + interpolationValPlus*(G[4] - G[0]).Normalized()*CUBE_DIMENSIONS[2];
05968                                     //texCoordList[8] = VTC[0] + interpolationValPlus*(VTC[4] - VTC[0]).Normalized()*CUBE_DIMENSIONS[2];
05969                                 }
05970                                 else {
05971                                     //
05972                                     // Interpotation Data
05973                                     interpolationValNeg = -VIntp[4].GetZ();
05974                                     //
05975                                     vertexList[ 8] = V[4] + interpolationValNeg*(V[4] - V[0]).Normalized()*CUBE_DIMENSIONS[2];
05976                                     //grad[8]        = G[4] + interpolationValNeg*(G[4] - G[0]).Normalized()*CUBE_DIMENSIONS[2];
05977                                     //texCoordList[8] = VTC[4] + interpolationValNeg*(VTC[4] - VTC[0]).Normalized()*CUBE_DIMENSIONS[2];
05978                                 }
05979                                 grad[8].Normalized();
05980                             }
05981                             if ( EdgeTable[cubeClass] & 512 ) {
05982                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
05983                                     //
05984                                     // Interpotation Data
05985                                     interpolationValPlus = VIntp[1].GetZ();
05986                                     //
05987                                     vertexList[ 9] = V[1] + interpolationValPlus*(V[5] - V[1]).Normalized()*CUBE_DIMENSIONS[2];
05988                                     //grad[9]        = G[1] + interpolationValPlus*(G[5] - G[1]).Normalized()*CUBE_DIMENSIONS[2];
05989                                     //texCoordList[9] = VTC[1] + interpolationValPlus*(VTC[5] - VTC[1]).Normalized()*CUBE_DIMENSIONS[2];
05990                                 }
05991                                 else {
05992                                     //
05993                                     // Interpotation Data
05994                                     interpolationValNeg = -VIntp[5].GetZ();
05995                                     //
05996                                     vertexList[ 9] = V[5] + interpolationValNeg*(V[5] - V[1]).Normalized()*CUBE_DIMENSIONS[2];
05997                                     //grad[9]        = G[5] + interpolationValNeg*(G[5] - G[1]).Normalized()*CUBE_DIMENSIONS[2];
05998                                     //texCoordList[9] = VTC[5] + interpolationValNeg*(VTC[5] - VTC[1]).Normalized()*CUBE_DIMENSIONS[2];
05999                                 }
06000                                 grad[9].Normalized();
06001                             }
06002                             if ( EdgeTable[cubeClass] & 1024 ) {
06003                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
06004                                     //
06005                                     // Interpotation Data
06006                                     interpolationValPlus = VIntp[2].GetZ();
06007                                     //
06008                                     vertexList[10] = V[2] + interpolationValPlus*(V[6] - V[2]).Normalized()*CUBE_DIMENSIONS[2];
06009                                     //grad[10]       = G[2] + interpolationValPlus*(G[6] - G[2]).Normalized()*CUBE_DIMENSIONS[2];
06010                                     //texCoordList[10] = VTC[2] + interpolationValPlus*(VTC[6] - VTC[2]).Normalized()*CUBE_DIMENSIONS[2];
06011                                 }
06012                                 else {
06013                                     //
06014                                     // Interpotation Data
06015                                     interpolationValNeg = -VIntp[6].GetZ();
06016                                     //
06017                                     vertexList[10] = V[6] + interpolationValNeg*(V[6] - V[2]).Normalized()*CUBE_DIMENSIONS[2];
06018                                     //grad[10]       = G[6] + interpolationValNeg*(G[6] - G[2]).Normalized()*CUBE_DIMENSIONS[2];
06019                                     //texCoordList[10] = VTC[6] + interpolationValNeg*(VTC[6] - VTC[2]).Normalized()*CUBE_DIMENSIONS[2];
06020                                 }
06021                                 grad[10].Normalized();
06022                             }
06023                             if ( EdgeTable[cubeClass] & 2048 ) {
06024                                 if ( dataForPosition[idx[3] + 3] > threshold ) {
06025                                     //
06026                                     // Interpotation Data
06027                                     interpolationValPlus = VIntp[3].GetZ();
06028                                     //
06029                                     vertexList[11] = V[3] + interpolationValPlus*(V[7] - V[3]).Normalized()*CUBE_DIMENSIONS[2];
06030                                     //grad[11]       = G[3] + interpolationValPlus*(G[7] - G[3]).Normalized()*CUBE_DIMENSIONS[2];
06031                                     //texCoordList[11] = VTC[3] + interpolationValPlus*(VTC[7] - VTC[3]).Normalized()*CUBE_DIMENSIONS[2];
06032                                 }
06033                                 else {
06034                                     //
06035                                     // Interpotation Data
06036                                     interpolationValNeg = -VIntp[7].GetZ();
06037                                     //
06038                                     vertexList[11] = V[7] + interpolationValNeg*(V[7] - V[3]).Normalized()*CUBE_DIMENSIONS[2];
06039                                     //grad[11]       = G[7] + interpolationValNeg*(G[7] - G[3]).Normalized()*CUBE_DIMENSIONS[2];
06040                                     //texCoordList[11] = VTC[7] + interpolationValNeg*(VTC[7] - VTC[3]).Normalized()*CUBE_DIMENSIONS[2];
06041                                 }
06042                                 grad[11].Normalized();
06043                             }
06044 } // End processing `left' or `right' cube
06045                             //---------------------------------------
06046                             //=======================================
06047                             //***************************************
06048                             //*/
06049 
06050 
06051 
06052                         } // END: Not Use Global Interpolation Value
06053                         //-----------------------------------------------
06054                         // Use Global Interpolation Value -- all elements have the same interpolation value
06055                         else {
06056                             //-------------------------------------
06057                             // Marching Cube Interpolation
06058                             T interpolationValPlus = m_tGlobalInterpolationVal;
06059                             T interpolationValNeg  = 1.0 - m_tGlobalInterpolationVal;
06060                             //T interpolationValPlus = 0.3;
06061                             //T interpolationValNeg  = 1.0 - 0.3;
06062 
06063 
06064                             /*
06065                             //***************************************
06066                             // Normal Interpolation
06067                             //=======================================
06068                             //---------------------------------------
06069                             // Edge# 0 to 11 <==> [ 1, 2, 4, ... , 2048 ]
06070                             if ( EdgeTable[cubeClass] & 1 ) {
06071                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06072                                     //
06073                                     // Interpotation Data
06074                                     //interpolationValPlus = VIntp[0].GetX();
06075                                     //
06076                                     vertexList[ 0] = V[0] + interpolationValPlus*(V[1] - V[0]);
06077                                     grad[0]        = G[0] + interpolationValPlus*(G[1] - G[0]);
06078                                     texCoordList[0] = VTC[0] + interpolationValPlus*(VTC[1] - VTC[0]);
06079                                 }
06080                                 else {
06081                                     //
06082                                     // Interpotation Data
06083                                     //interpolationValNeg = 1.0 - VIntp[1].GetX();
06084                                     //
06085                                     vertexList[ 0] = V[0] + interpolationValNeg*(V[1] - V[0]);
06086                                     grad[0]        = G[0] + interpolationValNeg*(G[1] - G[0]);
06087                                     texCoordList[0] = VTC[0] + interpolationValNeg*(VTC[1] - VTC[0]);
06088                                 }
06089                                 grad[0].Normalized();
06090                             }
06091                             if ( EdgeTable[cubeClass] & 2 ) {
06092                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
06093                                     //
06094                                     // Interpotation Data
06095                                     //interpolationValPlus = VIntp[1].GetY();
06096                                     //
06097                                     vertexList[ 1] = V[1] + interpolationValPlus*(V[2] - V[1]);
06098                                     grad[1]        = G[1] + interpolationValPlus*(G[2] - G[1]);
06099                                     texCoordList[1] = VTC[1] + interpolationValPlus*(VTC[2] - VTC[1]);
06100                                 }
06101                                 else {
06102                                     //
06103                                     // Interpotation Data
06104                                     //interpolationValNeg = 1.0 - VIntp[2].GetY();
06105                                     //
06106                                     vertexList[ 1] = V[1] + interpolationValNeg*(V[2] - V[1]);
06107                                     grad[1]        = G[1] + interpolationValNeg*(G[2] - G[1]);
06108                                     texCoordList[1] = VTC[1] + interpolationValNeg*(VTC[2] - VTC[1]);
06109                                 }
06110                                 grad[1].Normalized();
06111                             }
06112                             if ( EdgeTable[cubeClass] & 4 ) {
06113                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
06114                                     //
06115                                     // Interpotation Data
06116                                     //interpolationValPlus = VIntp[2].GetX();
06117                                     //
06118                                     vertexList[ 2] = V[2] + interpolationValPlus*(V[3] - V[2]);
06119                                     grad[2]        = G[2] + interpolationValPlus*(G[3] - G[2]);
06120                                     texCoordList[2] = VTC[2] + interpolationValPlus*(VTC[3] - VTC[2]);
06121                                 }
06122                                 else {
06123                                     //
06124                                     // Interpotation Data
06125                                     //interpolationValNeg = 1.0 - VIntp[3].GetX();
06126                                     //
06127                                     vertexList[ 2] = V[2] + interpolationValNeg*(V[3] - V[2]);
06128                                     grad[2]        = G[2] + interpolationValNeg*(G[3] - G[2]);
06129                                     texCoordList[2] = VTC[2] + interpolationValNeg*(VTC[3] - VTC[2]);
06130                                 }
06131                                 grad[2].Normalized();
06132                             }
06133                             if ( EdgeTable[cubeClass] & 8 ) {
06134                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06135                                     //
06136                                     // Interpotation Data
06137                                     //interpolationValPlus = VIntp[0].GetY();
06138                                     //
06139                                     vertexList[ 3] = V[0] + interpolationValPlus*(V[3] - V[0]);
06140                                     grad[3]        = G[0] + interpolationValPlus*(G[3] - G[0]);
06141                                     texCoordList[3] = VTC[0] + interpolationValPlus*(VTC[3] - VTC[0]);
06142                                 }
06143                                 else {
06144                                     //
06145                                     // Interpotation Data
06146                                     //interpolationValNeg = 1.0 - VIntp[3].GetY();
06147                                     //
06148                                     vertexList[ 3] = V[0] + interpolationValNeg*(V[3] - V[0]);
06149                                     grad[3]        = G[0] + interpolationValNeg*(G[3] - G[0]);
06150                                     texCoordList[3] = VTC[0] + interpolationValNeg*(VTC[3] - VTC[0]);
06151                                 }
06152                                 grad[3].Normalized();
06153                             }
06154                             if ( EdgeTable[cubeClass] & 16 ) {
06155                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
06156                                     //
06157                                     // Interpotation Data
06158                                     //interpolationValPlus = VIntp[4].GetX();
06159                                     //
06160                                     vertexList[ 4] = V[4] + interpolationValPlus*(V[5] - V[4]);
06161                                     grad[4]        = G[4] + interpolationValPlus*(G[5] - G[4]);
06162                                     texCoordList[4] = VTC[4] + interpolationValPlus*(VTC[5] - VTC[4]);
06163                                 }
06164                                 else {
06165                                     //
06166                                     // Interpotation Data
06167                                     //interpolationValNeg = 1.0 - VIntp[5].GetX();
06168                                     //
06169                                     vertexList[ 4] = V[4] + interpolationValNeg*(V[5] - V[4]);
06170                                     grad[4]        = G[4] + interpolationValNeg*(G[5] - G[4]);
06171                                     texCoordList[4] = VTC[4] + interpolationValNeg*(VTC[5] - VTC[4]);
06172                                 }
06173                                 grad[4].Normalized();
06174                             }
06175                             if ( EdgeTable[cubeClass] & 32 ) {
06176                                 if ( dataForPosition[idx[5] + 3] > threshold ) {
06177                                     //
06178                                     // Interpotation Data
06179                                     //interpolationValPlus = VIntp[5].GetY();
06180                                     //
06181                                     vertexList[ 5] = V[5] + interpolationValPlus*(V[6] - V[5]);
06182                                     grad[5]        = G[5] + interpolationValPlus*(G[6] - G[5]);
06183                                     texCoordList[5] = VTC[5] + interpolationValPlus*(VTC[6] - VTC[5]);
06184                                 }
06185                                 else {
06186                                     //
06187                                     // Interpotation Data
06188                                     //interpolationValNeg = 1.0 - VIntp[6].GetY();
06189                                     //
06190                                     vertexList[ 5] = V[5] + interpolationValNeg*(V[6] - V[5]);
06191                                     grad[5]        = G[5] + interpolationValNeg*(G[6] - G[5]);
06192                                     texCoordList[5] = VTC[5] + interpolationValNeg*(VTC[6] - VTC[5]);
06193                                 }
06194                                 grad[5].Normalized();
06195                             }
06196                             if ( EdgeTable[cubeClass] & 64 ) {
06197                                 if ( dataForPosition[idx[6] + 3] > threshold ) {
06198                                     //
06199                                     // Interpotation Data
06200                                     //interpolationValPlus = VIntp[6].GetX();
06201                                     //
06202                                     vertexList[ 6] = V[6] + interpolationValPlus*(V[7] - V[6]);
06203                                     grad[6]        = G[6] + interpolationValPlus*(G[7] - G[6]);
06204                                     texCoordList[6] = VTC[6] + interpolationValPlus*(VTC[7] - VTC[6]);
06205                                 }
06206                                 else {
06207                                     //
06208                                     // Interpotation Data
06209                                     //interpolationValNeg = 1.0 - VIntp[7].GetX();
06210                                     //
06211                                     vertexList[ 6] = V[6] + interpolationValNeg*(V[7] - V[6]);
06212                                     grad[6]        = G[6] + interpolationValNeg*(G[7] - G[6]);
06213                                     texCoordList[6] = VTC[6] + interpolationValNeg*(VTC[7] - VTC[6]);
06214                                 }
06215                                 grad[6].Normalized();
06216                             }
06217                             if ( EdgeTable[cubeClass] & 128 ) {
06218                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
06219                                     //
06220                                     // Interpotation Data
06221                                     //interpolationValPlus = VIntp[4].GetY();
06222                                     //
06223                                     vertexList[ 7] = V[4] + interpolationValPlus*(V[7] - V[4]);
06224                                     grad[7]        = G[4] + interpolationValPlus*(G[7] - G[4]);
06225                                     texCoordList[7] = VTC[4] + interpolationValPlus*(VTC[7] - VTC[4]);
06226                                 }
06227                                 else {
06228                                     //
06229                                     // Interpotation Data
06230                                     //interpolationValNeg = 1.0 - VIntp[7].GetY();
06231                                     //
06232                                     vertexList[ 7] = V[4] + interpolationValNeg*(V[7] - V[4]);
06233                                     grad[7]        = G[4] + interpolationValNeg*(G[7] - G[4]);
06234                                     texCoordList[7] = VTC[4] + interpolationValNeg*(VTC[7] - VTC[4]);
06235                                 }
06236                                 grad[7].Normalized();
06237                             }
06238                             if ( EdgeTable[cubeClass] & 256 ) {
06239                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06240                                     //
06241                                     // Interpotation Data
06242                                     //interpolationValPlus = VIntp[0].GetZ();
06243                                     //
06244                                     vertexList[ 8] = V[0] + interpolationValPlus*(V[4] - V[0]);
06245                                     grad[8]        = G[0] + interpolationValPlus*(G[4] - G[0]);
06246                                     texCoordList[8] = VTC[0] + interpolationValPlus*(VTC[4] - VTC[0]);
06247                                 }
06248                                 else {
06249                                     //
06250                                     // Interpotation Data
06251                                     //interpolationValNeg = 1.0 - VIntp[4].GetZ();
06252                                     //
06253                                     vertexList[ 8] = V[0] + interpolationValNeg*(V[4] - V[0]);
06254                                     grad[8]        = G[0] + interpolationValNeg*(G[4] - G[0]);
06255                                     texCoordList[8] = VTC[0] + interpolationValNeg*(VTC[4] - VTC[0]);
06256                                 }
06257                                 grad[8].Normalized();
06258                             }
06259                             if ( EdgeTable[cubeClass] & 512 ) {
06260                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
06261                                     //
06262                                     // Interpotation Data
06263                                     //interpolationValPlus = VIntp[1].GetZ();
06264                                     //
06265                                     vertexList[ 9] = V[1] + interpolationValPlus*(V[5] - V[1]);
06266                                     grad[9]        = G[1] + interpolationValPlus*(G[5] - G[1]);
06267                                     texCoordList[9] = VTC[1] + interpolationValPlus*(VTC[5] - VTC[1]);
06268                                 }
06269                                 else {
06270                                     //
06271                                     // Interpotation Data
06272                                     //interpolationValNeg = 1.0 - VIntp[5].GetZ();
06273                                     //
06274                                     vertexList[ 9] = V[1] + interpolationValNeg*(V[5] - V[1]);
06275                                     grad[9]        = G[1] + interpolationValNeg*(G[5] - G[1]);
06276                                     texCoordList[9] = VTC[1] + interpolationValNeg*(VTC[5] - VTC[1]);
06277                                 }
06278                                 grad[9].Normalized();
06279                             }
06280                             if ( EdgeTable[cubeClass] & 1024 ) {
06281                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
06282                                     //
06283                                     // Interpotation Data
06284                                     //interpolationValPlus = VIntp[2].GetZ();
06285                                     //
06286                                     vertexList[10] = V[2] + interpolationValPlus*(V[6] - V[2]);
06287                                     grad[10]       = G[2] + interpolationValPlus*(G[6] - G[2]);
06288                                     texCoordList[10] = VTC[2] + interpolationValPlus*(VTC[6] - VTC[2]);
06289                                 }
06290                                 else {
06291                                     //
06292                                     // Interpotation Data
06293                                     //interpolationValNeg = 1.0 - VIntp[6].GetZ();
06294                                     //
06295                                     vertexList[10] = V[2] + interpolationValNeg*(V[6] - V[2]);
06296                                     grad[10]       = G[2] + interpolationValNeg*(G[6] - G[2]);
06297                                     texCoordList[10] = VTC[2] + interpolationValNeg*(VTC[6] - VTC[2]);
06298                                 }
06299                                 grad[10].Normalized();
06300                             }
06301                             if ( EdgeTable[cubeClass] & 2048 ) {
06302                                 if ( dataForPosition[idx[3] + 3] > threshold ) {
06303                                     //
06304                                     // Interpotation Data
06305                                     //interpolationValPlus = VIntp[3].GetZ();
06306                                     //
06307                                     vertexList[11] = V[3] + interpolationValPlus*(V[7] - V[3]);
06308                                     grad[11]       = G[3] + interpolationValPlus*(G[7] - G[3]);
06309                                     texCoordList[11] = VTC[3] + interpolationValPlus*(VTC[7] - VTC[3]);
06310                                 }
06311                                 else {
06312                                     //
06313                                     // Interpotation Data
06314                                     //interpolationValNeg = 1.0 - VIntp[7].GetZ();
06315                                     //
06316                                     vertexList[11] = V[3] + interpolationValNeg*(V[7] - V[3]);
06317                                     grad[11]       = G[3] + interpolationValNeg*(G[7] - G[3]);
06318                                     texCoordList[11] = VTC[3] + interpolationValNeg*(VTC[7] - VTC[3]);
06319                                 }
06320                                 grad[11].Normalized();
06321                             }
06322                             //---------------------------------------
06323                             //=======================================
06324                             //***************************************
06325                             //*/
06326 
06327 
06328 
06329                             //*
06330                             //***************************************
06331                             // Fixed Distance Interpolation
06332                             //=======================================
06333                             //---------------------------------------
06334                             // Edge# 0 to 11 <==> [ 1, 2, 4, ... , 2048 ]
06335                             if ( EdgeTable[cubeClass] & 1 ) {
06336                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06337                                     //
06338                                     // Interpotation Data
06339                                     //interpolationValPlus = VIntp[0].GetX();
06340                                     //
06341                                     vertexList[ 0] = V[0] + interpolationValPlus*(V[1] - V[0]).Normalized()*CUBE_DIMENSIONS[0];
06342                                     grad[0]        = G[0] + interpolationValPlus*(G[1] - G[0]).Normalized()*CUBE_DIMENSIONS[0];
06343                                     texCoordList[0] = VTC[0] + interpolationValPlus*(VTC[1] - VTC[0]).Normalized()*CUBE_DIMENSIONS[0];
06344                                 }
06345                                 else {
06346                                     //
06347                                     // Interpotation Data
06348                                     //interpolationValNeg = 1.0 - VIntp[1].GetX();
06349                                     //
06350                                     vertexList[ 0] = V[1] - interpolationValPlus*(V[1] - V[0]).Normalized()*CUBE_DIMENSIONS[0];
06351                                     grad[0]        = G[1] - interpolationValPlus*(G[1] - G[0]).Normalized()*CUBE_DIMENSIONS[0];
06352                                     texCoordList[0] = VTC[1] - interpolationValPlus*(VTC[1] - VTC[0]).Normalized()*CUBE_DIMENSIONS[0];
06353                                 }
06354                                 grad[0].Normalized();
06355                             }
06356                             if ( EdgeTable[cubeClass] & 2 ) {
06357                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
06358                                     //
06359                                     // Interpotation Data
06360                                     //interpolationValPlus = VIntp[1].GetY();
06361                                     //
06362                                     vertexList[ 1] = V[1] + interpolationValPlus*(V[2] - V[1]).Normalized()*CUBE_DIMENSIONS[1];
06363                                     grad[1]        = G[1] + interpolationValPlus*(G[2] - G[1]).Normalized()*CUBE_DIMENSIONS[1];
06364                                     texCoordList[1] = VTC[1] + interpolationValPlus*(VTC[2] - VTC[1]).Normalized()*CUBE_DIMENSIONS[1];
06365                                 }
06366                                 else {
06367                                     //
06368                                     // Interpotation Data
06369                                     //interpolationValNeg = 1.0 - VIntp[2].GetY();
06370                                     //
06371                                     vertexList[ 1] = V[2] - interpolationValPlus*(V[2] - V[1]).Normalized()*CUBE_DIMENSIONS[1];
06372                                     grad[1]        = G[2] - interpolationValPlus*(G[2] - G[1]).Normalized()*CUBE_DIMENSIONS[1];
06373                                     texCoordList[1] = VTC[2] - interpolationValPlus*(VTC[2] - VTC[1]).Normalized()*CUBE_DIMENSIONS[1];
06374                                 }
06375                                 grad[1].Normalized();
06376                             }
06377                             if ( EdgeTable[cubeClass] & 4 ) {
06378                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
06379                                     //
06380                                     // Interpotation Data
06381                                     //interpolationValPlus = VIntp[2].GetX();
06382                                     //
06383                                     vertexList[ 2] = V[2] + interpolationValPlus*(V[3] - V[2]).Normalized()*CUBE_DIMENSIONS[0];
06384                                     grad[2]        = G[2] + interpolationValPlus*(G[3] - G[2]).Normalized()*CUBE_DIMENSIONS[0];
06385                                     texCoordList[2] = VTC[2] + interpolationValPlus*(VTC[3] - VTC[2]).Normalized()*CUBE_DIMENSIONS[0];
06386                                 }
06387                                 else {
06388                                     //
06389                                     // Interpotation Data
06390                                     //interpolationValNeg = 1.0 - VIntp[3].GetX();
06391                                     //
06392                                     vertexList[ 2] = V[3] - interpolationValPlus*(V[3] - V[2]).Normalized()*CUBE_DIMENSIONS[0];
06393                                     grad[2]        = G[3] - interpolationValPlus*(G[3] - G[2]).Normalized()*CUBE_DIMENSIONS[0];
06394                                     texCoordList[2] = VTC[3] - interpolationValPlus*(VTC[3] - VTC[2]).Normalized()*CUBE_DIMENSIONS[0];
06395                                 }
06396                                 grad[2].Normalized();
06397                             }
06398                             if ( EdgeTable[cubeClass] & 8 ) {
06399                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06400                                     //
06401                                     // Interpotation Data
06402                                     //interpolationValPlus = VIntp[0].GetY();
06403                                     //
06404                                     vertexList[ 3] = V[0] + interpolationValPlus*(V[3] - V[0]).Normalized()*CUBE_DIMENSIONS[1];
06405                                     grad[3]        = G[0] + interpolationValPlus*(G[3] - G[0]).Normalized()*CUBE_DIMENSIONS[1];
06406                                     texCoordList[3] = VTC[0] + interpolationValPlus*(VTC[3] - VTC[0]).Normalized()*CUBE_DIMENSIONS[1];
06407                                 }
06408                                 else {
06409                                     //
06410                                     // Interpotation Data
06411                                     //interpolationValNeg = 1.0 - VIntp[3].GetY();
06412                                     //
06413                                     vertexList[ 3] = V[3] - interpolationValPlus*(V[3] - V[0]).Normalized()*CUBE_DIMENSIONS[1];
06414                                     grad[3]        = G[3] - interpolationValPlus*(G[3] - G[0]).Normalized()*CUBE_DIMENSIONS[1];
06415                                     texCoordList[3] = VTC[3] - interpolationValPlus*(VTC[3] - VTC[0]).Normalized()*CUBE_DIMENSIONS[1];
06416                                 }
06417                                 grad[3].Normalized();
06418                             }
06419                             if ( EdgeTable[cubeClass] & 16 ) {
06420                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
06421                                     //
06422                                     // Interpotation Data
06423                                     //interpolationValPlus = VIntp[4].GetX();
06424                                     //
06425                                     vertexList[ 4] = V[4] + interpolationValPlus*(V[5] - V[4]).Normalized()*CUBE_DIMENSIONS[0];
06426                                     grad[4]        = G[4] + interpolationValPlus*(G[5] - G[4]).Normalized()*CUBE_DIMENSIONS[0];
06427                                     texCoordList[4] = VTC[4] + interpolationValPlus*(VTC[5] - VTC[4]).Normalized()*CUBE_DIMENSIONS[0];
06428                                 }
06429                                 else {
06430                                     //
06431                                     // Interpotation Data
06432                                     //interpolationValNeg = 1.0 - VIntp[5].GetX();
06433                                     //
06434                                     vertexList[ 4] = V[5] - interpolationValPlus*(V[5] - V[4]).Normalized()*CUBE_DIMENSIONS[0];
06435                                     grad[4]        = G[5] - interpolationValPlus*(G[5] - G[4]).Normalized()*CUBE_DIMENSIONS[0];
06436                                     texCoordList[4] = VTC[5] - interpolationValPlus*(VTC[5] - VTC[4]).Normalized()*CUBE_DIMENSIONS[0];
06437                                 }
06438                                 grad[4].Normalized();
06439                             }
06440                             if ( EdgeTable[cubeClass] & 32 ) {
06441                                 if ( dataForPosition[idx[5] + 3] > threshold ) {
06442                                     //
06443                                     // Interpotation Data
06444                                     //interpolationValPlus = VIntp[5].GetY();
06445                                     //
06446                                     vertexList[ 5] = V[5] + interpolationValPlus*(V[6] - V[5]).Normalized()*CUBE_DIMENSIONS[1];
06447                                     grad[5]        = G[5] + interpolationValPlus*(G[6] - G[5]).Normalized()*CUBE_DIMENSIONS[1];
06448                                     texCoordList[5] = VTC[5] + interpolationValPlus*(VTC[6] - VTC[5]).Normalized()*CUBE_DIMENSIONS[1];
06449                                 }
06450                                 else {
06451                                     //
06452                                     // Interpotation Data
06453                                     //interpolationValNeg = 1.0 - VIntp[6].GetY();
06454                                     //
06455                                     vertexList[ 5] = V[6] - interpolationValPlus*(V[6] - V[5]).Normalized()*CUBE_DIMENSIONS[1];
06456                                     grad[5]        = G[6] - interpolationValPlus*(G[6] - G[5]).Normalized()*CUBE_DIMENSIONS[1];
06457                                     texCoordList[5] = VTC[6] - interpolationValPlus*(VTC[6] - VTC[5]).Normalized()*CUBE_DIMENSIONS[1];
06458                                 }
06459                                 grad[5].Normalized();
06460                             }
06461                             if ( EdgeTable[cubeClass] & 64 ) {
06462                                 if ( dataForPosition[idx[6] + 3] > threshold ) {
06463                                     //
06464                                     // Interpotation Data
06465                                     //interpolationValPlus = VIntp[6].GetX();
06466                                     //
06467                                     vertexList[ 6] = V[6] + interpolationValPlus*(V[7] - V[6]).Normalized()*CUBE_DIMENSIONS[0];
06468                                     grad[6]        = G[6] + interpolationValPlus*(G[7] - G[6]).Normalized()*CUBE_DIMENSIONS[0];
06469                                     texCoordList[6] = VTC[6] + interpolationValPlus*(VTC[7] - VTC[6]).Normalized()*CUBE_DIMENSIONS[0];
06470                                 }
06471                                 else {
06472                                     //
06473                                     // Interpotation Data
06474                                     //interpolationValNeg = 1.0 - VIntp[7].GetX();
06475                                     //
06476                                     vertexList[ 6] = V[7] - interpolationValPlus*(V[7] - V[6]).Normalized()*CUBE_DIMENSIONS[0];
06477                                     grad[6]        = G[7] - interpolationValPlus*(G[7] - G[6]).Normalized()*CUBE_DIMENSIONS[0];
06478                                     texCoordList[6] = VTC[7] - interpolationValPlus*(VTC[7] - VTC[6]).Normalized()*CUBE_DIMENSIONS[0];
06479                                 }
06480                                 grad[6].Normalized();
06481                             }
06482                             if ( EdgeTable[cubeClass] & 128 ) {
06483                                 if ( dataForPosition[idx[4] + 3] > threshold ) {
06484                                     //
06485                                     // Interpotation Data
06486                                     //interpolationValPlus = VIntp[4].GetY();
06487                                     //
06488                                     vertexList[ 7] = V[4] + interpolationValPlus*(V[7] - V[4]).Normalized()*CUBE_DIMENSIONS[1];
06489                                     grad[7]        = G[4] + interpolationValPlus*(G[7] - G[4]).Normalized()*CUBE_DIMENSIONS[1];
06490                                     texCoordList[7] = VTC[4] + interpolationValPlus*(VTC[7] - VTC[4]).Normalized()*CUBE_DIMENSIONS[1];
06491                                 }
06492                                 else {
06493                                     //
06494                                     // Interpotation Data
06495                                     //interpolationValNeg = 1.0 - VIntp[7].GetY();
06496                                     //
06497                                     vertexList[ 7] = V[7] - interpolationValPlus*(V[7] - V[4]).Normalized()*CUBE_DIMENSIONS[1];
06498                                     grad[7]        = G[7] - interpolationValPlus*(G[7] - G[4]).Normalized()*CUBE_DIMENSIONS[1];
06499                                     texCoordList[7] = VTC[7] - interpolationValPlus*(VTC[7] - VTC[4]).Normalized()*CUBE_DIMENSIONS[1];
06500                                 }
06501                                 grad[7].Normalized();
06502                             }
06503                             if ( EdgeTable[cubeClass] & 256 ) {
06504                                 if ( dataForPosition[idx[0] + 3] > threshold ) {
06505                                     //
06506                                     // Interpotation Data
06507                                     //interpolationValPlus = VIntp[0].GetZ();
06508                                     //
06509                                     vertexList[ 8] = V[0] + interpolationValPlus*(V[4] - V[0]).Normalized()*CUBE_DIMENSIONS[2];
06510                                     grad[8]        = G[0] + interpolationValPlus*(G[4] - G[0]).Normalized()*CUBE_DIMENSIONS[2];
06511                                     texCoordList[8] = VTC[0] + interpolationValPlus*(VTC[4] - VTC[0]).Normalized()*CUBE_DIMENSIONS[2];
06512                                 }
06513                                 else {
06514                                     //
06515                                     // Interpotation Data
06516                                     //interpolationValNeg = 1.0 - VIntp[4].GetZ();
06517                                     //
06518                                     vertexList[ 8] = V[4] - interpolationValPlus*(V[4] - V[0]).Normalized()*CUBE_DIMENSIONS[2];
06519                                     grad[8]        = G[4] - interpolationValPlus*(G[4] - G[0]).Normalized()*CUBE_DIMENSIONS[2];
06520                                     texCoordList[8] = VTC[4] - interpolationValPlus*(VTC[4] - VTC[0]).Normalized()*CUBE_DIMENSIONS[2];
06521                                 }
06522                                 grad[8].Normalized();
06523                             }
06524                             if ( EdgeTable[cubeClass] & 512 ) {
06525                                 if ( dataForPosition[idx[1] + 3] > threshold ) {
06526                                     //
06527                                     // Interpotation Data
06528                                     //interpolationValPlus = VIntp[1].GetZ();
06529                                     //
06530                                     vertexList[ 9] = V[1] + interpolationValPlus*(V[5] - V[1]).Normalized()*CUBE_DIMENSIONS[2];
06531                                     grad[9]        = G[1] + interpolationValPlus*(G[5] - G[1]).Normalized()*CUBE_DIMENSIONS[2];
06532                                     texCoordList[9] = VTC[1] + interpolationValPlus*(VTC[5] - VTC[1]).Normalized()*CUBE_DIMENSIONS[2];
06533                                 }
06534                                 else {
06535                                     //
06536                                     // Interpotation Data
06537                                     //interpolationValNeg = 1.0 - VIntp[5].GetZ();
06538                                     //
06539                                     vertexList[ 9] = V[5] - interpolationValPlus*(V[5] - V[1]).Normalized()*CUBE_DIMENSIONS[2];
06540                                     grad[9]        = G[5] - interpolationValPlus*(G[5] - G[1]).Normalized()*CUBE_DIMENSIONS[2];
06541                                     texCoordList[9] = VTC[5] - interpolationValPlus*(VTC[5] - VTC[1]).Normalized()*CUBE_DIMENSIONS[2];
06542                                 }
06543                                 grad[9].Normalized();
06544                             }
06545                             if ( EdgeTable[cubeClass] & 1024 ) {
06546                                 if ( dataForPosition[idx[2] + 3] > threshold ) {
06547                                     //
06548                                     // Interpotation Data
06549                                     //interpolationValPlus = VIntp[2].GetZ();
06550                                     //
06551                                     vertexList[10] = V[2] + interpolationValPlus*(V[6] - V[2]).Normalized()*CUBE_DIMENSIONS[2];
06552                                     grad[10]       = G[2] + interpolationValPlus*(G[6] - G[2]).Normalized()*CUBE_DIMENSIONS[2];
06553                                     texCoordList[10] = VTC[2] + interpolationValPlus*(VTC[6] - VTC[2]).Normalized()*CUBE_DIMENSIONS[2];
06554                                 }
06555                                 else {
06556                                     //
06557                                     // Interpotation Data
06558                                     //interpolationValNeg = 1.0 - VIntp[6].GetZ();
06559                                     //
06560                                     vertexList[10] = V[6] - interpolationValPlus*(V[6] - V[2]).Normalized()*CUBE_DIMENSIONS[2];
06561                                     grad[10]       = G[6] - interpolationValPlus*(G[6] - G[2]).Normalized()*CUBE_DIMENSIONS[2];
06562                                     texCoordList[10] = VTC[6] - interpolationValPlus*(VTC[6] - VTC[2]).Normalized()*CUBE_DIMENSIONS[2];
06563                                 }
06564                                 grad[10].Normalized();
06565                             }
06566                             if ( EdgeTable[cubeClass] & 2048 ) {
06567                                 if ( dataForPosition[idx[3] + 3] > threshold ) {
06568                                     //
06569                                     // Interpotation Data
06570                                     //interpolationValPlus = VIntp[3].GetZ();
06571                                     //
06572                                     vertexList[11] = V[3] + interpolationValPlus*(V[7] - V[3]).Normalized()*CUBE_DIMENSIONS[2];
06573                                     grad[11]       = G[3] + interpolationValPlus*(G[7] - G[3]).Normalized()*CUBE_DIMENSIONS[2];
06574                                     texCoordList[11] = VTC[3] + interpolationValPlus*(VTC[7] - VTC[3]).Normalized()*CUBE_DIMENSIONS[2];
06575                                 }
06576                                 else {
06577                                     //
06578                                     // Interpotation Data
06579                                     //interpolationValNeg = 1.0 - VIntp[7].GetZ();
06580                                     //
06581                                     vertexList[11] = V[7] - interpolationValPlus*(V[7] - V[3]).Normalized()*CUBE_DIMENSIONS[2];
06582                                     grad[11]       = G[7] - interpolationValPlus*(G[7] - G[3]).Normalized()*CUBE_DIMENSIONS[2];
06583                                     texCoordList[11] = VTC[7] - interpolationValPlus*(VTC[7] - VTC[3]).Normalized()*CUBE_DIMENSIONS[2];
06584                                 }
06585                                 grad[11].Normalized();
06586                             }
06587                             //---------------------------------------
06588                             //=======================================
06589                             //***************************************
06590                             //*/
06591 
06592 
06593 
06594                         } // END: Use Global Interpolation Value
06595                         //-----------------------------------------------
06596                         // END: if/else statement for MC Interpolation
06597                         //===============================================
06598 
06599                         //===============================================
06600                         //-----------------------------------------------
06601                         // Generate Triangle Index
06602                         numOfTrisPerCell = 0;
06603                         for ( d = 0; TriTable[cubeClass][d] != -1; ++d ) {
06604                             triIndices[d] = TriTable[cubeClass][d];
06605                         }
06606                         numOfTrisPerCell = d / 3;
06607                         //-----------------------------------------------
06608                         //===============================================
06609                     }
06610 
06611                     /*
06612                     //-------------------------------
06613                     // Draw (Gradient) Normals
06614                     {
06615                         glPushAttrib( GL_ALL_ATTRIB_BITS );
06616                         glBegin( GL_LINES );
06617                         glDisable( GL_TEXTURE_3D );
06618                         glDisable( GL_BLEND );
06619                         glDisable( GL_LIGHTING );
06620                         for ( int i = 0; i < numOfTrisPerCell*3; i+=3 ) {
06621                             glColor3f( 1, 0, 0 );
06622                             glVertex3fv( vertexList[triIndices[i+2]].GetDataFloat() );
06623                             glVertex3fv( ( vertexList[triIndices[i+2]] + grad[triIndices[i+2]]*0.15 ).GetDataFloat() );
06624                             glColor3f( 0, 1, 0 );
06625                             glVertex3fv( vertexList[triIndices[i+1]].GetDataFloat() );
06626                             glVertex3fv( ( vertexList[triIndices[i+1]] + grad[triIndices[i+1]]*0.15 ).GetDataFloat() );
06627                             glColor3f( 0, 0, 1 );
06628                             glVertex3fv( vertexList[triIndices[i+0]].GetDataFloat() );
06629                             glVertex3fv( ( vertexList[triIndices[i+0]] + grad[triIndices[i+0]]*0.15 ).GetDataFloat() );
06630                         }
06631                         glEnd();
06632                         glPopAttrib();
06633                     }
06634                     //-------------------------------
06635                     //*/
06636 
06637                     //-------------------------------
06638                     // Draw PN-Triangles generated 
06639                     // from Marching Cube Triangles
06640                     if ( m_bDrawPNTriangle ) {
06641                         int i = 0;
06642                         while ( numOfTrisPerCell > 0 ) {
06643                             PNTriangle<T>::DrawByOpenGL( 
06644                                 vertexList[triIndices[i+2]], 
06645                                 vertexList[triIndices[i+1]], 
06646                                 vertexList[triIndices[i+0]], 
06647                                 grad[triIndices[i+2]], 
06648                                 grad[triIndices[i+1]], 
06649                                 grad[triIndices[i+0]], 
06650                                 texCoordList[triIndices[i+2]], 
06651                                 texCoordList[triIndices[i+1]], 
06652                                 texCoordList[triIndices[i+0]], 
06653                                 m_PNTriangleSmoothness 
06654                             );
06655                             --numOfTrisPerCell;
06656                             i += 3;
06657                         }
06658                     }
06659                     // DEBUG for PNTri Drawing
06660                     //glUseProgramObjectARB( g_PNTriProg );
06661                     //
06662                     // DEBUG for PNTri Drawing
06663                     //glUseProgramObjectARB( NULL );
06664 
06665                     //-------------------------------
06666                     // Draw Marching Cube Triangles
06667                     else {
06668 
06669                         /*
06670                         //*************************************
06671                         // DEBUG
06672                         {
06673                             glPushAttrib( GL_ALL_ATTRIB_BITS );
06674                             glDisable( GL_LIGHTING );
06675                             glDisable( GL_BLEND );
06676                             glDisable( GL_TEXTURE_3D );
06677                             glColor3ub( 200, 50, 100 );
06678                             //glColor3f( 0.0, 0.0, 1.0 );
06679                             glBegin( GL_POINTS );
06680                             int count = numOfTrisPerCell;
06681                             int i = 0;
06682                             while ( count > 0 ) {
06683                                 //glTexCoord3fv( texCoordList[triIndices[i+2]].GetDataFloat() );
06684                                 glColor3f( 1, 0, 0 );
06685                                 glNormal3fv( grad[triIndices[i+2]].GetDataFloat() );
06686                                 glVertex3fv( vertexList[triIndices[i+2]].GetDataFloat() );
06687                                 //
06688                                 //glTexCoord3fv( texCoordList[triIndices[i+1]].GetDataFloat() );
06689                                 glColor3f( 0, 1, 0 );
06690                                 glNormal3fv( grad[triIndices[i+1]].GetDataFloat() );
06691                                 glVertex3fv( vertexList[triIndices[i+1]].GetDataFloat() );
06692                                 //
06693                                 //glTexCoord3fv( texCoordList[triIndices[i+0]].GetDataFloat() );
06694                                 glColor3f( 0, 0, 1 );
06695                                 glNormal3fv( grad[triIndices[i+0]].GetDataFloat() );
06696                                 glVertex3fv( vertexList[triIndices[i+0]].GetDataFloat() );
06697                                 //
06698                                 --count;
06699                                 i += 3;
06700                             }
06701                             glEnd();
06702                             glPopAttrib();
06703                         }
06704                         //*************************************
06705                         //*/
06706 
06707                         //glBegin( GL_LINE_LOOP );
06708                         glBegin( GL_TRIANGLES );
06709                         int i = 0;
06710                         while ( numOfTrisPerCell > 0 ) {
06711                             glTexCoord3fv( texCoordList[triIndices[i+2]].GetDataFloat() );
06712                             glNormal3fv( grad[triIndices[i+2]].GetDataFloat() );
06713                             glVertex3fv( vertexList[triIndices[i+2]].GetDataFloat() );
06714                             //
06715                             glTexCoord3fv( texCoordList[triIndices[i+1]].GetDataFloat() );
06716                             glNormal3fv( grad[triIndices[i+1]].GetDataFloat() );
06717                             glVertex3fv( vertexList[triIndices[i+1]].GetDataFloat() );
06718                             //
06719                             glTexCoord3fv( texCoordList[triIndices[i+0]].GetDataFloat() );
06720                             glNormal3fv( grad[triIndices[i+0]].GetDataFloat() );
06721                             glVertex3fv( vertexList[triIndices[i+0]].GetDataFloat() );
06722                             //
06723                             --numOfTrisPerCell;
06724                             i += 3;
06725                         }
06726                         glEnd();
06727                     }
06728 
06729                     //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
06731                     //if ( isFirstRun ) {
06732                     //  if ( cubeClass < 100 )  std::cout << " ";
06733                     //  if ( cubeClass < 10 )   std::cout << " ";
06734                     //  std::cout << "  " << static_cast<int>( cubeClass );
06735                     //}
06736                     //#endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
06737 
06738                     // Restore Reloaded Threshold Values
06739                     for ( int R = 0; R < 8; ++R ) {
06740                         dataForPosition[idx[R] + 3] = reloadedThresholdValues[R];
06741                     }
06742 
06743                 } // END: // TWO ITERATIONS FOR LEFT AND RIGHT CUBE CONFIGURATIONS
06744 
06745                 //-------------------------------
06746                 // Next Cell
06747                 for ( i = 0; i < 8; ++i ) {
06748                     idx[i] += 4;
06749                     //
06750                     intpIdx[i] += 3;
06751                 }
06752                 //-------------------------------
06753 
06754             } // END: for loop for X
06755 
06756             //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
06758             //if ( isFirstRun ) std::cout << "\n";
06759             //#endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
06760 
06761         } // END: for loop for Y
06762 
06763         //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
06765         //if ( isFirstRun ) std::cout << "\n";
06766         //#endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
06767 
06768     } // END: for loop for Z
06769 
06770     //#ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
06772     //if ( isFirstRun ) {
06773     //  std::cout << "-------------------------\n";
06774     //}
06776     //#endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_GPU_MC_WITH_TRACKING_CUBES
06777 
06778     glPopAttrib();
06779 
06780     #ifdef TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
06781     m_glslProgramObjectForShaderSurfaceFromSW->EndGLSL();
06782     #endif // TAPs_MODEL_DEFORMABLE_VISUALIZATION_RT_GEN_MESH_USE_SHADER_FOR_SHADING_SURFACE_FROM_SW
06783     //---------------------------------------------------------------
06784     //===============================================================
06785     //*/
06786 
06787     //---------------------------------------------------------------
06788     //===============================================================
06789     delete [] dataForPosition;
06790     delete [] dataForEllipsoidInfluence;
06791 }
06792 //-----------------------------------------------------------------------------
06793 // END: DrawByGL
06794 //*****************************************************************************
06795 
06796 
06797 //-----------------------------------------------------------------------------
06798 //=============================================================================
06799 END_NAMESPACE_TAPs__OpenGL
06800 //-----------------------------------------------------------------------------
06801 //34567890123456789012345678901234567890123456789012345678901234567890123456789
06802 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines