00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
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 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 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
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 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
00170 unsigned long QuadStrip::countCSGFaceElements(){
00171 unsigned long size = vertices.size();
00172 return (size-2) / 2;
00173 }
00174
00175
00176
00177 };
00178 };
00179