3 // Written by Harald JOHNSEN, started April 2005.
5 // Copyright (C) 2005 Harald JOHNSEN - hjohnsen@evc.net
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program 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.
24 # include <simgear_config.h>
27 #include <osg/AlphaFunc>
29 #include <osg/Program>
30 #include <osg/Uniform>
31 #include <osg/ref_ptr>
32 #include <osg/Texture2D>
33 #include <osg/NodeVisitor>
34 #include <osg/PositionAttitudeTransform>
35 #include <osg/Material>
36 #include <osgUtil/UpdateVisitor>
37 #include <osgDB/ReadFile>
38 #include <osgDB/FileUtils>
41 #include <simgear/compiler.h>
43 #include <simgear/math/sg_random.h>
44 #include <simgear/misc/sg_path.hxx>
45 #include <simgear/props/props.hxx>
46 #include <simgear/scene/model/model.hxx>
47 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
48 #include <simgear/scene/util/StateAttributeFactory.hxx>
49 #include <simgear/scene/util/SGUpdateVisitor.hxx>
52 #include <osg/BlendFunc>
54 #include <osg/ShadeModel>
56 #include "cloudfield.hxx"
57 #include "newcloud.hxx"
58 #include "CloudShaderGeometry.hxx"
60 using namespace simgear;
67 typedef std::map<std::string, osg::observer_ptr<Effect> > EffectMap;
71 float SGNewCloud::sprite_density = 1.0;
73 SGNewCloud::SGNewCloud(const SGPath &texture_root, const SGPropertyNode *cld_def)
75 min_width = cld_def->getFloatValue("min-cloud-width-m", 500.0);
76 max_width = cld_def->getFloatValue("max-cloud-width-m", min_width*2);
77 min_height = cld_def->getFloatValue("min-cloud-height-m", 400.0);
78 max_height = cld_def->getFloatValue("max-cloud-height-m", min_height*2);
79 min_sprite_width = cld_def->getFloatValue("min-sprite-width-m", 200.0);
80 max_sprite_width = cld_def->getFloatValue("max-sprite-width-m", min_sprite_width*1.5);
81 min_sprite_height = cld_def->getFloatValue("min-sprite-height-m", 150);
82 max_sprite_height = cld_def->getFloatValue("max-sprite-height-m", min_sprite_height*1.5);
83 num_sprites = cld_def->getIntValue("num-sprites", 20);
84 num_textures_x = cld_def->getIntValue("num-textures-x", 4);
85 num_textures_y = cld_def->getIntValue("num-textures-y", 4);
86 height_map_texture = cld_def->getBoolValue("height-map-texture", false);
88 min_bottom_lighting_factor = cld_def->getFloatValue("min-bottom-lighting-factor", 1.0);
89 max_bottom_lighting_factor = cld_def->getFloatValue("max-bottom-lighting-factor", min(min_bottom_lighting_factor + 0.1, 1.0));
91 min_middle_lighting_factor = cld_def->getFloatValue("min-middle-lighting-factor", 1.0);
92 max_middle_lighting_factor = cld_def->getFloatValue("max-middle-lighting-factor", min(min_middle_lighting_factor + 0.1, 1.0));
94 min_top_lighting_factor = cld_def->getFloatValue("min-top-lighting-factor", 1.0);
95 max_top_lighting_factor = cld_def->getFloatValue("max-top-lighting-factor", min(min_top_lighting_factor + 0.1, 1.0));
97 min_shade_lighting_factor = cld_def->getFloatValue("min-shade-lighting-factor", 0.5);
98 max_shade_lighting_factor = cld_def->getFloatValue("max-shade-lighting-factor", min(min_shade_lighting_factor + 0.1, 1.0));
100 zscale = cld_def->getFloatValue("z-scale", 1.0);
101 texture = cld_def->getStringValue("texture", "cl_cumulus.png");
103 // Create a new Effect for the texture, if required.
104 EffectMap::iterator iter = effectMap.find(texture);
106 if ((iter == effectMap.end())||
107 (!iter->second.lock(effect)))
109 SGPropertyNode_ptr pcloudEffect = new SGPropertyNode;
110 makeChild(pcloudEffect, "inherits-from")->setValue("Effects/cloud");
111 setValue(makeChild(makeChild(makeChild(pcloudEffect, "parameters"),
115 ref_ptr<SGReaderWriterOptions> options;
116 options = SGReaderWriterOptions::fromPath(texture_root.str());
117 effect = makeEffect(pcloudEffect, true, options.get());
120 if (iter == effectMap.end())
121 effectMap.insert(EffectMap::value_type(texture, effect));
123 iter->second = effect; // update existing, but empty observer
128 SGNewCloud::~SGNewCloud() {
132 // return a random number between -n/2 and n/2, tending to 0
133 static float Rnd(float n) {
134 return n * (-0.5f + (sg_random() + sg_random()) / 2.0f);
138 osg::ref_ptr<EffectGeode> SGNewCloud::genCloud() {
140 osg::ref_ptr<EffectGeode> geode = new EffectGeode;
142 // Determine how big this specific cloud instance is. Note that we subtract
143 // the sprite size because the width/height is used to define the limits of
144 // the center of the sprites, not their edges.
145 float width = min_width + sg_random() * (max_width - min_width) - min_sprite_width;
146 float height = min_height + sg_random() * (max_height - min_height) - min_sprite_height;
148 if (width < 0.0) { width = 0.0; }
149 if (height < 0.0) { height = 0.0; }
151 // Determine appropriate shading factors
152 float top_factor = min_top_lighting_factor + sg_random() * (max_top_lighting_factor - min_top_lighting_factor);
153 float middle_factor = min_middle_lighting_factor + sg_random() * (max_middle_lighting_factor - min_middle_lighting_factor);
154 float bottom_factor = min_bottom_lighting_factor + sg_random() * (max_bottom_lighting_factor - min_bottom_lighting_factor);
155 float shade_factor = min_shade_lighting_factor + sg_random() * (max_shade_lighting_factor - min_shade_lighting_factor);
157 //printf("Cloud: %2f, %2f, %2f, %2f\n", top_factor, middle_factor, bottom_factor, shade_factor);
159 CloudShaderGeometry* sg = new CloudShaderGeometry(num_textures_x,
161 max_width + max_sprite_width,
162 max_height + max_sprite_height,
170 // Determine the cull distance. This is used to remove sprites that are too close together.
171 // The value is squared as we use vector calculations.
172 float cull_distance_squared = min_sprite_height * min_sprite_height * 0.1f;
174 // The number of sprites we actually use is a function of the (user-controlled) density
175 int n_sprites = num_sprites * sprite_density * (0.5f + sg_random());
177 for (int i = 0; i < n_sprites; i++)
179 // Determine the position of the sprite. Rather than being completely random,
180 // we place them on the surface of a distorted sphere. However, we place
181 // the first sprite in the center of the sphere (and at maximum size) to
182 // ensure good coverage and reduce the chance of there being "holes" in the
183 // middle of our cloud. Also note that (0,0,0) defines the _bottom_ of the
184 // cloud, not the middle.
193 float theta = sg_random() * SGD_2PI;
194 float elev = sg_random() * SGD_PI;
195 x = width * cos(theta) * 0.5f * sin(elev);
196 y = width * sin(theta) * 0.5f * sin(elev);
197 z = height * cos(elev) * 0.5f + height * 0.5f;
200 // Determine the height and width
201 float sprite_width = min_sprite_width + sg_random() * (max_sprite_width - min_sprite_width);
202 float sprite_height = min_sprite_height + sg_random() * (max_sprite_height - min_sprite_height);
204 // Sprites are never taller than square.
205 if (sprite_height > sprite_width )
207 sprite_height = sprite_width;
211 // The center sprite is always maximum size to fill up any holes.
212 sprite_width = max_sprite_width;
213 sprite_height = max_sprite_height;
216 // If the center of the sprite is less than half the sprite heightthe sprite will extend
217 // below the bottom of the cloud and must be shifted upwards. This is particularly important
218 // for cumulus clouds which have a very well defined base.
219 if (z < 0.5f * sprite_height)
221 z = 0.5f * sprite_height;
224 // Determine the sprite texture indexes.
225 int index_x = (int) floor(sg_random() * num_textures_x);
226 if (index_x >= num_textures_x) { index_x = num_textures_x - 1; }
228 int index_y = (int) floor(sg_random() * num_textures_y);
230 if (height_map_texture) {
231 // The y index depends on the position of the sprite within the cloud.
232 // This allows cloud designers to have particular sprites for the base
233 // and tops of the cloud.
234 index_y = (int) floor((z / height) * num_textures_y);
237 if (index_y >= num_textures_y) { index_y = num_textures_y - 1; }
239 sg->addSprite(SGVec3f(x, y, z),
244 cull_distance_squared);
247 sg->generateGeometry();
248 geode->addDrawable(sg);
249 geode->setName("3D cloud");
250 geode->setEffect(effect.get());