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