#include "TextureBuilder.hxx"
+#include "Pass.hxx"
+
+#include <osg/TexEnv>
+#include <osg/TexEnvCombine>
+#include <osg/TexGen>
#include <osg/Texture1D>
#include <osg/Texture2D>
#include <osg/Texture3D>
#include <osg/TextureRectangle>
#include <osgDB/FileUtils>
+#include <boost/lexical_cast.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <simgear/scene/util/SGSceneFeatures.hxx>
#include <simgear/scene/util/StateAttributeFactory.hxx>
-
#include <simgear/math/SGMath.hxx>
+#include <simgear/structure/OSGUtils.hxx>
#include "Noise.hxx"
using namespace effect;
+TexEnvCombine* buildTexEnvCombine(Effect* effect,
+ const SGPropertyNode* envProp);
+TexGen* buildTexGen(Effect* Effect, const SGPropertyNode* tgenProp);
+
// Hack to force inclusion of TextureBuilder.cxx in library
osg::Texture* TextureBuilder::buildFromType(Effect* effect, const string& type,
const SGPropertyNode*props,
Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
string> TexTuple;
+EffectNameValue<TexEnv::Mode> texEnvModesInit[] =
+{
+ {"add", TexEnv::ADD},
+ {"blend", TexEnv::BLEND},
+ {"decal", TexEnv::DECAL},
+ {"modulate", TexEnv::MODULATE},
+ {"replace", TexEnv::REPLACE}
+};
+EffectPropertyMap<TexEnv::Mode> texEnvModes(texEnvModesInit);
+
+TexEnv* buildTexEnv(Effect* effect, const SGPropertyNode* prop)
+{
+ const SGPropertyNode* modeProp = getEffectPropertyChild(effect, prop,
+ "mode");
+ const SGPropertyNode* colorProp = getEffectPropertyChild(effect, prop,
+ "color");
+ if (!modeProp)
+ return 0;
+ TexEnv::Mode mode = TexEnv::MODULATE;
+ findAttr(texEnvModes, modeProp, mode);
+ if (mode == TexEnv::MODULATE) {
+ return StateAttributeFactory::instance()->getStandardTexEnv();
+ }
+ TexEnv* env = new TexEnv(mode);
+ if (colorProp)
+ env->setColor(toOsg(colorProp->getValue<SGVec4d>()));
+ return env;
+ }
+
+
+void TextureUnitBuilder::buildAttribute(Effect* effect, Pass* pass,
+ const SGPropertyNode* prop,
+ const osgDB::ReaderWriter::Options* options)
+{
+ if (!isAttributeActive(effect, prop))
+ return;
+ // Decode the texture unit
+ int unit = 0;
+ const SGPropertyNode* pUnit = prop->getChild("unit");
+ if (pUnit) {
+ unit = pUnit->getValue<int>();
+ } else {
+ const SGPropertyNode* pName = prop->getChild("name");
+ if (pName)
+ try {
+ unit = boost::lexical_cast<int>(pName->getStringValue());
+ } catch (boost::bad_lexical_cast& lex) {
+ SG_LOG(SG_INPUT, SG_ALERT, "can't decode name as texture unit "
+ << lex.what());
+ }
+ }
+ const SGPropertyNode* pType = getEffectPropertyChild(effect, prop, "type");
+ string type;
+ if (!pType)
+ type = "2d";
+ else
+ type = pType->getStringValue();
+ Texture* texture = 0;
+ try {
+ texture = TextureBuilder::buildFromType(effect, type, prop,
+ options);
+ }
+ catch (BuilderException& e) {
+ SG_LOG(SG_INPUT, SG_ALERT, "No image file for texture, using white ");
+ texture = StateAttributeFactory::instance()->getWhiteTexture();
+ }
+ pass->setTextureAttributeAndModes(unit, texture);
+ const SGPropertyNode* envProp = prop->getChild("environment");
+ if (envProp) {
+ TexEnv* env = buildTexEnv(effect, envProp);
+ if (env)
+ pass->setTextureAttributeAndModes(unit, env);
+ }
+ const SGPropertyNode* combineProp = prop->getChild("texenv-combine");
+ TexEnvCombine* combiner = 0;
+ if (combineProp && ((combiner = buildTexEnvCombine(effect, combineProp))))
+ pass->setTextureAttributeAndModes(unit, combiner);
+ const SGPropertyNode* tgenProp = prop->getChild("texgen");
+ TexGen* tgen = 0;
+ if (tgenProp && (tgen = buildTexGen(effect, tgenProp)))
+ pass->setTextureAttributeAndModes(unit, tgen);
+}
+
+// InstallAttributeBuilder call is in Effect.cxx to force this file to
+// be linked in.
+
namespace
{
EffectNameValue<Texture::FilterMode> filterModesInit[] =
TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
}
+EffectNameValue<TexEnvCombine::CombineParam> combineParamInit[] =
+{
+ {"replace", TexEnvCombine::REPLACE},
+ {"modulate", TexEnvCombine::MODULATE},
+ {"add", TexEnvCombine::ADD},
+ {"add-signed", TexEnvCombine::ADD_SIGNED},
+ {"interpolate", TexEnvCombine::INTERPOLATE},
+ {"subtract", TexEnvCombine::SUBTRACT},
+ {"dot3-rgb", TexEnvCombine::DOT3_RGB},
+ {"dot3-rgba", TexEnvCombine::DOT3_RGBA}
+};
+
+EffectPropertyMap<TexEnvCombine::CombineParam> combineParams(combineParamInit);
+
+EffectNameValue<TexEnvCombine::SourceParam> sourceParamInit[] =
+{
+ {"constant", TexEnvCombine::CONSTANT},
+ {"primary_color", TexEnvCombine::PRIMARY_COLOR},
+ {"previous", TexEnvCombine::PREVIOUS},
+ {"texture", TexEnvCombine::TEXTURE},
+ {"texture0", TexEnvCombine::TEXTURE0},
+ {"texture1", TexEnvCombine::TEXTURE1},
+ {"texture2", TexEnvCombine::TEXTURE2},
+ {"texture3", TexEnvCombine::TEXTURE3},
+ {"texture4", TexEnvCombine::TEXTURE4},
+ {"texture5", TexEnvCombine::TEXTURE5},
+ {"texture6", TexEnvCombine::TEXTURE6},
+ {"texture7", TexEnvCombine::TEXTURE7}
+};
+
+EffectPropertyMap<TexEnvCombine::SourceParam> sourceParams(sourceParamInit);
+
+EffectNameValue<TexEnvCombine::OperandParam> opParamInit[] =
+{
+ {"src_color", TexEnvCombine::SRC_COLOR},
+ {"one_minus_src_color", TexEnvCombine::ONE_MINUS_SRC_COLOR},
+ {"src_alpha", TexEnvCombine::SRC_ALPHA},
+ {"one_minus_src_alpha", TexEnvCombine::ONE_MINUS_SRC_ALPHA}
+};
+
+EffectPropertyMap<TexEnvCombine::OperandParam> operandParams(opParamInit);
+
+TexEnvCombine* buildTexEnvCombine(Effect* effect, const SGPropertyNode* envProp)
+{
+ if (!isAttributeActive(effect, envProp))
+ return 0;
+ TexEnvCombine* result = new TexEnvCombine;
+ const SGPropertyNode* p = 0;
+ if ((p = getEffectPropertyChild(effect, envProp, "combine-rgb"))) {
+ TexEnvCombine::CombineParam crgb = TexEnvCombine::MODULATE;
+ findAttr(combineParams, p, crgb);
+ result->setCombine_RGB(crgb);
+ }
+ if ((p = getEffectPropertyChild(effect, envProp, "combine-alpha"))) {
+ TexEnvCombine::CombineParam calpha = TexEnvCombine::MODULATE;
+ findAttr(combineParams, p, calpha);
+ result->setCombine_RGB(calpha);
+ }
+ if ((p = getEffectPropertyChild(effect, envProp, "source0-rgb"))) {
+ TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
+ findAttr(sourceParams, p, source);
+ result->setSource0_RGB(source);
+ }
+ if ((p = getEffectPropertyChild(effect, envProp, "source1-rgb"))) {
+ TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
+ findAttr(sourceParams, p, source);
+ result->setSource1_RGB(source);
+ }
+ if ((p = getEffectPropertyChild(effect, envProp, "source2-rgb"))) {
+ TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
+ findAttr(sourceParams, p, source);
+ result->setSource2_RGB(source);
+ }
+ if ((p = getEffectPropertyChild(effect, envProp, "source0-alpha"))) {
+ TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
+ findAttr(sourceParams, p, source);
+ result->setSource0_Alpha(source);
+ }
+ if ((p = getEffectPropertyChild(effect, envProp, "source1-alpha"))) {
+ TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
+ findAttr(sourceParams, p, source);
+ result->setSource1_Alpha(source);
+ }
+ if ((p = getEffectPropertyChild(effect, envProp, "source2-alpha"))) {
+ TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
+ findAttr(sourceParams, p, source);
+ result->setSource2_Alpha(source);
+ }
+ if ((p = getEffectPropertyChild(effect, envProp, "operand0-rgb"))) {
+ TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
+ findAttr(operandParams, p, op);
+ result->setOperand0_RGB(op);
+ }
+ if ((p = getEffectPropertyChild(effect, envProp, "operand1-rgb"))) {
+ TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
+ findAttr(operandParams, p, op);
+ result->setOperand1_RGB(op);
+ }
+ if ((p = getEffectPropertyChild(effect, envProp, "operand2-rgb"))) {
+ TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
+ findAttr(operandParams, p, op);
+ result->setOperand2_RGB(op);
+ }
+ if ((p = getEffectPropertyChild(effect, envProp, "operand0-alpha"))) {
+ TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
+ findAttr(operandParams, p, op);
+ result->setOperand0_Alpha(op);
+ }
+ if ((p = getEffectPropertyChild(effect, envProp, "operand1-alpha"))) {
+ TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
+ findAttr(operandParams, p, op);
+ result->setOperand1_Alpha(op);
+ }
+ if ((p = getEffectPropertyChild(effect, envProp, "operand2-alpha"))) {
+ TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
+ findAttr(operandParams, p, op);
+ result->setOperand2_Alpha(op);
+ }
+ if ((p = getEffectPropertyChild(effect, envProp, "scale-rgb"))) {
+ result->setScale_RGB(p->getValue<float>());
+ }
+ if ((p = getEffectPropertyChild(effect, envProp, "scale-alpha"))) {
+ result->setScale_Alpha(p->getValue<float>());
+ }
+#if 0
+ if ((p = getEffectPropertyChild(effect, envProp, "constant-color"))) {
+ SGVec4d color = p->getValue<SGVec4d>();
+ result->setConstantColor(toOsg(color));
+ } else if ((p = getEffectPropertyChild(effect, envProp,
+ "light-direction"))) {
+ SGVec3d direction = p->getValue<SGVec3d>();
+ result->setConstantColorAsLightDirection(toOsg(direction));
+ }
+#endif
+ const SGPropertyNode* colorNode = envProp->getChild("constant-color");
+ if (colorNode)
+ initFromParameters(effect, colorNode, result,
+ &TexEnvCombine::setConstantColor, colorFields);
+ return result;
+}
+
+EffectNameValue<TexGen::Mode> tgenModeInit[] =
+{
+ { "object-linear", TexGen::OBJECT_LINEAR},
+ { "eye-linear", TexGen::EYE_LINEAR},
+ { "sphere-map", TexGen::SPHERE_MAP},
+ { "normal-map", TexGen::NORMAL_MAP},
+ { "reflection-map", TexGen::REFLECTION_MAP}
+};
+
+EffectPropertyMap<TexGen::Mode> tgenModes(tgenModeInit);
+
+EffectNameValue<TexGen::Coord> tgenCoordInit[] =
+{
+ {"s", TexGen::S},
+ {"t", TexGen::T},
+ {"r", TexGen::R},
+ {"q", TexGen::Q}
+};
+
+EffectPropertyMap<TexGen::Coord> tgenCoords(tgenCoordInit);
+
+TexGen* buildTexGen(Effect* effect, const SGPropertyNode* tgenProp)
+{
+ if (!isAttributeActive(effect, tgenProp))
+ return 0;
+ TexGen* result = new TexGen;
+ const SGPropertyNode* p = 0;
+ TexGen::Mode mode = TexGen::OBJECT_LINEAR;
+ if (findAttr(tgenModes, getEffectPropertyChild(effect, tgenProp, "mode"),
+ mode))
+ result->setMode(mode);
+ const SGPropertyNode* planesNode = tgenProp->getChild("planes");
+ if (planesNode) {
+ for (int i = 0; i < planesNode->nChildren(); ++i) {
+ const SGPropertyNode* planeNode = planesNode->getChild(i);
+ TexGen::Coord coord;
+ if (!findAttr(tgenCoords, planeNode->getName(), coord)) {
+ SG_LOG(SG_INPUT, SG_ALERT, "Unknown TexGen plane "
+ << planeNode->getName());
+ } else {
+ const SGPropertyNode* realNode
+ = getEffectPropertyNode(effect, planeNode);
+ SGVec4d plane = realNode->getValue<SGVec4d>();
+ result->setPlane(coord, toOsg(plane));
+ }
+ }
+ }
+ return result;
+}
+
+bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
+{
+ SGPropertyNode* texUnit = makeChild(paramRoot, "texture");
+ const Texture* tex = getStateAttribute<Texture>(0, ss);
+ const Texture2D* texture = dynamic_cast<const Texture2D*>(tex);
+ makeChild(texUnit, "unit")->setValue(0);
+ if (!tex) {
+ makeChild(texUnit, "active")->setValue(false);
+ makeChild(texUnit, "type")->setValue("white");
+ return false;
+ }
+ const Image* image = texture->getImage();
+ string imageName;
+ if (image) {
+ imageName = image->getFileName();
+ } else {
+ makeChild(texUnit, "active")->setValue(false);
+ makeChild(texUnit, "type")->setValue("white");
+ return false;
+ }
+ makeChild(texUnit, "active")->setValue(true);
+ makeChild(texUnit, "type")->setValue("2d");
+ string wrapS = findName(wrapModes, texture->getWrap(Texture::WRAP_S));
+ string wrapT = findName(wrapModes, texture->getWrap(Texture::WRAP_T));
+ string wrapR = findName(wrapModes, texture->getWrap(Texture::WRAP_R));
+ makeChild(texUnit, "image")->setStringValue(imageName);
+ makeChild(texUnit, "wrap-s")->setStringValue(wrapS);
+ makeChild(texUnit, "wrap-t")->setStringValue(wrapT);
+ makeChild(texUnit, "wrap-r")->setStringValue(wrapR);
+ return true;
+}
+
}