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

Sphere.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: Sphere.cpp,v 1.5 2002/02/16 12:41:39 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/Models/Sphere.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 
00041 Sphere::Sphere(
00042     const std::string &name,
00043     const double       rad,
00044     const int          stacks,
00045     const int          slices
00046 )
00047 :
00048 Model ( name   ),
00049 radius( rad    ),
00050 stacks( stacks ),
00051 slices( slices )
00052 {
00053     double r;
00054     double x;
00055     double y;
00056     double z;
00057     int    i;
00058     int    j;
00059     int    k;
00060 
00061     for( j=0; j<stacks; j++ ){
00062         QuadStrip *qs = new QuadStrip();
00063         for( i=0; i<slices+1; i++ ){
00064             for( k=1; k>=0; k-- ){
00065                 z = radius*cos( (j+k)*M_PI/stacks );
00066                 r = radius*sin( (j+k)*M_PI/stacks );
00067                 x = r*cos( i*M_2_PI/slices );
00068                 y = r*sin( i*M_2_PI/slices );
00069                 Vector normal = Vector( x, y, z );
00070                 normal.normalize();
00071                 double  s        = 1 - (double)(   i   ) / (double)( stacks );
00072                 double  t        =     (double)( (j+k) ) / (double)( slices );
00073                 Vertex *v        = new Vertex( x, y, z );
00074                 v->setNormal ( normal );
00075                 v->setTexture( s, t, 0.0f );
00076                 qs->add( v );
00077             }
00078         }
00079         add( qs );
00080     }
00081 
00082     setupClipRadius();
00083 }
00084 
00085 
00086 };  //  namespace Models
00087 };  //  namespace Teddy
00088