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/TriangleFan.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 TriangleFan::TriangleFan(){
00039
00040 }
00041
00042
00044 TriangleFan::~TriangleFan(){
00045
00046 }
00047
00048
00050 void TriangleFan::draw( Projection *p ){
00051
00052 if( isEnabled(EL_USE_ELEMENT_NORMAL|EL_HAS_ELEMENT_NORMAL) ){
00053 dmsg( M_INIT, "Drawing flat trianglefan" );
00054
00055 p->beginTriangles();
00056
00057 list<Vector*>::const_iterator n_it = normals .begin();
00058 list<Vertex*>::const_iterator v1_it = vertices.begin();
00059 list<Vertex*>::const_iterator v2_it = v1_it;
00060 Vertex *v0 = *v1_it++;
00061 while( n_it != normals.end() && v2_it != vertices.end() ){
00062 Vector *normal = *n_it++;
00063 p->normal( *normal );
00064 v0->draw( p );
00065 v2_it = v1_it;
00066 (*v2_it++)->draw( p ); if( v2_it == vertices.end() ){ dmsg( M_WARN, "TriangleFan missing vertices" ); break; }
00067 (*v2_it++)->draw( p );
00068 v1_it++;
00069 }
00070
00071 p->end();
00072
00073 }else{
00074 dmsg( M_INIT, "Drawing smoothed trianglefan" );
00075
00076 p->beginTriangleFan();
00077 list<Vertex*>::const_iterator v_it = vertices.begin();
00078 while( v_it != vertices.end() ){
00079 (*v_it)->draw( p );
00080 v_it++;
00081 }
00082
00083 p->end();
00084 }
00085
00086 }
00087
00088
00089 void TriangleFan::makeNormal(){
00090 list<Vertex*>::iterator v_i1 = vertices.begin();
00091 list<Vertex*>::iterator v_i2 = v_i1;
00092
00093 if( vertices.size()<3 ){
00094 emsg( M_MAT,
00095 "TriangleFan does not have enough vertices for normal calculation (%d vertices found)",
00096 vertices.size()
00097 );
00098 normal = Vector(0,1,0);
00099 return;
00100 }
00101
00102 msg( M_INIT, "TriangleFan has %d vertices", vertices.size() );
00103
00104 normals.clear();
00105 Vector a = (*v_i1++)->getVertex();
00106 float len;
00107 do{
00108 v_i2 = v_i1++;
00109
00110 Vector b = (*v_i2++)->getVertex();
00111 Vector ab = b-a;
00112 ab.normalize();
00113 msg( M_INIT, "TriangleFan ab %f, %f, %f a = %f, %f, %f", ab[0], ab[1], ab[2], a[0], a[1], a[2] );
00114
00115 if( v_i2 == vertices.end() ) break;
00116 Vector c = (*v_i2++)->getVertex();
00117 Vector ac = c-a;
00118 ac.normalize();
00119 msg( M_INIT, "TriangleFan ac %f, %f, %f b = %f, %f, %f", ac[0], ac[1], ac[2], b[0], b[1], b[2] );
00120
00121 float test = ab | ac;
00122 msg( M_INIT, "TriangleFan b %f, %f, %f c = %f, %f, %f test %f", b[0], b[1], b[2], c[0], c[1], c[2], test );
00123
00124 normal = (a-b)^(a-c);
00125 normal.normalize();
00126 len = normal.magnitude();
00127 if( len < 0.9 || len >11.1 ){
00128 emsg( M_MAT, "TriangleFan normal problem" );
00129 return;
00130 }
00131 normals.push_back( new Vector(normal) );
00132 if( test == -1.0 || test == 1.0 ){
00133 msg( M_INIT, "TriangleFan normal %f, %f, %f degenerate", normal[0], normal[1], normal[2] );
00134 }else{
00135 this->normal = normal;
00136 msg( M_INIT, "TriangleFan normal %f, %f, %f stored", normal[0], normal[1], normal[2] );
00137 }
00138 }while( v_i2 != vertices.end() );
00139
00140 enable( EL_HAS_ELEMENT_NORMAL | EL_USE_ELEMENT_NORMAL );
00141 }
00142
00143
00144 #if defined( TEDDY_INCLUDE_COLDET )
00145 int TriangleFan::addToCollisionModel( Teddy::ColDet::CollisionModel3D *collision_model ){
00146 int tris = 0;
00147 list<Vertex*>::const_iterator v_it = vertices.begin();
00148
00149 FloatVector a = (*v_it)->getVertex(); v_it++; if( v_it == vertices.end() ) return 0;
00150 FloatVector b = (*v_it)->getVertex(); v_it++;
00151 while( v_it != vertices.end() ){
00152 FloatVector c = (*v_it)->getVertex();
00153 collision_model->addTriangle( a, b, c );
00154 a = b;
00155 b = c;
00156 tris++;
00157 v_it++;
00158 }
00159 return tris;
00160 }
00161 #endif
00162
00163
00164
00165 unsigned long TriangleFan::countCSGFaceElements(){
00166 unsigned long size = vertices.size();
00167 return size - 2;
00168 }
00169
00170
00171
00172 };
00173 };
00174