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