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

Capsule.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:  $
00022 */
00023 
00024 
00025 #include "Teddy/Models/Capsule.h"
00026 #include "Teddy/Models/QuadStrip.h"
00027 #include "Teddy/Models/Vertex.h"
00028 #include "Teddy/SysSupport/StdMaths.h"
00029 
00030 
00031 namespace Teddy  {
00032 namespace Models {
00033 
00034 
00044 Capsule::Capsule(
00045     const std::string &name, 
00046     const double       len,
00047     const double       rad,
00048     const int          stacks,
00049     const int          slices
00050 )
00051 :Model( name ){
00052 
00053     double r;
00054     double x;
00055     double y;
00056     double z;
00057     int    i;
00058     int    j;
00059     int    k;
00060 
00061     this->radius = rad;
00062     this->stacks = stacks;
00063     this->slices = slices;
00064 
00065     for( j=0; j<stacks/2; j++ ){
00066         QuadStrip *qs = new QuadStrip();
00067         for( i=0; i<slices+1; i++ ){
00068             for( k=0; k<2; k++ ){
00069                 z = radius*cos( (j+k)*M_PI/stacks );
00070                 r = radius*sin( (j+k)*M_PI/stacks );
00071                 y = r*cos( i*M_2_PI/slices );
00072                 x = r*sin( i*M_2_PI/slices );
00073                 Vertex *v = new Vertex( x, y, z + len/2 );
00074                 Vector normal = Vector( x, y, z );
00075                 normal.normalize();
00076                 v->setNormal( normal );
00077                 qs->add( v );
00078             }
00079         }
00080         add( qs );
00081     }
00082 
00083     j=stacks/2;
00084 
00085     QuadStrip *qs = new QuadStrip();
00086     for( i=0; i<slices+1; i++ ){
00087         for( k=0; k<2; k++ ){
00088             z = radius*cos( (j/*+k*/)*M_PI/stacks );
00089             r = radius*sin( (j/*+k*/)*M_PI/stacks );
00090             y = r*cos( i*M_2_PI/slices );
00091             x = r*sin( i*M_2_PI/slices );
00092             Vertex *v = new Vertex( x, y, z + len/2 - (k * len) );
00093             Vector normal = Vector( x, y, z );
00094             normal.normalize();
00095             v->setNormal( normal );
00096             qs->add( v );
00097         }
00098     }
00099     add( qs );
00100 
00101     for( j=stacks/2; j<stacks; j++ ){
00102         QuadStrip *qs = new QuadStrip();
00103         for( i=0; i<slices+1; i++ ){
00104             for( k=0; k<2; k++ ){
00105                 z = radius*cos( (j+k)*M_PI/stacks );
00106                 r = radius*sin( (j+k)*M_PI/stacks );
00107                 y = r*cos( i*M_2_PI/slices );
00108                 x = r*sin( i*M_2_PI/slices );
00109                 Vertex *v = new Vertex( x, y, z - len/2 );
00110                 Vector normal = Vector( x, y, z );
00111                 normal.normalize();
00112                 v->setNormal( normal );
00113                 qs->add( v );
00114             }
00115         }
00116         add( qs );
00117     }
00118 
00119     setupClipRadius();
00120 }
00121 
00122 
00123 };  //  namespace Models
00124 };  //  namespace Teddy
00125 
00126