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

QuadStrip.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: QuadStrip.cpp,v 1.5 2002/01/17 18:57:38 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/Models/QuadStrip.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 QuadStrip::QuadStrip(){
00039 }
00040 
00041 
00043 QuadStrip::~QuadStrip(){
00044     //  FIX
00045 }
00046 
00047 
00049 void QuadStrip::draw( Projection *p ){
00050     p->beginQuadStrip();
00051 
00052     if( isEnabled(EL_USE_ELEMENT_NORMAL|EL_HAS_ELEMENT_NORMAL) ){
00053         dmsg( M_VERT, "Drawing flat quadstrip" );
00054         p->normal( normal );
00055     }else{
00056         dmsg( M_VERT, "Drawing smoothed quadstrip" );
00057     }
00058 
00059 /*  list<Vertex*>::const_iterator v_it = vertices.begin();
00060     while( v_it != vertices.end() ){
00061         (*v_it)->draw( p );
00062         v_it++;
00063     }*/
00064 
00065     /*
00066     list<Vertex*>::const_reverse_iterator v_it = vertices.rbegin();
00067     while( v_it != vertices.rend() ){
00068         (*v_it)->draw( p );
00069         v_it++;
00070     }
00071     */
00072 
00073     list<Vertex*>::const_iterator v_it = vertices.end();
00074     while( v_it != vertices.begin() ){
00075         v_it--;
00076         (*v_it)->draw( p );
00077     }
00078 
00079     p->end();
00080 }
00081 
00082 
00083 /*virtual*/ void QuadStrip::makeNormal(){
00084     list<Vertex*>::iterator v_i1 = vertices.begin();
00085     list<Vertex*>::iterator v_i2 = v_i1;
00086 
00087     if( vertices.size()<3 ){
00088         dmsg( M_WARN,
00089             "Element does not have enough vertices for normal calculation (%d vertices found)",
00090             vertices.size()
00091         );
00092         normal = Vector(0,1,0);
00093         return;
00094     }
00095 
00096     normals.clear();
00097     float len;
00098     do{
00099         v_i1++;
00100         if( v_i2 == vertices.end() ) return;
00101         v_i2 = v_i1++;
00102 
00103         if( v_i2 == vertices.end() ) return;
00104         Vector a  = (*v_i2++)->getVertex();
00105 
00106         if( v_i2 == vertices.end() ) return;
00107         Vector b  = (*v_i2++)->getVertex();
00108 
00109         if( v_i2 == vertices.end() ) return;
00110         Vector c  = (*v_i2++)->getVertex();
00111 
00112         if( v_i2 == vertices.end() ) return;
00113         Vector d  = (*v_i2++)->getVertex();
00114 
00115         normal    = (a-c)^(a-b);
00116         normal.normalize();
00117         len = normal.magnitude();
00118         if( len < 0.9 || len >11.1 ){
00119             dmsg( M_WARN, "TriangleStripe normal problem" );
00120             return;
00121         }
00122         normals.push_back( new Vector(normal) );
00123     }while( v_i2 != vertices.end() );
00124 
00125     enable( EL_HAS_ELEMENT_NORMAL | EL_USE_ELEMENT_NORMAL );
00126     printf( "\nQuadStrip normal done\n" );
00127 }
00128 
00129 
00130 /*virtual*/ void QuadStrip::reverse(){
00131     list<Vertex*>::iterator v1_it = vertices.begin();
00132     list<Vertex*>::iterator v2_it = vertices.begin();
00133     v2_it++;
00134     for(;;){
00135         if( v1_it == vertices.end() ) break;
00136         if( v2_it == vertices.end() ) break;
00137         //  Swap vertices
00138         std::swap( *v1_it, *v2_it );
00139         v1_it++;
00140         v2_it++;
00141         if( v1_it == vertices.end() ) break;
00142         if( v2_it == vertices.end() ) break;
00143         v1_it++;
00144         v2_it++;
00145     }
00146 }
00147 
00148 
00149 #if defined( TEDDY_INCLUDE_COLDET )
00150 /*virtual*/ int QuadStrip::addToCollisionModel( Teddy::ColDet::CollisionModel3D *collision_model ){
00151     int tris = 0;
00152     list<Vertex*>::const_iterator v_it  = vertices.begin();
00153 
00154     FloatVector a  = (*v_it)->getVertex(); v_it++; if( v_it == vertices.end() ) return 0;
00155     FloatVector b  = (*v_it)->getVertex(); v_it++;
00156     while( v_it != vertices.end() ){
00157         FloatVector c = (*v_it)->getVertex();
00158         collision_model->addTriangle( a, b, c );
00159         a  = b;
00160         b  = c;
00161         tris++;
00162         v_it++;
00163     }
00164     return tris;
00165 }
00166 #endif
00167 
00168 
00169 //  CSG
00170 /*virtual*/ unsigned long QuadStrip::countCSGFaceElements(){
00171     unsigned long size = vertices.size();
00172     return (size-2) / 2;
00173 }
00174 
00175 
00176 
00177 };  //  namespace Models
00178 };  //  namespace Teddy
00179