materials use only simgear::Effect

Eliminate SGMaterial::get_state function.

Use Effect in BVH visitor, ocean tile generation, and airport signs.
This commit is contained in:
timoore
2009-07-15 23:10:32 +00:00
committed by Tim Moore
parent 40fe078021
commit c3b1802e95
7 changed files with 47 additions and 72 deletions

View File

@@ -231,31 +231,6 @@ SGMaterial::init ()
}
}
osg::StateSet *
SGMaterial::get_state (int n)
{
if (_status.size() == 0) {
SG_LOG( SG_GENERAL, SG_WARN, "No state available.");
return NULL;
}
int i = n >= 0 ? n : _current_ptr;
if(!_status[i].texture_loaded)
{
assignTexture(_status[i].state.get(), _status[i].texture_path,
wrapu, wrapv, mipmap);
_status[i].texture_loaded = true;
}
osg::StateSet *st = _status[i].state.get();
_current_ptr += 1;
if (_current_ptr >= _status.size())
_current_ptr = 0;
return st;
}
Effect* SGMaterial::get_effect(int n)
{
if (_status.size() == 0) {

View File

@@ -120,7 +120,6 @@ public:
/**
* Get the textured state.
*/
osg::StateSet *get_state (int n = -1);
simgear::Effect *get_effect(int n = -1);
/**

View File

@@ -135,19 +135,15 @@ SGMaterialLib::~SGMaterialLib ( void ) {
}
const SGMaterial*
SGMaterialLib::findMaterial(const osg::StateSet* stateSet)
SGMaterialLib::findMaterial(const simgear::Effect* effect)
{
if (!stateSet)
return 0;
const osg::Referenced* base = stateSet->getUserData();
if (!base)
if (!effect)
return 0;
const SGMaterialUserData* matUserData
= dynamic_cast<const SGMaterialUserData*>(base);
= dynamic_cast<const SGMaterialUserData*>(effect->getUserData());
if (!matUserData)
return 0;
return matUserData->getMaterial();
else
return matUserData->getMaterial();
}

View File

@@ -48,6 +48,11 @@ using std::map;
using std::vector;
using std::less;
namespace simgear
{
class Effect;
}
// Material management class
class SGMaterialLib {
@@ -77,7 +82,7 @@ public:
material_map_iterator end() { return matlib.end(); }
const_material_map_iterator end() const { return matlib.end(); }
static const SGMaterial* findMaterial(const osg::StateSet* stateSet);
static const SGMaterial* findMaterial(const simgear::Effect* effect);
// Destructor
~SGMaterialLib ( void );

View File

@@ -27,6 +27,8 @@
#include <osg/Transform>
#include <osg/TriangleFunctor>
#include <simgear/scene/material/Effect.hxx>
#include <simgear/scene/material/EffectGeode.hxx>
#include <simgear/scene/material/mat.hxx>
#include <simgear/scene/material/matlib.hxx>
#include <simgear/scene/util/SGNodeMasks.hxx>
@@ -384,10 +386,10 @@ public:
{
}
const SGMaterial* pushMaterial(osg::StateSet* stateSet)
const SGMaterial* pushMaterial(Effect* effect)
{
const SGMaterial* oldMaterial = _primitiveFunctor.getCurrentMaterial();
const SGMaterial* material = SGMaterialLib::findMaterial(stateSet);
const SGMaterial* material = SGMaterialLib::findMaterial(effect);
if (material)
_primitiveFunctor.setCurrentMaterial(material);
return oldMaterial;
@@ -395,9 +397,7 @@ public:
void fillWith(osg::Drawable* drawable)
{
const SGMaterial* oldMaterial = pushMaterial(drawable->getStateSet());
drawable->accept(_primitiveFunctor);
_primitiveFunctor.setCurrentMaterial(oldMaterial);
}
virtual void apply(osg::Geode& geode)
@@ -405,7 +405,11 @@ public:
if (hasBoundingVolumeTree(geode))
return;
const SGMaterial* oldMaterial = pushMaterial(geode.getStateSet());
const SGMaterial* oldMaterial = 0;
EffectGeode *eg = dynamic_cast<EffectGeode*>(&geode);
if (eg)
oldMaterial = pushMaterial(eg->getEffect());
bool flushHere = getNodePath().size() <= 1 || _dumpIntoLeafs;
if (flushHere) {
@@ -426,8 +430,8 @@ public:
for(unsigned i = 0; i < geode.getNumDrawables(); ++i)
fillWith(geode.getDrawable(i));
}
_primitiveFunctor.setCurrentMaterial(oldMaterial);
if (eg)
_primitiveFunctor.setCurrentMaterial(oldMaterial);
}
virtual void apply(osg::Group& group)
@@ -453,8 +457,6 @@ public:
if (hasBoundingVolumeTree(node))
return;
const SGMaterial* oldMaterial = pushMaterial(node.getStateSet());
// push the current active primitive list
PFunctor previousPrimitives;
_primitiveFunctor.swap(previousPrimitives);
@@ -469,8 +471,6 @@ public:
// pop the current active primitive list
_primitiveFunctor.swap(previousPrimitives);
_primitiveFunctor.setCurrentMaterial(oldMaterial);
}
void traverseAndCollect(osg::Node& node)
@@ -488,12 +488,8 @@ public:
// Note that we do not need to push the already collected list of
// primitives, since we are now in the topmost node ...
const SGMaterial* oldMaterial = pushMaterial(node.getStateSet());
// walk the children
traverse(node);
_primitiveFunctor.setCurrentMaterial(oldMaterial);
}
void addBoundingVolumeTreeToNode(osg::Node& node)

View File

@@ -37,8 +37,11 @@
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/math/sg_types.hxx>
#include <simgear/misc/texcoord.hxx>
#include <simgear/scene/material/Effect.hxx>
#include <simgear/scene/material/EffectGeode.hxx>
#include <simgear/scene/material/mat.hxx>
#include <simgear/scene/material/matlib.hxx>
#include <simgear/scene/util/VectorArrayAdapter.hxx>
using namespace simgear;
@@ -254,7 +257,7 @@ void fillDrawElementsWithApron(short height, short width,
osg::Node* SGOceanTile(const SGBucket& b, SGMaterialLib *matlib)
{
osg::StateSet *stateSet = 0;
Effect *effect = 0;
double tex_width = 1000.0;
@@ -266,7 +269,7 @@ osg::Node* SGOceanTile(const SGBucket& b, SGMaterialLib *matlib)
tex_width = mat->get_xsize();
// set OSG State
stateSet = mat->get_state();
effect = mat->get_effect();
} else {
SG_LOG( SG_TERRAIN, SG_ALERT, "Ack! unknown use material name = Ocean");
}
@@ -302,10 +305,10 @@ osg::Node* SGOceanTile(const SGBucket& b, SGMaterialLib *matlib)
+ 2 * (latPoints - 1)));
fillDrawElementsWithApron(latPoints, lonPoints, drawElements->begin());
geometry->addPrimitiveSet(drawElements);
geometry->setStateSet(stateSet);
osg::Geode* geode = new osg::Geode;
EffectGeode* geode = new EffectGeode;
geode->setName("Ocean tile");
geode->setEffect(effect);
geode->addDrawable(geometry);
osg::MatrixTransform* transform = new osg::MatrixTransform;

View File

@@ -33,6 +33,8 @@
#include <simgear/debug/logstream.hxx>
#include <simgear/math/sg_types.hxx>
#include <simgear/scene/material/Effect.hxx>
#include <simgear/scene/material/EffectGeode.hxx>
#include <simgear/scene/material/mat.hxx>
#include <simgear/scene/material/matlib.hxx>
@@ -42,17 +44,18 @@
#define RWY "OBJECT_RUNWAY_SIGN: "
using std::vector;
using namespace simgear;
// for temporary storage of sign elements
struct element_info {
element_info(SGMaterial *m, osg::StateSet *s, SGMaterialGlyph *g, double h)
element_info(SGMaterial *m, Effect *s, SGMaterialGlyph *g, double h)
: material(m), state(s), glyph(g), height(h)
{
scale = h * m->get_xsize()
/ (m->get_ysize() < 0.001 ? 1.0 : m->get_ysize());
}
SGMaterial *material;
osg::StateSet *state;
Effect *state;
SGMaterialGlyph *glyph;
double height;
double scale;
@@ -104,8 +107,8 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
object->setName(content);
SGMaterial *material;
osg::StateSet *lighted_state;
osg::StateSet *unlighted_state;
Effect *lighted_state;
Effect *unlighted_state;
// Part I: parse & measure
for (const char *s = content.data(); *s; s++) {
@@ -224,12 +227,12 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
}
// set material states (lighted & unlighted)
lighted_state = material->get_state();
lighted_state = material->get_effect();
string u = newmat + ".unlighted";
SGMaterial *m = matlib->find(u);
if (m) {
unlighted_state = m->get_state();
unlighted_state = m->get_effect();
} else {
SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown material `" << u << '\'');
unlighted_state = lighted_state;
@@ -244,7 +247,7 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
}
// in managed mode push frame stop and frame start first
osg::StateSet *state = lighted ? lighted_state : unlighted_state;
Effect *state = lighted ? lighted_state : unlighted_state;
element_info *e;
if (newtype && newtype != oldtype) {
if (close) {
@@ -319,9 +322,9 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
geometry->setTexCoordArray(0, tl);
geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, vl->size()));
osg::Geode* geode = new osg::Geode;
EffectGeode* geode = new EffectGeode;
geode->addDrawable(geometry);
geode->setStateSet(element->state);
geode->setEffect(element->state);
object->addChild(geode);
hpos += abswidth;
@@ -344,11 +347,11 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
geometry->setNormalArray(nl);
geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);
geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, vl->size()));
osg::Geode* geode = new osg::Geode;
EffectGeode* geode = new EffectGeode;
geode->addDrawable(geometry);
SGMaterial *mat = matlib->find("BlackSign");
if (mat)
geode->setStateSet(mat->get_state());
geode->setEffect(mat->get_effect());
object->addChild(geode);
return object;
@@ -367,14 +370,12 @@ SGMakeRunwaySign(SGMaterialLib *matlib, const string& path, const string& name)
osg::Vec3 heightVec(0, 0, 1);
osg::Geometry* geometry;
geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
SGMaterial *mat = matlib->find(name);
if (mat)
geometry->setStateSet(mat->get_state());
osg::Geode* geode = new osg::Geode;
EffectGeode* geode = new EffectGeode;
geode->setName(name);
geode->addDrawable(geometry);
SGMaterial *mat = matlib->find(name);
if (mat)
geode->setEffect(mat->get_effect());
return geode;
}