Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

TriangleStrip.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id:  $
00022 */
00023 
00024 
00025 #include "Teddy/Models/TriangleStrip.h"
00026 #include "Teddy/Models/Vertex.h"
00027 #include "Teddy/PhysicalComponents/Projection.h"
00028 #include "Teddy/SysSupport/Messages.h"
00029 #include "Teddy/SysSupport/StdIO.h"
00030 using namespace Teddy::SysSupport;
00031 
00032 
00033 namespace Teddy  {
00034 namespace Models {
00035 
00036 
00038 TriangleStrip::TriangleStrip(){
00039 }
00040 
00041 
00043 /*virtual*/ TriangleStrip::~TriangleStrip(){
00044     //  FIX
00045 }
00046 
00047 
00049 /*virtual*/ void TriangleStrip::draw( Projection *p ){
00050 
00051     if( isEnabled(EL_USE_ELEMENT_NORMAL|EL_HAS_ELEMENT_NORMAL) ){
00052         dmsg( M_INIT, "Drawing flat trianglestrip" );
00053 
00054         p->beginTriangles();
00055 
00056         list<Vector*>::const_iterator n_it  = normals .begin();
00057         list<Vertex*>::const_iterator v1_it = vertices.begin();
00058         list<Vertex*>::const_iterator v2_it = v1_it;
00059         bool swap = true;
00060         while( n_it != normals.end() && v2_it != vertices.end() ){
00061             Vector *normal = *n_it++;
00062             p->normal( *normal );
00063             v2_it = v1_it;
00064             Vertex *a = *v2_it++; if( v2_it == vertices.end() ){ dmsg( M_WARN, "TriangleStrip missing vertices" ); break; }
00065             Vertex *b = *v2_it++; if( v2_it == vertices.end() ){ dmsg( M_WARN, "TriangleStrip missing vertices" ); break; }
00066             Vertex *c = *v2_it++;
00067             if( swap ){
00068                 b->draw( p );
00069                 a->draw( p );
00070             }else{
00071                 a->draw( p );
00072                 b->draw( p );
00073             }
00074             c->draw( p );
00075             dprint( M_INIT, "." );
00076             v1_it++;
00077             swap = !swap;
00078         }
00079 
00080         p->end();
00081 
00082     }else{
00083         dmsg( M_INIT, "Drawing smoothed trianglestrip" );
00084 
00085         p->beginTriangleStrip();
00086         list<Vertex*>::const_iterator v_it = vertices.begin();
00087         while( v_it != vertices.end() ){
00088             (*v_it)->draw( p );
00089             v_it++;
00090         }
00091 
00092         p->end();
00093     }
00094 
00095 }
00096 
00097 
00098 
00099 /*virtual*/ void TriangleStrip::makeNormal(){
00100     list<Vertex*>::iterator v_i1 = vertices.begin();
00101     list<Vertex*>::iterator v_i2 = v_i1;
00102 
00103     if( vertices.size()<3 ){
00104         dmsg( M_WARN,
00105             "Element does not have enough vertices for normal calculation (%d vertices found)",
00106             vertices.size()
00107         );
00108         normal = Vector(0,1,0);
00109         return;
00110     }
00111 
00112     normals.clear();
00113     float len;
00114     bool swap = true;
00115     Vector a;
00116     Vector b;
00117     Vector c;
00118     do{
00119         v_i2 = v_i1++;
00120 
00121         if( swap ){
00122             if( v_i2 == vertices.end() ){ dmsg( M_WARN, "TriangleStripe normal problem" ); return; }
00123             b  = (*v_i2++)->getVertex();
00124             if( v_i2 == vertices.end() ){ dmsg( M_WARN, "TriangleStripe normal problem" ); return; }
00125             a  = (*v_i2++)->getVertex();
00126         }else{
00127             if( v_i2 == vertices.end() ){ dmsg( M_WARN, "TriangleStripe normal problem" ); return; }
00128             a  = (*v_i2++)->getVertex();
00129             if( v_i2 == vertices.end() ){ dmsg( M_WARN, "TriangleStripe normal problem" ); return; }
00130             b  = (*v_i2++)->getVertex();
00131         }
00132 
00133         if( v_i2 == vertices.end() ){ dmsg( M_WARN, "TriangleStripe normal problem" ); return; }
00134         c  = (*v_i2++)->getVertex();
00135 
00136         Vector ab = b-a;
00137         ab.normalize();
00138         Vector bc = c-b;
00139         bc.normalize();
00140         float test = ab | bc;
00141         normal = (a-c)^(a-b);
00142         normal.normalize();
00143         len = normal.magnitude();
00144         if( len < 0.9 || len >11.1 ){
00145             dmsg( M_WARN, "TriangleStripe normal problem" );
00146             return;
00147         }
00148         if( test == -1.0 || test == 1.0 ){
00149         }else{
00150             this->normal = normal;
00151         }
00152         normals.push_back( new Vector(normal) );
00153         swap = !swap;
00154     }while( v_i2 != vertices.end() );
00155 
00156     enable( EL_HAS_ELEMENT_NORMAL | EL_USE_ELEMENT_NORMAL );
00157 }
00158 
00159 
00160 
00161 #if defined( TEDDY_INCLUDE_COLDET )
00162 /*virtual*/ int TriangleStrip::addToCollisionModel( Teddy::ColDet::CollisionModel3D *collision_model ){
00163     int tris = 0;
00164     list<Vertex*>::const_iterator v_it  = vertices.begin();
00165 
00166     FloatVector a  = (*v_it)->getVertex(); v_it++; if( v_it == vertices.end() ) return 0;
00167     FloatVector b  = (*v_it)->getVertex(); v_it++;
00168     while( v_it != vertices.end() ){
00169         FloatVector c = (*v_it)->getVertex();
00170         collision_model->addTriangle( a, b, c );
00171         a  = b;
00172         b  = c;
00173         tris++;
00174         v_it++;
00175     }
00176     return tris;
00177 }
00178 #endif
00179 
00180 
00181 //  CSG
00182 /*virtual*/ unsigned long TriangleStrip::countCSGFaceElements(){
00183     unsigned long size = vertices.size();
00184     return size - 2;
00185 }
00186 
00187 
00188 };  //  namespace Models
00189 };  //  namespace Teddy
00190