]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/newcloud.cxx
Add support for "squashing" the billboarding of individual 3D cloud sprites, to allow...
[simgear.git] / simgear / scene / sky / newcloud.cxx
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/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>
51
52 #include <algorithm>
53 #include <osg/BlendFunc>
54 #include <osg/GLU>
55 #include <osg/ShadeModel>
56
57 #include "cloudfield.hxx"
58 #include "newcloud.hxx"
59 #include "CloudShaderGeometry.hxx"
60
61 using namespace simgear;
62 using namespace osg;
63
64 namespace
65 {
66 typedef std::map<std::string, osg::ref_ptr<Effect> > EffectMap;
67 EffectMap effectMap;
68 }
69
70 double SGNewCloud::sprite_density = 1.0;
71
72 SGNewCloud::SGNewCloud(const SGPath &texture_root, const SGPropertyNode *cld_def)
73 {
74     min_width = cld_def->getDoubleValue("min-cloud-width-m", 500.0);
75     max_width = cld_def->getDoubleValue("max-cloud-width-m", 1000.0);
76     min_height = cld_def->getDoubleValue("min-cloud-height-m", min_width);
77     max_height = cld_def->getDoubleValue("max-cloud-height-m", max_width);
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);
80     min_sprite_height = cld_def->getDoubleValue("min-sprite-height-m", min_sprite_width);
81     max_sprite_height = cld_def->getDoubleValue("max-sprite-height-m", max_sprite_width);
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     bottom_shade = cld_def->getDoubleValue("bottom-shade", 1.0);
86     zscale = cld_def->getDoubleValue("z-scale", 1.0);
87     texture = cld_def->getStringValue("texture", "cl_cumulus.png");
88
89     // Create a new Effect for the texture, if required.
90     EffectMap::iterator iter = effectMap.find(texture);
91     if (iter == effectMap.end()) {
92         SGPropertyNode_ptr pcloudEffect = new SGPropertyNode;
93         makeChild(pcloudEffect, "inherits-from")->setValue("Effects/cloud");
94         setValue(makeChild(makeChild(makeChild(pcloudEffect, "parameters"),
95                                      "texture"),
96                            "image"),
97                  texture);
98         ref_ptr<osgDB::ReaderWriter::Options> options
99             = makeOptionsFromPath(texture_root);
100         ref_ptr<SGReaderWriterXMLOptions> sgOptions
101             = new SGReaderWriterXMLOptions(*options.get());
102         if ((effect = makeEffect(pcloudEffect, true, sgOptions.get())))
103             effectMap.insert(EffectMap::value_type(texture, effect));
104     } else {
105         effect = iter->second.get();
106     }
107     quad = createOrthQuad(min_sprite_width, min_sprite_height,
108                           num_textures_x, num_textures_y);
109 }
110
111 SGNewCloud::~SGNewCloud() {
112 }
113
114 osg::Geometry* SGNewCloud::createOrthQuad(float w, float h, int varieties_x, int varieties_y)
115 {
116     // Create front and back polygons so we don't need to screw around
117     // with two-sided lighting in the shader.
118     osg::Vec3Array& v = *(new osg::Vec3Array(4));
119     osg::Vec3Array& n = *(new osg::Vec3Array(4));
120     osg::Vec2Array& t = *(new osg::Vec2Array(4));
121     
122     float cw = w*0.5f;
123
124     v[0].set(0.0f, -cw, 0.0f);
125     v[1].set(0.0f,  cw, 0.0f);
126     v[2].set(0.0f,  cw, h);
127     v[3].set(0.0f, -cw, h);
128     
129     // The texture coordinate range is not the
130     // entire coordinate space - as the texture
131     // has a number of different clouds on it.
132     float tx = 1.0f/varieties_x;
133     float ty = 1.0f/varieties_y;
134
135     t[0].set(0.0f, 0.0f);
136     t[1].set(  tx, 0.0f);
137     t[2].set(  tx, ty);
138     t[3].set(0.0f, ty);
139
140     // The normal isn't actually use in lighting.
141     n[0].set(1.0f, -1.0f, -1.0f);
142     n[1].set(1.0f,  1.0f, -1.0f);
143     n[2].set(1.0f,  1.0f,  1.0f);
144     n[3].set(1.0f, -1.0f,  1.0f);
145
146     osg::Geometry *geom = new osg::Geometry;
147
148     geom->setVertexArray(&v);
149     geom->setTexCoordArray(0, &t);
150     geom->setNormalArray(&n);
151     geom->setNormalBinding(Geometry::BIND_PER_VERTEX);
152     // No color for now; that's used to pass the position.
153     geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
154
155     return geom;
156 }
157
158 #if 0
159 // return a random number between -n/2 and n/2, tending to 0
160 static float Rnd(float n) {
161     return n * (-0.5f + (sg_random() + sg_random()) / 2.0f);
162 }
163 #endif
164
165 osg::ref_ptr<EffectGeode> SGNewCloud::genCloud() {
166     
167     osg::ref_ptr<EffectGeode> geode = new EffectGeode;
168         
169     CloudShaderGeometry* sg = new CloudShaderGeometry(num_textures_x, num_textures_y, max_width, max_height, zscale);
170     
171     // Determine how big this specific cloud instance is. Note that we subtract
172     // the sprite size because the width/height is used to define the limits of
173     // the center of the sprites, not their edges.
174     float width = min_width + sg_random() * (max_width - min_width) - min_sprite_width;
175     float height = min_height + sg_random() * (max_height - min_height) - min_sprite_height;
176     
177     // Determine the cull distance. This is used to remove sprites that are too close together.
178     // The value is squared as we use vector calculations.
179     float cull_distance_squared = min_sprite_height * min_sprite_height * 0.1f;
180     
181     // The number of sprites we actually use is a function of the (user-controlled) density
182     int n_sprites = num_sprites * sprite_density * (0.5 + sg_random());
183     
184     for (int i = 0; i < n_sprites; i++)
185     {
186         // Determine the position of the sprite. Rather than being completely random,
187         // we place them on the surface of a distorted sphere. However, we place
188         // the first sprite in the center of the sphere (and at maximum size) to
189         // ensure good coverage and reduce the chance of there being "holes" in our
190
191         float x, y, z;
192
193         if (i == 0) {
194             x = 0;
195             y = 0;
196             z = 0;
197         } else {
198             double theta = sg_random() * SGD_2PI;
199             double elev  = sg_random() * SGD_PI;
200             x = width * cos(theta) * 0.5f * sin(elev);
201             y = width * sin(theta) * 0.5f * sin(elev);
202             z = height * cos(elev) * 0.5f;
203         }
204         
205         // Determine the height and width as scaling factors on the minimum size (used to create the quad).
206         float sprite_width = 1.0f + sg_random() * (max_sprite_width - min_sprite_width) / min_sprite_width;
207         float sprite_height = 1.0f + sg_random() * (max_sprite_height - min_sprite_height) / min_sprite_height;
208
209         // Sprites are never taller than square.
210         if (sprite_height * min_sprite_height > sprite_width * min_sprite_width)
211         {
212             sprite_height = sprite_width * min_sprite_width / min_sprite_height;
213         }
214
215         if (i == 0) {
216             // The center sprite is always maximum size to fill up any holes.
217             sprite_width = 1.0f + (max_sprite_width - min_sprite_width) / min_sprite_width;
218             sprite_height = 1.0f + (max_sprite_height - min_sprite_height) / min_sprite_height;
219         }
220
221         // Determine the sprite texture indexes.
222         int index_x = (int) floor(sg_random() * num_textures_x);
223         if (index_x == num_textures_x) { index_x--; }
224
225         // The y index depends on the positing of the sprite within the cloud.
226         // This allows cloud designers to have particular sprites for the base
227         // and tops of the cloud.
228         int index_y = (int) floor((z / height + 0.5f) * num_textures_y);
229         if (index_y == num_textures_y) { index_y--; }
230         
231         sg->addSprite(SGVec3f(x, y, z), 
232                     index_x, 
233                     index_y, 
234                     sprite_width, 
235                     sprite_height, 
236                     bottom_shade, 
237                     cull_distance_squared, 
238                     height * 0.5f);
239     }
240     
241     sg->setGeometry(quad);
242     geode->addDrawable(sg);
243     geode->setName("3D cloud");
244     geode->setEffect(effect.get());
245     
246     return geode;
247 }
248