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