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