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 program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // 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., 675 Mass Ave, Cambridge, MA 02139, USA.
45 // Set up star rendering call backs
46 static int sgStarPreDraw( ssgEntity *e ) {
47 /* cout << endl << "Star pre draw" << endl << "----------------"
49 glDisable( GL_DEPTH_TEST );
51 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
56 static int sgStarPostDraw( ssgEntity *e ) {
57 /* cout << endl << "Star post draw" << endl << "----------------"
59 glEnable( GL_DEPTH_TEST );
67 SGStars::SGStars( void ) {
72 SGStars::~SGStars( void ) {
76 // initialize the stars object and connect it into our scene graph root
77 ssgBranch * SGStars::build( int num, sgdVec3 *star_data, double star_dist ) {
80 if ( star_data == NULL ) {
81 cout << "WARNING: null star data passed to SGStars::build()" << endl;
84 // set up the orb state
85 state = new ssgSimpleState();
86 state->disable( GL_LIGHTING );
87 state->disable( GL_CULL_FACE );
88 state->disable( GL_TEXTURE_2D );
89 state->enable( GL_COLOR_MATERIAL );
90 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
91 state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
92 state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
93 state->enable( GL_BLEND );
94 state->disable( GL_ALPHA_TEST );
96 vl = new ssgVertexArray( num );
97 cl = new ssgColourArray( num );
98 // cl = new ssgColourArray( 1 );
99 // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
102 // Build ssg structure
104 for ( int i = 0; i < num; ++i ) {
105 // position seeded to arbitrary values
107 star_dist * cos( star_data[i][0] )
108 * cos( star_data[i][1] ),
109 star_dist * sin( star_data[i][0] )
110 * cos( star_data[i][1] ),
111 star_dist * sin( star_data[i][1] )
116 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
121 new ssgVtxTable ( GL_POINTS, vl, NULL, NULL, cl );
122 stars_obj->setState( state );
123 stars_obj->setCallback( SSG_CALLBACK_PREDRAW, sgStarPreDraw );
124 stars_obj->setCallback( SSG_CALLBACK_POSTDRAW, sgStarPostDraw );
126 // build the ssg scene graph sub tree for the sky and connected
127 // into the provide scene graph branch
128 stars_transform = new ssgTransform;
130 stars_transform->addKid( stars_obj );
132 cout << "stars = " << stars_transform << endl;
134 return stars_transform;
138 // repaint the sun colors based on current value of sun_angle in
139 // degrees relative to verticle
140 // 0 degrees = high noon
141 // 90 degrees = sun rise/set
142 // 180 degrees = darkest midnight
143 bool SGStars::repaint( double sun_angle, int num, sgdVec3 *star_data ) {
145 // double max = -100;
146 double mag, nmag, alpha, factor, cutoff;
149 // determine which star structure to draw
150 if ( sun_angle > (2 * SGD_PI + 10.0 * SGD_DEGREES_TO_RADIANS ) ) {
154 } else if ( sun_angle > (2 * SGD_PI + 8.8 * SGD_DEGREES_TO_RADIANS ) ) {
157 } else if ( sun_angle > (2 * SGD_PI + 7.5 * SGD_DEGREES_TO_RADIANS ) ) {
160 } else if ( sun_angle > (2 * SGD_PI + 7.0 * SGD_DEGREES_TO_RADIANS ) ) {
163 } else if ( sun_angle > (2 * SGD_PI + 6.5 * SGD_DEGREES_TO_RADIANS ) ) {
166 } else if ( sun_angle > (2 * SGD_PI + 6.0 * SGD_DEGREES_TO_RADIANS ) ) {
169 } else if ( sun_angle > (2 * SGD_PI + 5.5 * SGD_DEGREES_TO_RADIANS ) ) {
173 // early dusk or late dawn
178 for ( int i = 0; i < num; ++i ) {
179 // if ( star_data[i][2] < min ) { min = star_data[i][2]; }
180 // if ( star_data[i][2] > max ) { max = star_data[i][2]; }
182 // magnitude ranges from -1 (bright) to 4 (dim). The range of
183 // star and planet magnitudes can actually go outside of this,
184 // but for our purpose, if it is brighter that -1, we'll color
185 // it full white/alpha anyway and 4 is a convenient cutoff
186 // point which keeps the number of stars drawn at about 500.
189 mag = star_data[i][2];
190 if ( mag < cutoff ) {
191 nmag = ( 4.5 - mag ) / 5.5; // translate to 0 ... 1.0 scale
192 // alpha = nmag * 0.7 + 0.3; // translate to a 0.3 ... 1.0 scale
193 alpha = nmag * 0.85 + 0.15; // translate to a 0.15 ... 1.0 scale
194 alpha *= factor; // dim when the sun is brighter
199 if (alpha > 1.0) { alpha = 1.0; }
200 if (alpha < 0.0) { alpha = 0.0; }
202 color = cl->get( i );
203 sgSetVec4( color, 1.0, 1.0, 1.0, alpha );
204 // cout << "alpha[" << i << "] = " << alpha << endl;
207 // cout << "min = " << min << " max = " << max << " count = " << num
214 // reposition the stars for the specified time (GST rotation),
215 // offset by our current position (p) so that it appears fixed at a
216 // great distance from the viewer.
217 bool SGStars::reposition( sgVec3 p, double angle )
222 sgMakeTransMat4( T1, p );
224 sgSetVec3( axis, 0.0, 0.0, -1.0 );
225 sgMakeRotMat4( GST, angle, axis );
228 sgCopyMat4( TRANSFORM, T1 );
229 sgPreMultMat4( TRANSFORM, GST );
232 sgSetCoord( &skypos, TRANSFORM );
234 stars_transform->setTransform( &skypos );