1 // cloud.cxx -- model a single cloud layer
3 // Written by Curtis Olson, started June 2000.
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
12 // This library is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 # include <simgear_config.h>
26 #include <simgear/compiler.h>
32 #include <simgear/structure/OSGVersion.hxx>
33 #include <osg/AlphaFunc>
34 #include <osg/BlendFunc>
35 #include <osg/CullFace>
37 #include <osg/Geometry>
38 #include <osg/Material>
39 #include <osg/ShadeModel>
41 #include <osg/TexEnvCombine>
42 #include <osg/Texture2D>
43 #include <osg/TextureCubeMap>
46 #if SG_OSG_MIN_VERSION_REQUIRED(2,9,5)
47 #include <osgDB/Options>
50 #include <simgear/math/sg_random.h>
51 #include <simgear/misc/PathOptions.hxx>
52 #include <simgear/debug/logstream.hxx>
53 #include <simgear/scene/model/model.hxx>
54 #include <simgear/scene/util/RenderConstants.hxx>
55 #include <simgear/scene/util/StateAttributeFactory.hxx>
56 #include <simgear/screen/extensions.hxx>
58 #include "newcloud.hxx"
59 #include "cloudfield.hxx"
62 using namespace simgear;
65 #if defined(__MINGW32__)
66 #define isnan(x) _isnan(x)
69 // #if defined (__FreeBSD__)
70 // # if __FreeBSD_version < 500000
72 // inline int isnan(double r) { return !(r <= 0 || r >= 0); }
77 static osg::ref_ptr<osg::StateSet> layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
78 static osg::ref_ptr<osg::StateSet> layer_states2[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
79 static osg::ref_ptr<osg::TextureCubeMap> cubeMap;
80 static bool state_initialized = false;
81 static bool bump_mapping = false;
83 bool SGCloudLayer::enable_bump_mapping = false;
85 const std::string SGCloudLayer::SG_CLOUD_OVERCAST_STRING = "overcast";
86 const std::string SGCloudLayer::SG_CLOUD_BROKEN_STRING = "broken";
87 const std::string SGCloudLayer::SG_CLOUD_SCATTERED_STRING = "scattered";
88 const std::string SGCloudLayer::SG_CLOUD_FEW_STRING = "few";
89 const std::string SGCloudLayer::SG_CLOUD_CIRRUS_STRING = "cirrus";
90 const std::string SGCloudLayer::SG_CLOUD_CLEAR_STRING = "clear";
92 // make an StateSet for a cloud layer given the named texture
94 SGMakeState(const SGPath &path, const char* colorTexture,
95 const char* normalTexture)
97 osg::StateSet *stateSet = new osg::StateSet;
99 osg::ref_ptr<osgDB::ReaderWriter::Options> options
100 = makeOptionsFromPath(path);
101 stateSet->setTextureAttribute(0, SGLoadTexture2D(colorTexture,
103 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
104 StateAttributeFactory* attribFactory = StateAttributeFactory::instance();
105 stateSet->setAttributeAndModes(attribFactory->getSmoothShadeModel());
106 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
107 stateSet->setAttributeAndModes(attribFactory->getStandardAlphaFunc());
108 stateSet->setAttributeAndModes(attribFactory->getStandardBlendFunc());
110 // osg::Material* material = new osg::Material;
111 // material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
112 // material->setEmission(osg::Material::FRONT_AND_BACK,
113 // osg::Vec4(0.05, 0.05, 0.05, 0));
114 // material->setSpecular(osg::Material::FRONT_AND_BACK,
115 // osg::Vec4(0, 0, 0, 1));
116 // stateSet->setAttribute(material);
118 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
120 // OSGFIXME: invented by me ...
121 // stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
122 // stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
124 // stateSet->setMode(GL_LIGHT0, osg::StateAttribute::OFF);
126 // If the normal texture is given prepare a bumpmapping enabled state
127 // if (normalTexture) {
128 // SGPath normalPath(path);
129 // normalPath.append(normalTexture);
130 // stateSet->setTextureAttribute(2, SGLoadTexture2D(normalPath));
131 // stateSet->setTextureMode(2, GL_TEXTURE_2D, osg::StateAttribute::ON);
138 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
139 cloud_root(new osg::Switch),
140 layer_root(new osg::Switch),
141 group_top(new osg::Group),
142 group_bottom(new osg::Group),
143 layer_transform(new osg::MatrixTransform),
145 texture_path(tex_path),
148 layer_thickness(0.0),
149 layer_transition(0.0),
150 layer_visibility(25.0),
151 layer_coverage(SG_CLOUD_CLEAR),
159 // Render bottoms before the rest of transparent objects (rendered
160 // in bin 10), tops after. The negative numbers on the bottoms
161 // RenderBins and the positive numbers on the tops enforce this
163 cloud_root->addChild(layer_root.get(), true);
164 layer_root->addChild(group_bottom.get());
165 layer_root->addChild(group_top.get());
166 osg::StateSet *rootSet = layer_root->getOrCreateStateSet();
167 rootSet->setRenderBinDetails(CLOUDS_BIN, "DepthSortedBin");
168 rootSet->setTextureAttribute(0, new osg::TexMat);
169 rootSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
170 // Combiner for fog color and cloud alpha
171 osg::TexEnvCombine* combine0 = new osg::TexEnvCombine;
172 osg::TexEnvCombine* combine1 = new osg::TexEnvCombine;
173 combine0->setCombine_RGB(osg::TexEnvCombine::MODULATE);
174 combine0->setSource0_RGB(osg::TexEnvCombine::PREVIOUS);
175 combine0->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
176 combine0->setSource1_RGB(osg::TexEnvCombine::TEXTURE0);
177 combine0->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
178 combine0->setCombine_Alpha(osg::TexEnvCombine::MODULATE);
179 combine0->setSource0_Alpha(osg::TexEnvCombine::PREVIOUS);
180 combine0->setOperand0_Alpha(osg::TexEnvCombine::SRC_ALPHA);
181 combine0->setSource1_Alpha(osg::TexEnvCombine::TEXTURE0);
182 combine0->setOperand1_Alpha(osg::TexEnvCombine::SRC_ALPHA);
184 combine1->setCombine_RGB(osg::TexEnvCombine::MODULATE);
185 combine1->setSource0_RGB(osg::TexEnvCombine::PREVIOUS);
186 combine1->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
187 combine1->setSource1_RGB(osg::TexEnvCombine::CONSTANT);
188 combine1->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
189 combine1->setCombine_Alpha(osg::TexEnvCombine::MODULATE);
190 combine1->setSource0_Alpha(osg::TexEnvCombine::PREVIOUS);
191 combine1->setOperand0_Alpha(osg::TexEnvCombine::SRC_ALPHA);
192 combine1->setSource1_Alpha(osg::TexEnvCombine::CONSTANT);
193 combine1->setOperand1_Alpha(osg::TexEnvCombine::SRC_ALPHA);
194 combine1->setDataVariance(osg::Object::DYNAMIC);
195 rootSet->setTextureAttributeAndModes(0, combine0);
196 rootSet->setTextureAttributeAndModes(1, combine1);
197 rootSet->setTextureMode(1, GL_TEXTURE_2D, osg::StateAttribute::ON);
198 rootSet->setTextureAttributeAndModes(1, StateAttributeFactory::instance()
200 osg::StateAttribute::ON);
201 rootSet->setDataVariance(osg::Object::DYNAMIC);
203 base = osg::Vec2(sg_random(), sg_random());
204 group_top->addChild(layer_transform.get());
205 group_bottom->addChild(layer_transform.get());
207 layer3D = new SGCloudField();
208 cloud_root->addChild(layer3D->getNode(), false);
214 SGCloudLayer::~SGCloudLayer()
220 SGCloudLayer::getSpan_m () const
226 SGCloudLayer::setSpan_m (float span_m)
228 if (span_m != layer_span) {
235 SGCloudLayer::getElevation_m () const
241 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
243 layer_asl = elevation_m;
246 if (elevation_m > 4000)
247 setSpan_m( elevation_m * 10 );
254 SGCloudLayer::getThickness_m () const
256 return layer_thickness;
260 SGCloudLayer::setThickness_m (float thickness_m)
262 layer_thickness = thickness_m;
266 SGCloudLayer::getVisibility_m() const
268 return layer_visibility;
272 SGCloudLayer::setVisibility_m (float visibility_m)
274 layer_visibility = visibility_m;
278 SGCloudLayer::getTransition_m () const
280 return layer_transition;
284 SGCloudLayer::setTransition_m (float transition_m)
286 layer_transition = transition_m;
289 SGCloudLayer::Coverage
290 SGCloudLayer::getCoverage () const
292 return layer_coverage;
296 SGCloudLayer::setCoverage (Coverage coverage)
298 if (coverage != layer_coverage) {
299 layer_coverage = coverage;
305 SGCloudLayer::getCoverageString( Coverage coverage )
308 case SG_CLOUD_OVERCAST:
309 return SG_CLOUD_OVERCAST_STRING;
310 case SG_CLOUD_BROKEN:
311 return SG_CLOUD_BROKEN_STRING;
312 case SG_CLOUD_SCATTERED:
313 return SG_CLOUD_SCATTERED_STRING;
315 return SG_CLOUD_FEW_STRING;
316 case SG_CLOUD_CIRRUS:
317 return SG_CLOUD_CIRRUS_STRING;
320 return SG_CLOUD_CLEAR_STRING;
324 SGCloudLayer::Coverage
325 SGCloudLayer::getCoverageType( const std::string & coverage )
327 if( SG_CLOUD_OVERCAST_STRING == coverage ) {
328 return SG_CLOUD_OVERCAST;
329 } else if( SG_CLOUD_BROKEN_STRING == coverage ) {
330 return SG_CLOUD_BROKEN;
331 } else if( SG_CLOUD_SCATTERED_STRING == coverage ) {
332 return SG_CLOUD_SCATTERED;
333 } else if( SG_CLOUD_FEW_STRING == coverage ) {
335 } else if( SG_CLOUD_CIRRUS_STRING == coverage ) {
336 return SG_CLOUD_CIRRUS;
338 return SG_CLOUD_CLEAR;
343 SGCloudLayer::getCoverageString() const
345 return getCoverageString(layer_coverage);
349 SGCloudLayer::setCoverageString( const std::string & coverage )
351 setCoverage( getCoverageType(coverage) );
355 SGCloudLayer::setTextureOffset(const osg::Vec2& offset)
357 osg::StateAttribute* attr = layer_root->getStateSet()
358 ->getTextureAttribute(0, osg::StateAttribute::TEXMAT);
359 osg::TexMat* texMat = dynamic_cast<osg::TexMat*>(attr);
362 texMat->setMatrix(osg::Matrix::translate(offset[0], offset[1], 0.0));
365 // colors for debugging the cloud layers
367 Vec3 cloudColors[] = {Vec3(1.0f, 1.0f, 1.0f), Vec3(1.0f, 0.0f, 0.0f),
368 Vec3(0.0f, 1.0f, 0.0f), Vec3(0.0f, 0.0f, 1.0f)};
370 Vec3 cloudColors[] = {Vec3(1.0f, 1.0f, 1.0f), Vec3(1.0f, 1.0f, 1.0f),
371 Vec3(1.0f, 1.0f, 1.0f), Vec3(1.0f, 1.0f, 1.0f)};
374 // build the cloud object
376 SGCloudLayer::rebuild()
378 // Initialize states and sizes if necessary.
379 if ( !state_initialized ) {
380 state_initialized = true;
382 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
384 osg::Texture::Extensions* extensions;
385 extensions = osg::Texture::getExtensions(0, true);
387 bump_mapping = extensions->isMultiTexturingSupported() &&
388 (2 <= extensions->numTextureUnits()) &&
389 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_combine") &&
390 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_dot3");
392 osg::TextureCubeMap::Extensions* extensions2;
393 extensions2 = osg::TextureCubeMap::getExtensions(0, true);
394 bump_mapping = bump_mapping && extensions2->isCubeMapSupported();
396 // This bump mapping code was inspired by the tutorial available at
397 // http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
398 // and a NVidia white paper
399 // http://developer.nvidia.com/object/bumpmappingwithregistercombiners.html
400 // The normal map textures were generated by the normal map Gimp plugin :
401 // http://nifelheim.dyndns.org/~cocidius/normalmap/
403 cubeMap = new osg::TextureCubeMap;
404 cubeMap->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
405 cubeMap->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
406 cubeMap->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
407 cubeMap->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
408 cubeMap->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
411 const float half_size = 16.0f;
412 const float offset = 0.5f;
413 osg::Vec3 zero_normal(0.5, 0.5, 0.5);
415 osg::Image* image = new osg::Image;
416 image->allocateImage(size, size, 1, GL_RGB, GL_UNSIGNED_BYTE);
417 unsigned char *ptr = image->data(0, 0);
418 for (int j = 0; j < size; j++ ) {
419 for (int i = 0; i < size; i++ ) {
420 osg::Vec3 tmp(half_size, -( j + offset - half_size ),
421 -( i + offset - half_size ) );
423 tmp = tmp*0.5 - zero_normal;
425 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
426 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
427 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
430 cubeMap->setImage(osg::TextureCubeMap::POSITIVE_X, image);
432 image = new osg::Image;
433 image->allocateImage(size, size, 1, GL_RGB, GL_UNSIGNED_BYTE);
434 ptr = image->data(0, 0);
435 for (int j = 0; j < size; j++ ) {
436 for (int i = 0; i < size; i++ ) {
437 osg::Vec3 tmp(-half_size, -( j + offset - half_size ),
438 ( i + offset - half_size ) );
440 tmp = tmp*0.5 - zero_normal;
442 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
443 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
444 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
447 cubeMap->setImage(osg::TextureCubeMap::NEGATIVE_X, image);
449 image = new osg::Image;
450 image->allocateImage(size, size, 1, GL_RGB, GL_UNSIGNED_BYTE);
451 ptr = image->data(0, 0);
452 for (int j = 0; j < size; j++ ) {
453 for (int i = 0; i < size; i++ ) {
454 osg::Vec3 tmp(( i + offset - half_size ), half_size,
455 ( j + offset - half_size ) );
457 tmp = tmp*0.5 - zero_normal;
459 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
460 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
461 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
464 cubeMap->setImage(osg::TextureCubeMap::POSITIVE_Y, image);
466 image = new osg::Image;
467 image->allocateImage(size, size, 1, GL_RGB, GL_UNSIGNED_BYTE);
468 ptr = image->data(0, 0);
469 for (int j = 0; j < size; j++ ) {
470 for (int i = 0; i < size; i++ ) {
471 osg::Vec3 tmp(( i + offset - half_size ), -half_size,
472 -( j + offset - half_size ) );
474 tmp = tmp*0.5 - zero_normal;
476 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
477 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
478 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
481 cubeMap->setImage(osg::TextureCubeMap::NEGATIVE_Y, image);
483 image = new osg::Image;
484 image->allocateImage(size, size, 1, GL_RGB, GL_UNSIGNED_BYTE);
485 ptr = image->data(0, 0);
486 for (int j = 0; j < size; j++ ) {
487 for (int i = 0; i < size; i++ ) {
488 osg::Vec3 tmp(( i + offset - half_size ),
489 -( j + offset - half_size ), half_size );
491 tmp = tmp*0.5 - zero_normal;
493 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
494 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
495 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
498 cubeMap->setImage(osg::TextureCubeMap::POSITIVE_Z, image);
500 image = new osg::Image;
501 image->allocateImage(size, size, 1, GL_RGB, GL_UNSIGNED_BYTE);
502 ptr = image->data(0, 0);
503 for (int j = 0; j < size; j++ ) {
504 for (int i = 0; i < size; i++ ) {
505 osg::Vec3 tmp(-( i + offset - half_size ),
506 -( j + offset - half_size ), -half_size );
508 tmp = tmp*0.5 - zero_normal;
509 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
510 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
511 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
514 cubeMap->setImage(osg::TextureCubeMap::NEGATIVE_Z, image);
516 osg::StateSet* state;
517 state = SGMakeState(texture_path, "overcast.png", "overcast_n.png");
518 layer_states[SG_CLOUD_OVERCAST] = state;
519 state = SGMakeState(texture_path, "overcast_top.png", "overcast_top_n.png");
520 layer_states2[SG_CLOUD_OVERCAST] = state;
522 state = SGMakeState(texture_path, "broken.png", "broken_n.png");
523 layer_states[SG_CLOUD_BROKEN] = state;
524 layer_states2[SG_CLOUD_BROKEN] = state;
526 state = SGMakeState(texture_path, "scattered.png", "scattered_n.png");
527 layer_states[SG_CLOUD_SCATTERED] = state;
528 layer_states2[SG_CLOUD_SCATTERED] = state;
530 state = SGMakeState(texture_path, "few.png", "few_n.png");
531 layer_states[SG_CLOUD_FEW] = state;
532 layer_states2[SG_CLOUD_FEW] = state;
534 state = SGMakeState(texture_path, "cirrus.png", "cirrus_n.png");
535 layer_states[SG_CLOUD_CIRRUS] = state;
536 layer_states2[SG_CLOUD_CIRRUS] = state;
538 layer_states[SG_CLOUD_CLEAR] = 0;
539 layer_states2[SG_CLOUD_CLEAR] = 0;
541 // experimental optimization that may not make any difference
544 for (int i = 0; i < SG_MAX_CLOUD_COVERAGES; ++i) {
545 StateAttributeFactory *saf = StateAttributeFactory::instance();
546 if (layer_states[i].valid()) {
547 if (layer_states[i] == layer_states2[i])
548 layer_states2[i] = static_cast<osg::StateSet*>(layer_states[i]->clone(copyOp));
549 layer_states[i]->setAttribute(saf ->getCullFaceFront());
550 layer_states2[i]->setAttribute(saf ->getCullFaceBack());
558 setTextureOffset(base);
559 // build the cloud layer
560 const float layer_scale = layer_span / scale;
561 const float mpi = SG_PI/4;
563 // caclculate the difference between a flat-earth model and
564 // a round earth model given the span and altutude ASL of
565 // the cloud layer. This is the difference in altitude between
566 // the top of the inverted bowl and the edge of the bowl.
567 // const float alt_diff = layer_asl * 0.8;
568 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
569 const float layer_angle = 0.5*layer_span / layer_to_core; // The angle is half the span
570 const float border_to_core = layer_to_core * cos(layer_angle);
571 const float alt_diff = layer_to_core - border_to_core;
573 for (int i = 0; i < 4; i++) {
574 if ( layer[i] != NULL ) {
575 layer_transform->removeChild(layer[i].get()); // automatic delete
578 vl[i] = new osg::Vec3Array;
579 cl[i] = new osg::Vec4Array;
580 tl[i] = new osg::Vec2Array;
583 osg::Vec3 vertex(layer_span*(i-2)/2, -layer_span,
584 alt_diff * (sin(i*mpi) - 2));
585 osg::Vec2 tc(layer_scale * i/4, 0.0f);
586 osg::Vec4 color(cloudColors[0], (i == 0) ? 0.0f : 0.15f);
588 cl[i]->push_back(color);
589 vl[i]->push_back(vertex);
590 tl[i]->push_back(tc);
592 for (int j = 0; j < 4; j++) {
593 vertex = osg::Vec3(layer_span*(i-1)/2, layer_span*(j-2)/2,
594 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2));
595 tc = osg::Vec2(layer_scale * (i+1)/4, layer_scale * j/4);
596 color = osg::Vec4(cloudColors[0],
597 ( (j == 0) || (i == 3)) ?
598 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
600 cl[i]->push_back(color);
601 vl[i]->push_back(vertex);
602 tl[i]->push_back(tc);
604 vertex = osg::Vec3(layer_span*(i-2)/2, layer_span*(j-1)/2,
605 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
606 tc = osg::Vec2(layer_scale * i/4, layer_scale * (j+1)/4 );
607 color = osg::Vec4(cloudColors[0],
608 ((j == 3) || (i == 0)) ?
609 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
610 cl[i]->push_back(color);
611 vl[i]->push_back(vertex);
612 tl[i]->push_back(tc);
615 vertex = osg::Vec3(layer_span*(i-1)/2, layer_span,
616 alt_diff * (sin((i+1)*mpi) - 2));
618 tc = osg::Vec2(layer_scale * (i+1)/4, layer_scale);
620 color = osg::Vec4(cloudColors[0], (i == 3) ? 0.0f : 0.15f );
622 cl[i]->push_back( color );
623 vl[i]->push_back( vertex );
624 tl[i]->push_back( tc );
626 osg::Geometry* geometry = new osg::Geometry;
627 geometry->setUseDisplayList(false);
628 geometry->setVertexArray(vl[i].get());
629 geometry->setNormalBinding(osg::Geometry::BIND_OFF);
630 geometry->setColorArray(cl[i].get());
631 geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
632 geometry->setTexCoordArray(0, tl[i].get());
633 geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, vl[i]->size()));
634 layer[i] = new osg::Geode;
636 std::stringstream sstr;
637 sstr << "Cloud Layer (" << i << ")";
638 geometry->setName(sstr.str());
639 layer[i]->setName(sstr.str());
640 layer[i]->addDrawable(geometry);
641 layer_transform->addChild(layer[i].get());
645 if ( layer_states[layer_coverage].valid() ) {
646 osg::CopyOp copyOp; // shallow copy
647 // render bin will be set in reposition
648 osg::StateSet* stateSet = static_cast<osg::StateSet*>(layer_states2[layer_coverage]->clone(copyOp));
649 stateSet->setDataVariance(osg::Object::DYNAMIC);
650 group_top->setStateSet(stateSet);
651 stateSet = static_cast<osg::StateSet*>(layer_states[layer_coverage]->clone(copyOp));
652 stateSet->setDataVariance(osg::Object::DYNAMIC);
653 group_bottom->setStateSet(stateSet);
657 // repaint the cloud layer colors
658 bool SGCloudLayer::repaint( const SGVec3f& fog_color ) {
659 osg::Vec4f combineColor(toOsg(fog_color), cloud_alpha);
660 osg::TexEnvCombine* combiner
661 = dynamic_cast<osg::TexEnvCombine*>(layer_root->getStateSet()
662 ->getTextureAttribute(1, osg::StateAttribute::TEXENV));
663 combiner->setConstantColor(combineColor);
665 // Set the fog color for the 3D clouds too.
666 //cloud3dfog->setColor(combineColor);
670 // reposition the cloud layer at the specified origin and orientation
671 // lon specifies a rotation about the Z axis
672 // lat specifies a rotation about the new Y axis
673 // spin specifies a rotation about the new Z axis (and orients the
674 // sunrise/set effects
675 bool SGCloudLayer::reposition( const SGVec3f& p, const SGVec3f& up, double lon, double lat,
676 double alt, double dt )
679 if (getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR)
681 // combine p and asl (meters) to get translation offset
682 osg::Vec3 asl_offset(toOsg(up));
683 asl_offset.normalize();
684 if ( alt <= layer_asl ) {
685 asl_offset *= layer_asl;
687 asl_offset *= layer_asl + layer_thickness;
690 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
691 // << "," << asl_offset[2] << endl;
692 asl_offset += toOsg(p);
693 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
694 // << "," << asl_offset[2] << endl;
696 osg::Matrix T, LON, LAT;
697 // Translate to zero elevation
698 // Point3D zero_elev = current_view.get_cur_zero_elev();
699 T.makeTranslate( asl_offset );
701 // printf(" Translated to %.2f %.2f %.2f\n",
702 // zero_elev.x, zero_elev.y, zero_elev.z );
704 // Rotate to proper orientation
705 // printf(" lon = %.2f lat = %.2f\n",
706 // lon * SGD_RADIANS_TO_DEGREES,
707 // lat * SGD_RADIANS_TO_DEGREES);
708 LON.makeRotate(lon, osg::Vec3(0, 0, 1));
710 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
712 LAT.makeRotate(90.0 * SGD_DEGREES_TO_RADIANS - lat, osg::Vec3(0, 1, 0));
714 layer_transform->setMatrix( LAT*LON*T );
716 // The layers need to be drawn in order because they are
717 // translucent, but OSG transparency sorting doesn't work because
718 // the cloud polys are huge. However, the ordering is simple: the
719 // bottom polys should be drawn from high altitude to low, and the
720 // top polygons from low to high. The altitude can be used
721 // directly to order the polygons!
722 group_bottom->getStateSet()->setRenderBinDetails(-(int)layer_asl,
724 group_top->getStateSet()->setRenderBinDetails((int)layer_asl,
726 if ( alt <= layer_asl ) {
727 layer_root->setSingleChildOn(0);
728 } else if ( alt >= layer_asl + layer_thickness ) {
729 layer_root->setSingleChildOn(1);
731 layer_root->setAllChildrenOff();
735 // now calculate update texture coordinates
736 SGGeod pos = SGGeod::fromRad(lon, lat);
737 if ( last_pos == SGGeod() ) {
741 double sp_dist = speed*dt;
744 if ( lon != last_pos.getLongitudeRad() || lat != last_pos.getLatitudeRad() || sp_dist != 0 ) {
745 double course = SGGeodesy::courseDeg(last_pos, pos) * SG_DEGREES_TO_RADIANS,
746 dist = SGGeodesy::distanceM(last_pos, pos);
748 // if start and dest are too close together,
749 // calc_gc_course_dist() can return a course of "nan". If
750 // this happens, lets just use the last known good course.
751 // This is a hack, and it would probably be better to make
752 // calc_gc_course_dist() more robust.
753 if ( isnan(course) ) {
754 course = last_course;
756 last_course = course;
759 // calculate cloud movement due to external forces
760 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
763 ax = -cos(course) * dist;
764 ay = sin(course) * dist;
768 bx = cos((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
769 by = sin((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
773 double xoff = (ax + bx) / (2 * scale);
774 double yoff = (ay + by) / (2 * scale);
777 // const float layer_scale = layer_span / scale;
779 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
782 // the while loops can lead to *long* pauses if base[0] comes
783 // with a bogus value.
784 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
785 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
786 if ( base[0] > -10.0 && base[0] < 10.0 ) {
787 base[0] -= (int)base[0];
789 SG_LOG(SG_ASTRO, SG_DEBUG,
790 "Error: base = " << base[0] << "," << base[1] <<
791 " course = " << course << " dist = " << dist );
796 // the while loops can lead to *long* pauses if base[0] comes
797 // with a bogus value.
798 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
799 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
800 if ( base[1] > -10.0 && base[1] < 10.0 ) {
801 base[1] -= (int)base[1];
803 SG_LOG(SG_ASTRO, SG_DEBUG,
804 "Error: base = " << base[0] << "," << base[1] <<
805 " course = " << course << " dist = " << dist );
809 // cout << "base = " << base[0] << "," << base[1] << endl;
811 setTextureOffset(base);
816 layer3D->reposition( p, up, lon, lat, dt, layer_asl, speed, direction);
820 void SGCloudLayer::set_enable3dClouds(bool enable) {
822 if (layer3D->isDefined3D() && enable) {
823 cloud_root->setChildValue(layer3D->getNode(), true);
824 cloud_root->setChildValue(layer_root.get(), false);
826 cloud_root->setChildValue(layer3D->getNode(), false);
827 cloud_root->setChildValue(layer_root.get(), true);