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

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