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