1 // sphere.cxx -- build an ssg sphere object
3 // Pulled straight out of MesaGLU/quadratic.c
5 // Original gluSphere code is Copyright (C) 1999-2000 Brian Paul and
6 // licensed under the GPL
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 // return a sphere object as an ssgBranch
29 ssgBranch *ssgMakeSphere( ssgSimpleState *state, ssgColourArray *cl,
30 double radius, int slices, int stacks )
32 float rho, drho, theta, dtheta;
37 ssgBranch *sphere = new ssgBranch;
41 // handle cl whether it is preinitialized or not
43 // create a new array if needed
44 cl = new ssgColourArray( 1 );
48 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
50 if ( cl->getNum() > 1 ) {
53 } else if ( cl->getNum() == 0 ) {
56 // accept value as given to us in
59 drho = M_PI / (float) stacks;
60 dtheta = 2.0 * M_PI / (float) slices;
62 /* texturing: s goes from 0.0/0.25/0.5/0.75/1.0 at +y/+x/-y/-x/+y
63 axis t goes from -1.0/+1.0 at z = -radius/+radius (linear along
64 longitudes) cannot use triangle fan on texturing (s coord. at
65 top/bottom tip varies) */
69 t = 1.0; /* because loop now runs from 0 */
73 /* build slices as quad strips */
74 for ( i = imin; i < imax; i++ ) {
75 ssgVertexArray *vl = new ssgVertexArray();
76 ssgNormalArray *nl = new ssgNormalArray();
77 ssgTexCoordArray *tl = new ssgTexCoordArray();
81 for ( j = 0; j <= slices; j++ ) {
82 theta = (j == slices) ? 0.0 : j * dtheta;
83 x = -sin(theta) * sin(rho);
84 y = cos(theta) * sin(rho);
87 // glNormal3f( x*nsign, y*nsign, z*nsign );
88 sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
89 sgNormalizeVec3( vec3 );
93 sgSetVec2( vec2, s, t );
96 // glVertex3f( x*radius, y*radius, z*radius );
97 sgSetVec3( vec3, x*radius, y*radius, z*radius );
100 x = -sin(theta) * sin(rho+drho);
101 y = cos(theta) * sin(rho+drho);
102 z = nsign * cos(rho+drho);
104 // glNormal3f( x*nsign, y*nsign, z*nsign );
105 sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
106 sgNormalizeVec3( vec3 );
109 // glTexCoord2f(s,t-dt);
110 sgSetVec2( vec2, s, t-dt );
114 // glVertex3f( x*radius, y*radius, z*radius );
115 sgSetVec3( vec3, x*radius, y*radius, z*radius );
120 new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, cl );
121 slice->setState( state );
123 sphere->addKid( slice );