![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 OpenGL3DModel.cpp 00003 00004 An OpenGL 3D Texture Model 00005 00006 SUKITTI PUNAK (04/28/2007) 00007 UPDATE (04/29/2007) 00008 ******************************************************************************/ 00009 #include "TAPsOpenGL3DModel.hpp" 00010 // Using Inclusion Model (i.e. definitions are included in declarations) 00011 // (this name.cpp is included in name.hpp) 00012 // Each friend is defined directly inside its declaration. 00013 00014 BEGIN_NAMESPACE_TAPs__OpenGL 00015 //============================================================================= 00016 //----------------------------------------------------------------------------- 00017 // Default Constructor 00018 template <typename T> 00019 OpenGL3DModel<T>::OpenGL3DModel () : OpenGLModel<T>() 00020 { 00021 Default(); 00022 00023 #ifdef TAPs_DEBUG_MODE 00024 std::cout << "OpenGL3DModel<" << typeid(T).name() << "> constructor\n"; 00025 #endif//TAPs_DEBUG_MODE 00026 } 00027 //----------------------------------------------------------------------------- 00028 // Destructor 00029 template <typename T> 00030 OpenGL3DModel<T>::~OpenGL3DModel () 00031 { 00032 //--------------------------------------------------------------- 00033 // Delete The 3D Texture 00034 if ( m_gluiName3DTexture > 0 ) { 00035 glDeleteTextures( 1, &m_gluiName3DTexture ); 00036 } 00037 //--------------------------------------------------------------- 00038 00039 #ifdef TAPs_DEBUG_MODE 00040 std::cout << "OpenGL3DModel<" << typeid(T).name() << "> destructor\n"; 00041 #endif//TAPs_DEBUG_MODE 00042 } 00043 //----------------------------------------------------------------------------- 00044 // Default 00045 template <typename T> 00046 void OpenGL3DModel<T>::Default () 00047 { 00048 m_gluiName3DTexture = 0; 00049 m_3dt_target = GL_TEXTURE_3D; 00050 m_3dt_level = 0; 00051 m_3dt_internalFormat = GL_RGBA32F_ARB; 00052 m_3dt_size[0] = m_3dt_size[1] = m_3dt_size[2] = 0; 00053 m_3dt_border = 0; 00054 m_3dt_pixelFormat = GL_RGBA; 00055 m_3dt_dataType = GL_FLOAT; 00056 } 00057 //----------------------------------------------------------------------------- 00058 // Initialize 00059 template <typename T> 00060 void OpenGL3DModel<T>::Initialize () 00061 {} 00062 //----------------------------------------------------------------------------- 00063 // SetData 00064 template <typename T> 00065 bool OpenGL3DModel<T>::SetData ( int sizeX, int sizeY, int sizeZ, unsigned char * pr3DRawData ) 00066 { 00067 //--------------------------------------------------------------- 00068 // Delete The 3D Texture 00069 if ( m_gluiName3DTexture > 0 ) { 00070 glDeleteTextures( 1, &m_gluiName3DTexture ); 00071 m_gluiName3DTexture = 0; 00072 } 00073 //------------- 00074 m_3dt_target = GL_TEXTURE_3D; 00075 m_3dt_level = 0; 00076 //m_3dt_internalFormat = GL_RGBA32F_ARB; 00077 m_3dt_internalFormat = GL_RGBA; 00078 //------------- 00079 m_3dt_size[0] = sizeX; 00080 m_3dt_size[1] = sizeY; 00081 m_3dt_size[2] = sizeZ; 00082 //------------- 00083 m_3dt_border = 0; 00084 m_3dt_pixelFormat = GL_RGBA; 00085 m_3dt_dataType = GL_UNSIGNED_BYTE; 00086 //m_3dt_dataType = GL_FLOAT; 00087 //----------------------------------------------------- 00088 int totalSize = sizeX*sizeY*sizeZ; 00089 //----------------------------------------------------- 00090 // Set bounding size 00091 // Min Point 00092 for ( int i = 0; i < 3; ++i ) { 00093 double half = m_3dt_size[i] / 2.0; 00094 m_paBoundingAABB[0][i] = -half; // Min Pt 00095 m_paBoundingAABB[1][i] = half; // Max Pt 00096 } 00097 //----------------------------------------------------- 00098 GLubyte * data = new GLubyte[ totalSize * 4 ]; 00099 //GLfloat * data = new GLfloat[ totalSize * 4 ]; 00100 00101 // DEBUG 00102 //for ( int i = 0; i < totalSize * 4; ++i ) { 00103 // data[i] = 0; 00104 //} 00105 00106 int count = 0; 00107 unsigned char minThreshold = 50; // 0 --> min 00108 unsigned char maxThreshold = 200; // 255 --> max 00109 for ( int i = 0; i < totalSize; ++i ) { 00110 if ( minThreshold <= pr3DRawData[i] && pr3DRawData[i] <= maxThreshold ) { 00111 data[count++] = 135; 00112 data[count++] = 135; 00113 data[count++] = 145; 00114 data[count++] = pr3DRawData[i]; 00115 } 00116 else { 00117 data[count++] = 0; 00118 data[count++] = 0; 00119 data[count++] = 0; 00120 data[count++] = 0; 00121 } 00122 } 00123 //----------------------------------------------------- 00124 glGenTextures( 1, &m_gluiName3DTexture ); 00125 glBindTexture( m_3dt_target, m_gluiName3DTexture ); 00126 //------------------------------------------- 00127 glTexImage3D( 00128 m_3dt_target, // target 00129 m_3dt_level, // level 00130 m_3dt_internalFormat, // internal format 00131 //GL_FLOAT_RGBA32_NV, // internal format (doesn't work!) 00132 //GL_RGBA_FLOAT32_ATI, // internal format (doesn't work!) 00133 m_3dt_size[0], // width 00134 m_3dt_size[1], // height 00135 m_3dt_size[2], // depth 00136 m_3dt_border, // border 00137 m_3dt_pixelFormat, // pixel format 00138 m_3dt_dataType, // data type 00139 data // texels 00140 ); 00141 //------------------------------------------- 00142 glBindTexture( m_3dt_target, 0 ); 00143 TAPs::OpenGL::Fn::CHECK_GL_ERROR(); 00144 //----------------------------------------------------- 00145 delete [] data; 00146 //----------------------------------------------------- 00147 #ifdef TAPs_OPENGL_3D_MODEL_DEBUG 00148 PrintDataInThe3DTexture(); 00149 #endif 00150 //----------------------------------------------------- 00151 return true; 00152 } 00153 //----------------------------------------------------------------------------- 00154 // GetMaxHalfLength 00155 template <typename T> 00156 T OpenGL3DModel<T>::GetMaxHalfLength () const 00157 { 00158 return 1; 00159 } 00160 //----------------------------------------------------------------------------- 00161 // Display By OpenGL 00162 template <typename T> 00163 void OpenGL3DModel<T>::DrawGL ( GLenum drawMode = GL_QUADS ) 00164 { 00165 //--------------------------------------------------------------- 00166 // Draw The 3D Texture 00167 if ( m_gluiName3DTexture ) { 00168 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT ); 00169 glEnable( GL_LIGHTING ); 00170 //glDisable( GL_LIGHTING ); 00171 //glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE ); 00172 //--------------------------------------- 00173 // Set Color Material 00174 GLfloat vAmbient[4] = { 0.2, 0.2, 0.2, 1.0 }; 00175 GLfloat vDiffuse[4] = { 0.8, 0.8, 0.8, 1.0 }; 00176 GLfloat vSpecular[4] = { 0.2, 0.2, 0.2, 1.0 }; 00177 GLfloat vEmission[4] = { 0.0, 0.0, 0.0, 1.0 }; 00178 GLfloat vShininess[1] = { 128 }; 00179 glMaterialfv( GL_FRONT, GL_AMBIENT, vAmbient ); 00180 glMaterialfv( GL_FRONT, GL_DIFFUSE, vDiffuse ); 00181 glMaterialfv( GL_FRONT, GL_SPECULAR, vSpecular ); 00182 glMaterialfv( GL_FRONT, GL_EMISSION, vEmission ); 00183 glMaterialfv( GL_FRONT, GL_SHININESS, vShininess ); 00184 //--------------------------------------- 00185 //glEnable( GL_COLOR_MATERIAL ); 00186 //glColor3f( 0.5, 0.5, 0.5 ); 00187 glEnable( GL_BLEND ); 00188 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); 00189 //glActiveTexture( GL_TEXTURE0 ); 00190 glEnable( GL_TEXTURE_3D ); 00191 //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); 00192 //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL ); 00193 glBindTexture( GL_TEXTURE_3D, m_gluiName3DTexture ); 00194 //glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); 00195 //glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); 00196 //glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE ); 00197 glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP ); 00198 glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP ); 00199 glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP ); 00200 //glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); 00201 //glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00202 glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 00203 glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); 00204 00205 Vector3<T> vAABBMinPt = GetBoundingAABBLowPoint(); 00206 Vector3<T> vAABBMaxPt = GetBoundingAABBHighPoint(); 00207 T scale = (vAABBMaxPt[0] - vAABBMinPt[0]) / 3; 00208 for ( int i = 0; i < 3; ++i ) { 00209 vAABBMaxPt[i] /= scale; 00210 vAABBMinPt[i] /= scale; 00211 } 00212 //----------------------------------------------------------- 00213 glBegin( GL_QUADS ); 00214 //----------------------------------------------------------- 00215 // DrawSliceStack_NegativeZ(); // back-to-front is from -z to +z 00216 //if ( iDrawDir == -3 ) { 00217 if ( true ) { 00218 int iSubdivision = 1; 00219 int iAxis = 2; 00220 T zValue = vAABBMinPt[iAxis]; 00221 T numberOfSlices = m_3dt_size[iAxis] * iSubdivision; 00222 T zStep = (vAABBMaxPt[iAxis] - vAABBMinPt[iAxis] ) / (numberOfSlices-1); 00223 T tValue = 0; 00224 T tStep = 1.0 / (numberOfSlices-1); 00225 for ( int i = 0; i < numberOfSlices; ++i ) 00226 { 00227 glTexCoord3f( 0, 0, tValue ); 00228 glNormal3f( 0, 0, 1 ); 00229 glVertex3f( vAABBMinPt[0], vAABBMinPt[1], zValue ); 00230 // 00231 glTexCoord3f( 1, 0, tValue ); 00232 glNormal3f( 0, 0, 1 ); 00233 glVertex3f( vAABBMaxPt[0], vAABBMinPt[1], zValue ); 00234 // 00235 glTexCoord3f( 1, 1, tValue ); 00236 glNormal3f( 0, 0, 1 ); 00237 glVertex3f( vAABBMaxPt[0], vAABBMaxPt[1], zValue ); 00238 // 00239 glTexCoord3f( 0, 1, tValue ); 00240 glNormal3f( 0, 0, 1 ); 00241 glVertex3f( vAABBMinPt[0], vAABBMaxPt[1], zValue ); 00242 // 00243 zValue += zStep; 00244 tValue += tStep; 00245 } 00246 } 00247 glEnd(); 00248 glBindTexture( GL_TEXTURE_3D, 0 ); 00249 //glutSolidTeapot( 1.0 ); 00250 } 00251 //--------------------------------------------------------------- 00252 } 00253 //----------------------------------------------------------------------------- 00254 #ifdef TAPs_OPENGL_3D_MODEL_DEBUG 00255 // Display By OpenGL 00256 // Get data from 3D texture and print them out 00257 template <typename T> 00258 void OpenGL3DModel<T>::PrintDataInThe3DTexture () 00259 { 00260 if ( m_gluiName3DTexture == 0 ) return; 00261 //--------------------------------------------------------------- 00262 //--------------------------------------------------------------- 00263 GLsizei numPoints = m_3dt_size[0] * m_3dt_size[1] * m_3dt_size[2] * 4; 00264 //--------------------------------------------------------------- 00265 glPushAttrib( GL_ALL_ATTRIB_BITS ); 00266 //----------------------------------------------------- 00267 GLubyte * data = new GLubyte[ numPoints ]; 00268 //GLfloat * data = new GLfloat[ numPoints ]; 00269 //================================================= 00270 // Read data for positions from the 3D texture to memory 00271 glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 ); 00272 glBindTexture( m_3dt_target, m_gluiName3DTexture ); 00273 glGetTexImage( 00274 m_3dt_target, 00275 m_3dt_level, 00276 m_3dt_pixelFormat, 00277 m_3dt_dataType, 00278 data 00279 ); 00280 TAPs::OpenGL::Fn::CHECK_GL_ERROR(); 00281 //=================================== 00282 //--------------------------------------------------------------- 00283 TAPs::OpenGL::Fn::CHECK_GL_ERROR(); 00284 //----------------------------------------------------- 00285 glPopAttrib(); 00286 //--------------------------------------------------------------- 00287 // Print Data 00288 std::cout << "numPoints: " << numPoints/4 << "\n"; 00289 for ( int i = 0; i < numPoints; i+=4 ) { 00290 std::cout << (i/4) << ": " 00291 << (unsigned int)data[i] << ", " 00292 << (unsigned int)data[i+1] << ", " 00293 << (unsigned int)data[i+2] << ", " 00294 << (unsigned int)data[i+3] << "\n"; 00295 } 00296 //--------------------------------------------------------------- 00297 delete [] data; 00298 //--------------------------------------------------------------- 00299 } 00300 #endif 00301 //----------------------------------------------------------------------------- 00302 //============================================================================= 00303 END_NAMESPACE_TAPs__OpenGL 00304 //----------------------------------------------------------------------------- 00305 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00306 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8