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