From: Torsten Dreyer Date: Thu, 6 Oct 2011 14:47:03 +0000 (+0200) Subject: Stuart Buchanan: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=c1f51d5be79ef9e7c746b534347963288b1202fb;p=simgear.git Stuart Buchanan: Add additional lighting factors for 3D clouds so you can set the bottom, middle, top, and shade lighting factors. --- diff --git a/simgear/scene/sky/CloudShaderGeometry.cxx b/simgear/scene/sky/CloudShaderGeometry.cxx index ed9e94f6..b2ac9252 100644 --- a/simgear/scene/sky/CloudShaderGeometry.cxx +++ b/simgear/scene/sky/CloudShaderGeometry.cxx @@ -110,20 +110,24 @@ void CloudShaderGeometry::drawImplementation(RenderInfo& renderInfo) const const CloudSprite& t = _cloudsprites[itr->idx]; GLfloat ua1[3] = { (GLfloat) t.texture_index_x/varieties_x, (GLfloat) t.texture_index_y/varieties_y, - (GLfloat) t.width }; + (GLfloat) t.width }; GLfloat ua2[3] = { (GLfloat) t.height, - (GLfloat) t.shade, - (GLfloat) t.cloud_height }; + (GLfloat) shade_factor, + (GLfloat) cloud_height }; + GLfloat ua3[3] = { (GLfloat) bottom_factor, + (GLfloat) middle_factor, + (GLfloat) top_factor }; + extensions->glVertexAttrib3fv(USR_ATTR_1, ua1 ); extensions->glVertexAttrib3fv(USR_ATTR_2, ua2 ); + extensions->glVertexAttrib3fv(USR_ATTR_3, ua3 ); glColor4f(t.position.x(), t.position.y(), t.position.z(), zscale); _geometry->draw(renderInfo); } } void CloudShaderGeometry::addSprite(const SGVec3f& p, int tx, int ty, - float w, float h, - float s, float cull, float cloud_height) + float w, float h, float cull) { // Only add the sprite if it is further than the cull distance to all other sprites // except for the center sprite. @@ -137,8 +141,8 @@ void CloudShaderGeometry::addSprite(const SGVec3f& p, int tx, int ty, return; } } - - _cloudsprites.push_back(CloudSprite(p, tx, ty, w, h, s, cloud_height)); + + _cloudsprites.push_back(CloudSprite(p, tx, ty, w, h)); } bool CloudShaderGeometry_readLocalData(Object& obj, Input& fr) @@ -166,13 +170,13 @@ bool CloudShaderGeometry_readLocalData(Object& obj, Input& fr) while (!fr.eof() && fr[0].getNoNestedBrackets() > entry) { SGVec3f v; int tx, ty; - float w, h, s, ch; + float w, h; if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z()) && fr[3].getInt(tx) && fr[4].getInt(ty) && - fr[5].getFloat(w) && fr[6].getFloat(h)&& fr[7].getFloat(s) && fr[8].getFloat(ch)) { + fr[5].getFloat(w) && fr[6].getFloat(h)) { fr += 5; //SGVec3f* v = new SGVec3f(v.x(), v.y(), v.z()); - geom._cloudsprites.push_back(CloudShaderGeometry::CloudSprite(v, tx, ty, w, h,s,ch)); + geom._cloudsprites.push_back(CloudShaderGeometry::CloudSprite(v, tx, ty, w, h)); } else { ++fr; } @@ -197,8 +201,7 @@ bool CloudShaderGeometry_writeLocalData(const Object& obj, Output& fw) fw.indent() << itr->position.x() << " " << itr->position.y() << " " << itr->position.z() << " " << itr->texture_index_x << " " << itr->texture_index_y << " " << itr->width << " " - << itr->height << " " << itr->shade - << itr->cloud_height << " "<< std::endl; + << itr->height << " " << std::endl; } fw.moveOut(); fw.indent() << "}" << std::endl; diff --git a/simgear/scene/sky/CloudShaderGeometry.hxx b/simgear/scene/sky/CloudShaderGeometry.hxx index 78f245ab..1b63f805 100644 --- a/simgear/scene/sky/CloudShaderGeometry.hxx +++ b/simgear/scene/sky/CloudShaderGeometry.hxx @@ -46,14 +46,15 @@ class CloudShaderGeometry : public osg::Drawable const static unsigned int USR_ATTR_1 = 10; const static unsigned int USR_ATTR_2 = 11; - + const static unsigned int USR_ATTR_3 = 12; + CloudShaderGeometry() { setUseDisplayList(false); } - CloudShaderGeometry(int vx, int vy, float width, float height, float zsc) : - varieties_x(vx), varieties_y(vy), zscale(zsc) + CloudShaderGeometry(int vx, int vy, float width, float height, float ts, float ms, float bs, float shade, float ch, float zsc) : + varieties_x(vx), varieties_y(vy), top_factor(ts), middle_factor(ms), bottom_factor(bs), shade_factor(shade), cloud_height(ch), zscale(zsc) { setUseDisplayList(false); float x = width/2.0f; @@ -69,9 +70,8 @@ class CloudShaderGeometry : public osg::Drawable META_Object(flightgear, CloudShaderGeometry); struct CloudSprite { - CloudSprite(const SGVec3f& p, int tx, int ty, float w, float h, - float s, float ch) : - position(p), texture_index_x(tx), texture_index_y(ty), width(w), height(h), shade(s), cloud_height(ch) + CloudSprite(const SGVec3f& p, int tx, int ty, float w, float h) : + position(p), texture_index_x(tx), texture_index_y(ty), width(w), height(h) { } SGVec3f position; @@ -79,8 +79,6 @@ class CloudShaderGeometry : public osg::Drawable int texture_index_y; float width; float height; - float shade; - float cloud_height; }; typedef std::vector CloudSpriteList; @@ -88,8 +86,8 @@ class CloudShaderGeometry : public osg::Drawable void insert(const CloudSprite& t) { _cloudsprites.push_back(t); } - void insert(SGVec3f& p, int tx, int ty, float w, float h, float s, float ch) - { insert(CloudSprite(p, tx, ty, w, h, s, ch)); } + void insert(SGVec3f& p, int tx, int ty, float w, float h) + { insert(CloudSprite(p, tx, ty, w, h)); } unsigned getNumCloudSprite() const { return _cloudsprites.size(); } @@ -107,15 +105,19 @@ class CloudShaderGeometry : public osg::Drawable _geometry = geometry; } - void addSprite(const SGVec3f& p, int tx, int ty, float w, float h, - float s, float cull, float cloud_height); + void addSprite(const SGVec3f& p, int tx, int ty, float w, float h, float cull); osg::ref_ptr _geometry; int varieties_x; int varieties_y; + float top_factor; + float middle_factor; + float bottom_factor; + float shade_factor; + float cloud_height; float zscale; - + // Bounding box extents. osg::BoundingBox _bbox; diff --git a/simgear/scene/sky/newcloud.cxx b/simgear/scene/sky/newcloud.cxx index 3b5321f8..0e9b7325 100644 --- a/simgear/scene/sky/newcloud.cxx +++ b/simgear/scene/sky/newcloud.cxx @@ -60,6 +60,8 @@ using namespace simgear; using namespace osg; +using namespace std; + namespace { @@ -67,24 +69,36 @@ typedef std::map > EffectMap; EffectMap effectMap; } -double SGNewCloud::sprite_density = 1.0; +float SGNewCloud::sprite_density = 1.0; SGNewCloud::SGNewCloud(const SGPath &texture_root, const SGPropertyNode *cld_def) { - min_width = cld_def->getDoubleValue("min-cloud-width-m", 500.0); - max_width = cld_def->getDoubleValue("max-cloud-width-m", min_width*2); - min_height = cld_def->getDoubleValue("min-cloud-height-m", 400.0); - max_height = cld_def->getDoubleValue("max-cloud-height-m", min_height*2); - min_sprite_width = cld_def->getDoubleValue("min-sprite-width-m", 200.0); - max_sprite_width = cld_def->getDoubleValue("max-sprite-width-m", min_sprite_width*1.5); - min_sprite_height = cld_def->getDoubleValue("min-sprite-height-m", 150); - max_sprite_height = cld_def->getDoubleValue("max-sprite-height-m", min_sprite_height*1.5); + min_width = cld_def->getFloatValue("min-cloud-width-m", 500.0); + max_width = cld_def->getFloatValue("max-cloud-width-m", min_width*2); + min_height = cld_def->getFloatValue("min-cloud-height-m", 400.0); + max_height = cld_def->getFloatValue("max-cloud-height-m", min_height*2); + min_sprite_width = cld_def->getFloatValue("min-sprite-width-m", 200.0); + max_sprite_width = cld_def->getFloatValue("max-sprite-width-m", min_sprite_width*1.5); + min_sprite_height = cld_def->getFloatValue("min-sprite-height-m", 150); + max_sprite_height = cld_def->getFloatValue("max-sprite-height-m", min_sprite_height*1.5); num_sprites = cld_def->getIntValue("num-sprites", 20); num_textures_x = cld_def->getIntValue("num-textures-x", 4); num_textures_y = cld_def->getIntValue("num-textures-y", 4); height_map_texture = cld_def->getBoolValue("height-map-texture", false); - bottom_shade = cld_def->getDoubleValue("bottom-shade", 1.0); - zscale = cld_def->getDoubleValue("z-scale", 1.0); + + min_bottom_lighting_factor = cld_def->getFloatValue("min-bottom-lighting-factor", 1.0); + max_bottom_lighting_factor = cld_def->getFloatValue("max-bottom-lighting-factor", min(min_bottom_lighting_factor + 0.1, 1.0)); + + min_middle_lighting_factor = cld_def->getFloatValue("min-middle-lighting-factor", 1.0); + max_middle_lighting_factor = cld_def->getFloatValue("max-middle-lighting-factor", min(min_middle_lighting_factor + 0.1, 1.0)); + + min_top_lighting_factor = cld_def->getFloatValue("min-top-lighting-factor", 1.0); + max_top_lighting_factor = cld_def->getFloatValue("max-top-lighting-factor", min(min_top_lighting_factor + 0.1, 1.0)); + + min_shade_lighting_factor = cld_def->getFloatValue("min-shade-lighting-factor", 0.5); + max_shade_lighting_factor = cld_def->getFloatValue("max-shade-lighting-factor", min(min_shade_lighting_factor + 0.1, 1.0)); + + zscale = cld_def->getFloatValue("z-scale", 1.0); texture = cld_def->getStringValue("texture", "cl_cumulus.png"); // Create a new Effect for the texture, if required. @@ -167,24 +181,40 @@ osg::ref_ptr SGNewCloud::genCloud() { osg::ref_ptr geode = new EffectGeode; - CloudShaderGeometry* sg = new CloudShaderGeometry(num_textures_x, - num_textures_y, - max_width + max_sprite_width, - max_height + max_sprite_height, - zscale); - // Determine how big this specific cloud instance is. Note that we subtract // the sprite size because the width/height is used to define the limits of // the center of the sprites, not their edges. float width = min_width + sg_random() * (max_width - min_width) - min_sprite_width; float height = min_height + sg_random() * (max_height - min_height) - min_sprite_height; + if (width < 0.0) { width = 0.0; } + if (height < 0.0) { height = 0.0; } + + // Determine appropriate shading factors + float top_factor = min_top_lighting_factor + sg_random() * (max_top_lighting_factor - min_top_lighting_factor); + float middle_factor = min_middle_lighting_factor + sg_random() * (max_middle_lighting_factor - min_middle_lighting_factor); + float bottom_factor = min_bottom_lighting_factor + sg_random() * (max_bottom_lighting_factor - min_bottom_lighting_factor); + float shade_factor = min_shade_lighting_factor + sg_random() * (max_shade_lighting_factor - min_shade_lighting_factor); + + //printf("Cloud: %2f, %2f, %2f, %2f\n", top_factor, middle_factor, bottom_factor, shade_factor); + + CloudShaderGeometry* sg = new CloudShaderGeometry(num_textures_x, + num_textures_y, + max_width + max_sprite_width, + max_height + max_sprite_height, + top_factor, + middle_factor, + bottom_factor, + shade_factor, + height, + zscale); + // Determine the cull distance. This is used to remove sprites that are too close together. // The value is squared as we use vector calculations. float cull_distance_squared = min_sprite_height * min_sprite_height * 0.1f; // The number of sprites we actually use is a function of the (user-controlled) density - int n_sprites = num_sprites * sprite_density * (0.5 + sg_random()); + int n_sprites = num_sprites * sprite_density * (0.5f + sg_random()); for (int i = 0; i < n_sprites; i++) { @@ -202,8 +232,8 @@ osg::ref_ptr SGNewCloud::genCloud() { y = 0; z = height * 0.5; } else { - double theta = sg_random() * SGD_2PI; - double elev = sg_random() * SGD_PI; + float theta = sg_random() * SGD_2PI; + float elev = sg_random() * SGD_PI; x = width * cos(theta) * 0.5f * sin(elev); y = width * sin(theta) * 0.5f * sin(elev); z = height * cos(elev) * 0.5f + height * 0.5f; @@ -235,7 +265,7 @@ osg::ref_ptr SGNewCloud::genCloud() { // Determine the sprite texture indexes. int index_x = (int) floor(sg_random() * num_textures_x); - if (index_x == num_textures_x) { index_x--; } + if (index_x >= num_textures_x) { index_x = num_textures_x - 1; } int index_y = (int) floor(sg_random() * num_textures_y); @@ -243,20 +273,17 @@ osg::ref_ptr SGNewCloud::genCloud() { // The y index depends on the position of the sprite within the cloud. // This allows cloud designers to have particular sprites for the base // and tops of the cloud. - index_y = (int) floor((z / height + 0.5f) * num_textures_y); + index_y = (int) floor((z / height) * num_textures_y); } - if (index_y == num_textures_y) { index_y--; } - + if (index_y >= num_textures_y) { index_y = num_textures_y - 1; } sg->addSprite(SGVec3f(x, y, z), - index_x, - index_y, - sprite_width, - sprite_height, - bottom_shade, - cull_distance_squared, - height * 0.5f); + index_x, + index_y, + sprite_width, + sprite_height, + cull_distance_squared); } sg->setGeometry(quad); diff --git a/simgear/scene/sky/newcloud.hxx b/simgear/scene/sky/newcloud.hxx index 91db9f60..f6e55733 100644 --- a/simgear/scene/sky/newcloud.hxx +++ b/simgear/scene/sky/newcloud.hxx @@ -47,7 +47,7 @@ public: // Generate a Cloud osg::ref_ptr genCloud (); - static double getDensity(void) + static float getDensity(void) { return sprite_density; } @@ -61,16 +61,37 @@ public: private: - double min_width; - double max_width; - double min_height; - double max_height; - double min_sprite_width; - double max_sprite_width; - double min_sprite_height; - double max_sprite_height; - double bottom_shade; - double zscale; + float min_width; + float max_width; + float min_height; + float max_height; + float min_sprite_width; + float max_sprite_width; + float min_sprite_height; + float max_sprite_height; + + // Minimum and maximum bottom, middle, top, sunny, shade lighting + // factors. For individual clouds we choose a bottom/middle/top + // shade from between each min/max value + float min_bottom_lighting_factor; + float max_bottom_lighting_factor; + float min_middle_lighting_factor; + float max_middle_lighting_factor; + float min_top_lighting_factor; + float max_top_lighting_factor; + float min_shade_lighting_factor; + float max_shade_lighting_factor; + + // The density of the cloud is the shading applied + // to cloud sprites on the opposite side of the cloud + // from the sun. For an invidual cloud instance a value + // between min_density and max_density is chosen. + float min_density; + float max_density; + + // zscale indicates how sprites should be scaled vertically + // after billboarding. + float zscale; bool height_map_texture; int num_sprites; int num_textures_x; @@ -78,7 +99,7 @@ private: string texture; osg::Geometry* quad; osg::ref_ptr effect; - static double sprite_density; + static float sprite_density; osg::Geometry* createOrthQuad(float w, float h, int varieties_x, int varieties_y);