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 library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the
20 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 // Boston, MA 02111-1307, USA.
26 #include <simgear/compiler.h>
33 #if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
39 // return a sphere object as an ssgBranch
40 ssgBranch *ssgMakeSphere( ssgSimpleState *state, ssgColourArray *cl,
41 double radius, int slices, int stacks,
42 ssgCallback predraw, ssgCallback postdraw )
44 float rho, drho, theta, dtheta;
49 ssgBranch *sphere = new ssgBranch;
53 drho = SGD_PI / (float) stacks;
54 dtheta = 2.0 * SGD_PI / (float) slices;
56 /* texturing: s goes from 0.0/0.25/0.5/0.75/1.0 at +y/+x/-y/-x/+y
57 axis t goes from -1.0/+1.0 at z = -radius/+radius (linear along
58 longitudes) cannot use triangle fan on texturing (s coord. at
59 top/bottom tip varies) */
63 t = 1.0; /* because loop now runs from 0 */
67 /* build slices as quad strips */
68 for ( i = imin; i < imax; i++ ) {
69 ssgVertexArray *vl = new ssgVertexArray();
70 ssgNormalArray *nl = new ssgNormalArray();
71 ssgTexCoordArray *tl = new ssgTexCoordArray();
75 for ( j = 0; j <= slices; j++ ) {
76 theta = (j == slices) ? 0.0 : j * dtheta;
77 x = -sin(theta) * sin(rho);
78 y = cos(theta) * sin(rho);
81 // glNormal3f( x*nsign, y*nsign, z*nsign );
82 sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
83 sgNormalizeVec3( vec3 );
87 sgSetVec2( vec2, s, t );
90 // glVertex3f( x*radius, y*radius, z*radius );
91 sgSetVec3( vec3, x*radius, y*radius, z*radius );
94 x = -sin(theta) * sin(rho+drho);
95 y = cos(theta) * sin(rho+drho);
96 z = nsign * cos(rho+drho);
98 // glNormal3f( x*nsign, y*nsign, z*nsign );
99 sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
100 sgNormalizeVec3( vec3 );
103 // glTexCoord2f(s,t-dt);
104 sgSetVec2( vec2, s, t-dt );
108 // glVertex3f( x*radius, y*radius, z*radius );
109 sgSetVec3( vec3, x*radius, y*radius, z*radius );
114 new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, cl );
116 if ( vl->getNum() != nl->getNum() ) {
117 cout << "bad sphere1" << endl;
120 if ( vl->getNum() != tl->getNum() ) {
121 cout << "bad sphere2" << endl;
124 slice->setState( state );
125 slice->setCallback( SSG_CALLBACK_PREDRAW, predraw );
126 slice->setCallback( SSG_CALLBACK_POSTDRAW, postdraw );
128 sphere->addKid( slice );