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