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/misc/PathOptions.hxx>
46 #include <simgear/props/props.hxx>
47 #include <simgear/scene/model/model.hxx>
48 #include <simgear/scene/model/SGReaderWriterXMLOptions.hxx>
49 #include <simgear/scene/util/StateAttributeFactory.hxx>
50 #include <simgear/scene/util/SGUpdateVisitor.hxx>
53 #include <osg/BlendFunc>
55 #include <osg/ShadeModel>
57 #include "cloudfield.hxx"
58 #include "newcloud.hxx"
59 #include "CloudShaderGeometry.hxx"
61 using namespace simgear;
66 typedef std::map<std::string, osg::ref_ptr<Effect> > EffectMap;
70 double SGNewCloud::sprite_density = 1.0;
72 SGNewCloud::SGNewCloud(const SGPath &texture_root, const SGPropertyNode *cld_def)
74 min_width = cld_def->getDoubleValue("min-cloud-width-m", 500.0);
75 max_width = cld_def->getDoubleValue("max-cloud-width-m", min_width*2);
76 min_height = cld_def->getDoubleValue("min-cloud-height-m", 400.0);
77 max_height = cld_def->getDoubleValue("max-cloud-height-m", min_height*2);
78 min_sprite_width = cld_def->getDoubleValue("min-sprite-width-m", 200.0);
79 max_sprite_width = cld_def->getDoubleValue("max-sprite-width-m", min_sprite_width*1.5);
80 min_sprite_height = cld_def->getDoubleValue("min-sprite-height-m", 150);
81 max_sprite_height = cld_def->getDoubleValue("max-sprite-height-m", min_sprite_height*1.5);
82 num_sprites = cld_def->getIntValue("num-sprites", 20);
83 num_textures_x = cld_def->getIntValue("num-textures-x", 4);
84 num_textures_y = cld_def->getIntValue("num-textures-y", 4);
85 height_map_texture = cld_def->getBoolValue("height-map-texture", false);
86 bottom_shade = cld_def->getDoubleValue("bottom-shade", 1.0);
87 zscale = cld_def->getDoubleValue("z-scale", 1.0);
88 texture = cld_def->getStringValue("texture", "cl_cumulus.png");
90 // Create a new Effect for the texture, if required.
91 EffectMap::iterator iter = effectMap.find(texture);
92 if (iter == effectMap.end()) {
93 SGPropertyNode_ptr pcloudEffect = new SGPropertyNode;
94 makeChild(pcloudEffect, "inherits-from")->setValue("Effects/cloud");
95 setValue(makeChild(makeChild(makeChild(pcloudEffect, "parameters"),
99 ref_ptr<osgDB::ReaderWriter::Options> options
100 = makeOptionsFromPath(texture_root);
101 ref_ptr<SGReaderWriterXMLOptions> sgOptions
102 = new SGReaderWriterXMLOptions(*options.get());
103 if ((effect = makeEffect(pcloudEffect, true, sgOptions.get())))
104 effectMap.insert(EffectMap::value_type(texture, effect));
106 effect = iter->second.get();
108 quad = createOrthQuad(min_sprite_width, min_sprite_height,
109 num_textures_x, num_textures_y);
112 SGNewCloud::~SGNewCloud() {
115 osg::Geometry* SGNewCloud::createOrthQuad(float w, float h, int varieties_x, int varieties_y)
117 // Create front and back polygons so we don't need to screw around
118 // with two-sided lighting in the shader.
119 osg::Vec3Array& v = *(new osg::Vec3Array(4));
120 osg::Vec3Array& n = *(new osg::Vec3Array(4));
121 osg::Vec2Array& t = *(new osg::Vec2Array(4));
125 v[0].set(0.0f, -cw, 0.0f);
126 v[1].set(0.0f, cw, 0.0f);
127 v[2].set(0.0f, cw, h);
128 v[3].set(0.0f, -cw, h);
130 // The texture coordinate range is not the
131 // entire coordinate space - as the texture
132 // has a number of different clouds on it.
133 float tx = 1.0f/varieties_x;
134 float ty = 1.0f/varieties_y;
136 t[0].set(0.0f, 0.0f);
141 // The normal isn't actually use in lighting.
142 n[0].set(1.0f, -1.0f, -1.0f);
143 n[1].set(1.0f, 1.0f, -1.0f);
144 n[2].set(1.0f, 1.0f, 1.0f);
145 n[3].set(1.0f, -1.0f, 1.0f);
147 osg::Geometry *geom = new osg::Geometry;
149 geom->setVertexArray(&v);
150 geom->setTexCoordArray(0, &t);
151 geom->setNormalArray(&n);
152 geom->setNormalBinding(Geometry::BIND_PER_VERTEX);
153 // No color for now; that's used to pass the position.
154 geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
160 // return a random number between -n/2 and n/2, tending to 0
161 static float Rnd(float n) {
162 return n * (-0.5f + (sg_random() + sg_random()) / 2.0f);
166 osg::ref_ptr<EffectGeode> SGNewCloud::genCloud() {
168 osg::ref_ptr<EffectGeode> geode = new EffectGeode;
170 CloudShaderGeometry* sg = new CloudShaderGeometry(num_textures_x,
172 max_width + max_sprite_width,
173 max_height + max_sprite_height,
176 // Determine how big this specific cloud instance is. Note that we subtract
177 // the sprite size because the width/height is used to define the limits of
178 // the center of the sprites, not their edges.
179 float width = min_width + sg_random() * (max_width - min_width) - min_sprite_width;
180 float height = min_height + sg_random() * (max_height - min_height) - min_sprite_height;
182 // Determine the cull distance. This is used to remove sprites that are too close together.
183 // The value is squared as we use vector calculations.
184 float cull_distance_squared = min_sprite_height * min_sprite_height * 0.1f;
186 // The number of sprites we actually use is a function of the (user-controlled) density
187 int n_sprites = num_sprites * sprite_density * (0.5 + sg_random());
189 for (int i = 0; i < n_sprites; i++)
191 // Determine the position of the sprite. Rather than being completely random,
192 // we place them on the surface of a distorted sphere. However, we place
193 // the first sprite in the center of the sphere (and at maximum size) to
194 // ensure good coverage and reduce the chance of there being "holes" in the
195 // middle of our cloud. Also note that (0,0,0) defines the _bottom_ of the
196 // cloud, not the middle.
205 double theta = sg_random() * SGD_2PI;
206 double elev = sg_random() * SGD_PI;
207 x = width * cos(theta) * 0.5f * sin(elev);
208 y = width * sin(theta) * 0.5f * sin(elev);
209 z = height * cos(elev) * 0.5f + height * 0.5f;
212 // Determine the height and width as scaling factors on the minimum size (used to create the quad).
213 float sprite_width = 1.0f + sg_random() * (max_sprite_width - min_sprite_width) / min_sprite_width;
214 float sprite_height = 1.0f + sg_random() * (max_sprite_height - min_sprite_height) / min_sprite_height;
216 // Sprites are never taller than square.
217 if (sprite_height * min_sprite_height > sprite_width * min_sprite_width)
219 sprite_height = sprite_width * min_sprite_width / min_sprite_height;
223 // The center sprite is always maximum size to fill up any holes.
224 sprite_width = 1.0f + (max_sprite_width - min_sprite_width) / min_sprite_width;
225 sprite_height = 1.0f + (max_sprite_height - min_sprite_height) / min_sprite_height;
228 // If the center of the sprite is less than half the sprite heightthe sprite will extend
229 // below the bottom of the cloud and must be shifted upwards. This is particularly important
230 // for cumulus clouds which have a very well defined base.
231 if (z < 0.5f * sprite_height * min_sprite_height)
233 z = 0.5f * sprite_height * min_sprite_height;
236 // Determine the sprite texture indexes.
237 int index_x = (int) floor(sg_random() * num_textures_x);
238 if (index_x == num_textures_x) { index_x--; }
240 int index_y = (int) floor(sg_random() * num_textures_y);
242 if (height_map_texture) {
243 // The y index depends on the position of the sprite within the cloud.
244 // This allows cloud designers to have particular sprites for the base
245 // and tops of the cloud.
246 index_y = (int) floor((z / height + 0.5f) * num_textures_y);
249 if (index_y == num_textures_y) { index_y--; }
252 sg->addSprite(SGVec3f(x, y, z),
258 cull_distance_squared,
262 sg->setGeometry(quad);
263 geode->addDrawable(sg);
264 geode->setName("3D cloud");
265 geode->setEffect(effect.get());