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/PhysicalComponents/Projection.h"
00026 #include "Teddy/Models/Vertex.h"
00027 #include "Teddy/Models/Face.h"
00028 #include "Teddy/SysSupport/Messages.h"
00029 using namespace Teddy::SysSupport;
00030
00031
00032 namespace Teddy {
00033 namespace Models {
00034
00035
00037 Vertex::Vertex()
00038 :
00039 Element ( 0 ),
00040 parent ( NULL ),
00041 vert ( 0, 0, 0 ),
00042 normal ( 0, 1, 0 ),
00043 color ( 1, 1, 1, 1 ),
00044 texturecoord( 0, 0, 0 )
00045 {
00046 }
00047
00048
00054 Vertex::Vertex( Vertex *v )
00055 :
00056 Element ( VX_HAS_PARENT | VX_USE_PARENT_ALL ),
00057 parent ( v ),
00058 vert ( 0, 0, 0 ),
00059 normal ( 0, 1, 0 ),
00060 color ( 1, 1, 1, 1 ),
00061 texturecoord( 0, 0, 0 )
00062 {
00063 }
00064
00065
00072 Vertex::Vertex( Vertex &v )
00073 :
00074 Element ( v.getOptions() ),
00075 parent ( v.getParent () ),
00076 vert ( v.vert ),
00077 normal ( v.normal ),
00078 color ( v.color ),
00079 texturecoord( v.texturecoord )
00080 {
00081 }
00082
00083
00085 Vertex::Vertex( const Vector &v )
00086 :
00087 Element ( VX_HAS_VERTEX | VX_USE_THIS_VERTEX ),
00088 parent ( NULL ),
00089 vert ( v ),
00090 normal ( 0, 1, 0 ),
00091 color ( 1, 1, 1, 1 ),
00092 texturecoord( 0, 0, 0 )
00093 {
00094 }
00095
00096
00098 Vertex::Vertex( const float x, const float y, const float z)
00099 :
00100 Element ( VX_HAS_VERTEX | VX_USE_THIS_VERTEX ),
00101 parent ( NULL ),
00102 vert ( x, y, z ),
00103 normal ( 0, 1, 0 ),
00104 color ( 1, 1, 1, 1 ),
00105 texturecoord( 0, 0, 0 )
00106 {
00107 }
00108
00109
00110 Vertex::~Vertex(){
00111 }
00112
00113
00115 void Vertex::neg(){
00116 vert.v[0] = -vert.v[0];
00117 vert.v[1] = -vert.v[1];
00118 vert.v[2] = -vert.v[2];
00119 }
00120
00121
00123 void Vertex::transform( const Matrix &m, const Matrix &normal_matrix ){
00124 vert = m * vert;
00125 normal = normal_matrix * normal;
00126 normal.normalize();
00127 }
00128
00129
00131 void Vertex::setColor( const Color &color ){
00132 enable ( VX_USE_THIS_COLOR | VX_HAS_COLOR );
00133 disable( VX_USE_PARENT_COLOR );
00134 this->color = color;
00135 }
00136
00137
00139 void Vertex::setNormal( const Vector &normal ){
00140 enable ( VX_USE_THIS_NORMAL | VX_HAS_NORMAL );
00141 disable( VX_USE_PARENT_NORMAL );
00142 this->normal = normal;
00143 }
00144 void Vertex::setNormal( const float x, const float y, const float z ){
00145 enable ( VX_USE_THIS_NORMAL | VX_HAS_NORMAL );
00146 disable( VX_USE_PARENT_NORMAL );
00147 normal = Vector( x, y, z );;
00148 }
00149
00150
00152 void Vertex::setVertex( const Vector &vertex ){
00153 enable ( VX_USE_THIS_VERTEX | VX_HAS_VERTEX );
00154 disable( VX_USE_PARENT_VERTEX );
00155 this->vert = vertex;
00156 }
00157 void Vertex::setVertex( const float x, const float y, const float z ){
00158 enable ( VX_USE_THIS_VERTEX | VX_HAS_VERTEX );
00159 disable( VX_USE_PARENT_VERTEX );
00160 vert = Vector( x, y, z );
00161 }
00162
00163
00165 void Vertex::addNormal( const Vector &add ){
00166 this->normal += add;
00167 }
00168
00169
00171 void Vertex::normNormal(){
00172 if( isDisabled(VX_HAS_NORMAL) ){
00173 emsg( M_VERT, "Vertex has no normal which to normalize" );
00174 return;
00175 }
00176
00177 enable ( VX_USE_THIS_NORMAL | VX_HAS_NORMAL );
00178 disable( VX_USE_PARENT_NORMAL );
00179 normal.normalize();
00180
00181
00182 float len = normal.magnitude();
00183 if( len > 1.1 || len < 0.9 ){
00184 emsg( M_VERT, "Bad normal" );
00185 }
00186 }
00187
00188
00190 void Vertex::setTexture( const Vector &texture ){
00191 enable ( VX_USE_THIS_TEXTURE | VX_HAS_TEXTURE );
00192 disable( VX_USE_PARENT_TEXTURE );
00193 this->texturecoord = texture;
00194 }
00195 void Vertex::setTexture( const float s, const float t, const float u ){
00196 enable ( VX_USE_THIS_TEXTURE | VX_HAS_TEXTURE );
00197 disable( VX_USE_PARENT_TEXTURE );
00198 texturecoord = Vector( s, t, u );
00199 }
00200
00201
00203 void Vertex::addFace( Face *face ){
00204 getRoot()->faces.push_back( face );
00205 }
00206
00207
00209 list<Face*> &Vertex::getFaces(){
00210 return getRoot()->faces;
00211 }
00212
00213
00215 Vertex *Vertex::getRoot(){
00216 Vertex *p = getParent();
00217 if( p==NULL ){
00218 return this;
00219 }else{
00220 return p->getRoot();
00221 }
00222 }
00223
00224
00226 Vertex *Vertex::getParent(){
00227 return this->parent;
00228 }
00229
00230
00232 Vector &Vertex::getVertex(){
00233 if( isEnabled(VX_USE_THIS_VERTEX|VX_HAS_VERTEX) ){
00234 return this->vert;
00235 }else if( isEnabled(VX_USE_PARENT_VERTEX|VX_HAS_PARENT) ){
00236 return parent->getVertex();
00237 }else{
00238 dmsg( M_VERT, "Vertex not found in vertex" );
00239 return this->vert;
00240 }
00241 }
00242
00244 Color &Vertex::getColor(){
00245 if( isEnabled(VX_USE_THIS_COLOR|VX_HAS_COLOR) ){
00246 return this->color;
00247 }else if( isEnabled(VX_USE_PARENT_COLOR|VX_HAS_PARENT) ){
00248 return parent->getColor();
00249 }else{
00250 emsg( M_VERT, "Color not found in vertex" );
00251 return this->color;
00252 }
00253 }
00254
00255
00257 Vector &Vertex::getNormal(){
00258 if( isEnabled(VX_USE_THIS_NORMAL|VX_HAS_NORMAL) ){
00259 return this->normal;
00260 }else if( isEnabled(VX_USE_PARENT_NORMAL|VX_HAS_PARENT) ){
00261 return parent->getNormal();
00262 }else{
00263 emsg( M_VERT, "Normal not found in vertex" );
00264 return this->normal;
00265 }
00266 }
00267
00268
00270 Vector &Vertex::getTexture(){
00271 if( isEnabled(VX_USE_THIS_TEXTURE|VX_HAS_TEXTURE) ){
00272 return this->texturecoord;
00273 }else if( isEnabled(VX_USE_PARENT_TEXTURE|VX_HAS_PARENT) ){
00274 return parent->getTexture();
00275 }else{
00276 emsg( M_VERT, "Texture coordinates not found in Vertex" );
00277 return this->texturecoord;
00278 }
00279 }
00280
00281
00291 void Vertex::draw( Projection *p ){
00292 applyColor ( p );
00293 applyNormal ( p );
00294 applyTexture( p );
00295 applyVertex ( p );
00296 }
00297
00298
00299 void Vertex::applyColor( Projection *p ){
00300 if( isEnabled(VX_USE_THIS_COLOR|VX_HAS_COLOR) ){
00301 p->color( color );
00302 }else if( isEnabled(VX_USE_PARENT_COLOR|VX_HAS_PARENT) ){
00303
00304 parent->applyColor( p );
00305 }else{
00306
00307 }
00308 }
00309
00310
00311 void Vertex::applyNormal( Projection *p ){
00312 if( isEnabled(VX_USE_THIS_NORMAL|VX_HAS_NORMAL) ){
00313 p->normal( normal );
00314 }else if( isEnabled(VX_USE_PARENT_NORMAL|VX_HAS_PARENT) ){
00315 dmsg( M_VERT, "Using parent normal" );
00316 parent->applyNormal( p );
00317 }else{
00318 debug();
00319 dmsg( M_VERT, "This vertex has no normal" );
00320 }
00321 }
00322
00323
00324 void Vertex::applyTexture( Projection *p ){
00325 if( isEnabled(VX_USE_THIS_TEXTURE|VX_HAS_TEXTURE) ){
00326 p->texture( texturecoord );
00327 }else if( isEnabled(VX_USE_PARENT_TEXTURE|VX_HAS_PARENT) ){
00328
00329 parent->applyTexture( p );
00330 }else{
00331
00332 }
00333 }
00334
00335
00336 void Vertex::applyVertex( Projection *p ){
00337 if( isEnabled(VX_USE_THIS_VERTEX|VX_HAS_VERTEX) ){
00338 p->vertex( vert );
00339 }else if( isEnabled(VX_USE_PARENT_VERTEX|VX_HAS_PARENT) ){
00340
00341 parent->applyVertex( p );
00342 }else{
00343
00344
00345 }
00346 }
00347
00348
00349 void Vertex::debug(){
00350 if( isEnabled(VX_HAS_PARENT ) ) dmsg( M_VERT, "VX_HAS_PARENT ");
00351 if( isEnabled(VX_HAS_VERTEX ) ) dmsg( M_VERT, "VX_HAS_VERTEX ");
00352 if( isEnabled(VX_HAS_NORMAL ) ) dmsg( M_VERT, "VX_HAS_NORMAL ");
00353 if( isEnabled(VX_HAS_COLOR ) ) dmsg( M_VERT, "VX_HAS_COLOR ");
00354 if( isEnabled(VX_HAS_TEXTURE ) ) dmsg( M_VERT, "VX_HAS_TEXTURE ");
00355
00356 if( isEnabled(VX_USE_THIS_VERTEX ) ) dmsg( M_VERT, "VX_USE_THIS_VERTEX ");
00357 if( isEnabled(VX_USE_THIS_NORMAL ) ) dmsg( M_VERT, "VX_USE_THIS_NORMAL ");
00358 if( isEnabled(VX_USE_THIS_COLOR ) ) dmsg( M_VERT, "VX_USE_THIS_COLOR ");
00359 if( isEnabled(VX_USE_THIS_TEXTURE ) ) dmsg( M_VERT, "VX_USE_THIS_TEXTURE ");
00360
00361 if( isEnabled(VX_USE_PARENT_VERTEX ) ) dmsg( M_VERT, "VX_USE_PARENT_VERTEX ");
00362 if( isEnabled(VX_USE_PARENT_NORMAL ) ) dmsg( M_VERT, "VX_USE_PARENT_NORMAL ");
00363 if( isEnabled(VX_USE_PARENT_COLOR ) ) dmsg( M_VERT, "VX_USE_PARENT_COLOR ");
00364 if( isEnabled(VX_USE_PARENT_TEXTURE) ) dmsg( M_VERT, "VX_USE_PARENT_TEXTURE");
00365 }
00366
00367
00368 double Vertex::getMaxVector() const {
00369 return vert.magnitude();
00370 }
00371
00372
00373 };
00374 };
00375