tgShapeCreator.cpp
Go to the documentation of this file.
00001 
00002 #include <blort/TomGine/tgShapeCreator.h>
00003 #include <blort/TomGine/tgCollission.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <stdio.h>
00007 
00008 using namespace TomGine;
00009 
00010 void tgShapeCreator::init_tetrahedron(){ 
00011   float sqrt3 = 1.0f / sqrt(3.0f);
00012   float tetrahedron_vertices[] = {sqrt3, sqrt3, sqrt3,
00013                                   -sqrt3, -sqrt3, sqrt3,
00014                                   -sqrt3, sqrt3, -sqrt3,
00015                                   sqrt3, -sqrt3, -sqrt3}; 
00016   int tetrahedron_faces[] = {0, 2, 1, 0, 1, 3, 2, 3, 1, 3, 2, 0};
00017 
00018   n_vertices = 4; 
00019   n_faces = 4; 
00020   n_edges = 6; 
00021   vertices = (float*)malloc(3*n_vertices*sizeof(float)); 
00022   faces = (int*)malloc(3*n_faces*sizeof(int)); 
00023   memcpy ((void*)vertices, (void*)tetrahedron_vertices, 3*n_vertices*sizeof(float)); 
00024   memcpy ((void*)faces, (void*)tetrahedron_faces, 3*n_faces*sizeof(int)); 
00025 } 
00026 
00027 void tgShapeCreator::init_octahedron(){ 
00028   float octahedron_vertices[] = {0.0, 0.0, -1.0,
00029                                  1.0, 0.0, 0.0,
00030                                  0.0, -1.0, 0.0,
00031                                  -1.0, 0.0, 0.0,
00032                                  0.0, 1.0, 0.0,
00033                                  0.0, 0.0, 1.0}; 
00034   int octahedron_faces[] = {0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 1, 5, 2, 1, 5, 3, 2, 5, 4, 3, 5, 1, 4}; 
00035 
00036   n_vertices = 6; 
00037   n_faces = 8;
00038   n_edges = 12; 
00039   vertices = (float*)malloc(3*n_vertices*sizeof(float)); 
00040   faces = (int*)malloc(3*n_faces*sizeof(int)); 
00041   memcpy ((void*)vertices, (void*)octahedron_vertices, 3*n_vertices*sizeof(float)); 
00042   memcpy ((void*)faces, (void*)octahedron_faces, 3*n_faces*sizeof(int)); 
00043 } 
00044 
00045 void tgShapeCreator::init_icosahedron(){ 
00046   float t = (1+sqrt(5.0f))/2;
00047   float tau = t/sqrt(1+t*t);
00048   float one = 1/sqrt(1+t*t);
00049 
00050   float icosahedron_vertices[] = {tau, one, 0.0,
00051                                   -tau, one, 0.0,
00052                                   -tau, -one, 0.0,
00053                                   tau, -one, 0.0,
00054                                   one, 0.0 ,  tau,
00055                                   one, 0.0 , -tau,
00056                                   -one, 0.0 , -tau,
00057                                   -one, 0.0 , tau,
00058                                   0.0 , tau, one,
00059                                   0.0 , -tau, one,
00060                                   0.0 , -tau, -one,
00061                                   0.0 , tau, -one};
00062  int icosahedron_faces[] = {4, 8, 7,
00063                             4, 7, 9,
00064                             5, 6, 11,
00065                             5, 10, 6,
00066                             0, 4, 3,
00067                             0, 3, 5,
00068                             2, 7, 1,
00069                             2, 1, 6,
00070                             8, 0, 11,
00071                             8, 11, 1,
00072                             9, 10, 3,
00073                             9, 2, 10,
00074                             8, 4, 0,
00075                             11, 0, 5,
00076                             4, 9, 3,
00077                             5, 3, 10,
00078                             7, 8, 1,
00079                             6, 1, 11,
00080                             7, 2, 9,
00081                             6, 10, 2};
00082  
00083   n_vertices = 12; 
00084   n_faces = 20;
00085   n_edges = 30;
00086   vertices = (float*)malloc(3*n_vertices*sizeof(float)); 
00087   faces = (int*)malloc(3*n_faces*sizeof(int)); 
00088   memcpy ((void*)vertices, (void*)icosahedron_vertices, 3*n_vertices*sizeof(float)); 
00089   memcpy ((void*)faces, (void*)icosahedron_faces, 3*n_faces*sizeof(int)); 
00090 } 
00091 
00092 int tgShapeCreator::search_midpoint(int index_start, int index_end){ 
00093   int i;
00094   for (i=0; i<edge_walk; i++) 
00095     if ((start[i] == index_start && end[i] == index_end) || 
00096         (start[i] == index_end && end[i] == index_start)) 
00097       {
00098         int res = midpoint[i];
00099 
00100         /* update the arrays */
00101         start[i]    = start[edge_walk-1];
00102         end[i]      = end[edge_walk-1];
00103         midpoint[i] = midpoint[edge_walk-1];
00104         edge_walk--;
00105         
00106         return res; 
00107       }
00108 
00109   /* vertex not in the list, so we add it */
00110   start[edge_walk] = index_start;
00111   end[edge_walk] = index_end; 
00112   midpoint[edge_walk] = n_vertices; 
00113   
00114   /* create new vertex */ 
00115   vertices[3*n_vertices]   = (vertices[3*index_start] + vertices[3*index_end]) / 2.0f;
00116   vertices[3*n_vertices+1] = (vertices[3*index_start+1] + vertices[3*index_end+1]) / 2.0f;
00117   vertices[3*n_vertices+2] = (vertices[3*index_start+2] + vertices[3*index_end+2]) / 2.0f;
00118   
00119   /* normalize the new vertex */ 
00120   float length = sqrt (vertices[3*n_vertices] * vertices[3*n_vertices] +
00121                        vertices[3*n_vertices+1] * vertices[3*n_vertices+1] +
00122                        vertices[3*n_vertices+2] * vertices[3*n_vertices+2]);
00123   length = 1/length;
00124   vertices[3*n_vertices] *= length;
00125   vertices[3*n_vertices+1] *= length;
00126   vertices[3*n_vertices+2] *= length;
00127   
00128   n_vertices++;
00129   edge_walk++;
00130   return midpoint[edge_walk-1];
00131 } 
00132 
00133 void tgShapeCreator::subdivide(){ 
00134   int n_vertices_new = n_vertices+2*n_edges; 
00135   int n_faces_new = 4*n_faces; 
00136   int i; 
00137 
00138   edge_walk = 0; 
00139   n_edges = 2*n_vertices + 3*n_faces; 
00140   start = (int*)malloc(n_edges*sizeof (int)); 
00141   end = (int*)malloc(n_edges*sizeof (int)); 
00142   midpoint = (int*)malloc(n_edges*sizeof (int)); 
00143 
00144   int *faces_old = (int*)malloc (3*n_faces*sizeof(int)); 
00145   faces_old = (int*)memcpy((void*)faces_old, (void*)faces, 3*n_faces*sizeof(int)); 
00146   vertices = (float*)realloc ((void*)vertices, 3*n_vertices_new*sizeof(float)); 
00147   faces = (int*)realloc ((void*)faces, 3*n_faces_new*sizeof(int)); 
00148   n_faces_new = 0; 
00149 
00150   for (i=0; i<n_faces; i++) 
00151     { 
00152       int a = faces_old[3*i]; 
00153       int b = faces_old[3*i+1]; 
00154       int c = faces_old[3*i+2]; 
00155 
00156       int ab_midpoint = search_midpoint (b, a); 
00157       int bc_midpoint = search_midpoint (c, b); 
00158       int ca_midpoint = search_midpoint (a, c); 
00159 
00160       faces[3*n_faces_new] = a; 
00161       faces[3*n_faces_new+1] = ab_midpoint; 
00162       faces[3*n_faces_new+2] = ca_midpoint; 
00163       n_faces_new++; 
00164       faces[3*n_faces_new] = ca_midpoint; 
00165       faces[3*n_faces_new+1] = ab_midpoint; 
00166       faces[3*n_faces_new+2] = bc_midpoint; 
00167       n_faces_new++; 
00168       faces[3*n_faces_new] = ca_midpoint; 
00169       faces[3*n_faces_new+1] = bc_midpoint; 
00170       faces[3*n_faces_new+2] = c; 
00171       n_faces_new++; 
00172       faces[3*n_faces_new] = ab_midpoint; 
00173       faces[3*n_faces_new+1] = b; 
00174       faces[3*n_faces_new+2] = bc_midpoint; 
00175       n_faces_new++; 
00176     } 
00177   n_faces = n_faces_new; 
00178   free (start); 
00179   free (end); 
00180   free (midpoint); 
00181   free (faces_old); 
00182 } 
00183 
00184 tgShapeCreator::tgShapeCreator(){
00185         vertices = 0;
00186         faces = 0; 
00187         start = 0; 
00188         end = 0; 
00189         midpoint = 0; 
00190 }
00191 
00192 void tgShapeCreator::CreateSphere(tgModel& model, float radius, int subdevisions, int method){
00193         int i;
00194         int vidx = model.getVertexSize();
00195         tgVertex v;
00196         tgFace f;
00197         
00198         // Basic geometry used for sphere
00199         switch(method){
00200                 case TETRAHEDRON:
00201                         init_tetrahedron ();
00202                         break;
00203                 case OCTAHEDRON:
00204                         init_octahedron ();
00205                         break;
00206                 case ICOSAHEDRON:
00207                         init_icosahedron();
00208                         break;
00209                 default:
00210                         init_icosahedron();
00211                         break;
00212         }
00213 
00214         // Subdevide basic geometry
00215   for(i=0; i<subdevisions; i++) 
00216     subdivide();
00217    
00218   // Copy vertices
00219   for(i=0; i<n_vertices; i++){
00220         v.pos.x = radius * vertices[3*i+0];
00221         v.pos.y = radius * vertices[3*i+1];
00222         v.pos.z = radius * vertices[3*i+2];
00223         v.normal = v.pos;
00224         v.normal.normalize();
00225         model.m_vertices.push_back(v);
00226   }
00227  
00228   // Copy faces
00229   for(i=0; i<n_faces; i++){
00230         f.v.clear();
00231                 f.v.push_back(vidx+faces[3*i+0]);
00232                 f.v.push_back(vidx+faces[3*i+1]);
00233                 f.v.push_back(vidx+faces[3*i+2]);
00234                 model.m_faces.push_back(f);
00235         }
00236 
00237   if(vertices) free (vertices); 
00238   if(faces) free (faces); 
00239 
00240 } 
00241 
00242 void tgShapeCreator::CreateBox(tgModel& model, float x, float y, float z){
00243         tgVertex v;
00244         tgFace f;
00245         int vidx = model.getVertexSize();
00246         x = x*0.5f;
00247         y = y*0.5f;
00248         z = z*0.5f;
00249         
00250         
00251         // Front
00252         v.pos = vec3(-x,-y, z); v.normal = vec3( 0.0f, 0.0f, 1.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00253         v.pos = vec3( x,-y, z); v.normal = vec3( 0.0f, 0.0f, 1.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00254         v.pos = vec3( x, y, z); v.normal = vec3( 0.0f, 0.0f, 1.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00255         v.pos = vec3(-x, y, z); v.normal = vec3( 0.0f, 0.0f, 1.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00256         model.m_faces.push_back(f); f.v.clear();
00257         
00258         // Back
00259         v.pos = vec3( x,-y,-z); v.normal = vec3( 0.0f, 0.0f,-1.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00260         v.pos = vec3(-x,-y,-z); v.normal = vec3( 0.0f, 0.0f,-1.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00261         v.pos = vec3(-x, y,-z); v.normal = vec3( 0.0f, 0.0f,-1.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00262         v.pos = vec3( x, y,-z); v.normal = vec3( 0.0f, 0.0f,-1.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00263         model.m_faces.push_back(f); f.v.clear();
00264         
00265         // Right
00266         v.pos = vec3( x,-y, z); v.normal = vec3( 1.0f, 0.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00267         v.pos = vec3( x,-y,-z); v.normal = vec3( 1.0f, 0.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00268         v.pos = vec3( x, y,-z); v.normal = vec3( 1.0f, 0.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00269         v.pos = vec3( x, y, z); v.normal = vec3( 1.0f, 0.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00270         model.m_faces.push_back(f); f.v.clear();
00271         
00272         // Left
00273         v.pos = vec3(-x,-y,-z); v.normal = vec3(-1.0f, 0.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00274         v.pos = vec3(-x,-y, z); v.normal = vec3(-1.0f, 0.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00275         v.pos = vec3(-x, y, z); v.normal = vec3(-1.0f, 0.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00276         v.pos = vec3(-x, y,-z); v.normal = vec3(-1.0f, 0.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00277         model.m_faces.push_back(f); f.v.clear();
00278         
00279         // Top
00280         v.pos = vec3(-x, y, z); v.normal = vec3( 0.0f, 1.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00281         v.pos = vec3( x, y, z); v.normal = vec3( 0.0f, 1.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00282         v.pos = vec3( x, y,-z); v.normal = vec3( 0.0f, 1.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00283         v.pos = vec3(-x, y,-z); v.normal = vec3( 0.0f, 1.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00284         model.m_faces.push_back(f); f.v.clear();
00285         
00286         // Bottom
00287         v.pos = vec3( x,-y, z); v.normal = vec3( 0.0f,-1.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00288         v.pos = vec3(-x,-y, z); v.normal = vec3( 0.0f,-1.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00289         v.pos = vec3(-x,-y,-z); v.normal = vec3( 0.0f,-1.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00290         v.pos = vec3( x,-y,-z); v.normal = vec3( 0.0f,-1.0f, 0.0f); model.m_vertices.push_back(v); f.v.push_back(vidx++);
00291         model.m_faces.push_back(f); f.v.clear();
00292 }
00293 
00294 void tgShapeCreator::CreateCylinder(tgModel &model, float radius, float height, int slices, int stacks, bool closed){
00295         int x,z;
00296         int vidx = model.getVertexSize();
00297         float alpha = 2*PI / slices;
00298         float deltaH = height / stacks;
00299         tgVertex v[4];
00300         tgFace f;
00301         
00302         for(z=0; z<stacks; z++){
00303                 for(x=0; x<slices; x++){
00304                         f.v.clear();
00305                         
00306                         v[0].pos.x = radius * cos(alpha*x);
00307                         v[0].pos.y = radius * sin(alpha*x);
00308                         v[0].pos.z = deltaH*z - height*0.5f;
00309                         v[0].normal.x = v[0].pos.x;
00310                         v[0].normal.y = v[0].pos.y;
00311                         v[0].normal.z = 0.0f;
00312                         v[0].normal = normalize(v[0].normal);
00313                         model.m_vertices.push_back(v[0]);
00314                         f.v.push_back(vidx++);
00315                         
00316                         v[1].pos.x = radius * cos(alpha*(x+1));
00317                         v[1].pos.y = radius * sin(alpha*(x+1));
00318                         v[1].pos.z = deltaH*z - height*0.5f;
00319                         v[1].normal.x = v[1].pos.x;
00320                         v[1].normal.y = v[1].pos.y;
00321                         v[1].normal.z = 0.0f;
00322                         v[1].normal = normalize(v[1].normal);
00323                         model.m_vertices.push_back(v[1]);
00324                         f.v.push_back(vidx++);
00325                         
00326                         v[2].pos.x = radius * cos(alpha*(x+1));
00327                         v[2].pos.y = radius * sin(alpha*(x+1));
00328                         v[2].pos.z = deltaH*(z+1) - height*0.5f;
00329                         v[2].normal.x = v[2].pos.x;
00330                         v[2].normal.y = v[2].pos.y;
00331                         v[2].normal.z = 0.0f;
00332                         v[2].normal = normalize(v[2].normal);
00333                         model.m_vertices.push_back(v[2]);
00334                         f.v.push_back(vidx++);
00335                         
00336                         v[3].pos.x = radius * cos(alpha*x);
00337                         v[3].pos.y = radius * sin(alpha*x);
00338                         v[3].pos.z = deltaH*(z+1) - height*0.5f;
00339                         v[3].normal.x = v[3].pos.x;
00340                         v[3].normal.y = v[3].pos.y;
00341                         v[3].normal.z = 0.0f;
00342                         v[3].normal = normalize(v[3].normal);
00343                         model.m_vertices.push_back(v[3]);
00344                         f.v.push_back(vidx++);
00345                         model.m_faces.push_back(f);
00346                 }
00347         }
00348         
00349         if(closed){
00350                 for(z=0; z<2; z++){
00351                         deltaH = height*z - height*0.5f;
00352                         
00353                         for(x=0; x<slices; x++){
00354                                 f.v.clear();
00355                                 
00356                                 v[0].pos = vec3(0.0,0.0,deltaH);
00357                                 v[0].normal = normalize(v[0].pos);
00358                                 model.m_vertices.push_back(v[0]);
00359                                 f.v.push_back(vidx++);
00360                                 
00361                                 v[1].pos.x = radius * cos(alpha*x);
00362                                 v[1].pos.y = radius * sin(alpha*x);
00363                                 v[1].pos.z = deltaH;
00364                                 v[1].normal = v[0].normal;
00365                                 model.m_vertices.push_back(v[1]);
00366                                 f.v.push_back(vidx++);
00367                                 
00368                                 v[2].pos.x = radius * cos(alpha*(x+(float(z)-0.5f)*2.0f));
00369                                 v[2].pos.y = radius * sin(alpha*(x+(float(z)-0.5f)*2.0f));
00370                                 v[2].pos.z = deltaH;
00371                                 v[2].normal = v[0].normal;
00372                                 model.m_vertices.push_back(v[2]);
00373                                 f.v.push_back(vidx++);
00374                                 model.m_faces.push_back(f);
00375                         }
00376                 }
00377         }
00378 }
00379 
00380 void tgShapeCreator::CreateCone(tgModel &model, float radius, float height, int slices, int stacks, bool closed){
00381         
00382         if(stacks<=0 || slices<=2)
00383                 return;
00384         
00385         int x,z;
00386         int vidx = model.getVertexSize();
00387         float alpha = 2*PI / slices;
00388         float deltaH = height / stacks;
00389         float deltaR = radius / stacks;
00390         tgVertex v[4];
00391         tgFace f;
00392         
00393         for(z=0; z<stacks; z++){
00394                 for(x=0; x<slices; x++){
00395                         f.v.clear();
00396                         
00397                         v[0].pos.x = deltaR*z * cos(alpha*x);
00398                         v[0].pos.y = deltaR*z * sin(alpha*x);
00399                         v[0].pos.z = deltaH*z;
00400                         v[0].normal.x = v[0].pos.x;
00401                         v[0].normal.y = v[0].pos.y;
00402                         v[0].normal.z = 0.0f;
00403                         v[0].normal = normalize(v[0].normal);
00404                         model.m_vertices.push_back(v[0]);
00405                         f.v.push_back(vidx++);
00406                         
00407                         v[1].pos.x = deltaR*z * cos(alpha*(x+1));
00408                         v[1].pos.y = deltaR*z * sin(alpha*(x+1));
00409                         v[1].pos.z = deltaH*z;
00410                         v[1].normal.x = v[1].pos.x;
00411                         v[1].normal.y = v[1].pos.y;
00412                         v[1].normal.z = 0.0f;
00413                         v[1].normal = normalize(v[1].normal);
00414                         model.m_vertices.push_back(v[1]);
00415                         f.v.push_back(vidx++);
00416                         
00417                         v[2].pos.x = deltaR*(z+1) * cos(alpha*(x+1));
00418                         v[2].pos.y = deltaR*(z+1) * sin(alpha*(x+1));
00419                         v[2].pos.z = deltaH*(z+1);
00420                         v[2].normal.x = v[2].pos.x;
00421                         v[2].normal.y = v[2].pos.y;
00422                         v[2].normal.z = 0.0f;
00423                         v[2].normal = normalize(v[2].normal);
00424                         model.m_vertices.push_back(v[2]);
00425                         f.v.push_back(vidx++);
00426                         
00427                         v[3].pos.x = deltaR*(z+1) * cos(alpha*x);
00428                         v[3].pos.y = deltaR*(z+1) * sin(alpha*x);
00429                         v[3].pos.z = deltaH*(z+1);
00430                         v[3].normal.x = v[3].pos.x;
00431                         v[3].normal.y = v[3].pos.y;
00432                         v[3].normal.z = 0.0f;
00433                         v[3].normal = normalize(v[3].normal);
00434                         model.m_vertices.push_back(v[3]);
00435                         f.v.push_back(vidx++);
00436                         model.m_faces.push_back(f);
00437                 }
00438         }
00439         
00440         if(closed){
00441                 for(x=0; x<slices; x++){
00442                         f.v.clear();
00443                         
00444                         v[0].pos = vec3(0.0f,0.0f,height);
00445                         v[0].normal = normalize(v[0].pos);
00446                         model.m_vertices.push_back(v[0]);
00447                         f.v.push_back(vidx++);
00448                         
00449                         v[1].pos.x = radius * cos(alpha*x);
00450                         v[1].pos.y = radius * sin(alpha*x);
00451                         v[1].pos.z = height;
00452                         v[1].normal = v[0].normal;
00453                         model.m_vertices.push_back(v[1]);
00454                         f.v.push_back(vidx++);
00455                         
00456                         v[2].pos.x = radius * cos(alpha*(x+(float(z)-0.5f)*2.0f));
00457                         v[2].pos.y = radius * sin(alpha*(x+(float(z)-0.5f)*2.0f));
00458                         v[2].pos.z = height;
00459                         v[2].normal = v[0].normal;
00460                         model.m_vertices.push_back(v[2]);
00461                         f.v.push_back(vidx++);
00462                         model.m_faces.push_back(f);
00463                 }
00464         }
00465 }
00466 
00467 
00468 void tgShapeCreator::TriangulatePolygon(tgModel& model, std::vector<vec3> points){
00469         int i,s;
00470         int idx = model.m_vertices.size();
00471         bool pointInTriangle;
00472         bool pointIsConvex;
00473         vec3 e1, e2, n, poly_normal;
00474         vec3 v0, v1, v2;
00475         tgVertex v;
00476         tgFace f;
00477         std::vector<tgVertex> vertices;
00478         std::vector<tgVertex>::iterator v_act, v_pre, v_post, v_in;
00479         
00480         s=points.size();
00481         if(s<3){
00482                 printf("[tgModel::TriangulatePolygon] Warning to few points for polygon: %d\n", s);
00483                 return;
00484         }
00485         
00486         for(i=0; i<s; i++){
00487                 // Calculate normal
00488                 v0 = points[i];
00489                 v1 = points[(i+1)%s];
00490                 v2 = points[(i+s-1)%s];
00491                 e1 = v1-v0;
00492                 e2 = v2-v0;
00493                 e1.normalize();
00494                 e2.normalize();
00495                 n.cross(e1,e2);
00496                 poly_normal = poly_normal + n;  // polygon normal = sum of all normals
00497                 v.pos = v0;
00498                 vertices.push_back(v);
00499         }
00500         
00501         poly_normal.normalize();        // normalize polygon normal
00502         v_act = vertices.begin();
00503         while(vertices.size()>2){
00504 
00505                 if(v_act==vertices.end()-1){
00506                         v_pre           = v_act-1;
00507                         v_post  = vertices.begin();
00508                 }else if(v_act==vertices.begin()){
00509                         v_pre           = vertices.end()-1;
00510                         v_post  = v_act+1;
00511                 }else{
00512                         v_pre   = v_act-1;
00513                         v_post  = v_act+1;
00514                 }
00515                 
00516                 // Test if triangle is convex
00517                 v0 = (*v_act).pos;
00518                 v1 = (*v_post).pos;
00519                 v2 = (*v_pre).pos;
00520                 e1 = v1-v0;
00521                 e2 = v2-v0;
00522                 e1.normalize();
00523                 e2.normalize();
00524                 (*v_act).normal.cross(e1,e2);
00525                 (*v_act).normal.normalize();
00526                 
00527                 if((*v_act).normal * poly_normal > 0.0){
00528                         pointIsConvex = true;
00529                 }
00530                 
00531                         // Test if any other point of remaining polygon lies in this triangle
00532                         pointInTriangle = false;
00533                 if(pointIsConvex)
00534                 {
00535                         pointInTriangle = false;
00536                         for(v_in=vertices.begin(); v_in<vertices.end() && vertices.size()>3; ++v_in)
00537                         {
00538                                 if(v_in!=v_act && v_in!=v_pre && v_in!=v_post)
00539                                                 pointInTriangle = tgCollission::PointInTriangle( (*v_in).pos, (*v_post).pos, (*v_act).pos, (*v_pre).pos );
00540                         }
00541                 }
00542                         
00543                 if(pointIsConvex && !pointInTriangle){
00544                         // Generate face
00545                         f.v.clear();
00546                         
00547                         (*v_pre).normal = poly_normal;
00548                         (*v_act).normal = poly_normal;
00549                         (*v_post).normal = poly_normal;
00550                         
00551                         model.m_vertices.push_back(*v_pre);     f.v.push_back(idx); idx++;
00552                         model.m_vertices.push_back(*v_act);     f.v.push_back(idx); idx++;
00553                         model.m_vertices.push_back(*v_post);    f.v.push_back(idx); idx++;
00554                         
00555                         f.normal = poly_normal;
00556                         model.m_faces.push_back(f);
00557                         vertices.erase(v_act);
00558                         
00559                 }else{
00560                         v_act = v_post;
00561                 }
00562         }
00563 }
00564 


blort
Author(s): Michael Zillich, Thomas Mörwald, Johann Prankl, Andreas Richtsfeld, Bence Magyar (ROS version)
autogenerated on Thu Jan 2 2014 11:38:26