*
*/
+#ifdef HAVE_CONFIG_H
+# include <simgear_config.h>
+#endif
+
#include <algorithm>
#include <vector>
#include <string>
#include <boost/tuple/tuple_comparison.hpp>
-#include <osg/AlphaFunc>
-#include <osg/Billboard>
-#include <osg/BlendFunc>
#include <osg/Geode>
#include <osg/Geometry>
-#include <osg/Material>
#include <osg/Math>
#include <osg/MatrixTransform>
#include <osg/Matrix>
-#include <osg/StateSet>
-#include <osg/Texture2D>
-#include <osg/TexEnv>
#include <osgDB/ReadFile>
#include <osgDB/FileUtils>
#include <simgear/debug/logstream.hxx>
#include <simgear/math/sg_random.h>
#include <simgear/misc/sg_path.hxx>
+#include <simgear/scene/material/Effect.hxx>
+#include <simgear/scene/material/EffectGeode.hxx>
+#include <simgear/props/props.hxx>
#include <simgear/scene/util/QuadTreeBuilder.hxx>
#include <simgear/scene/util/RenderConstants.hxx>
#include <simgear/scene/util/StateAttributeFactory.hxx>
#include "TreeBin.hxx"
#define SG_TREE_QUAD_TREE_DEPTH 3
+#define SG_TREE_FADE_OUT_LEVELS 10
using namespace osg;
(*rotation)[1] = PI_2;
quadGeom->setFogCoordArray(rotation);
quadGeom->setFogCoordBinding(Geometry::BIND_PER_PRIMITIVE_SET);
+ // The primitive sets render the same geometry, but the second
+ // will rotated 90 degrees by the vertex shader, which uses the
+ // fog coordinate as a rotation.
for (int i = 0; i < 2; ++i)
quadGeom->addPrimitiveSet(new DrawArrays(PrimitiveSet::QUADS));
return quadGeom;
}
-Geode* createTreeGeode(float width, float height, int varieties)
+EffectGeode* createTreeGeode(float width, float height, int varieties)
{
- Geode* result = new Geode;
+ EffectGeode* result = new EffectGeode;
result->addDrawable(createTreeGeometry(width, height, varieties));
return result;
}
void addTreeToLeafGeode(Geode* geode, const SGVec3f& p)
{
- const Vec3& pos = p.osg();
+ Vec3 pos = toOsg(p);
unsigned int numDrawables = geode->getNumDrawables();
Geometry* geom
= static_cast<Geometry*>(geode->getDrawable(numDrawables - 1));
}
}
- static char vertexShaderSource[] =
- "varying float fogFactor;\n"
- "\n"
- "void main(void)\n"
- "{\n"
- " float numVarieties = gl_Normal.z;\n"
- " float texFract = floor(fract(gl_MultiTexCoord0.x) * numVarieties) / numVarieties;\n"
- " texFract += floor(gl_MultiTexCoord0.x) / numVarieties;\n"
- " float sr = sin(gl_FogCoord);\n"
- " float cr = cos(gl_FogCoord);\n"
- " gl_TexCoord[0] = vec4(texFract, gl_MultiTexCoord0.y, 0.0, 0.0);\n"
- // scaling
- " vec3 position = gl_Vertex.xyz * gl_Normal.xxy;\n"
- // Rotation of the generic quad to specific one for the tree.
- " position.xy = vec2(dot(position.xy, vec2(cr, sr)), dot(position.xy, vec2(-sr, cr)));\n"
- " position = position + gl_Color.xyz;\n"
- " gl_Position = gl_ModelViewProjectionMatrix * vec4(position,1.0);\n"
- " vec3 ecPosition = vec3(gl_ModelViewMatrix * vec4(position, 1.0));\n"
- " float n = dot(normalize(gl_LightSource[0].position.xyz), normalize(-ecPosition));\n"
- " vec3 diffuse = gl_FrontMaterial.diffuse.rgb * max(0.1, n);\n"
- " vec4 ambientColor = gl_FrontLightModelProduct.sceneColor + gl_LightSource[0].ambient * gl_FrontMaterial.ambient;\n"
- " gl_FrontColor = ambientColor + gl_LightSource[0].diffuse * vec4(diffuse, 1.0);\n"
- " gl_BackColor = gl_FrontColor;\n"
- " float fogCoord = abs(ecPosition.z);\n"
- " fogFactor = exp( -gl_Fog.density * gl_Fog.density * fogCoord * fogCoord);\n"
- " fogFactor = clamp(fogFactor, 0.0, 1.0);\n"
- "}\n";
-
-static char fragmentShaderSource[] =
- "uniform sampler2D baseTexture; \n"
- "varying float fogFactor;\n"
- "\n"
- "void main(void) \n"
- "{ \n"
- " vec4 base = texture2D( baseTexture, gl_TexCoord[0].st);\n"
- " vec4 finalColor = base * gl_Color;\n"
- " gl_FragColor = mix(gl_Fog.color, finalColor, fogFactor );\n"
- "}\n";
-
-typedef std::map<std::string, osg::ref_ptr<StateSet> > StateSetMap;
-
-static StateSetMap treeTextureMap;
+typedef std::map<std::string, osg::ref_ptr<Effect> > EffectMap;
+
+static EffectMap treeEffectMap;
// Helper classes for creating the quad tree
namespace
{
struct MakeTreesLeaf
{
- MakeTreesLeaf(float range, int varieties, float width, float height) :
+ MakeTreesLeaf(float range, int varieties, float width, float height,
+ Effect* effect) :
_range(range), _varieties(varieties),
- _width(width), _height(height) {}
+ _width(width), _height(height), _effect(effect) {}
MakeTreesLeaf(const MakeTreesLeaf& rhs) :
_range(rhs._range),
- _varieties(rhs._varieties), _width(rhs._width), _height(rhs._height)
+ _varieties(rhs._varieties), _width(rhs._width), _height(rhs._height),
+ _effect(rhs._effect)
{}
LOD* operator() () const
{
LOD* result = new LOD;
- Geode* geode = createTreeGeode(_width, _height, _varieties);
- result->addChild(geode, 0, _range);
+
+ // Create a series of LOD nodes so trees cover decreases slightly
+ // gradually with distance from _range to 2*_range
+ for (float i = 0.0; i < SG_TREE_FADE_OUT_LEVELS; i++)
+ {
+ EffectGeode* geode = createTreeGeode(_width, _height, _varieties);
+ geode->setEffect(_effect.get());
+ result->addChild(geode, 0, _range * (1.0 + i / (SG_TREE_FADE_OUT_LEVELS - 1.0)));
+ }
return result;
}
float _range;
int _varieties;
float _width;
float _height;
+ ref_ptr<Effect> _effect;
};
struct AddTreesLeafObject
{
void operator() (LOD* lod, const TreeBin::Tree& tree) const
{
- Geode* geode = static_cast<Geode*>(lod->getChild(0));
+ Geode* geode = static_cast<Geode*>(lod->getChild(int(tree.position.x() * 10.0f) % lod->getNumChildren()));
addTreeToLeafGeode(geode, tree.position);
}
};
{
Vec3 operator() (const TreeBin::Tree& tree) const
{
- return tree.position.osg();
+ return toOsg(tree.position);
}
};
TreeTransformer(Matrix& mat_) : mat(mat_) {}
TreeBin::Tree operator()(const TreeBin::Tree& tree) const
{
- const Vec3& pos = tree.position.osg();
- return TreeBin::Tree(SGVec3f(pos * mat));
+ Vec3 pos = toOsg(tree.position);
+ return TreeBin::Tree(toSG(pos * mat));
}
Matrix mat;
};
// forest into the local Z-up coordinate system we can reuse the
// primitive tree geometry for all the forests of the same type.
-osg::Group* createForest(TreeBin& forest, const osg::Matrix& transform)
+osg::Group* createForest(SGTreeBinList& forestList, const osg::Matrix& transform)
{
Matrix transInv = Matrix::inverse(transform);
static Matrix ident;
// Set up some shared structures.
ref_ptr<Group> group;
+ MatrixTransform* mt = new MatrixTransform(transform);
- osg::StateSet* stateset = 0;
- StateSetMap::iterator iter = treeTextureMap.find(forest.texture);
- if (iter == treeTextureMap.end()) {
- osg::Texture2D *tex = new osg::Texture2D;
- tex->setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP );
- tex->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP );
- tex->setImage(osgDB::readImageFile(forest.texture));
-
- static ref_ptr<AlphaFunc> alphaFunc;
- static ref_ptr<Program> program;
- static ref_ptr<Uniform> baseTextureSampler;
- static ref_ptr<Material> material;
-
- stateset = new osg::StateSet;
- stateset->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON );
- stateset->setRenderBinDetails(RANDOM_OBJECTS_BIN, "DepthSortedBin");
- if (!program.valid()) {
- alphaFunc = new AlphaFunc;
- alphaFunc->setFunction(AlphaFunc::GEQUAL,0.33f);
- program = new Program;
- baseTextureSampler = new osg::Uniform("baseTexture", 0);
- Shader* vertex_shader = new Shader(Shader::VERTEX,
- vertexShaderSource);
- program->addShader(vertex_shader);
- Shader* fragment_shader = new Shader(Shader::FRAGMENT,
- fragmentShaderSource);
- program->addShader(fragment_shader);
- material = new Material;
- // DonĀ“t track vertex color
- material->setColorMode(Material::OFF);
- material->setAmbient(Material::FRONT_AND_BACK,
- Vec4(1.0f, 1.0f, 1.0f, 1.0f));
- material->setDiffuse(Material::FRONT_AND_BACK,
- Vec4(1.0f, 1.0f, 1.0f, 1.0f));
+ SGTreeBinList::iterator i;
+
+ for (i = forestList.begin(); i != forestList.end(); ++i) {
+ TreeBin* forest = *i;
+
+ Effect* effect = 0;
+ EffectMap::iterator iter = treeEffectMap.find(forest->texture);
+ if (iter == treeEffectMap.end()) {
+ SGPropertyNode_ptr effectProp = new SGPropertyNode;
+ makeChild(effectProp, "inherits-from")->setStringValue("Effects/tree");
+ SGPropertyNode* params = makeChild(effectProp, "parameters");
+ // emphasize n = 0
+ params->getChild("texture", 0, true)->getChild("image", 0, true)
+ ->setStringValue(forest->texture);
+ effect = makeEffect(effectProp, true);
+ treeEffectMap.insert(EffectMap::value_type(forest->texture, effect));
+ } else {
+ effect = iter->second.get();
}
- stateset->setAttributeAndModes(alphaFunc.get());
- stateset->setAttribute(program.get());
- stateset->addUniform(baseTextureSampler.get());
- stateset->setMode(GL_VERTEX_PROGRAM_TWO_SIDE, StateAttribute::ON);
- stateset->setAttribute(material.get());
-
- treeTextureMap.insert(StateSetMap::value_type(forest.texture,
- stateset));
- } else {
- stateset = iter->second.get();
- }
- // Now, create a quadtree for the forest.
- {
+
+ // Now, create a quadtree for the forest.
ShaderGeometryQuadtree
quadtree(GetTreeCoord(), AddTreesLeafObject(),
SG_TREE_QUAD_TREE_DEPTH,
- MakeTreesLeaf(forest.range, forest.texture_varieties,
- forest.width, forest.height));
+ MakeTreesLeaf(forest->range, forest->texture_varieties,
+ forest->width, forest->height, effect));
// Transform tree positions from the "geocentric" positions we
// get from the scenery polys into the local Z-up coordinate
// system.
std::vector<TreeBin::Tree> rotatedTrees;
- rotatedTrees.reserve(forest._trees.size());
- std::transform(forest._trees.begin(), forest._trees.end(),
+ rotatedTrees.reserve(forest->_trees.size());
+ std::transform(forest->_trees.begin(), forest->_trees.end(),
std::back_inserter(rotatedTrees),
TreeTransformer(transInv));
quadtree.buildQuadTree(rotatedTrees.begin(), rotatedTrees.end());
group = quadtree.getRoot();
+
+ for (size_t i = 0; i < group->getNumChildren(); ++i)
+ mt->addChild(group->getChild(i));
}
- MatrixTransform* mt = new MatrixTransform(transform);
- for (size_t i = 0; i < group->getNumChildren(); ++i)
- mt->addChild(group->getChild(i));
- mt->setStateSet(stateset);
+
return mt;
}