]> git.mxchange.org Git - simgear.git/blob - simgear/sky/sphere.cxx
Changes for the native Irix CC compiler contributed by Erik Hofman.
[simgear.git] / simgear / sky / sphere.cxx
1 // sphere.cxx -- build an ssg sphere object
2 //
3 // Pulled straight out of MesaGLU/quadratic.c
4 //
5 // Original gluSphere code is Copyright (C) 1999-2000  Brian Paul and
6 // licensed under the GPL
7 //
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.
12 //
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.
17 //
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.
22 //
23 // $Id$
24
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 #include <simgear/compiler.h>
31
32 #include STL_IOSTREAM
33
34 #include <plib/sg.h>
35 #include <plib/ssg.h>
36
37 #if !defined (FG_HAVE_NATIVE_SGI_COMPILERS)
38 FG_USING_STD(cout);
39 FG_USING_STD(endl);
40 #endif
41
42
43 // return a sphere object as an ssgBranch
44 ssgBranch *ssgMakeSphere( ssgSimpleState *state, ssgColourArray *cl,
45                           double radius, int slices, int stacks,
46                           ssgCallback predraw, ssgCallback postdraw )
47 {
48     float rho, drho, theta, dtheta;
49     float x, y, z;
50     float s, t, ds, dt;
51     int i, j, imin, imax;
52     float nsign = 1.0;
53     ssgBranch *sphere = new ssgBranch;
54     sgVec2 vec2;
55     sgVec3 vec3;
56
57     drho = SG_PI / (float) stacks;
58     dtheta = 2.0 * SG_PI / (float) slices;
59
60     /* texturing: s goes from 0.0/0.25/0.5/0.75/1.0 at +y/+x/-y/-x/+y
61        axis t goes from -1.0/+1.0 at z = -radius/+radius (linear along
62        longitudes) cannot use triangle fan on texturing (s coord. at
63        top/bottom tip varies) */
64
65     ds = 1.0 / slices;
66     dt = 1.0 / stacks;
67     t = 1.0;  /* because loop now runs from 0 */
68     imin = 0;
69     imax = stacks;
70
71     /* build slices as quad strips */
72     for ( i = imin; i < imax; i++ ) {
73         ssgVertexArray   *vl = new ssgVertexArray();
74         ssgNormalArray   *nl = new ssgNormalArray();
75         ssgTexCoordArray *tl = new ssgTexCoordArray();
76
77         rho = i * drho;
78         s = 0.0;
79         for ( j = 0; j <= slices; j++ ) {
80             theta = (j == slices) ? 0.0 : j * dtheta;
81             x = -sin(theta) * sin(rho);
82             y = cos(theta) * sin(rho);
83             z = nsign * cos(rho);
84
85             // glNormal3f( x*nsign, y*nsign, z*nsign );
86             sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
87             sgNormalizeVec3( vec3 );
88             nl->add( vec3 );
89
90             // glTexCoord2f(s,t);
91             sgSetVec2( vec2, s, t );
92             tl->add( vec2 );
93
94             // glVertex3f( x*radius, y*radius, z*radius );
95             sgSetVec3( vec3, x*radius, y*radius, z*radius );
96             vl->add( vec3 );
97
98             x = -sin(theta) * sin(rho+drho);
99             y = cos(theta) * sin(rho+drho);
100             z = nsign * cos(rho+drho);
101
102             // glNormal3f( x*nsign, y*nsign, z*nsign );
103             sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
104             sgNormalizeVec3( vec3 );
105             nl->add( vec3 );
106
107             // glTexCoord2f(s,t-dt);
108             sgSetVec2( vec2, s, t-dt );
109             tl->add( vec2 );
110             s += ds;
111
112             // glVertex3f( x*radius, y*radius, z*radius );
113             sgSetVec3( vec3, x*radius, y*radius, z*radius );
114             vl->add( vec3 );
115         }
116
117         ssgLeaf *slice = 
118             new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, cl );
119
120         if ( vl->getNum() != nl->getNum() ) {
121             cout << "bad sphere1" << endl;
122             exit(-1);
123         }
124         if ( vl->getNum() != tl->getNum() ) {
125             cout << "bad sphere2" << endl;
126             exit(-1);
127         }
128         slice->setState( state );
129         slice->setCallback( SSG_CALLBACK_PREDRAW, predraw );
130         slice->setCallback( SSG_CALLBACK_POSTDRAW, postdraw );
131
132         sphere->addKid( slice );
133
134         t -= dt;
135     }
136
137     return sphere;
138 }