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 tgShapeCreator::~tgShapeCreator(){
00193     if(vertices != 0)
00194         delete[] vertices;
00195     if(faces != 0)
00196         delete[] faces;
00197 }
00198 
00199 void tgShapeCreator::CreateSphere(tgModel& model, float radius, int subdevisions, int method){
00200         int i;
00201         int vidx = model.getVertexSize();
00202         tgVertex v;
00203         tgFace f;
00204         
00205         // Basic geometry used for sphere
00206         switch(method){
00207                 case TETRAHEDRON:
00208                         init_tetrahedron ();
00209                         break;
00210                 case OCTAHEDRON:
00211                         init_octahedron ();
00212                         break;
00213                 case ICOSAHEDRON:
00214                         init_icosahedron();
00215                         break;
00216                 default:
00217                         init_icosahedron();
00218                         break;
00219         }
00220 
00221         // Subdevide basic geometry
00222   for(i=0; i<subdevisions; i++) 
00223     subdivide();
00224    
00225   // Copy vertices
00226   for(i=0; i<n_vertices; i++){
00227         v.pos.x = radius * vertices[3*i+0];
00228         v.pos.y = radius * vertices[3*i+1];
00229         v.pos.z = radius * vertices[3*i+2];
00230         v.normal = v.pos;
00231         v.normal.normalize();
00232         model.m_vertices.push_back(v);
00233   }
00234  
00235   // Copy faces
00236   for(i=0; i<n_faces; i++){
00237         f.v.clear();
00238                 f.v.push_back(vidx+faces[3*i+0]);
00239                 f.v.push_back(vidx+faces[3*i+1]);
00240                 f.v.push_back(vidx+faces[3*i+2]);
00241                 model.m_faces.push_back(f);
00242         }
00243 
00244   if(vertices) free (vertices); 
00245   if(faces) free (faces); 
00246 
00247 } 
00248 
00249 void tgShapeCreator::CreateBox(tgModel& model, float x, float y, float z){
00250         tgVertex v;
00251         tgFace f;
00252         int vidx = model.getVertexSize();
00253         x = x*0.5f;
00254         y = y*0.5f;
00255         z = z*0.5f;
00256         
00257         
00258         // Front
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         // Back
00266         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++);
00267         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++);
00268         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++);
00269         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++);
00270         model.m_faces.push_back(f); f.v.clear();
00271         
00272         // Right
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         // Left
00280         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++);
00281         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++);
00282         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++);
00283         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++);
00284         model.m_faces.push_back(f); f.v.clear();
00285         
00286         // Top
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         // Bottom
00294         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++);
00295         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++);
00296         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++);
00297         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++);
00298         model.m_faces.push_back(f); f.v.clear();
00299 }
00300 
00301 void tgShapeCreator::CreateCylinder(tgModel &model, float radius, float height, int slices, int stacks, bool closed){
00302         int x,z;
00303         int vidx = model.getVertexSize();
00304         float alpha = 2*PI / slices;
00305         float deltaH = height / stacks;
00306         tgVertex v[4];
00307         tgFace f;
00308         
00309         for(z=0; z<stacks; z++){
00310                 for(x=0; x<slices; x++){
00311                         f.v.clear();
00312                         
00313                         v[0].pos.x = radius * cos(alpha*x);
00314                         v[0].pos.y = radius * sin(alpha*x);
00315                         v[0].pos.z = deltaH*z - height*0.5f;
00316                         v[0].normal.x = v[0].pos.x;
00317                         v[0].normal.y = v[0].pos.y;
00318                         v[0].normal.z = 0.0f;
00319                         v[0].normal = normalize(v[0].normal);
00320                         model.m_vertices.push_back(v[0]);
00321                         f.v.push_back(vidx++);
00322                         
00323                         v[1].pos.x = radius * cos(alpha*(x+1));
00324                         v[1].pos.y = radius * sin(alpha*(x+1));
00325                         v[1].pos.z = deltaH*z - height*0.5f;
00326                         v[1].normal.x = v[1].pos.x;
00327                         v[1].normal.y = v[1].pos.y;
00328                         v[1].normal.z = 0.0f;
00329                         v[1].normal = normalize(v[1].normal);
00330                         model.m_vertices.push_back(v[1]);
00331                         f.v.push_back(vidx++);
00332                         
00333                         v[2].pos.x = radius * cos(alpha*(x+1));
00334                         v[2].pos.y = radius * sin(alpha*(x+1));
00335                         v[2].pos.z = deltaH*(z+1) - height*0.5f;
00336                         v[2].normal.x = v[2].pos.x;
00337                         v[2].normal.y = v[2].pos.y;
00338                         v[2].normal.z = 0.0f;
00339                         v[2].normal = normalize(v[2].normal);
00340                         model.m_vertices.push_back(v[2]);
00341                         f.v.push_back(vidx++);
00342                         
00343                         v[3].pos.x = radius * cos(alpha*x);
00344                         v[3].pos.y = radius * sin(alpha*x);
00345                         v[3].pos.z = deltaH*(z+1) - height*0.5f;
00346                         v[3].normal.x = v[3].pos.x;
00347                         v[3].normal.y = v[3].pos.y;
00348                         v[3].normal.z = 0.0f;
00349                         v[3].normal = normalize(v[3].normal);
00350                         model.m_vertices.push_back(v[3]);
00351                         f.v.push_back(vidx++);
00352                         model.m_faces.push_back(f);
00353                 }
00354         }
00355         
00356         if(closed){
00357                 for(z=0; z<2; z++){
00358                         deltaH = height*z - height*0.5f;
00359                         
00360                         for(x=0; x<slices; x++){
00361                                 f.v.clear();
00362                                 
00363                                 v[0].pos = vec3(0.0,0.0,deltaH);
00364                                 v[0].normal = normalize(v[0].pos);
00365                                 model.m_vertices.push_back(v[0]);
00366                                 f.v.push_back(vidx++);
00367                                 
00368                                 v[1].pos.x = radius * cos(alpha*x);
00369                                 v[1].pos.y = radius * sin(alpha*x);
00370                                 v[1].pos.z = deltaH;
00371                                 v[1].normal = v[0].normal;
00372                                 model.m_vertices.push_back(v[1]);
00373                                 f.v.push_back(vidx++);
00374                                 
00375                                 v[2].pos.x = radius * cos(alpha*(x+(float(z)-0.5f)*2.0f));
00376                                 v[2].pos.y = radius * sin(alpha*(x+(float(z)-0.5f)*2.0f));
00377                                 v[2].pos.z = deltaH;
00378                                 v[2].normal = v[0].normal;
00379                                 model.m_vertices.push_back(v[2]);
00380                                 f.v.push_back(vidx++);
00381                                 model.m_faces.push_back(f);
00382                         }
00383                 }
00384         }
00385 }
00386 
00387 void tgShapeCreator::CreateCone(tgModel &model, float radius, float height, int slices, int stacks, bool closed){
00388         
00389         if(stacks<=0 || slices<=2)
00390                 return;
00391         
00392         int x,z;
00393         int vidx = model.getVertexSize();
00394         float alpha = 2*PI / slices;
00395         float deltaH = height / stacks;
00396         float deltaR = radius / stacks;
00397         tgVertex v[4];
00398         tgFace f;
00399         
00400         for(z=0; z<stacks; z++){
00401                 for(x=0; x<slices; x++){
00402                         f.v.clear();
00403                         
00404                         v[0].pos.x = deltaR*z * cos(alpha*x);
00405                         v[0].pos.y = deltaR*z * sin(alpha*x);
00406                         v[0].pos.z = deltaH*z;
00407                         v[0].normal.x = v[0].pos.x;
00408                         v[0].normal.y = v[0].pos.y;
00409                         v[0].normal.z = 0.0f;
00410                         v[0].normal = normalize(v[0].normal);
00411                         model.m_vertices.push_back(v[0]);
00412                         f.v.push_back(vidx++);
00413                         
00414                         v[1].pos.x = deltaR*z * cos(alpha*(x+1));
00415                         v[1].pos.y = deltaR*z * sin(alpha*(x+1));
00416                         v[1].pos.z = deltaH*z;
00417                         v[1].normal.x = v[1].pos.x;
00418                         v[1].normal.y = v[1].pos.y;
00419                         v[1].normal.z = 0.0f;
00420                         v[1].normal = normalize(v[1].normal);
00421                         model.m_vertices.push_back(v[1]);
00422                         f.v.push_back(vidx++);
00423                         
00424                         v[2].pos.x = deltaR*(z+1) * cos(alpha*(x+1));
00425                         v[2].pos.y = deltaR*(z+1) * sin(alpha*(x+1));
00426                         v[2].pos.z = deltaH*(z+1);
00427                         v[2].normal.x = v[2].pos.x;
00428                         v[2].normal.y = v[2].pos.y;
00429                         v[2].normal.z = 0.0f;
00430                         v[2].normal = normalize(v[2].normal);
00431                         model.m_vertices.push_back(v[2]);
00432                         f.v.push_back(vidx++);
00433                         
00434                         v[3].pos.x = deltaR*(z+1) * cos(alpha*x);
00435                         v[3].pos.y = deltaR*(z+1) * sin(alpha*x);
00436                         v[3].pos.z = deltaH*(z+1);
00437                         v[3].normal.x = v[3].pos.x;
00438                         v[3].normal.y = v[3].pos.y;
00439                         v[3].normal.z = 0.0f;
00440                         v[3].normal = normalize(v[3].normal);
00441                         model.m_vertices.push_back(v[3]);
00442                         f.v.push_back(vidx++);
00443                         model.m_faces.push_back(f);
00444                 }
00445         }
00446         
00447         if(closed){
00448                 for(x=0; x<slices; x++){
00449                         f.v.clear();
00450                         
00451                         v[0].pos = vec3(0.0f,0.0f,height);
00452                         v[0].normal = normalize(v[0].pos);
00453                         model.m_vertices.push_back(v[0]);
00454                         f.v.push_back(vidx++);
00455                         
00456                         v[1].pos.x = radius * cos(alpha*x);
00457                         v[1].pos.y = radius * sin(alpha*x);
00458                         v[1].pos.z = height;
00459                         v[1].normal = v[0].normal;
00460                         model.m_vertices.push_back(v[1]);
00461                         f.v.push_back(vidx++);
00462                         
00463                         v[2].pos.x = radius * cos(alpha*(x+(float(z)-0.5f)*2.0f));
00464                         v[2].pos.y = radius * sin(alpha*(x+(float(z)-0.5f)*2.0f));
00465                         v[2].pos.z = height;
00466                         v[2].normal = v[0].normal;
00467                         model.m_vertices.push_back(v[2]);
00468                         f.v.push_back(vidx++);
00469                         model.m_faces.push_back(f);
00470                 }
00471         }
00472 }
00473 
00474 
00475 void tgShapeCreator::TriangulatePolygon(tgModel& model, std::vector<vec3> points){
00476         int i,s;
00477         int idx = model.m_vertices.size();
00478         bool pointInTriangle;
00479         bool pointIsConvex;
00480         vec3 e1, e2, n, poly_normal;
00481         vec3 v0, v1, v2;
00482         tgVertex v;
00483         tgFace f;
00484         std::vector<tgVertex> vertices;
00485         std::vector<tgVertex>::iterator v_act, v_pre, v_post, v_in;
00486         
00487         s=points.size();
00488         if(s<3){
00489                 printf("[tgModel::TriangulatePolygon] Warning to few points for polygon: %d\n", s);
00490                 return;
00491         }
00492         
00493         for(i=0; i<s; i++){
00494                 // Calculate normal
00495                 v0 = points[i];
00496                 v1 = points[(i+1)%s];
00497                 v2 = points[(i+s-1)%s];
00498                 e1 = v1-v0;
00499                 e2 = v2-v0;
00500                 e1.normalize();
00501                 e2.normalize();
00502                 n.cross(e1,e2);
00503                 poly_normal = poly_normal + n;  // polygon normal = sum of all normals
00504                 v.pos = v0;
00505                 vertices.push_back(v);
00506         }
00507         
00508         poly_normal.normalize();        // normalize polygon normal
00509         v_act = vertices.begin();
00510         while(vertices.size()>2){
00511 
00512                 if(v_act==vertices.end()-1){
00513                         v_pre           = v_act-1;
00514                         v_post  = vertices.begin();
00515                 }else if(v_act==vertices.begin()){
00516                         v_pre           = vertices.end()-1;
00517                         v_post  = v_act+1;
00518                 }else{
00519                         v_pre   = v_act-1;
00520                         v_post  = v_act+1;
00521                 }
00522                 
00523                 // Test if triangle is convex
00524                 v0 = (*v_act).pos;
00525                 v1 = (*v_post).pos;
00526                 v2 = (*v_pre).pos;
00527                 e1 = v1-v0;
00528                 e2 = v2-v0;
00529                 e1.normalize();
00530                 e2.normalize();
00531                 (*v_act).normal.cross(e1,e2);
00532                 (*v_act).normal.normalize();
00533                 
00534                 if((*v_act).normal * poly_normal > 0.0){
00535                         pointIsConvex = true;
00536                 }
00537                 
00538                         // Test if any other point of remaining polygon lies in this triangle
00539                         pointInTriangle = false;
00540                 if(pointIsConvex)
00541                 {
00542                         pointInTriangle = false;
00543                         for(v_in=vertices.begin(); v_in<vertices.end() && vertices.size()>3; ++v_in)
00544                         {
00545                                 if(v_in!=v_act && v_in!=v_pre && v_in!=v_post)
00546                                                 pointInTriangle = tgCollission::PointInTriangle( (*v_in).pos, (*v_post).pos, (*v_act).pos, (*v_pre).pos );
00547                         }
00548                 }
00549                         
00550                 if(pointIsConvex && !pointInTriangle){
00551                         // Generate face
00552                         f.v.clear();
00553                         
00554                         (*v_pre).normal = poly_normal;
00555                         (*v_act).normal = poly_normal;
00556                         (*v_post).normal = poly_normal;
00557                         
00558                         model.m_vertices.push_back(*v_pre);     f.v.push_back(idx); idx++;
00559                         model.m_vertices.push_back(*v_act);     f.v.push_back(idx); idx++;
00560                         model.m_vertices.push_back(*v_post);    f.v.push_back(idx); idx++;
00561                         
00562                         f.normal = poly_normal;
00563                         model.m_faces.push_back(f);
00564                         vertices.erase(v_act);
00565                         
00566                 }else{
00567                         v_act = v_post;
00568                 }
00569         }
00570 }
00571 


blort
Author(s): Thomas Mörwald , Michael Zillich , Andreas Richtsfeld , Johann Prankl , Markus Vincze , Bence Magyar
autogenerated on Wed Aug 26 2015 15:24:12