]> git.mxchange.org Git - simgear.git/blob - simgear/sky/sky.cxx
Moving cloud functionality to sky section.
[simgear.git] / simgear / sky / sky.cxx
1 // sky.cxx -- ssg based sky model
2 //
3 // Written by Curtis Olson, started December 1997.
4 // SSG-ified by Curtis Olson, February 2000.
5 //
6 // Copyright (C) 1997-2000  Curtis L. Olson  - curt@flightgear.org
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <plib/ssg.h>           // plib include
30
31 #include "sky.hxx"
32
33
34 // Constructor
35 SGSky::SGSky( void ) {
36 }
37
38
39 // Destructor
40 SGSky::~SGSky( void ) {
41 }
42
43
44 // initialize the sky and connect the components to the scene graph at
45 // the provided branch
46 void SGSky::build(  double sun_size, double moon_size,
47                     int nplanets, sgdVec3 *planet_data,
48                     double planet_dist,
49                     int nstars, sgdVec3 *star_data, double star_dist )
50 {
51     pre_root = new ssgRoot;
52     post_root = new ssgRoot;
53
54     pre_selector = new ssgSelector;
55     post_selector = new ssgSelector;
56
57     pre_transform = new ssgTransform;
58     post_transform = new ssgTransform;
59
60     dome = new SGSkyDome;
61     pre_transform -> addKid( dome->build() );
62
63     planets = new SGStars;
64     pre_transform -> addKid( planets->build(nplanets, planet_data,
65                                             planet_dist)
66                              );
67
68     stars = new SGStars;
69     pre_transform -> addKid( stars->build(nstars, star_data, star_dist) );
70     
71     moon = new SGMoon;
72     pre_transform -> addKid( moon->build(tex_path, moon_size) );
73
74     oursun = new SGSun;
75     pre_transform -> addKid( oursun->build(tex_path, sun_size) );
76
77     pre_selector->addKid( pre_transform );
78     pre_selector->clrTraversalMaskBits( SSGTRAV_HOT );
79
80     post_selector->addKid( post_transform );
81     post_selector->clrTraversalMaskBits( SSGTRAV_HOT );
82
83     pre_root->addKid( pre_selector );
84     post_root->addKid( post_selector );
85 }
86
87
88 // repaint the sky components based on current value of sun_angle,
89 // sky, and fog colors.
90 //
91 // sun angle in degrees relative to verticle
92 // 0 degrees = high noon
93 // 90 degrees = sun rise/set
94 // 180 degrees = darkest midnight
95 bool SGSky::repaint( sgVec4 sky_color, sgVec4 fog_color,
96                      double sun_angle, double moon_angle,
97                      int nplanets, sgdVec3 *planet_data,
98                      int nstars, sgdVec3 *star_data )
99 {
100     dome->repaint( sky_color, fog_color, sun_angle );
101     oursun->repaint( sun_angle );
102     moon->repaint( moon_angle );
103     planets->repaint( sun_angle, nplanets, planet_data );
104     stars->repaint( sun_angle, nstars, star_data );
105
106     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
107         cloud_layers[i]->repaint( fog_color );
108     }
109
110     return true;
111 }
112
113
114 // reposition the sky at the specified origin and orientation
115 //
116 // lon specifies a rotation about the Z axis
117 // lat specifies a rotation about the new Y axis
118 // spin specifies a rotation about the new Z axis (this allows
119 // additional orientation for the sunrise/set effects and is used by
120 // the skydome and perhaps clouds.
121 bool SGSky::reposition( sgVec3 view_pos, sgVec3 zero_elev, sgVec3 view_up,
122                         double lon, double lat, double spin,
123                         double gst, 
124                         double sun_ra, double sun_dec, double sun_dist,
125                         double moon_ra, double moon_dec, double moon_dist )
126 {
127     double angle = gst * 15;    // degrees
128     dome->reposition( zero_elev, lon, lat, spin );
129     oursun->reposition( view_pos, angle, sun_ra, sun_dec, sun_dist );
130     moon->reposition( view_pos, angle, moon_ra, moon_dec, moon_dist );
131     planets->reposition( view_pos, angle );
132     stars->reposition( view_pos, angle );
133
134     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
135         cloud_layers[i]->reposition( zero_elev, view_up, lon, lat );
136     }
137
138     return true;
139 }
140
141
142 // draw background portions of the sky
143 void SGSky::draw_background() {
144     ssgCullAndDraw( pre_root );
145 }
146
147
148 // draw scenery elements of the sky
149 void SGSky::draw_scene() {
150     ssgCullAndDraw( post_root );
151 }
152
153  
154 void SGSky::add_cloud_layer( double asl ) {
155     SGCloudLayer *layer = new SGCloudLayer;
156     post_transform -> addKid( layer->build(tex_path, 20000.0f, asl) );
157     cloud_layers.push_back( layer );
158 }