+
+// Image names for all sides
+typedef boost::tuple<string, string, string, string, string, string> CubeMapTuple;
+
+CubeMapTuple makeCubeMapTuple(Effect* effect, const SGPropertyNode* props)
+{
+ const SGPropertyNode* ep = 0;
+
+ string positive_x;
+ if ((ep = getEffectPropertyChild(effect, props, "positive-x")))
+ positive_x = ep->getStringValue();
+ string negative_x;
+ if ((ep = getEffectPropertyChild(effect, props, "negative-x")))
+ negative_x = ep->getStringValue();
+ string positive_y;
+ if ((ep = getEffectPropertyChild(effect, props, "positive-y")))
+ positive_y = ep->getStringValue();
+ string negative_y;
+ if ((ep = getEffectPropertyChild(effect, props, "negative-y")))
+ negative_y = ep->getStringValue();
+ string positive_z;
+ if ((ep = getEffectPropertyChild(effect, props, "positive-z")))
+ positive_z = ep->getStringValue();
+ string negative_z;
+ if ((ep = getEffectPropertyChild(effect, props, "negative-z")))
+ negative_z = ep->getStringValue();
+ return CubeMapTuple(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z);
+}
+
+
+class CubeMapBuilder : public TextureBuilder
+{
+public:
+ Texture* build(Effect* effect, const SGPropertyNode*,
+ const SGReaderWriterXMLOptions* options);
+protected:
+ typedef map<CubeMapTuple, ref_ptr<TextureCubeMap> > CubeMap;
+ CubeMap _cubemaps;
+};
+
+Texture* CubeMapBuilder::build(Effect* effect, const SGPropertyNode* props,
+ const SGReaderWriterXMLOptions* options)
+{
+ // First check that there is a <images> tag
+ const SGPropertyNode* texturesProp = getEffectPropertyChild(effect, props, "images");
+ if (!texturesProp) {
+ throw BuilderException("no <images> for cube map");
+ return NULL; // This is redundant
+ }
+
+ CubeMapTuple _tuple = makeCubeMapTuple(effect, texturesProp);
+
+ CubeMap::iterator itr = _cubemaps.find(_tuple);
+ if (itr != _cubemaps.end())
+ return itr->second.get();
+
+ TextureCubeMap* cubeTexture = new osg::TextureCubeMap;
+
+ // TODO: Read these from effect file? Maybe these are sane for all cuebmaps?
+ cubeTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
+ cubeTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture::LINEAR);
+ cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
+ cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
+ cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
+
+ osgDB::ReaderWriter::ReadResult result =
+ osgDB::Registry::instance()->readImage(_tuple.get<0>(), options);
+ if(result.success()) {
+ osg::Image* image = result.getImage();
+ cubeTexture->setImage(TextureCubeMap::POSITIVE_X, image);
+ }
+ result = osgDB::Registry::instance()->readImage(_tuple.get<1>(), options);
+ if(result.success()) {
+ osg::Image* image = result.getImage();
+ cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, image);
+ }
+ result = osgDB::Registry::instance()->readImage(_tuple.get<2>(), options);
+ if(result.success()) {
+ osg::Image* image = result.getImage();
+ cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, image);
+ }
+ result = osgDB::Registry::instance()->readImage(_tuple.get<3>(), options);
+ if(result.success()) {
+ osg::Image* image = result.getImage();
+ cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, image);
+ }
+ result = osgDB::Registry::instance()->readImage(_tuple.get<4>(), options);
+ if(result.success()) {
+ osg::Image* image = result.getImage();
+ cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, image);
+ }
+ result = osgDB::Registry::instance()->readImage(_tuple.get<5>(), options);
+ if(result.success()) {
+ osg::Image* image = result.getImage();
+ cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, image);
+ }
+
+ _cubemaps[_tuple] = cubeTexture;
+
+ return cubeTexture;
+}
+
+namespace {
+TextureBuilder::Registrar installCubeMap("cubemap", new CubeMapBuilder);
+}
+
+
+
+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,
+ const SGReaderWriterXMLOptions* options)
+{
+ 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_Alpha(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,
+ options);
+ 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;
+ TexGen::Mode mode = TexGen::OBJECT_LINEAR;
+ 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;
+ findAttr(tgenCoords, planeNode->getName(), coord);
+ const SGPropertyNode* realNode
+ = getEffectPropertyNode(effect, planeNode);
+ SGVec4d plane = realNode->getValue<SGVec4d>();
+ result->setPlane(coord, toOsg(plane));
+ }
+ }
+ return result;
+}
+