]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/stars.cxx
Clean up some compiler warnings.
[simgear.git] / simgear / scene / sky / stars.cxx
1 // stars.cxx -- model the stars
2 //
3 // Written by Durk Talsma. Originally started October 1997, for distribution  
4 // with the FlightGear project. Version 2 was written in August and 
5 // September 1998. This code is based upon algorithms and data kindly 
6 // provided by Mr. Paul Schlyter. (pausch@saaf.se). 
7 //
8 // Separated out rendering pieces and converted to ssg by Curt Olson,
9 // March 2000
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Library General Public License for more details.
19 //
20 // You should have received a copy of the GNU Library General Public
21 // License along with this library; if not, write to the
22 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 // Boston, MA  02111-1307, USA.
24 //
25 // $Id$
26
27
28 #include <simgear/compiler.h>
29 #include <simgear/constants.h>
30 #include <simgear/debug/logstream.hxx>
31
32 #include <stdio.h>
33 #include STL_IOSTREAM
34
35 #include <plib/sg.h>
36 #include <plib/ssg.h>
37
38 #include "stars.hxx"
39
40
41 // Set up star rendering call backs
42 static int sgStarPreDraw( ssgEntity *e ) {
43     /* cout << endl << "Star pre draw" << endl << "----------------" 
44        << endl << endl; */
45
46     ssgLeaf *f = (ssgLeaf *)e;
47     if ( f -> hasState () ) f->getState()->apply() ;
48
49     glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT | GL_COLOR_BUFFER_BIT );
50
51     glDisable( GL_DEPTH_TEST );
52     glDisable( GL_FOG );
53     // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
54
55     return true;
56 }
57
58 static int sgStarPostDraw( ssgEntity *e ) {
59     /* cout << endl << "Star post draw" << endl << "----------------" 
60        << endl << endl; */
61
62     glPopAttrib();
63
64     return true;
65 }
66
67
68 // Constructor
69 SGStars::SGStars( void ) :
70 old_phase(-1)
71 {
72 }
73
74
75 // Destructor
76 SGStars::~SGStars( void ) {
77 }
78
79
80 // initialize the stars object and connect it into our scene graph root
81 ssgBranch * SGStars::build( int num, sgdVec3 *star_data, double star_dist ) {
82     sgVec4 color;
83
84     if ( star_data == NULL )
85         SG_LOG( SG_EVENT, SG_WARN, "null star data passed to SGStars::build()");
86
87
88     // set up the orb state
89     state = new ssgSimpleState();
90     state->disable( GL_LIGHTING );
91     state->disable( GL_CULL_FACE );
92     state->disable( GL_TEXTURE_2D );
93     state->enable( GL_COLOR_MATERIAL );
94     state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
95     state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
96     state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
97     state->enable( GL_BLEND );
98     state->disable( GL_ALPHA_TEST );
99
100     vl = new ssgVertexArray( num );
101     cl = new ssgColourArray( num );
102     // cl = new ssgColourArray( 1 );
103     // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
104     // cl->add( color );
105
106     // Build ssg structure
107     sgVec3 p;
108     for ( int i = 0; i < num; ++i ) {
109         // position seeded to arbitrary values
110         sgSetVec3( p, 
111                    star_dist * cos( star_data[i][0] )
112                    * cos( star_data[i][1] ),
113                    star_dist * sin( star_data[i][0] )
114                    * cos( star_data[i][1] ),
115                    star_dist * sin( star_data[i][1] )
116                    );
117         vl->add( p );
118
119         // color (magnitude)
120         sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
121         cl->add( color );
122     }
123
124     ssgLeaf *stars_obj = 
125         new ssgVtxTable ( GL_POINTS, vl, NULL, NULL, cl );
126     stars_obj->setState( state );
127     stars_obj->setCallback( SSG_CALLBACK_PREDRAW, sgStarPreDraw );
128     stars_obj->setCallback( SSG_CALLBACK_POSTDRAW, sgStarPostDraw );
129
130     // build the ssg scene graph sub tree for the sky and connected
131     // into the provide scene graph branch
132     stars_transform = new ssgTransform;
133
134     stars_transform->addKid( stars_obj );
135
136     SG_LOG( SG_EVENT, SG_INFO, "stars = " << stars_transform);
137
138     return stars_transform;
139 }
140
141
142 // repaint the sun colors based on current value of sun_angle in
143 // degrees relative to verticle
144 // 0 degrees = high noon
145 // 90 degrees = sun rise/set
146 // 180 degrees = darkest midnight
147 bool SGStars::repaint( double sun_angle, int num, sgdVec3 *star_data ) {
148     // cout << "repainting stars" << endl;
149     // double min = 100;
150     // double max = -100;
151     double mag, nmag, alpha, factor, cutoff;
152     float *color;
153
154     int phase;
155
156     // determine which star structure to draw
157     if ( sun_angle > (SGD_PI_2 + 10.0 * SGD_DEGREES_TO_RADIANS ) ) {
158         // deep night
159         factor = 1.0;
160         cutoff = 4.5;
161         phase = 0;
162     } else if ( sun_angle > (SGD_PI_2 + 8.8 * SGD_DEGREES_TO_RADIANS ) ) {
163         factor = 1.0;
164         cutoff = 3.8;
165         phase = 1;
166     } else if ( sun_angle > (SGD_PI_2 + 7.5 * SGD_DEGREES_TO_RADIANS ) ) {
167         factor = 0.95;
168         cutoff = 3.1;
169         phase = 2;
170     } else if ( sun_angle > (SGD_PI_2 + 7.0 * SGD_DEGREES_TO_RADIANS ) ) {
171         factor = 0.9;
172         cutoff = 2.4;
173         phase = 3;
174     } else if ( sun_angle > (SGD_PI_2 + 6.5 * SGD_DEGREES_TO_RADIANS ) ) {
175         factor = 0.85;
176         cutoff = 1.8;
177         phase = 4;
178     } else if ( sun_angle > (SGD_PI_2 + 6.0 * SGD_DEGREES_TO_RADIANS ) ) {
179         factor = 0.8;
180         cutoff = 1.2;
181         phase = 5;
182     } else if ( sun_angle > (SGD_PI_2 + 5.5 * SGD_DEGREES_TO_RADIANS ) ) {
183         factor = 0.75;
184         cutoff = 0.6;
185         phase = 6;
186     } else {
187         // early dusk or late dawn
188         factor = 0.7;
189         cutoff = 0.0;
190         phase = 7;
191     }
192
193     if( phase != old_phase ) {
194         // cout << "  phase change, repainting stars, num = " << num << endl;
195         old_phase = phase;
196         for ( int i = 0; i < num; ++i ) {
197             // if ( star_data[i][2] < min ) { min = star_data[i][2]; }
198             // if ( star_data[i][2] > max ) { max = star_data[i][2]; }
199
200             // magnitude ranges from -1 (bright) to 4 (dim).  The
201             // range of star and planet magnitudes can actually go
202             // outside of this, but for our purpose, if it is brighter
203             // that -1, we'll color it full white/alpha anyway and 4
204             // is a convenient cutoff point which keeps the number of
205             // stars drawn at about 500.
206
207             // color (magnitude)
208             mag = star_data[i][2];
209             if ( mag < cutoff ) {
210                 nmag = ( 4.5 - mag ) / 5.5; // translate to 0 ... 1.0 scale
211                 // alpha = nmag * 0.7 + 0.3; // translate to a 0.3 ... 1.0 scale
212                 alpha = nmag * 0.85 + 0.15; // translate to a 0.15 ... 1.0 scale
213                 alpha *= factor;          // dim when the sun is brighter
214             } else {
215                 alpha = 0.0;
216             }
217
218             if (alpha > 1.0) { alpha = 1.0; }
219             if (alpha < 0.0) { alpha = 0.0; }
220
221             color = cl->get( i );
222             sgSetVec4( color, 1.0, 1.0, 1.0, alpha );
223             // cout << "alpha[" << i << "] = " << alpha << endl;
224         }
225     } else {
226         // cout << "  no phase change, skipping" << endl;
227     }
228
229     // cout << "min = " << min << " max = " << max << " count = " << num 
230     //      << endl;
231
232     return true;
233 }
234
235
236 // reposition the stars for the specified time (GST rotation),
237 // offset by our current position (p) so that it appears fixed at a
238 // great distance from the viewer.
239 bool SGStars::reposition( sgVec3 p, double angle )
240 {
241     sgMat4 T1, GST;
242     sgVec3 axis;
243
244     sgMakeTransMat4( T1, p );
245
246     sgSetVec3( axis, 0.0, 0.0, -1.0 );
247     sgMakeRotMat4( GST, angle, axis );
248
249     sgMat4 TRANSFORM;
250     sgCopyMat4( TRANSFORM, T1 );
251     sgPreMultMat4( TRANSFORM, GST );
252
253     sgCoord skypos;
254     sgSetCoord( &skypos, TRANSFORM );
255
256     stars_transform->setTransform( &skypos );
257
258     return true;
259 }