]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/newcloud.cxx
Fixes for headless mode.
[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 using namespace std;
64
65
66 namespace
67 {
68 typedef std::map<std::string, osg::ref_ptr<Effect> > EffectMap;
69 EffectMap effectMap;
70 }
71
72 float SGNewCloud::sprite_density = 1.0;
73
74 SGNewCloud::SGNewCloud(const SGPath &texture_root, const SGPropertyNode *cld_def)
75 {
76     min_width = cld_def->getFloatValue("min-cloud-width-m", 500.0);
77     max_width = cld_def->getFloatValue("max-cloud-width-m", min_width*2);
78     min_height = cld_def->getFloatValue("min-cloud-height-m", 400.0);
79     max_height = cld_def->getFloatValue("max-cloud-height-m", min_height*2);
80     min_sprite_width = cld_def->getFloatValue("min-sprite-width-m", 200.0);
81     max_sprite_width = cld_def->getFloatValue("max-sprite-width-m", min_sprite_width*1.5);
82     min_sprite_height = cld_def->getFloatValue("min-sprite-height-m", 150);
83     max_sprite_height = cld_def->getFloatValue("max-sprite-height-m", min_sprite_height*1.5);
84     num_sprites = cld_def->getIntValue("num-sprites", 20);
85     num_textures_x = cld_def->getIntValue("num-textures-x", 4);
86     num_textures_y = cld_def->getIntValue("num-textures-y", 4);
87     height_map_texture = cld_def->getBoolValue("height-map-texture", false);
88     
89     min_bottom_lighting_factor = cld_def->getFloatValue("min-bottom-lighting-factor", 1.0);
90     max_bottom_lighting_factor = cld_def->getFloatValue("max-bottom-lighting-factor", min(min_bottom_lighting_factor  + 0.1, 1.0));
91     
92     min_middle_lighting_factor = cld_def->getFloatValue("min-middle-lighting-factor", 1.0);
93     max_middle_lighting_factor = cld_def->getFloatValue("max-middle-lighting-factor", min(min_middle_lighting_factor  + 0.1, 1.0));
94
95     min_top_lighting_factor = cld_def->getFloatValue("min-top-lighting-factor", 1.0);
96     max_top_lighting_factor = cld_def->getFloatValue("max-top-lighting-factor", min(min_top_lighting_factor  + 0.1, 1.0));
97
98     min_shade_lighting_factor = cld_def->getFloatValue("min-shade-lighting-factor", 0.5);
99     max_shade_lighting_factor = cld_def->getFloatValue("max-shade-lighting-factor", min(min_shade_lighting_factor  + 0.1, 1.0));
100     
101     zscale = cld_def->getFloatValue("z-scale", 1.0);
102     texture = cld_def->getStringValue("texture", "cl_cumulus.png");
103
104     // Create a new Effect for the texture, if required.
105     EffectMap::iterator iter = effectMap.find(texture);
106     if (iter == effectMap.end()) {
107         SGPropertyNode_ptr pcloudEffect = new SGPropertyNode;
108         makeChild(pcloudEffect, "inherits-from")->setValue("Effects/cloud");
109         setValue(makeChild(makeChild(makeChild(pcloudEffect, "parameters"),
110                                      "texture"),
111                            "image"),
112                  texture);
113         ref_ptr<osgDB::ReaderWriter::Options> options
114             = makeOptionsFromPath(texture_root);
115         ref_ptr<SGReaderWriterXMLOptions> sgOptions
116             = new SGReaderWriterXMLOptions(*options.get());
117         if ((effect = makeEffect(pcloudEffect, true, sgOptions.get())))
118             effectMap.insert(EffectMap::value_type(texture, effect));
119     } else {
120         effect = iter->second.get();
121     }
122     quad = createOrthQuad(min_sprite_width, min_sprite_height,
123                           num_textures_x, num_textures_y);
124 }
125
126 SGNewCloud::~SGNewCloud() {
127 }
128
129 osg::Geometry* SGNewCloud::createOrthQuad(float w, float h, int varieties_x, int varieties_y)
130 {
131     // Create front and back polygons so we don't need to screw around
132     // with two-sided lighting in the shader.
133     osg::Vec3Array& v = *(new osg::Vec3Array(4));
134     osg::Vec3Array& n = *(new osg::Vec3Array(4));
135     osg::Vec2Array& t = *(new osg::Vec2Array(4));
136     
137     float cw = w*0.5f;
138     float ch = h*0.5f;
139     
140     v[0].set(0.0f, -cw, -ch);
141     v[1].set(0.0f,  cw, -ch);
142     v[2].set(0.0f,  cw, ch);
143     v[3].set(0.0f, -cw, ch);
144     
145     // The texture coordinate range is not the
146     // entire coordinate space - as the texture
147     // has a number of different clouds on it.
148     float tx = 1.0f/varieties_x;
149     float ty = 1.0f/varieties_y;
150
151     t[0].set(0.0f, 0.0f);
152     t[1].set(  tx, 0.0f);
153     t[2].set(  tx, ty);
154     t[3].set(0.0f, ty);
155
156     // The normal isn't actually use in lighting.
157     n[0].set(1.0f, -1.0f, -1.0f);
158     n[1].set(1.0f,  1.0f, -1.0f);
159     n[2].set(1.0f,  1.0f,  1.0f);
160     n[3].set(1.0f, -1.0f,  1.0f);
161
162     osg::Geometry *geom = new osg::Geometry;
163
164     geom->setVertexArray(&v);
165     geom->setTexCoordArray(0, &t);
166     geom->setNormalArray(&n);
167     geom->setNormalBinding(Geometry::BIND_PER_VERTEX);
168     // No color for now; that's used to pass the position.
169     geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
170
171     return geom;
172 }
173
174 #if 0
175 // return a random number between -n/2 and n/2, tending to 0
176 static float Rnd(float n) {
177     return n * (-0.5f + (sg_random() + sg_random()) / 2.0f);
178 }
179 #endif
180
181 osg::ref_ptr<EffectGeode> SGNewCloud::genCloud() {
182     
183     osg::ref_ptr<EffectGeode> geode = new EffectGeode;
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     if (width  < 0.0) { width  = 0.0; }
192     if (height < 0.0) { height = 0.0; }
193     
194     // Determine appropriate shading factors
195     float top_factor = min_top_lighting_factor + sg_random() * (max_top_lighting_factor - min_top_lighting_factor);
196     float middle_factor = min_middle_lighting_factor + sg_random() * (max_middle_lighting_factor - min_middle_lighting_factor);
197     float bottom_factor = min_bottom_lighting_factor + sg_random() * (max_bottom_lighting_factor - min_bottom_lighting_factor);
198     float shade_factor = min_shade_lighting_factor + sg_random() * (max_shade_lighting_factor - min_shade_lighting_factor);
199     
200     //printf("Cloud: %2f, %2f, %2f, %2f\n", top_factor, middle_factor, bottom_factor, shade_factor); 
201     
202     CloudShaderGeometry* sg = new CloudShaderGeometry(num_textures_x, 
203                                                       num_textures_y, 
204                                                       max_width + max_sprite_width, 
205                                                       max_height + max_sprite_height, 
206                                                       top_factor,
207                                                       middle_factor,
208                                                       bottom_factor,
209                                                       shade_factor,
210                                                       height,
211                                                       zscale);
212         
213     // Determine the cull distance. This is used to remove sprites that are too close together.
214     // The value is squared as we use vector calculations.
215     float cull_distance_squared = min_sprite_height * min_sprite_height * 0.1f;
216     
217     // The number of sprites we actually use is a function of the (user-controlled) density
218     int n_sprites = num_sprites * sprite_density * (0.5f + sg_random());
219     
220     for (int i = 0; i < n_sprites; i++)
221     {
222         // Determine the position of the sprite. Rather than being completely random,
223         // we place them on the surface of a distorted sphere. However, we place
224         // the first sprite in the center of the sphere (and at maximum size) to
225               // ensure good coverage and reduce the chance of there being "holes" in the
226               // middle of our cloud. Also note that (0,0,0) defines the _bottom_ of the
227               // cloud, not the middle. 
228
229         float x, y, z;
230
231         if (i == 0) {
232             x = 0;
233             y = 0;
234             z = height * 0.5;
235         } else {
236             float theta = sg_random() * SGD_2PI;
237             float elev  = sg_random() * SGD_PI;
238             x = width * cos(theta) * 0.5f * sin(elev);
239             y = width * sin(theta) * 0.5f * sin(elev);
240             z = height * cos(elev) * 0.5f + height * 0.5f;
241         }
242         
243         // Determine the height and width as scaling factors on the minimum size (used to create the quad).
244         float sprite_width = 1.0f + sg_random() * (max_sprite_width - min_sprite_width) / min_sprite_width;
245         float sprite_height = 1.0f + sg_random() * (max_sprite_height - min_sprite_height) / min_sprite_height;
246         
247         // Sprites are never taller than square.
248         if (sprite_height * min_sprite_height > sprite_width * min_sprite_width)
249         {
250             sprite_height = sprite_width * min_sprite_width / min_sprite_height;
251         }
252
253         if (i == 0) {
254             // The center sprite is always maximum size to fill up any holes.
255             sprite_width = 1.0f + (max_sprite_width - min_sprite_width) / min_sprite_width;
256             sprite_height = 1.0f + (max_sprite_height - min_sprite_height) / min_sprite_height;
257         }
258         
259         // If the center of the sprite is less than half the sprite heightthe sprite will extend 
260         // below the bottom of the cloud and must be shifted upwards. This is particularly important 
261         // for cumulus clouds which have a very well defined base.
262         if (z < 0.5f * sprite_height * min_sprite_height)
263         {
264             z = 0.5f * sprite_height * min_sprite_height;          
265         }        
266
267         // Determine the sprite texture indexes.
268         int index_x = (int) floor(sg_random() * num_textures_x);
269         if (index_x >= num_textures_x) { index_x = num_textures_x - 1; }
270                 
271         int index_y = (int) floor(sg_random() * num_textures_y);
272         
273         if (height_map_texture) {
274           // The y index depends on the position of the sprite within the cloud.
275           // This allows cloud designers to have particular sprites for the base
276           // and tops of the cloud.
277           index_y = (int) floor((z / height) * num_textures_y);
278         }
279         
280         if (index_y >= num_textures_y) { index_y = num_textures_y - 1; }
281         
282         sg->addSprite(SGVec3f(x, y, z), 
283                       index_x, 
284                       index_y, 
285                       sprite_width, 
286                       sprite_height, 
287                       cull_distance_squared);
288     }
289     
290     sg->setGeometry(quad);
291     geode->addDrawable(sg);
292     geode->setName("3D cloud");
293     geode->setEffect(effect.get());
294     
295     return geode;
296 }
297