1 // dome.cxx -- model sky with an upside down "bowl"
3 // Written by Curtis Olson, started December 1997.
4 // SSG-ified by Curtis Olson, February 2000.
6 // Copyright (C) 1997-2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
23 # include <simgear_config.h>
29 #include <simgear/compiler.h>
33 #include <osg/Geometry>
36 #include <osg/MatrixTransform>
37 #include <osg/Material>
38 #include <osg/ShadeModel>
39 #include <osg/PrimitiveSet>
40 #include <osg/CullFace>
42 #include <simgear/debug/logstream.hxx>
43 #include <simgear/math/Math.hxx>
44 #include <simgear/scene/util/VectorArrayAdapter.hxx>
45 #include <simgear/scene/material/Effect.hxx>
46 #include <simgear/scene/material/EffectGeode.hxx>
51 using namespace simgear;
53 // proportions of max dimensions fed to the build() routine
54 static const float center_elev = 1.0;
63 } domeParams[] = {{.5, .8660}, // 60deg from horizon
64 {.8660, .5}, // 30deg from horizon
65 // Original dome horizon vertices
72 const int numRings = 64; //sizeof(domeParams) / sizeof(domeParams[0]);
73 const int numBands = 64; // 12
74 const int halfBands = numBands / 2;
76 // Make dome a bit over half sphere
77 const float domeAngle = 120.0;
79 const float bandDelta = 360.0 / numBands;
80 const float ringDelta = domeAngle / (numRings+1);
82 // Which band is at horizon
83 const int halfRings = numRings * (90.0 / domeAngle);
84 const int upperRings = numRings * (60.0 / domeAngle); // top half
85 const int middleRings = numRings * (15.0 / domeAngle);
89 static const float upper_radius = 0.9701; // (.6, 0.15)
90 static const float upper_elev = 0.2425;
92 static const float middle_radius = 0.9960; // (.9, .08)
93 static const float middle_elev = 0.0885;
95 static const float lower_radius = 1.0;
96 static const float lower_elev = 0.0;
98 static const float bottom_radius = 0.9922; // (.8, -.1)
99 static const float bottom_elev = -0.1240;
103 SGSkyDome::SGSkyDome( void ) {
109 SGSkyDome::~SGSkyDome( void ) {
112 // Generate indices for a dome mesh. Assume a center vertex at 0, then
113 // rings of vertices. Each ring's vertices are stored together. An
114 // even number of longitudinal bands are assumed.
118 // Calculate the index of a vertex in the grid by using its address in
119 // the array that holds its location.
122 VectorArrayAdapter<Vec3Array> gridAdapter;
124 GridIndex(Vec3Array& array, int rowStride, int baseOffset) :
125 gridAdapter(array, rowStride, baseOffset), grid(array)
128 unsigned short operator() (int ring, int band)
130 return (unsigned short)(&gridAdapter(ring, band) - &grid[0]);
134 void SGSkyDome::makeDome(int rings, int bands, DrawElementsUShort& elements)
136 std::back_insert_iterator<DrawElementsUShort> pusher
137 = std::back_inserter(elements);
138 GridIndex grid(*dome_vl, numBands, 1);
139 for (int i = 0; i < bands; i++) {
140 *pusher = 0; *pusher = grid(0, i+1); *pusher = grid(0, i);
142 for (int j = 0; j < rings - 1; ++j) {
143 *pusher = grid(j, i); *pusher = grid(j, (i + 1)%bands);
144 *pusher = grid(j + 1, (i + 1)%bands);
145 *pusher = grid(j, i); *pusher = grid(j + 1, (i + 1)%bands);
146 *pusher = grid(j + 1, i);
148 // and up the next one
149 /* for (int j = rings - 1; j > 0; --j) {
150 *pusher = grid(j, i + 1); *pusher = grid(j - 1, i + 1);
151 *pusher = grid(j, (i + 2) % bands);
152 *pusher = grid(j, (i + 2) % bands); *pusher = grid(j - 1, i + 1);
153 *pusher = grid(j - 1, (i + 2) % bands);
155 *pusher = grid(0, i + 1); *pusher = 0;
156 *pusher = grid(0, (i + 2) % bands);*/
160 // initialize the sky object and connect it into our scene graph
162 SGSkyDome::build( double hscale, double vscale ) {
164 EffectGeode* geode = new EffectGeode;
165 // Geode* geode = new Geode;
166 geode->setName("Skydome");
167 geode->setCullingActive(false); // Prevent skydome from being culled away
169 Effect *effect = makeEffect("Effects/skydome", true);
171 geode->setEffect(effect);
174 osg::StateSet* stateSet = geode->getOrCreateStateSet();
175 stateSet->setRenderBinDetails(-10, "RenderBin");
177 osg::ShadeModel* shadeModel = new osg::ShadeModel;
178 shadeModel->setMode(osg::ShadeModel::SMOOTH);
179 stateSet->setAttributeAndModes(shadeModel);
180 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
181 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
182 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
183 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
184 stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF);
185 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
187 stateSet->setAttribute(new osg::CullFace(osg::CullFace::BACK));
189 osg::Material* material = new osg::Material;
190 stateSet->setAttribute(material);
192 dome_vl = new osg::Vec3Array(1 + numRings * numBands);
193 dome_cl = new osg::Vec3Array(1 + numRings * numBands);
194 // generate the raw vertex data
196 (*dome_vl)[0].set(0.0, 0.0, center_elev * vscale);
197 simgear::VectorArrayAdapter<Vec3Array> vertices(*dome_vl, numBands, 1);
199 for ( int i = 0; i < numBands; ++i ) {
200 double theta = (i * bandDelta) * SGD_DEGREES_TO_RADIANS;
201 double sTheta = hscale*sin(theta);
202 double cTheta = hscale*cos(theta);
203 for (int j = 0; j < numRings; ++j) {
204 vertices(j, i).set(cTheta * sin((j+1)*ringDelta*SGD_DEGREES_TO_RADIANS), //domeParams[j].radius,
205 sTheta * sin((j+1)*ringDelta*SGD_DEGREES_TO_RADIANS),// domeParams[j].radius,
206 vscale * cos((j+1)*ringDelta*SGD_DEGREES_TO_RADIANS)); //domeParams[j].elev * vscale);
210 DrawElementsUShort* domeElements
211 = new osg::DrawElementsUShort(GL_TRIANGLES);
212 makeDome(numRings, numBands, *domeElements);
213 osg::Geometry* geom = new Geometry;
214 geom->setName("Dome Elements");
215 geom->setUseDisplayList(false);
216 geom->setVertexArray(dome_vl.get());
217 geom->setColorArray(dome_cl.get());
218 geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
219 geom->setNormalBinding(osg::Geometry::BIND_OFF);
220 geom->addPrimitiveSet(domeElements);
221 geode->addDrawable(geom);
222 // force a repaint of the sky colors with ugly defaults
223 repaint(SGVec3f(1, 1, 1), SGVec3f(1, 1, 1), SGVec3f(1, 1, 1), 0.0, 5000.0 );
224 dome_transform = new osg::MatrixTransform;
225 dome_transform->addChild(geode);
227 return dome_transform.get();
230 static void fade_to_black(osg::Vec3 sky_color[], float asl, int count) {
231 const float ref_asl = 10000.0f;
232 const float d = exp( - asl / ref_asl );
233 for(int i = 0; i < count ; i++)
237 inline void clampColor(osg::Vec3& color)
239 color.x() = osg::clampTo(color.x(), 0.0f, 1.0f);
240 color.y() = osg::clampTo(color.y(), 0.0f, 1.0f);
241 color.z() = osg::clampTo(color.z(), 0.0f, 1.0f);
244 // repaint the sky colors based on current value of sun_angle, sky,
245 // and fog colors. This updates the color arrays for ssgVtxTable.
246 // sun angle in degrees relative to verticle
247 // 0 degrees = high noon
248 // 90 degrees = sun rise/set
249 // 180 degrees = darkest midnight
251 SGSkyDome::repaint( const SGVec3f& sun_color, const SGVec3f& sky_color,
252 const SGVec3f& fog_color, double sun_angle, double vis )
254 SGVec3f outer_param, outer_diff;
255 SGVec3f middle_param, middle_diff;
257 // Check for sunrise/sunset condition
258 if (sun_angle > 80) {
260 double sunAngleFactor = 10.0 - fabs(90.0 - sun_angle);
261 static const SGVec3f outerConstant(1.0 / 20.0, 1.0 / 40.0, -1.0 / 30.0);
262 static const SGVec3f middleConstant(1.0 / 40.0, 1.0 / 80.0, 0.0);
263 outer_param = sunAngleFactor * outerConstant;
264 middle_param = sunAngleFactor * middleConstant;
265 outer_diff = (1.0 / numRings) * outer_param;
266 middle_diff = (1.0 / numRings) * middle_param;
268 outer_param = SGVec3f(0, 0, 0);
269 middle_param = SGVec3f(0, 0, 0);
270 outer_diff = SGVec3f(0, 0, 0);
271 middle_diff = SGVec3f(0, 0, 0);
273 // printf(" outer_red_param = %.2f outer_red_diff = %.2f\n",
274 // outer_red_param, outer_red_diff);
276 // calculate transition colors between sky and fog
277 SGVec3f outer_amt = outer_param;
278 SGVec3f middle_amt = middle_param;
281 // First, recalulate the basic colors
284 // Magic factors for coloring the sky according visibility and
286 const double cvf = osg::clampBelow(vis, 45000.0);
287 const double vis_factor = osg::clampTo((vis - 1000.0) / 2000.0, 0.0, 1.0);
288 const float upperVisFactor = 1.0 - vis_factor * (0.7 + 0.3 * cvf/45000);
289 const float middleVisFactor = 1.0 - vis_factor * (0.1 + 0.85 * cvf/45000);
291 // Dome top is always sky_color
292 (*dome_cl)[0] = toOsg(sky_color);
293 simgear::VectorArrayAdapter<Vec3Array> colors(*dome_cl, numBands, 1);
294 const double saif = sun_angle/SG_PI;
295 static const SGVec3f blueShift(0.8, 1.0, 1.2);
296 const SGVec3f skyFogDelta = sky_color - fog_color;
297 const SGVec3f sunSkyDelta = sun_color - sky_color;
299 // For now the colors of the upper two rings are linearly
300 // interpolated between the zenith color and the first horizon
301 // ring color. Means angles from top to 30 degrees
303 for (int i = 0; i < halfBands+1; i++) {
304 SGVec3f diff = mult(skyFogDelta, blueShift);
305 diff *= (0.8 + saif - ((halfBands-i)/(float)(numBands-2)));
307 // Color the ~60 deg ring
308 colors(upperRings, i) = toOsg(sky_color - upperVisFactor * diff);
311 // Color top half by linear interpolation (90...60 degrees)
312 for (; j < upperRings; j++)
313 colors(j, i) = simgear::math::lerp(toOsg(sky_color), colors(upperRings, i), j / (float)upperRings);
315 j++; // Skip the 60 deg ring
316 // From 60 to ~85 degrees
317 for (int l = 0; j < upperRings + middleRings + 1; j++, l++)
318 colors(j, i) = simgear::math::lerp(colors(upperRings, i),
319 toOsg(sky_color - middleVisFactor * diff + middle_amt), l / (float)middleRings);
322 for (int l = 0; j < halfRings; j++, l++)
323 colors(j, i) = simgear::math::lerp(colors(upperRings + middleRings, i), toOsg(fog_color + outer_amt),
324 l / (float)(halfRings - upperRings - middleRings));
327 //colors(2, i) = toOsg(sky_color - upperVisFactor * diff);
328 //colors(3, i) = toOsg(sky_color - middleVisFactor * diff + middle_amt);
329 //colors(4, i) = toOsg(fog_color + outer_amt);
330 //colors(0, i) = simgear::math::lerp(toOsg(sky_color), colors(2, i), .3942);
331 //colors(1, i) = simgear::math::lerp(toOsg(sky_color), colors(2, i), .7885);
333 for (int j = 0; j < numRings - 1; ++j)
334 clampColor(colors(j, i));
336 outer_amt -= outer_diff;
337 middle_amt -= middle_diff;
340 // Other side of dome is mirror of the other
341 for (int i = halfBands+1; i < numBands; ++i)
342 for (int j = 0; j < numRings-1; ++j)
343 colors(j, i) = colors(j, numBands - i);
345 // Fade colors to black when going to space
346 // Center of dome is blackest and then fade decreases towards horizon
347 fade_to_black(&(*dome_cl)[0], asl * center_elev, 1);
348 for (int i = 0; i < numRings - 1; ++i) {
349 float fadeValue = (asl+0.05f) * cos(i*ringDelta*SGD_DEGREES_TO_RADIANS);
350 if(fadeValue < 0.0) fadeValue = 0.0; // Prevent brightening up if dome is over 90 degrees
351 fade_to_black(&colors(i, 0), fadeValue, //domeParams[i].elev,
355 // All rings below horizon are fog color
356 for ( int i = halfRings; i < numRings; i++)
357 for ( int j = 0; j < numBands; j++ )
358 colors(i, j) = toOsg(fog_color);
365 // reposition the sky at the specified origin and orientation
366 // lon specifies a rotation about the Z axis
367 // lat specifies a rotation about the new Y axis
368 // spin specifies a rotation about the new Z axis (and orients the
369 // sunrise/set effects
371 SGSkyDome::reposition( const SGVec3f& p, double _asl,
372 double lon, double lat, double spin ) {
375 osg::Matrix T, LON, LAT, SPIN;
377 // Translate to view position
378 // Point3D zero_elev = current_view.get_cur_zero_elev();
379 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
380 T.makeTranslate( toOsg(p) );
382 // printf(" Translated to %.2f %.2f %.2f\n",
383 // zero_elev.x, zero_elev.y, zero_elev.z );
385 // Rotate to proper orientation
386 // printf(" lon = %.2f lat = %.2f\n",
387 // lon * SGD_RADIANS_TO_DEGREES,
388 // lat * SGD_RADIANS_TO_DEGREES);
389 // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
390 LON.makeRotate(lon, osg::Vec3(0, 0, 1));
392 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
394 LAT.makeRotate(90.0 * SGD_DEGREES_TO_RADIANS - lat, osg::Vec3(0, 1, 0));
396 // xglRotatef( l->sun_rotation * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
397 SPIN.makeRotate(spin, osg::Vec3(0, 0, 1));
399 dome_transform->setMatrix( SPIN*LAT*LON*T );