00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef HAVE_CONFIG_H
00024 # include <simgear_config.h>
00025 #endif
00026
00027 #include <osg/AlphaFunc>
00028 #include <osg/Depth>
00029 #include <osg/Program>
00030 #include <osg/Uniform>
00031 #include <osg/ref_ptr>
00032 #include <osg/Texture2D>
00033 #include <osg/NodeVisitor>
00034 #include <osg/PositionAttitudeTransform>
00035 #include <osg/Material>
00036 #include <osgUtil/UpdateVisitor>
00037 #include <osgDB/ReadFile>
00038 #include <osgDB/FileUtils>
00039
00040
00041 #include <simgear/compiler.h>
00042
00043 #include <plib/sg.h>
00044 #include <simgear/math/sg_random.h>
00045 #include <simgear/misc/sg_path.hxx>
00046 #include <simgear/misc/PathOptions.hxx>
00047 #include <simgear/props/props.hxx>
00048 #include <simgear/scene/model/model.hxx>
00049 #include <simgear/scene/util/StateAttributeFactory.hxx>
00050 #include <simgear/scene/util/SGUpdateVisitor.hxx>
00051
00052 #include <algorithm>
00053 #include <osg/BlendFunc>
00054 #include <osg/GLU>
00055 #include <osg/ShadeModel>
00056
00057 #include "cloudfield.hxx"
00058 #include "newcloud.hxx"
00059 #include "CloudShaderGeometry.hxx"
00060
00061 using namespace simgear;
00062 using namespace osg;
00063
00064 namespace
00065 {
00066 typedef std::map<std::string, osg::ref_ptr<Effect> > EffectMap;
00067 EffectMap effectMap;
00068 }
00069
00070 double SGNewCloud::sprite_density = 1.0;
00071
00072 SGNewCloud::SGNewCloud(string type,
00073 const SGPath &tex_path,
00074 string tex,
00075 double min_w,
00076 double max_w,
00077 double min_h,
00078 double max_h,
00079 double min_sprite_w,
00080 double max_sprite_w,
00081 double min_sprite_h,
00082 double max_sprite_h,
00083 double b,
00084 int n,
00085 int nt_x,
00086 int nt_y) :
00087 min_width(min_w),
00088 max_width(max_w),
00089 min_height(min_h),
00090 max_height(max_h),
00091 min_sprite_width(min_sprite_w),
00092 max_sprite_width(max_sprite_w),
00093 min_sprite_height(min_sprite_h),
00094 max_sprite_height(max_sprite_h),
00095 bottom_shade(b),
00096 num_sprites(n),
00097 num_textures_x(nt_x),
00098 num_textures_y(nt_y),
00099 texture(tex),
00100 name(type)
00101 {
00102
00103 EffectMap::iterator iter = effectMap.find(texture);
00104 if (iter == effectMap.end()) {
00105 SGPropertyNode_ptr pcloudEffect = new SGPropertyNode;
00106 makeChild(pcloudEffect, "inherits-from")->setValue("Effects/cloud");
00107 setValue(makeChild(makeChild(makeChild(pcloudEffect, "parameters"),
00108 "texture"),
00109 "image"),
00110 texture);
00111 osg::ref_ptr<osgDB::ReaderWriter::Options> options
00112 = makeOptionsFromPath(tex_path);
00113 if ((effect = makeEffect(pcloudEffect, true, options)))
00114 effectMap.insert(EffectMap::value_type(texture, effect));
00115 } else {
00116 effect = iter->second.get();
00117 }
00118 quad = createOrthQuad(min_sprite_width, min_sprite_height,
00119 num_textures_x, num_textures_y);
00120 }
00121
00122 SGNewCloud::~SGNewCloud() {
00123 }
00124
00125 osg::Geometry* SGNewCloud::createOrthQuad(float w, float h, int varieties_x, int varieties_y)
00126 {
00127
00128
00129 osg::Vec3Array& v = *(new osg::Vec3Array(4));
00130 osg::Vec3Array& n = *(new osg::Vec3Array(4));
00131 osg::Vec2Array& t = *(new osg::Vec2Array(4));
00132
00133 float cw = w*0.5f;
00134
00135 v[0].set(0.0f, -cw, 0.0f);
00136 v[1].set(0.0f, cw, 0.0f);
00137 v[2].set(0.0f, cw, h);
00138 v[3].set(0.0f, -cw, h);
00139
00140
00141
00142
00143 float tx = 1.0f/varieties_x;
00144 float ty = 1.0f/varieties_y;
00145
00146 t[0].set(0.0f, 0.0f);
00147 t[1].set( tx, 0.0f);
00148 t[2].set( tx, ty);
00149 t[3].set(0.0f, ty);
00150
00151
00152 n[0].set(1.0f, -1.0f, -1.0f);
00153 n[1].set(1.0f, 1.0f, -1.0f);
00154 n[2].set(1.0f, 1.0f, 1.0f);
00155 n[3].set(1.0f, -1.0f, 1.0f);
00156
00157 osg::Geometry *geom = new osg::Geometry;
00158
00159 geom->setVertexArray(&v);
00160 geom->setTexCoordArray(0, &t);
00161 geom->setNormalArray(&n);
00162 geom->setNormalBinding(Geometry::BIND_PER_VERTEX);
00163
00164 geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
00165
00166 return geom;
00167 }
00168
00169 #if 0
00170
00171 static float Rnd(float n) {
00172 return n * (-0.5f + (sg_random() + sg_random()) / 2.0f);
00173 }
00174 #endif
00175
00176 osg::ref_ptr<EffectGeode> SGNewCloud::genCloud() {
00177
00178 osg::ref_ptr<EffectGeode> geode = new EffectGeode;
00179
00180 CloudShaderGeometry* sg = new CloudShaderGeometry(num_textures_x, num_textures_y, max_width, max_height);
00181
00182
00183
00184
00185 float width = min_width + sg_random() * (max_width - min_width) - min_sprite_width;
00186 float height = min_height + sg_random() * (max_height - min_height) - min_sprite_height;
00187
00188
00189
00190 float cull_distance_squared = min_sprite_height * min_sprite_height * 0.1f;
00191
00192
00193 int n_sprites = num_sprites * sprite_density * (0.5 + sg_random());
00194
00195 for (int i = 0; i < n_sprites; i++)
00196 {
00197
00198
00199
00200
00201
00202 float x, y, z;
00203
00204 if (i == 0) {
00205 x = 0;
00206 y = 0;
00207 z = 0;
00208 } else {
00209 double theta = sg_random() * SGD_2PI;
00210 double elev = sg_random() * SGD_PI;
00211 x = width * cos(theta) * 0.5f * sin(elev);
00212 y = width * sin(theta) * 0.5f * sin(elev);
00213 z = height * cos(elev) * 0.5f;
00214 }
00215
00216
00217 float sprite_width = 1.0f + sg_random() * (max_sprite_width - min_sprite_width) / min_sprite_width;
00218 float sprite_height = 1.0f + sg_random() * (max_sprite_height - min_sprite_height) / min_sprite_height;
00219
00220
00221 if (sprite_height * min_sprite_height > sprite_width * min_sprite_width)
00222 {
00223 sprite_height = sprite_width * min_sprite_width / min_sprite_height;
00224 }
00225
00226 if (i == 0) {
00227
00228 sprite_width = 1.0f + (max_sprite_width - min_sprite_width) / min_sprite_width;
00229 sprite_height = 1.0f + (max_sprite_height - min_sprite_height) / min_sprite_height;
00230 }
00231
00232
00233 int index_x = (int) floor(sg_random() * num_textures_x);
00234 if (index_x == num_textures_x) { index_x--; }
00235
00236
00237
00238
00239 int index_y = (int) floor((z / height + 0.5f) * num_textures_y);
00240 if (index_y == num_textures_y) { index_y--; }
00241
00242 sg->addSprite(SGVec3f(x, y, z),
00243 index_x,
00244 index_y,
00245 sprite_width,
00246 sprite_height,
00247 bottom_shade,
00248 cull_distance_squared,
00249 height * 0.5f);
00250 }
00251
00252 sg->setGeometry(quad);
00253 geode->addDrawable(sg);
00254 geode->setName("3D cloud");
00255 geode->setEffect(effect.get());
00256
00257 return geode;
00258 }
00259