1 // 3D cloud class
2 //
3 // Written by Harald JOHNSEN, started April 2005.
4 //
5 // Copyright (C) 2005  Harald JOHNSEN - hjohnsen@evc.net
6 //
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.
11 //
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.
16 //
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.
20 //
21 //
22 
23 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26 
27 #include <osg/AlphaFunc>
28 #include <osg/Depth>
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>
39 
40 
41 #include <simgear/compiler.h>
42 
43 #include <simgear/misc/sg_path.hxx>
44 #include <simgear/props/props.hxx>
45 #include <simgear/scene/model/model.hxx>
46 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
47 #include <simgear/scene/util/StateAttributeFactory.hxx>
48 #include <simgear/scene/util/SGUpdateVisitor.hxx>
49 #include <simgear/scene/util/RenderConstants.hxx>
50 
51 #include <algorithm>
52 #include <osg/BlendFunc>
53 #include <osg/GLU>
54 #include <osg/ShadeModel>
55 
56 #include "cloudfield.hxx"
57 #include "newcloud.hxx"
58 #include "CloudShaderGeometry.hxx"
59 
60 using namespace simgear;
61 using namespace osg;
62 using namespace std;
63 
64 
65 namespace
66 {
67 typedef std::map<std::string, osg::observer_ptr<Effect> > EffectMap;
68 EffectMap effectMap;
69 }
70 
71 float SGNewCloud::sprite_density = 1.0;
72 
SGNewCloud(const SGPath & texture_root,const SGPropertyNode * cld_def,mt * s)73 SGNewCloud::SGNewCloud(const SGPath &texture_root, const SGPropertyNode *cld_def, mt* s)
74 {
75     // Set up the RNG with the passed in seed. This allows us to make the RNG repeatable
76     // if required.
77     seed = s;
78 
79     min_width = cld_def->getFloatValue("min-cloud-width-m", 500.0);
80     max_width = cld_def->getFloatValue("max-cloud-width-m", min_width*2);
81     min_height = cld_def->getFloatValue("min-cloud-height-m", 400.0);
82     max_height = cld_def->getFloatValue("max-cloud-height-m", min_height*2);
83     min_sprite_width = cld_def->getFloatValue("min-sprite-width-m", 200.0);
84     max_sprite_width = cld_def->getFloatValue("max-sprite-width-m", min_sprite_width*1.5);
85     min_sprite_height = cld_def->getFloatValue("min-sprite-height-m", 150);
86     max_sprite_height = cld_def->getFloatValue("max-sprite-height-m", min_sprite_height*1.5);
87     num_sprites = cld_def->getIntValue("num-sprites", 20);
88     num_textures_x = cld_def->getIntValue("num-textures-x", 4);
89     num_textures_y = cld_def->getIntValue("num-textures-y", 4);
90     height_map_texture = cld_def->getBoolValue("height-map-texture", false);
91 
92     min_bottom_lighting_factor = cld_def->getFloatValue("min-bottom-lighting-factor", 1.0);
93     max_bottom_lighting_factor = cld_def->getFloatValue("max-bottom-lighting-factor", min(min_bottom_lighting_factor  + 0.1, 1.0));
94 
95     min_middle_lighting_factor = cld_def->getFloatValue("min-middle-lighting-factor", 1.0);
96     max_middle_lighting_factor = cld_def->getFloatValue("max-middle-lighting-factor", min(min_middle_lighting_factor  + 0.1, 1.0));
97 
98     min_top_lighting_factor = cld_def->getFloatValue("min-top-lighting-factor", 1.0);
99     max_top_lighting_factor = cld_def->getFloatValue("max-top-lighting-factor", min(min_top_lighting_factor  + 0.1, 1.0));
100 
101     min_shade_lighting_factor = cld_def->getFloatValue("min-shade-lighting-factor", 0.5);
102     max_shade_lighting_factor = cld_def->getFloatValue("max-shade-lighting-factor", min(min_shade_lighting_factor  + 0.1, 1.0));
103 
104     zscale = cld_def->getFloatValue("z-scale", 1.0);
105     alpha_factor = cld_def->getFloatValue("alpha-factor",1.0);
106     texture = cld_def->getStringValue("texture", "cl_cumulus.png");
107 
108     // Create a new Effect for the texture, if required.
109     EffectMap::iterator iter = effectMap.find(texture);
110 
111     if ((iter == effectMap.end())||
112         (!iter->second.lock(effect)))
113     {
114         SGPropertyNode_ptr pcloudEffect = new SGPropertyNode;
115         makeChild(pcloudEffect, "inherits-from")->setValue("Effects/cloud");
116         setValue(makeChild(makeChild(makeChild(pcloudEffect, "parameters"),
117                                      "texture"),
118                            "image"),
119                  texture);
120         ref_ptr<SGReaderWriterOptions> options;
121         options = SGReaderWriterOptions::fromPath(texture_root);
122         effect = makeEffect(pcloudEffect, true, options.get());
123         if (effect.valid())
124         {
125             if (iter == effectMap.end())
126                 effectMap.insert(EffectMap::value_type(texture, effect));
127             else
128                 iter->second = effect; // update existing, but empty observer
129         }
130     }
131 }
132 
~SGNewCloud()133 SGNewCloud::~SGNewCloud() {
134 }
135 
136 #if 0
137 // return a random number between -n/2 and n/2, tending to 0
138 static float Rnd(float n) {
139     return n * (-0.5f + (mt_rand(seed) + mt_rand(seed)) / 2.0f);
140 }
141 #endif
142 
genCloud()143 osg::ref_ptr<EffectGeode> SGNewCloud::genCloud() {
144 
145     osg::ref_ptr<EffectGeode> geode = new EffectGeode;
146 
147     // Determine how big this specific cloud instance is. Note that we subtract
148     // the sprite size because the width/height is used to define the limits of
149     // the center of the sprites, not their edges.
150     float width = min_width + mt_rand(seed) * (max_width - min_width) - min_sprite_width;
151     float height = min_height + mt_rand(seed) * (max_height - min_height) - min_sprite_height;
152 
153     if (width  < 0.0) { width  = 0.0; }
154 
155     // Protect against divide by 0 issues later when assigning index_y
156     if (height <= 0.0) { height = 0.01; }
157 
158     // Determine appropriate shading factors
159     float top_factor = min_top_lighting_factor + mt_rand(seed) * (max_top_lighting_factor - min_top_lighting_factor);
160     float middle_factor = min_middle_lighting_factor + mt_rand(seed) * (max_middle_lighting_factor - min_middle_lighting_factor);
161     float bottom_factor = min_bottom_lighting_factor + mt_rand(seed) * (max_bottom_lighting_factor - min_bottom_lighting_factor);
162     float shade_factor = min_shade_lighting_factor + mt_rand(seed) * (max_shade_lighting_factor - min_shade_lighting_factor);
163 
164     //printf("Cloud: %2f, %2f, %2f, %2f\n", top_factor, middle_factor, bottom_factor, shade_factor);
165 
166     CloudShaderGeometry* sg = new CloudShaderGeometry(num_textures_x,
167                                                       num_textures_y,
168                                                       max_width + max_sprite_width,
169                                                       max_height + max_sprite_height,
170                                                       top_factor,
171                                                       middle_factor,
172                                                       bottom_factor,
173                                                       shade_factor,
174                                                       height,
175                                                       zscale,
176                                                       alpha_factor);
177 
178     // Determine the cull distance. This is used to remove sprites that are too close together.
179     // The value is squared as we use vector calculations.
180     float cull_distance_squared = min_sprite_height * min_sprite_height * 0.1f;
181 
182     // The number of sprites we actually use is a function of the (user-controlled) density
183     int n_sprites = num_sprites * sprite_density * (0.5f + mt_rand(seed));
184 
185     for (int i = 0; i < n_sprites; i++)
186     {
187         // Determine the position of the sprite. Rather than being completely random,
188         // we place them on the surface of a distorted sphere. However, we place
189         // the first sprite in the center of the sphere (and at maximum size) to
190 	      // ensure good coverage and reduce the chance of there being "holes" in the
191 	      // middle of our cloud. Also note that (0,0,0) defines the _bottom_ of the
192 	      // cloud, not the middle.
193 
194         float x, y, z;
195 
196         if (i == 0) {
197             x = 0;
198             y = 0;
199             z = height * 0.5;
200         } else {
201             float theta = mt_rand(seed) * SGD_2PI;
202             float elev  = mt_rand(seed) * SGD_PI;
203             x = width * cos(theta) * 0.5f * sin(elev);
204             y = width * sin(theta) * 0.5f * sin(elev);
205             z = height * cos(elev) * 0.5f + height * 0.5f;
206         }
207 
208         // Determine the height and width
209         float sprite_width = min_sprite_width + mt_rand(seed) * (max_sprite_width - min_sprite_width);
210         float sprite_height = min_sprite_height + mt_rand(seed) * (max_sprite_height - min_sprite_height);
211 
212         // Sprites are never taller than square.
213         if (sprite_height > sprite_width )
214         {
215             sprite_height = sprite_width;
216         }
217 
218         if (i == 0) {
219             // The center sprite is always maximum size to fill up any holes.
220             sprite_width = max_sprite_width;
221             sprite_height = max_sprite_height;
222         }
223 
224         // If the center of the sprite is less than half the sprite heightthe sprite will extend
225         // below the bottom of the cloud and must be shifted upwards. This is particularly important
226         // for cumulus clouds which have a very well defined base.
227         if (z < 0.5f * sprite_height)
228         {
229             z = 0.5f * sprite_height;
230         }
231 
232         // Determine the sprite texture indexes.
233         int index_x = (int) floor(mt_rand(seed) * num_textures_x);
234         if (index_x >= num_textures_x) { index_x = num_textures_x - 1; }
235 
236         int index_y = (int) floor(mt_rand(seed) * num_textures_y);
237 
238         if (height_map_texture) {
239           // The y index depends on the position of the sprite within the cloud.
240           // This allows cloud designers to have particular sprites for the base
241           // and tops of the cloud.
242           index_y = (int) floor((z / height) * num_textures_y);
243         }
244 
245         if (index_y >= num_textures_y) { index_y = num_textures_y - 1; }
246 
247         sg->addSprite(SGVec3f(x, y, z),
248                       index_x,
249                       index_y,
250                       sprite_width,
251                       sprite_height,
252                       cull_distance_squared);
253     }
254 
255     sg->generateGeometry();
256     geode->addDrawable(sg);
257     geode->setName("3D cloud");
258     geode->setEffect(effect.get());
259     geode->setNodeMask( ~(simgear::CASTSHADOW_BIT | simgear::MODELLIGHT_BIT) );
260 
261     return geode;
262 }
263 
264