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/Materials/Material.h"
00026 #include "Teddy/Models/Model.h"
00027 #include "Teddy/Models/Face.h"
00028 #include "Teddy/Models/Geometry.h"
00029 #include "Teddy/PhysicalComponents/Projection.h"
00030 #include "Teddy/Scenes/Camera.h"
00031 #include "Teddy/SysSupport/StdMaths.h"
00032 #include "Teddy/SysSupport/StdSDL.h"
00033 #include "Teddy/SysSupport/StdIO.h"
00034 using namespace Teddy::Graphics;
00035 using namespace Teddy::Materials;
00036 using namespace Teddy::Scenes;
00037
00038
00039 namespace Teddy {
00040 namespace Models {
00041
00042
00043 const unsigned long Model::OPT_VISIBLE = (1ul<<1ul);
00044 const unsigned long Model::OPT_RECURS_MATERIAL = (1ul<<2ul);
00045
00046
00052 Model::Model( std::string name, Model *shape )
00053 :
00054 Named ( name ),
00055 Options ( OPT_VISIBLE ),
00056 attitude ( 0.0f,0.0f,0.0f,1.0f ),
00057 position ( 0.0, 0.0 ,0.0 ),
00058 clip_radius( 0.0f ),
00059 geometry ( NULL ),
00060 material ( NULL )
00061 {
00062 if( shape != NULL ){
00063 add( shape );
00064 setClipRadius( shape->getClipRadius() );
00065 }
00066 }
00067
00068
00070 void Model::add( Model *child ){
00071 children.push_back( child );
00072 }
00073
00074
00079 void Model::add( Element *e ){
00080 if( geometry == NULL ){
00081 geometry = new Geometry();
00082 }
00083 geometry->insert( e );
00084 }
00085
00086
00088 void Model::remove( Model *child ){
00089 children.remove( child );
00090 }
00091
00092
00100 Material *Model::getMaterial() const {
00101 return material;
00102 }
00103
00104
00109 void Model::setMaterial( Material *m, bool recursive ){
00110 this->material = m;
00111 if( recursive ){
00112 list<Model*>::iterator m_it;
00113 m_it = children.begin();
00114 while( m_it != children.end() ){
00115 (*m_it)->setMaterial( m, recursive );
00116 m_it++;
00117 }
00118 }
00119 }
00120
00121
00122
00127 void Model::transformVertices( const Matrix &m ){
00128
00129
00130 Matrix normal_matrix = m;
00131 normal_matrix.invert ();
00132 normal_matrix.transpose();
00133 Vector x = m.get3Row( 0 );
00134 Vector y = m.get3Row( 1 );
00135 Vector z = m.get3Row( 2 );
00136 Vector z2 = x^y;
00137 double dp = z | z2;
00138 bool reverse = false;
00139
00140 if( dp < 0 ){
00141 reverse = true;
00142 }
00143
00144
00145 transformVertices( m, normal_matrix, reverse );
00146 }
00147
00148
00149 void Model::clearTraversal(){
00150 if( geometry != NULL ){
00151 geometry->clearTraversal();
00152 }
00153
00154 list<Model*>::iterator m_it;
00155 m_it = children.begin();
00156 while( m_it != children.end() ){
00157 (*m_it)->clearTraversal();
00158 m_it++;
00159 }
00160 }
00161
00162 void Model::transformVertices( const Matrix &m, const Matrix &normal_matrix, const bool reverse ){
00163 clearTraversal();
00164 if( geometry != NULL ){
00165 geometry->transformVertices( m, normal_matrix, reverse );
00166 }
00167
00168 list<Model*>::iterator m_it;
00169 m_it = children.begin();
00170 while( m_it != children.end() ){
00171 (*m_it)->transformVertices( m, normal_matrix, reverse );
00172 m_it++;
00173 }
00174 setupClipRadius();
00175 }
00176
00177
00178
00179
00180
00181
00182 void Model::setupClipRadius(){
00183 double max_len = geometry != NULL ? geometry->getMaxVector() : 0;
00184
00185 list<Model*>::iterator m_it;
00186 m_it = children.begin();
00187 while( m_it != children.end() ){
00188 Model *child = (*m_it);
00189 child->setupClipRadius();
00190 double len = child->getClipRadius();
00191 if( len>max_len ) max_len = len;
00192 m_it++;
00193 }
00194 setClipRadius( max_len );
00195 }
00196
00197
00198 void Model::setClipRadius( const double clip_radius ){
00199 this->clip_radius = clip_radius;
00200 }
00201
00202
00203 double Model::getClipRadius() const {
00204 return clip_radius;
00205 }
00206
00207
00208 void Model::setGeometry( Geometry *geometry ){
00209 this->geometry = geometry;
00210 }
00211
00212
00213 Geometry *Model::getGeometry() const {
00214 return geometry;
00215 }
00216
00217
00218 list<Model*> &Model::getChildren(){
00219 return children;
00220 }
00221
00222
00223 void Model::addTri( Vertex *a, Vertex *b, Vertex *c ){
00224 Face *face = new Face();
00225 face->add( a );
00226 face->add( b );
00227 face->add( c );
00228 face->makeConvexNormal();
00229 add( face );
00230 }
00231
00232
00233 void Model::addQuad( Vertex *a, Vertex *b, Vertex *c, Vertex *d ){
00234 Face *face = new Face();
00235 face->add( a );
00236 face->add( b );
00237 face->add( c );
00238 face->add( d );
00239 face->makeConvexNormal();
00240 add( face );
00241 }
00242
00243
00244 void Model::addPenta( Vertex *a, Vertex *b, Vertex *c, Vertex *d, Vertex *e ){
00245 Face *face = new Face();
00246 face->add( a );
00247 face->add( b );
00248 face->add( c );
00249 face->add( d );
00250 face->add( e );
00251 face->makeConvexNormal();
00252 add( face );
00253 }
00254
00255
00256 void Model::addHexa( Vertex *a, Vertex *b, Vertex *c, Vertex *d, Vertex *e, Vertex *f ){
00257 Face *face = new Face();
00258 face->add( a );
00259 face->add( b );
00260 face->add( c );
00261 face->add( d );
00262 face->add( e );
00263 face->add( f );
00264 face->makeConvexNormal();
00265 add( face );
00266 }
00267
00268
00269 void Model::addOcta( Vertex *a, Vertex *b, Vertex *c, Vertex *d, Vertex *e, Vertex *f, Vertex *g, Vertex *h ){
00270 Face *face = new Face();
00271 face->add( a );
00272 face->add( b );
00273 face->add( c );
00274 face->add( d );
00275 face->add( e );
00276 face->add( f );
00277 face->add( g );
00278 face->add( h );
00279 face->makeConvexNormal();
00280 add( face );
00281 }
00282
00283
00284 };
00285 };
00286