![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsPNTriangle.hpp 00003 00004 A static class for PNTriangle (with option drawing by OpenGL). 00005 00006 SUKITTI PUNAK (06/12/2007) 00007 UPDATE (06/12/2007) 00008 ******************************************************************************/ 00009 #include "TAPsPNTriangle.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 00015 //============================================================================= 00016 //----------------------------------------------------------------------------- 00017 template <typename T> Vector3<T> PNTriangle<T>::m_coeffs[10]; 00018 template <typename T> Vector3<T> PNTriangle<T>::m_normals[6]; 00019 template <typename T> Vector3<T> PNTriangle<T>::m_texcoords[10]; 00020 //----------------------------------------------------------------------------- 00021 //============================================================================= 00022 00023 //============================================================================= 00024 //----------------------------------------------------------------------------- 00025 template <typename T> 00026 void PNTriangle<T>::CalculatePNTriangleCoefficients ( 00027 Vector3<T> const & P1, 00028 Vector3<T> const & P2, 00029 Vector3<T> const & P3, 00030 Vector3<T> const & N1, 00031 Vector3<T> const & N2, 00032 Vector3<T> const & N3 ) 00033 { 00034 // Coefficients or Control Points 00035 // b003 9 00036 // / \ 00037 // / \ 00038 // b102 b012 7 8 00039 // / \ <===> 00040 // / \ 00041 // b201 b111 b021 4 5 6 00042 // / \ 00043 // / \ 00044 // b300 ---- b210 ---- b120 ---- b030 0 1 2 3 00045 Vector3<T> sum; 00046 T w12, w21, w23, w32, w31, w13; 00047 // Calculate w12, w23, and w31 00048 w12 = ( P2 - P1 ) * N1; 00049 w21 = ( P1 - P2 ) * N2; 00050 w23 = ( P3 - P2 ) * N2; 00051 w32 = ( P2 - P3 ) * N3; 00052 w31 = ( P1 - P3 ) * N3; 00053 w13 = ( P3 - P1 ) * N1; 00054 // b300 00055 m_coeffs[0] = P1; 00056 // b030 00057 m_coeffs[3] = P2; 00058 // b003 00059 m_coeffs[9] = P3; 00060 // b210 00061 sum = m_coeffs[1] = (2*P1 + P2 - w12*N1) / 3.0; 00062 // b120 00063 sum += m_coeffs[2] = (2*P2 + P1 - w21*N2) / 3.0; 00064 // b021 00065 sum += m_coeffs[6] = (2*P2 + P3 - w23*N2) / 3.0; 00066 // b012 00067 sum += m_coeffs[8] = (2*P3 + P2 - w32*N3) / 3.0; 00068 // b102 00069 sum += m_coeffs[7] = (2*P3 + P1 - w31*N3) / 3.0; 00070 // b201 00071 sum += m_coeffs[4] = (2*P1 + P3 - w13*N1) / 3.0; 00072 // b111 00073 sum /= 6.0; 00074 m_coeffs[5] = (P1 + P2 + P3) / 3.0; 00075 m_coeffs[5] = sum + (sum - m_coeffs[5]) / 2.0; 00076 } 00077 //----------------------------------------------------------------------------- 00078 template <typename T> 00079 void PNTriangle<T>::CalculatePNTriangleNormals ( 00080 Vector3<T> const & P1, 00081 Vector3<T> const & P2, 00082 Vector3<T> const & P3, 00083 Vector3<T> const & N1, 00084 Vector3<T> const & N2, 00085 Vector3<T> const & N3 ) 00086 { 00087 // Normals 00088 // n002 5 00089 // / \ 00090 // / \ 00091 // n101 n011 <===> 3 4 00092 // / \ 00093 // / \ 00094 // n200 ---- n110 ---- n020 0 1 2 00095 Vector3<T> p12(P2 - P1); 00096 Vector3<T> p23(P3 - P2); 00097 Vector3<T> p31(P1 - P3); 00098 Vector3<T> n12(N2 + N1); 00099 Vector3<T> n23(N3 + N2); 00100 Vector3<T> n31(N1 + N3); 00101 T v12 = 2.0 * (p12 * n12) / (p12 * p12); 00102 T v23 = 2.0 * (p23 * n23) / (p23 * p23); 00103 T v31 = 2.0 * (p31 * n31) / (p31 * p31); 00104 // n200 00105 m_normals[0] = N1; 00106 // n020 00107 m_normals[2] = N2; 00108 // n002 00109 m_normals[5] = N3; 00110 // n110 00111 m_normals[1] = (n12 - v12*p12).Normalized(); 00112 // n101 00113 m_normals[3] = (n23 - v23*p23).Normalized(); 00114 // n011 00115 m_normals[4] = (n31 - v31*p31).Normalized(); 00116 } 00117 //----------------------------------------------------------------------------- 00118 //============================================================================= 00119 00120 //============================================================================= 00121 // With Texture Coordinates 00122 //----------------------------------------------------------------------------- 00123 template <typename T> 00124 void PNTriangle<T>::CalculatePNTriangleCoefficients ( 00125 Vector3<T> const & P1, 00126 Vector3<T> const & P2, 00127 Vector3<T> const & P3, 00128 Vector3<T> const & N1, 00129 Vector3<T> const & N2, 00130 Vector3<T> const & N3, 00131 Vector3<T> const & TC1, 00132 Vector3<T> const & TC2, 00133 Vector3<T> const & TC3 ) 00134 { 00135 // Coefficients or Control Points 00136 // b003 9 00137 // / \ 00138 // / \ 00139 // b102 b012 7 8 00140 // / \ <===> 00141 // / \ 00142 // b201 b111 b021 4 5 6 00143 // / \ 00144 // / \ 00145 // b300 ---- b210 ---- b120 ---- b030 0 1 2 3 00146 Vector3<T> sum, sumTC; 00147 // Calculate w12, w23, and w31 00148 T w12 = ( P2 - P1 ) * N1; 00149 T w21 = ( P1 - P2 ) * N2; 00150 T w23 = ( P3 - P2 ) * N2; 00151 T w32 = ( P2 - P3 ) * N3; 00152 T w31 = ( P1 - P3 ) * N3; 00153 T w13 = ( P3 - P1 ) * N1; 00154 // 00155 T tc12 = ( TC2 - TC1 ) * N1; 00156 T tc21 = ( TC1 - TC2 ) * N2; 00157 T tc23 = ( TC3 - TC2 ) * N2; 00158 T tc32 = ( TC2 - TC3 ) * N3; 00159 T tc31 = ( TC1 - TC3 ) * N3; 00160 T tc13 = ( TC3 - TC1 ) * N1; 00161 // b300 00162 m_coeffs[0] = P1; 00163 m_texcoords[0] = TC1; 00164 // b030 00165 m_coeffs[3] = P2; 00166 m_texcoords[3] = TC2; 00167 // b003 00168 m_coeffs[9] = P3; 00169 m_texcoords[9] = TC3; 00170 // b210 00171 sum = m_coeffs[1] = (2*P1 + P2 - w12*N1) / 3.0; 00172 sumTC = m_texcoords[1] = (2*TC1 + TC2 - tc12*N1) / 3.0; 00173 // b120 00174 sum += m_coeffs[2] = (2*P2 + P1 - w21*N2) / 3.0; 00175 sumTC += m_texcoords[2] = (2*TC2 + TC1 - w21*N2) / 3.0; 00176 // b021 00177 sum += m_coeffs[6] = (2*P2 + P3 - w23*N2) / 3.0; 00178 sumTC += m_texcoords[6] = (2*TC2 + TC3 - tc23*N2) / 3.0; 00179 // b012 00180 sum += m_coeffs[8] = (2*P3 + P2 - w32*N3) / 3.0; 00181 sumTC += m_texcoords[8] = (2*TC3 + TC2 - tc32*N3) / 3.0; 00182 // b102 00183 sum += m_coeffs[7] = (2*P3 + P1 - w31*N3) / 3.0; 00184 sumTC += m_texcoords[7] = (2*TC3 + TC1 - tc31*N3) / 3.0; 00185 // b201 00186 sum += m_coeffs[4] = (2*P1 + P3 - w13*N1) / 3.0; 00187 sumTC += m_texcoords[4] = (2*TC1 + TC3 - tc13*N1) / 3.0; 00188 // b111 00189 sum /= 6.0; 00190 m_coeffs[5] = (P1 + P2 + P3) / 3.0; 00191 m_coeffs[5] = sum + (sum - m_coeffs[5]) / 2.0; 00192 // 00193 sumTC /= 6.0; 00194 m_texcoords[5] = (TC1 + TC2 + TC3) / 3.0; 00195 m_texcoords[5] = sumTC + (sumTC - m_texcoords[5]) / 2.0; 00196 } 00197 //----------------------------------------------------------------------------- 00198 //============================================================================= 00199 00200 //***************************************************************************** 00201 #if defined(__gl_h_) || defined(__GL_H__) 00202 //============================================================================= 00203 // OpenGL Fns 00204 //----------------------------------------------------------------------------- 00205 // Without Texture Coordinates 00206 template <typename T> 00207 void PNTriangle<T>::DrawByOpenGL ( 00208 Vector3<T> const & P1, 00209 Vector3<T> const & P2, 00210 Vector3<T> const & P3, 00211 Vector3<T> const & N1, 00212 Vector3<T> const & N2, 00213 Vector3<T> const & N3, 00214 int smoothness ) 00215 { 00216 //----------------------------------------------------- 00217 CalculatePNTriangle( P1, P2, P3, N1, N2, N3 ); 00218 //----------------------------------------------------- 00219 int noOfVertices = smoothness*(smoothness+1)/2; 00220 float *vertex = new float[noOfVertices*3]; 00221 float *normal = new float[noOfVertices*3]; 00222 float uInc = 1.0 / (smoothness-1); 00223 float vInc = 1.0 / (smoothness-1); 00224 float uu, vv, ww; 00225 //----------------------------------------------------- 00226 int index = 0; 00227 float u = 0.0f, v = 0.0f, w; 00228 for ( int i = 0; i < smoothness; ++i, u += uInc ) { 00229 v = 0.0f; 00230 for ( int j = 0; j < smoothness; ++j, v += vInc ) { 00231 w = 1.0f - u - v; 00232 if ( w < -0.0000001f ) break; 00233 ww = w*w; 00234 uu = u*u; 00235 vv = v*v; 00236 T magnitude = 0.0; 00237 for ( int d = 0; d < 3; ++d, ++index ) { 00238 vertex[index] = m_coeffs[0][d]*ww*w + m_coeffs[3][d]*uu*u + m_coeffs[9][d]*vv*v 00239 + 3.0f*( m_coeffs[1][d]*ww*u + m_coeffs[2][d]*w*uu 00240 + m_coeffs[4][d]*ww*v + m_coeffs[6][d]*uu*v 00241 + m_coeffs[7][d]*w*vv + m_coeffs[8][d]*u*vv ) 00242 + m_coeffs[5][d]*6.0f*w*u*v; 00243 00244 normal[index] = m_normals[0][d]*ww + m_normals[2][d]*uu + m_normals[5][d]*vv 00245 + m_normals[1][d]*w*u + m_normals[3][d]*u*v + m_normals[4][d]*w*v; 00246 00247 magnitude += normal[index]*normal[index]; 00248 } 00249 magnitude = sqrt(magnitude); 00250 normal[index-3] /= magnitude; 00251 normal[index-2] /= magnitude; 00252 normal[index-1] /= magnitude; 00253 } 00254 } 00255 int row1 = 0, row2 = smoothness*3; 00256 for ( int r = 1; r < smoothness; ++r, row1+=3 ) { 00257 glBegin( GL_TRIANGLE_STRIP ); 00258 for ( int i = 0; i < smoothness-r; ++i, row1+=3, row2+=3 ) { 00259 glNormal3f( normal[row1], normal[row1+1], normal[row1+2] ); 00260 glVertex3f( vertex[row1], vertex[row1+1], vertex[row1+2] ); 00261 glNormal3f( normal[row2], normal[row2+1], normal[row2+2] ); 00262 glVertex3f( vertex[row2], vertex[row2+1], vertex[row2+2] ); 00263 } 00264 glNormal3f( normal[row1], normal[row1+1], normal[row1+2] ); 00265 glVertex3f( vertex[row1], vertex[row1+1], vertex[row1+2] ); 00266 glEnd(); 00267 } 00268 //----------------------------------------------------- 00269 delete [] vertex; 00270 delete [] normal; 00271 } 00272 //----------------------------------------------------------------------------- 00273 // With Texture Coordinates 00274 template <typename T> 00275 void PNTriangle<T>::DrawByOpenGL ( 00276 Vector3<T> const & P1, 00277 Vector3<T> const & P2, 00278 Vector3<T> const & P3, 00279 Vector3<T> const & N1, 00280 Vector3<T> const & N2, 00281 Vector3<T> const & N3, 00282 Vector3<T> const & TC1, 00283 Vector3<T> const & TC2, 00284 Vector3<T> const & TC3, 00285 int smoothness ) 00286 { 00287 //----------------------------------------------------- 00288 CalculatePNTriangle( P1, P2, P3, N1, N2, N3, TC1, TC2, TC3 ); 00289 //----------------------------------------------------- 00290 int noOfVertices = smoothness*(smoothness+1)/2; 00291 float *vertex = new float[noOfVertices*3]; 00292 float *normal = new float[noOfVertices*3]; 00293 float *texcoord = new float[noOfVertices*3]; 00294 float uInc = 1.0 / (smoothness-1); 00295 float vInc = 1.0 / (smoothness-1); 00296 float uu, vv, ww; 00297 //----------------------------------------------------- 00298 int index = 0; 00299 float u = 0.0f, v = 0.0f, w; 00300 for ( int i = 0; i < smoothness; ++i, u += uInc ) { 00301 v = 0.0f; 00302 for ( int j = 0; j < smoothness; ++j, v += vInc ) { 00303 w = 1.0f - u - v; 00304 if ( w < -0.0000001f ) break; 00305 ww = w*w; 00306 uu = u*u; 00307 vv = v*v; 00308 T magnitude = 0.0; 00309 for ( int d = 0; d < 3; ++d, ++index ) { 00310 vertex[index] = m_coeffs[0][d]*ww*w + m_coeffs[3][d]*uu*u + m_coeffs[9][d]*vv*v 00311 + 3.0f*( m_coeffs[1][d]*ww*u + m_coeffs[2][d]*w*uu 00312 + m_coeffs[4][d]*ww*v + m_coeffs[6][d]*uu*v 00313 + m_coeffs[7][d]*w*vv + m_coeffs[8][d]*u*vv ) 00314 + m_coeffs[5][d]*6.0f*w*u*v; 00315 00316 texcoord[index] = m_texcoords[0][d]*ww*w + m_texcoords[3][d]*uu*u + m_texcoords[9][d]*vv*v 00317 + 3.0f*( m_texcoords[1][d]*ww*u + m_texcoords[2][d]*w*uu 00318 + m_texcoords[4][d]*ww*v + m_texcoords[6][d]*uu*v 00319 + m_texcoords[7][d]*w*vv + m_texcoords[8][d]*u*vv ) 00320 + m_texcoords[5][d]*6.0f*w*u*v; 00321 00322 normal[index] = m_normals[0][d]*ww + m_normals[2][d]*uu + m_normals[5][d]*vv 00323 + m_normals[1][d]*w*u + m_normals[3][d]*u*v + m_normals[4][d]*w*v; 00324 00325 magnitude += normal[index]*normal[index]; 00326 } 00327 magnitude = sqrt(magnitude); 00328 normal[index-3] /= magnitude; 00329 normal[index-2] /= magnitude; 00330 normal[index-1] /= magnitude; 00331 } 00332 } 00333 int row1 = 0, row2 = smoothness*3; 00334 for ( int r = 1; r < smoothness; ++r, row1+=3 ) { 00335 glBegin( GL_TRIANGLE_STRIP ); 00336 for ( int i = 0; i < smoothness-r; ++i, row1+=3, row2+=3 ) { 00337 glTexCoord3f( texcoord[row1], texcoord[row1+1], texcoord[row1+2] ); 00338 glNormal3f( normal[row1], normal[row1+1], normal[row1+2] ); 00339 glVertex3f( vertex[row1], vertex[row1+1], vertex[row1+2] ); 00340 glTexCoord3f( texcoord[row2], texcoord[row2+1], texcoord[row2+2] ); 00341 glNormal3f( normal[row2], normal[row2+1], normal[row2+2] ); 00342 glVertex3f( vertex[row2], vertex[row2+1], vertex[row2+2] ); 00343 } 00344 glTexCoord3f( texcoord[row1], texcoord[row1+1], texcoord[row1+2] ); 00345 glNormal3f( normal[row1], normal[row1+1], normal[row1+2] ); 00346 glVertex3f( vertex[row1], vertex[row1+1], vertex[row1+2] ); 00347 glEnd(); 00348 } 00349 //----------------------------------------------------- 00350 delete [] vertex; 00351 delete [] normal; 00352 delete [] texcoord; 00353 } 00354 #endif 00355 //============================================================================= 00356 //***************************************************************************** 00357 00358 //============================================================================= 00359 END_NAMESPACE_TAPs 00360 //----------------------------------------------------------------------------- 00361 //34567890123456789012345678901234567890123456789012345678901234567890123456789 00362 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----