]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/dome.cxx
Merge branch 'ehofman/sound'
[simgear.git] / simgear / scene / sky / dome.cxx
1 // dome.cxx -- model sky with an upside down "bowl"
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  - http://www.flightgear.org/~curt
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // Library 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21
22 #ifdef HAVE_CONFIG_H
23 #  include <simgear_config.h>
24 #endif
25
26 #include <math.h>
27
28 #include <simgear/compiler.h>
29
30 #include <osg/Array>
31 #include <osg/Geode>
32 #include <osg/Geometry>
33 #include <osg/Node>
34 #include <osg/Math>
35 #include <osg/MatrixTransform>
36 #include <osg/Material>
37 #include <osg/ShadeModel>
38 #include <osg/PrimitiveSet>
39
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/math/Math.hxx>
42 #include <simgear/scene/util/VectorArrayAdapter.hxx>
43
44 #include "dome.hxx"
45
46 using namespace osg;
47 using namespace simgear;
48
49 // proportions of max dimensions fed to the build() routine
50 static const float center_elev = 1.0;
51
52 namespace
53 {
54 struct DomeParam
55 {
56     float radius;
57     float elev;
58 } domeParams[] = {{.5, .8660},  // 60deg from horizon
59                   {.8660, .5},  // 30deg from horizon
60                   // Original dome horizon vertices
61                   {0.9701, 0.2425}, {0.9960, 0.0885},
62                   {1.0, 0.0}, {0.9922, -0.1240}};
63
64 const int numRings = sizeof(domeParams) / sizeof(domeParams[0]);
65 const int numBands = 12;
66 const int halfBands = numBands/2;
67 }
68
69 static const float upper_radius = 0.9701; // (.6, 0.15)
70 static const float upper_elev = 0.2425;
71
72 static const float middle_radius = 0.9960; // (.9, .08)
73 static const float middle_elev = 0.0885;
74
75 static const float lower_radius = 1.0;
76 static const float lower_elev = 0.0;
77
78 static const float bottom_radius = 0.9922; // (.8, -.1)
79 static const float bottom_elev = -0.1240;
80
81
82 // Constructor
83 SGSkyDome::SGSkyDome( void ) {
84     asl = 0;
85 }
86
87
88 // Destructor
89 SGSkyDome::~SGSkyDome( void ) {
90 }
91
92 // Generate indices for a dome mesh. Assume a center vertex at 0, then
93 // rings of vertices. Each ring's vertices are stored together. An
94 // even number of longitudinal bands are assumed.
95
96 namespace
97 {
98 // Calculate the index of a vertex in the grid by using its address in
99 // the array that holds its location.
100 struct GridIndex
101 {
102     VectorArrayAdapter<Vec3Array> gridAdapter;
103     Vec3Array& grid;
104     GridIndex(Vec3Array& array, int rowStride, int baseOffset) :
105         gridAdapter(array, rowStride, baseOffset), grid(array)
106     {
107     }
108     unsigned short operator() (int ring, int band)
109     {
110         return (unsigned short)(&gridAdapter(ring, band) - &grid[0]);
111     }
112 };
113 }
114 void SGSkyDome::makeDome(int rings, int bands, DrawElementsUShort& elements)
115 {
116     std::back_insert_iterator<DrawElementsUShort> pusher
117         = std::back_inserter(elements);
118     GridIndex grid(*dome_vl, numBands, 1);
119     for (int i = 0; i < bands; i += 2) {
120         *pusher = 0;  *pusher = grid(0, i);  *pusher = grid(0, i + 1);  
121         // down a band
122         for (int j = 0; j < rings - 1; ++j) {
123             *pusher = grid(j, i);  *pusher = grid(j, i + 1);
124             *pusher = grid(j + 1, i + 1);
125             *pusher = grid(j, i);  *pusher =  grid(j + 1, i + 1);
126             *pusher =  grid(j + 1, i);
127         }
128         // and up the next one
129         for (int j = rings - 1; j > 0; --j) {
130             *pusher = grid(j, i + 1);  *pusher = grid(j - 1, i + 1);
131             *pusher = grid(j, (i + 2) % bands);
132             *pusher = grid(j, (i + 2) % bands); *pusher = grid(j - 1, i + 1);
133             *pusher = grid(j - 1, (i + 2) % bands);
134         }
135         *pusher = grid(0, i + 1);  *pusher = 0;
136         *pusher = grid(0, (i + 2) % bands);
137     }
138 }
139
140 // initialize the sky object and connect it into our scene graph
141 osg::Node*
142 SGSkyDome::build( double hscale, double vscale ) {
143
144     osg::Geode* geode = new osg::Geode;
145
146     // set up the state
147     osg::StateSet* stateSet = geode->getOrCreateStateSet();
148     stateSet->setRenderBinDetails(-10, "RenderBin");
149
150     osg::ShadeModel* shadeModel = new osg::ShadeModel;
151     shadeModel->setMode(osg::ShadeModel::SMOOTH);
152     stateSet->setAttributeAndModes(shadeModel);
153     stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
154     stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
155     stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
156     stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
157     stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF);
158     stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
159     osg::Material* material = new osg::Material;
160     stateSet->setAttribute(material);
161
162     dome_vl = new osg::Vec3Array(1 + numRings * numBands);
163     dome_cl = new osg::Vec3Array(1 + numRings * numBands);
164     // generate the raw vertex data
165
166     (*dome_vl)[0].set(0.0, 0.0, center_elev * vscale);
167     simgear::VectorArrayAdapter<Vec3Array> vertices(*dome_vl, numBands, 1);
168
169     for ( int i = 0; i < numBands; ++i ) {
170         double theta = (i * 30) * SGD_DEGREES_TO_RADIANS;
171         double sTheta = hscale*sin(theta);
172         double cTheta = hscale*cos(theta);
173         for (int j = 0; j < numRings; ++j) {
174             vertices(j, i).set(cTheta * domeParams[j].radius,
175                                sTheta * domeParams[j].radius,
176                                domeParams[j].elev * vscale);
177         }
178     }
179
180     DrawElementsUShort* domeElements
181         = new osg::DrawElementsUShort(GL_TRIANGLES);
182     makeDome(numRings, numBands, *domeElements);
183     osg::Geometry* geom = new Geometry;
184     geom->setName("Dome Elements");
185     geom->setUseDisplayList(false);
186     geom->setVertexArray(dome_vl.get());
187     geom->setColorArray(dome_cl.get());
188     geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
189     geom->setNormalBinding(osg::Geometry::BIND_OFF);
190     geom->addPrimitiveSet(domeElements);
191     geode->addDrawable(geom);
192     // force a repaint of the sky colors with ugly defaults
193     repaint(SGVec3f(1, 1, 1), SGVec3f(1, 1, 1), SGVec3f(1, 1, 1), 0.0, 5000.0 );
194     dome_transform = new osg::MatrixTransform;
195     dome_transform->addChild(geode);
196
197     return dome_transform.get();
198 }
199
200 static void fade_to_black(osg::Vec3 sky_color[], float asl, int count) {
201     const float ref_asl = 10000.0f;
202     const float d = exp( - asl / ref_asl );
203     for(int i = 0; i < count ; i++)
204         sky_color[i] *= d;
205 }
206
207 inline void clampColor(osg::Vec3& color)
208 {
209     color.x() = osg::clampTo(color.x(), 0.0f, 1.0f);
210     color.y() = osg::clampTo(color.y(), 0.0f, 1.0f);
211     color.z() = osg::clampTo(color.z(), 0.0f, 1.0f);
212 }
213
214 // repaint the sky colors based on current value of sun_angle, sky,
215 // and fog colors.  This updates the color arrays for ssgVtxTable.
216 // sun angle in degrees relative to verticle
217 // 0 degrees = high noon
218 // 90 degrees = sun rise/set
219 // 180 degrees = darkest midnight
220 bool
221 SGSkyDome::repaint( const SGVec3f& sun_color, const SGVec3f& sky_color,
222                     const SGVec3f& fog_color, double sun_angle, double vis )
223 {
224     SGVec3f outer_param, outer_diff;
225     SGVec3f middle_param, middle_diff;
226
227     // Check for sunrise/sunset condition
228     if (sun_angle > 80) {
229         // 0.0 - 0.4
230         double sunAngleFactor = 10.0 - fabs(90.0 - sun_angle);
231         static const SGVec3f outerConstant(1.0 / 20.0, 1.0 / 40.0, -1.0 / 30.0);
232         static const SGVec3f middleConstant(1.0 / 40.0, 1.0 / 80.0, 0.0);
233         outer_param = sunAngleFactor * outerConstant;
234         middle_param = sunAngleFactor * middleConstant;
235         outer_diff = (1.0 / 6.0) * outer_param;
236         middle_diff = (1.0 / 6.0) * middle_param;
237     } else {
238         outer_param = SGVec3f(0, 0, 0);
239         middle_param = SGVec3f(0, 0, 0);
240         outer_diff = SGVec3f(0, 0, 0);
241         middle_diff = SGVec3f(0, 0, 0);
242     }
243     // printf("  outer_red_param = %.2f  outer_red_diff = %.2f\n", 
244     //        outer_red_param, outer_red_diff);
245
246     // calculate transition colors between sky and fog
247     SGVec3f outer_amt = outer_param;
248     SGVec3f middle_amt = middle_param;
249
250     //
251     // First, recalulate the basic colors
252     //
253
254     // Magic factors for coloring the sky according visibility and
255     // zenith angle.
256     const double cvf = osg::clampBelow(vis, 45000.0);
257     const double vis_factor = osg::clampTo((vis - 1000.0) / 2000.0, 0.0, 1.0);
258     const float upperVisFactor = 1.0 - vis_factor * (0.7 + 0.3 * cvf/45000);
259     const float middleVisFactor = 1.0 - vis_factor * (0.1 + 0.85 * cvf/45000);
260
261     (*dome_cl)[0] = toOsg(sky_color);
262     simgear::VectorArrayAdapter<Vec3Array> colors(*dome_cl, numBands, 1);
263     const double saif = sun_angle/SG_PI;
264     static const SGVec3f blueShift(0.8, 1.0, 1.2);
265     const SGVec3f skyFogDelta = sky_color - fog_color;
266     const SGVec3f sunSkyDelta = sun_color - sky_color;
267     // For now the colors of the upper two rings are linearly
268     // interpolated between the zenith color and the first horizon
269     // ring color.
270     
271     for (int i = 0; i < halfBands+1; i++) {
272         SGVec3f diff = mult(skyFogDelta, blueShift);
273         diff *= (0.8 + saif - ((halfBands-i)/10));
274         colors(2, i) = toOsg(sky_color - upperVisFactor * diff);
275         colors(3, i) = toOsg(sky_color - middleVisFactor * diff + middle_amt);
276         colors(4, i) = toOsg(fog_color + outer_amt);
277         colors(0, i) = simgear::math::lerp(toOsg(sky_color), colors(2, i), .3942);
278         colors(1, i) = simgear::math::lerp(toOsg(sky_color), colors(2, i), .7885);
279         for (int j = 0; j < numRings - 1; ++j)
280             clampColor(colors(j, i));
281         outer_amt -= outer_diff;
282         middle_amt -= middle_diff;
283     }
284
285     for (int i = halfBands+1; i < numBands; ++i)
286         for (int j = 0; j < 5; ++j)
287             colors(j, i) = colors(j, numBands - i);
288
289     fade_to_black(&(*dome_cl)[0], asl * center_elev, 1);
290     for (int i = 0; i < numRings - 1; ++i)
291         fade_to_black(&colors(i, 0), (asl+0.05f) * domeParams[i].elev,
292                       numBands);
293
294     for ( int i = 0; i < numBands; i++ )
295         colors(numRings - 1, i) = toOsg(fog_color);
296     dome_cl->dirty();
297     return true;
298 }
299
300
301 // reposition the sky at the specified origin and orientation
302 // lon specifies a rotation about the Z axis
303 // lat specifies a rotation about the new Y axis
304 // spin specifies a rotation about the new Z axis (and orients the
305 // sunrise/set effects
306 bool
307 SGSkyDome::reposition( const SGVec3f& p, double _asl,
308                        double lon, double lat, double spin ) {
309     asl = _asl;
310
311     osg::Matrix T, LON, LAT, SPIN;
312
313     // Translate to view position
314     // Point3D zero_elev = current_view.get_cur_zero_elev();
315     // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
316     T.makeTranslate( toOsg(p) );
317
318     // printf("  Translated to %.2f %.2f %.2f\n", 
319     //        zero_elev.x, zero_elev.y, zero_elev.z );
320
321     // Rotate to proper orientation
322     // printf("  lon = %.2f  lat = %.2f\n",
323     //        lon * SGD_RADIANS_TO_DEGREES,
324     //        lat * SGD_RADIANS_TO_DEGREES);
325     // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
326     LON.makeRotate(lon, osg::Vec3(0, 0, 1));
327
328     // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
329     //             0.0, 1.0, 0.0 );
330     LAT.makeRotate(90.0 * SGD_DEGREES_TO_RADIANS - lat, osg::Vec3(0, 1, 0));
331
332     // xglRotatef( l->sun_rotation * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
333     SPIN.makeRotate(spin, osg::Vec3(0, 0, 1));
334
335     dome_transform->setMatrix( SPIN*LAT*LON*T );
336     return true;
337 }