]> git.mxchange.org Git - simgear.git/blob - simgear/sky/stars.cxx
Norman Vine contributed an optimization to eliminate unnecessary color
[simgear.git] / simgear / 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
30 #include <stdio.h>
31 #include STL_IOSTREAM
32
33 #include <plib/sg.h>
34 #include <plib/ssg.h>
35
36 #include "stars.hxx"
37
38 #if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
39 SG_USING_STD(cout);
40 SG_USING_STD(endl);
41 #endif
42
43
44 // Set up star rendering call backs
45 static int sgStarPreDraw( ssgEntity *e ) {
46     /* cout << endl << "Star pre draw" << endl << "----------------" 
47        << endl << endl; */
48
49     ssgLeaf *f = (ssgLeaf *)e;
50     if ( f -> hasState () ) f->getState()->apply() ;
51
52     glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
53
54     glDisable( GL_DEPTH_TEST );
55     glDisable( GL_FOG );
56     // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
57
58     return true;
59 }
60
61 static int sgStarPostDraw( ssgEntity *e ) {
62     /* cout << endl << "Star post draw" << endl << "----------------" 
63        << endl << endl; */
64
65     glPopAttrib();
66
67     // glEnable( GL_DEPTH_TEST );
68     // glEnable( GL_FOG );
69
70     return true;
71 }
72
73
74 // Constructor
75 SGStars::SGStars( void ) {
76 }
77
78
79 // Destructor
80 SGStars::~SGStars( void ) {
81 }
82
83
84 // initialize the stars object and connect it into our scene graph root
85 ssgBranch * SGStars::build( int num, sgdVec3 *star_data, double star_dist ) {
86     sgVec4 color;
87
88     if ( star_data == NULL ) {
89         cout << "WARNING: null star data passed to SGStars::build()" << endl;
90     }
91
92     // set up the orb state
93     state = new ssgSimpleState();
94     state->disable( GL_LIGHTING );
95     state->disable( GL_CULL_FACE );
96     state->disable( GL_TEXTURE_2D );
97     state->enable( GL_COLOR_MATERIAL );
98     state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
99     state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
100     state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
101     state->enable( GL_BLEND );
102     state->disable( GL_ALPHA_TEST );
103
104     vl = new ssgVertexArray( num );
105     cl = new ssgColourArray( num );
106     // cl = new ssgColourArray( 1 );
107     // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
108     // cl->add( color );
109
110     // Build ssg structure
111     sgVec3 p;
112     for ( int i = 0; i < num; ++i ) {
113         // position seeded to arbitrary values
114         sgSetVec3( p, 
115                    star_dist * cos( star_data[i][0] )
116                    * cos( star_data[i][1] ),
117                    star_dist * sin( star_data[i][0] )
118                    * cos( star_data[i][1] ),
119                    star_dist * sin( star_data[i][1] )
120                    );
121         vl->add( p );
122
123         // color (magnitude)
124         sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
125         cl->add( color );
126     }
127
128     ssgLeaf *stars_obj = 
129         new ssgVtxTable ( GL_POINTS, vl, NULL, NULL, cl );
130     stars_obj->setState( state );
131     stars_obj->setCallback( SSG_CALLBACK_PREDRAW, sgStarPreDraw );
132     stars_obj->setCallback( SSG_CALLBACK_POSTDRAW, sgStarPostDraw );
133
134     // build the ssg scene graph sub tree for the sky and connected
135     // into the provide scene graph branch
136     stars_transform = new ssgTransform;
137
138     stars_transform->addKid( stars_obj );
139
140     cout << "stars = " << stars_transform << endl;
141
142     return stars_transform;
143 }
144
145
146 // repaint the sun colors based on current value of sun_angle in
147 // degrees relative to verticle
148 // 0 degrees = high noon
149 // 90 degrees = sun rise/set
150 // 180 degrees = darkest midnight
151 bool SGStars::repaint( double sun_angle, int num, sgdVec3 *star_data ) {
152     // double min = 100;
153     // double max = -100;
154     double mag, nmag, alpha, factor, cutoff;
155     float *color;
156
157     static int old_phase = -1;
158     int phase;
159
160     // determine which star structure to draw
161     if ( sun_angle > (0.5 * SGD_PI + 10.0 * SGD_DEGREES_TO_RADIANS ) ) {
162         // deep night
163         factor = 1.0;
164         cutoff = 4.5;
165         phase = 0;
166     } else if ( sun_angle > (0.5 * SGD_PI + 8.8 * SGD_DEGREES_TO_RADIANS ) ) {
167         factor = 1.0;
168         cutoff = 3.8;
169         phase = 1;
170     } else if ( sun_angle > (0.5 * SGD_PI + 7.5 * SGD_DEGREES_TO_RADIANS ) ) {
171         factor = 0.95;
172         cutoff = 3.1;
173         phase = 2;
174     } else if ( sun_angle > (0.5 * SGD_PI + 7.0 * SGD_DEGREES_TO_RADIANS ) ) {
175         factor = 0.9;
176         cutoff = 2.4;
177         phase = 3;
178     } else if ( sun_angle > (0.5 * SGD_PI + 6.5 * SGD_DEGREES_TO_RADIANS ) ) {
179         factor = 0.85;
180         cutoff = 1.8;
181         phase = 4;
182     } else if ( sun_angle > (0.5 * SGD_PI + 6.0 * SGD_DEGREES_TO_RADIANS ) ) {
183         factor = 0.8;
184         cutoff = 1.2;
185         phase = 5;
186     } else if ( sun_angle > (0.5 * SGD_PI + 5.5 * SGD_DEGREES_TO_RADIANS ) ) {
187         factor = 0.75;
188         cutoff = 0.6;
189         phase = 6;
190     } else {
191         // early dusk or late dawn
192         factor = 0.7;
193         cutoff = 0.0;
194         phase = 7;
195     }
196
197     if( phase != old_phase ) {
198         old_phase = phase;
199         for ( int i = 0; i < num; ++i ) {
200             // if ( star_data[i][2] < min ) { min = star_data[i][2]; }
201             // if ( star_data[i][2] > max ) { max = star_data[i][2]; }
202
203             // magnitude ranges from -1 (bright) to 4 (dim).  The
204             // range of star and planet magnitudes can actually go
205             // outside of this, but for our purpose, if it is brighter
206             // that -1, we'll color it full white/alpha anyway and 4
207             // is a convenient cutoff point which keeps the number of
208             // stars drawn at about 500.
209
210             // color (magnitude)
211             mag = star_data[i][2];
212             if ( mag < cutoff ) {
213                 nmag = ( 4.5 - mag ) / 5.5; // translate to 0 ... 1.0 scale
214                 // alpha = nmag * 0.7 + 0.3; // translate to a 0.3 ... 1.0 scale
215                 alpha = nmag * 0.85 + 0.15; // translate to a 0.15 ... 1.0 scale
216                 alpha *= factor;          // dim when the sun is brighter
217             } else {
218                 alpha = 0.0;
219             }
220
221             if (alpha > 1.0) { alpha = 1.0; }
222             if (alpha < 0.0) { alpha = 0.0; }
223
224             color = cl->get( i );
225             sgSetVec4( color, 1.0, 1.0, 1.0, alpha );
226             // cout << "alpha[" << i << "] = " << alpha << endl;
227         }
228     }
229
230     // cout << "min = " << min << " max = " << max << " count = " << num 
231     //      << endl;
232
233     return true;
234 }
235
236
237 // reposition the stars for the specified time (GST rotation),
238 // offset by our current position (p) so that it appears fixed at a
239 // great distance from the viewer.
240 bool SGStars::reposition( sgVec3 p, double angle )
241 {
242     sgMat4 T1, GST;
243     sgVec3 axis;
244
245     sgMakeTransMat4( T1, p );
246
247     sgSetVec3( axis, 0.0, 0.0, -1.0 );
248     sgMakeRotMat4( GST, angle, axis );
249
250     sgMat4 TRANSFORM;
251     sgCopyMat4( TRANSFORM, T1 );
252     sgPreMultMat4( TRANSFORM, GST );
253
254     sgCoord skypos;
255     sgSetCoord( &skypos, TRANSFORM );
256
257     stars_transform->setTransform( &skypos );
258
259     return true;
260 }