head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.40; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.58; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.04; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.22.01; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# sphere.cxx

    00001 // sphere.cxx -- build an ssg sphere object
    00002 //
    00003 // Pulled straight out of MesaGLU/quadratic.c
    00004 //
    00005 // Original gluSphere code is Copyright (C) 1999-2000  Brian Paul and
    00006 // licensed under the GPL
    00007 //
    00008 // This library is free software; you can redistribute it and/or
    00009 // modify it under the terms of the GNU Library General Public
    00010 // License as published by the Free Software Foundation; either
    00011 // version 2 of the License, or (at your option) any later version.
    00012 //
    00013 // This library is distributed in the hope that it will be useful,
    00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00016 // Library General Public License for more details.
    00017 //
    00018 // You should have received a copy of the GNU Library General Public
    00019 // License along with this library; if not, write to the
    00020 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00021 // Boston, MA  02111-1307, USA.
    00022 //
    00023 // $Id: sphere.cxx,v 1.16 2001/05/15 19:26:24 curt Exp $
    00024
    00025
    00026 #include <simgear/compiler.h>
    00027
    00028 #include STL_IOSTREAM
    00029
    00030 #include <plib/sg.h>
    00031 #include <plib/ssg.h>
    00032
    00033 #if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
    00034 SG_USING_STD(cout);
    00035 SG_USING_STD(endl);
    00036 #endif
    00037
    00038
    00039 // return a sphere object as an ssgBranch
    00040 ssgBranch *ssgMakeSphere( ssgSimpleState *state, ssgColourArray *cl,
    00041                           double radius, int slices, int stacks,
    00042                           ssgCallback predraw, ssgCallback postdraw )
    00043 {
    00044     float rho, drho, theta, dtheta;
    00045     float x, y, z;
    00046     float s, t, ds, dt;
    00047     int i, j, imin, imax;
    00048     float nsign = 1.0;
    00049     ssgBranch *sphere = new ssgBranch;
    00050     sgVec2 vec2;
    00051     sgVec3 vec3;
    00052
    00053     drho = SGD_PI / (float) stacks;
    00054     dtheta = 2.0 * SGD_PI / (float) slices;
    00055
    00056     /* texturing: s goes from 0.0/0.25/0.5/0.75/1.0 at +y/+x/-y/-x/+y
    00057        axis t goes from -1.0/+1.0 at z = -radius/+radius (linear along
    00058        longitudes) cannot use triangle fan on texturing (s coord. at
    00059        top/bottom tip varies) */
    00060
    00061     ds = 1.0 / slices;
    00062     dt = 1.0 / stacks;
    00063     t = 1.0;  /* because loop now runs from 0 */
    00064     imin = 0;
    00065     imax = stacks;
    00066
    00067     /* build slices as quad strips */
    00068     for ( i = imin; i < imax; i++ ) {
    00069         ssgVertexArray   *vl = new ssgVertexArray();
    00070         ssgNormalArray   *nl = new ssgNormalArray();
    00071         ssgTexCoordArray *tl = new ssgTexCoordArray();
    00072
    00073         rho = i * drho;
    00074         s = 0.0;
    00075         for ( j = 0; j <= slices; j++ ) {
    00076             theta = (j == slices) ? 0.0 : j * dtheta;
    00077             x = -sin(theta) * sin(rho);
    00078             y = cos(theta) * sin(rho);
    00079             z = nsign * cos(rho);
    00080
    00081             // glNormal3f( x*nsign, y*nsign, z*nsign );
    00082             sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
    00083             sgNormalizeVec3( vec3 );
    00084             nl->add( vec3 );
    00085
    00086             // glTexCoord2f(s,t);
    00087             sgSetVec2( vec2, s, t );
    00088             tl->add( vec2 );
    00089
    00090             // glVertex3f( x*radius, y*radius, z*radius );
    00091             sgSetVec3( vec3, x*radius, y*radius, z*radius );
    00092             vl->add( vec3 );
    00093
    00094             x = -sin(theta) * sin(rho+drho);
    00095             y = cos(theta) * sin(rho+drho);
    00096             z = nsign * cos(rho+drho);
    00097
    00098             // glNormal3f( x*nsign, y*nsign, z*nsign );
    00099             sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
    00100             sgNormalizeVec3( vec3 );
    00101             nl->add( vec3 );
    00102
    00103             // glTexCoord2f(s,t-dt);
    00104             sgSetVec2( vec2, s, t-dt );
    00105             tl->add( vec2 );
    00106             s += ds;
    00107
    00108             // glVertex3f( x*radius, y*radius, z*radius );
    00109             sgSetVec3( vec3, x*radius, y*radius, z*radius );
    00110             vl->add( vec3 );
    00111         }
    00112
    00113         ssgLeaf *slice =
    00114             new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, cl );
    00115
    00116         if ( vl->getNum() != nl->getNum() ) {
    00117             cout << "bad sphere1" << endl;
    00118             exit(-1);
    00119         }
    00120         if ( vl->getNum() != tl->getNum() ) {
    00121             cout << "bad sphere2" << endl;
    00122             exit(-1);
    00123         }
    00124         slice->setState( state );
    00125         slice->setCallback( SSG_CALLBACK_PREDRAW, predraw );
    00126         slice->setCallback( SSG_CALLBACK_POSTDRAW, postdraw );
    00127
    00128         sphere->addKid( slice );
    00129
    00130         t -= dt;
    00131     }
    00132
    00133     return sphere;
    00134 }

@ 1.3 log @Oops, try again. @ text @d143 1 a143 1