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