1 // stars.cxx -- model the stars
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).
8 // Separated out rendering pieces and converted to ssg by Curt Olson,
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.
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.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <simgear/compiler.h>
28 #include <simgear/constants.h>
29 #include <simgear/debug/logstream.hxx>
40 // Set up star rendering call backs
41 static int sgStarPreDraw( ssgEntity *e ) {
42 /* cout << endl << "Star pre draw" << endl << "----------------"
45 ssgLeaf *f = (ssgLeaf *)e;
46 if ( f -> hasState () ) f->getState()->apply() ;
48 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT | GL_COLOR_BUFFER_BIT );
50 glDisable( GL_DEPTH_TEST );
52 // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
57 static int sgStarPostDraw( ssgEntity *e ) {
58 /* cout << endl << "Star post draw" << endl << "----------------"
68 SGStars::SGStars( void ) :
75 SGStars::~SGStars( void ) {
79 // initialize the stars object and connect it into our scene graph root
80 ssgBranch * SGStars::build( int num, sgdVec3 *star_data, double star_dist ) {
83 if ( star_data == NULL )
84 SG_LOG( SG_EVENT, SG_WARN, "null star data passed to SGStars::build()");
87 // set up the orb state
88 state = new ssgSimpleState();
89 state->disable( GL_LIGHTING );
90 state->disable( GL_CULL_FACE );
91 state->disable( GL_TEXTURE_2D );
92 state->enable( GL_COLOR_MATERIAL );
93 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
94 state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
95 state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
96 state->enable( GL_BLEND );
97 state->disable( GL_ALPHA_TEST );
99 vl = new ssgVertexArray( num );
100 cl = new ssgColourArray( num );
101 // cl = new ssgColourArray( 1 );
102 // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
105 // Build ssg structure
107 for ( int i = 0; i < num; ++i ) {
108 // position seeded to arbitrary values
110 star_dist * cos( star_data[i][0] )
111 * cos( star_data[i][1] ),
112 star_dist * sin( star_data[i][0] )
113 * cos( star_data[i][1] ),
114 star_dist * sin( star_data[i][1] )
119 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
124 new ssgVtxTable ( GL_POINTS, vl, NULL, NULL, cl );
125 stars_obj->setState( state );
126 stars_obj->setCallback( SSG_CALLBACK_PREDRAW, sgStarPreDraw );
127 stars_obj->setCallback( SSG_CALLBACK_POSTDRAW, sgStarPostDraw );
129 // build the ssg scene graph sub tree for the sky and connected
130 // into the provide scene graph branch
131 stars_transform = new ssgTransform;
133 stars_transform->addKid( stars_obj );
135 SG_LOG( SG_EVENT, SG_INFO, "stars = " << stars_transform);
137 return stars_transform;
141 // repaint the sun colors based on current value of sun_angle in
142 // degrees relative to verticle
143 // 0 degrees = high noon
144 // 90 degrees = sun rise/set
145 // 180 degrees = darkest midnight
146 bool SGStars::repaint( double sun_angle, int num, sgdVec3 *star_data ) {
147 // cout << "repainting stars" << endl;
149 // double max = -100;
150 double mag, nmag, alpha, factor, cutoff;
155 // determine which star structure to draw
156 if ( sun_angle > (SGD_PI_2 + 10.0 * SGD_DEGREES_TO_RADIANS ) ) {
161 } else if ( sun_angle > (SGD_PI_2 + 8.8 * SGD_DEGREES_TO_RADIANS ) ) {
165 } else if ( sun_angle > (SGD_PI_2 + 7.5 * SGD_DEGREES_TO_RADIANS ) ) {
169 } else if ( sun_angle > (SGD_PI_2 + 7.0 * SGD_DEGREES_TO_RADIANS ) ) {
173 } else if ( sun_angle > (SGD_PI_2 + 6.5 * SGD_DEGREES_TO_RADIANS ) ) {
177 } else if ( sun_angle > (SGD_PI_2 + 6.0 * SGD_DEGREES_TO_RADIANS ) ) {
181 } else if ( sun_angle > (SGD_PI_2 + 5.5 * SGD_DEGREES_TO_RADIANS ) ) {
186 // early dusk or late dawn
192 if( phase != old_phase ) {
193 // cout << " phase change, repainting stars, num = " << num << endl;
195 for ( int i = 0; i < num; ++i ) {
196 // if ( star_data[i][2] < min ) { min = star_data[i][2]; }
197 // if ( star_data[i][2] > max ) { max = star_data[i][2]; }
199 // magnitude ranges from -1 (bright) to 4 (dim). The
200 // range of star and planet magnitudes can actually go
201 // outside of this, but for our purpose, if it is brighter
202 // that -1, we'll color it full white/alpha anyway and 4
203 // is a convenient cutoff point which keeps the number of
204 // stars drawn at about 500.
207 mag = star_data[i][2];
208 if ( mag < cutoff ) {
209 nmag = ( 4.5 - mag ) / 5.5; // translate to 0 ... 1.0 scale
210 // alpha = nmag * 0.7 + 0.3; // translate to a 0.3 ... 1.0 scale
211 alpha = nmag * 0.85 + 0.15; // translate to a 0.15 ... 1.0 scale
212 alpha *= factor; // dim when the sun is brighter
217 if (alpha > 1.0) { alpha = 1.0; }
218 if (alpha < 0.0) { alpha = 0.0; }
220 color = cl->get( i );
221 sgSetVec4( color, 1.0, 1.0, 1.0, alpha );
222 // cout << "alpha[" << i << "] = " << alpha << endl;
225 // cout << " no phase change, skipping" << endl;
228 // cout << "min = " << min << " max = " << max << " count = " << num
235 // reposition the stars for the specified time (GST rotation),
236 // offset by our current position (p) so that it appears fixed at a
237 // great distance from the viewer.
238 bool SGStars::reposition( sgVec3 p, double angle )
243 sgMakeTransMat4( T1, p );
245 sgSetVec3( axis, 0.0, 0.0, -1.0 );
246 sgMakeRotMat4( GST, angle, axis );
249 sgCopyMat4( TRANSFORM, T1 );
250 sgPreMultMat4( TRANSFORM, GST );
253 sgSetCoord( &skypos, TRANSFORM );
255 stars_transform->setTransform( &skypos );