X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fscene%2Fmaterial%2FTextureBuilder.cxx;h=0c141c475c3415b627c354ad8cb53b4a1d52eb69;hb=aef8f13290c74359c1a7a54bd5e6e375b3f0c49e;hp=cc927341ede6c631247c6e3d92739e95fc347370;hpb=6db9138eeb183d8ad00f0ccaf6a4e6ecb49e0197;p=simgear.git diff --git a/simgear/scene/material/TextureBuilder.cxx b/simgear/scene/material/TextureBuilder.cxx index cc927341..0c141c47 100644 --- a/simgear/scene/material/TextureBuilder.cxx +++ b/simgear/scene/material/TextureBuilder.cxx @@ -19,20 +19,30 @@ #endif #include "TextureBuilder.hxx" +#include "mipmap.hxx" +#include "Pass.hxx" + +#include +#include +#include #include #include #include #include +#include #include +#include +#include #include #include +#include #include #include - #include +#include #include "Noise.hxx" @@ -43,10 +53,15 @@ using namespace osg; using namespace effect; +TexEnvCombine* buildTexEnvCombine(Effect* effect, + const SGPropertyNode* envProp, + const SGReaderWriterOptions* options); +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, - const osgDB::ReaderWriter::Options* + const SGReaderWriterOptions* options) { return EffectBuilder::buildFromType(effect, type, props, options); @@ -54,7 +69,96 @@ osg::Texture* TextureBuilder::buildFromType(Effect* effect, const string& type, typedef boost::tuple TexTuple; + string, MipMapTuple> TexTuple; + +EffectNameValue texEnvModesInit[] = +{ + {"add", TexEnv::ADD}, + {"blend", TexEnv::BLEND}, + {"decal", TexEnv::DECAL}, + {"modulate", TexEnv::MODULATE}, + {"replace", TexEnv::REPLACE} +}; +EffectPropertyMap 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())); + return env; +} + + +void TextureUnitBuilder::buildAttribute(Effect* effect, Pass* pass, + const SGPropertyNode* prop, + const SGReaderWriterOptions* options) +{ + if (!isAttributeActive(effect, prop)) + return; + // Decode the texture unit + int unit = 0; + const SGPropertyNode* pUnit = prop->getChild("unit"); + if (pUnit) { + unit = pUnit->getValue(); + } else { + const SGPropertyNode* pName = prop->getChild("name"); + if (pName) + try { + unit = boost::lexical_cast(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, e.getFormattedMessage() << ", " + << "maybe the reader did not set the filename attribute, " + << "using white for type '" << type << "' on '" << pass->getName() << "', in " << prop->getPath() ); + 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, + options)))) + 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 { @@ -79,52 +183,70 @@ EffectNameValue wrapModesInit[] = }; EffectPropertyMap wrapModes(wrapModesInit); - TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props, - const osgDB::ReaderWriter::Options* options, + const SGReaderWriterOptions* options, const string& texType) { Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR; - findAttr(filterModes, getEffectPropertyChild(effect, props, "filter"), - minFilter); + const SGPropertyNode* ep = 0; + if ((ep = getEffectPropertyChild(effect, props, "filter"))) + findAttr(filterModes, ep, minFilter); Texture::FilterMode magFilter = Texture::LINEAR; - findAttr(filterModes, getEffectPropertyChild(effect, props, - "mag-filter"), - magFilter); + if ((ep = getEffectPropertyChild(effect, props, "mag-filter"))) + findAttr(filterModes, ep, magFilter); const SGPropertyNode* pWrapS = getEffectPropertyChild(effect, props, "wrap-s"); Texture::WrapMode sWrap = Texture::CLAMP; - findAttr(wrapModes, pWrapS, sWrap); + if (pWrapS) + findAttr(wrapModes, pWrapS, sWrap); const SGPropertyNode* pWrapT = getEffectPropertyChild(effect, props, "wrap-t"); Texture::WrapMode tWrap = Texture::CLAMP; - findAttr(wrapModes, pWrapT, tWrap); + if (pWrapT) + findAttr(wrapModes, pWrapT, tWrap); const SGPropertyNode* pWrapR = getEffectPropertyChild(effect, props, "wrap-r"); Texture::WrapMode rWrap = Texture::CLAMP; - findAttr(wrapModes, pWrapR, rWrap); + if (pWrapR) + findAttr(wrapModes, pWrapR, rWrap); const SGPropertyNode* pImage = getEffectPropertyChild(effect, props, "image"); string imageName; + string absFileName; if (pImage) + { imageName = pImage->getStringValue(); - string absFileName = osgDB::findDataFile(imageName, options); + absFileName = SGModelLib::findDataFile(imageName, options); + if (absFileName.empty()) + { + SG_LOG(SG_INPUT, SG_ALERT, "Texture file not found: '" + << imageName << "'"); + } + } + + const SGPropertyNode* pMipmapControl + = getEffectPropertyChild(effect, props, "mipmap-control"); + MipMapTuple mipmapFunctions( AUTOMATIC, AUTOMATIC, AUTOMATIC, AUTOMATIC ); + if ( pMipmapControl ) + mipmapFunctions = makeMipMapTuple(effect, pMipmapControl, options); + return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap, - texType); + texType, mipmapFunctions); } void setAttrs(const TexTuple& attrs, Texture* tex, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterOptions* options) { const string& imageName = attrs.get<0>(); if (imageName.empty()) { throw BuilderException("no image file"); } else { - osgDB::ReaderWriter::ReadResult result - = osgDB::Registry::instance()->readImage(imageName, options); + osgDB::ReaderWriter::ReadResult result; + result = osgDB::readImageFile(imageName, options); if (result.success()) { - osg::Image* image = result.getImage(); - tex->setImage(GL_FRONT_AND_BACK, image); + osg::ref_ptr image = result.getImage(); + image = computeMipmap( image.get(), attrs.get<7>() ); + tex->setImage(GL_FRONT_AND_BACK, image.get()); int s = image->s(); int t = image->t(); if (s <= t && 32 <= s) { @@ -154,7 +276,7 @@ class TexBuilder : public TextureBuilder public: TexBuilder(const string& texType) : _type(texType) {} Texture* build(Effect* effect, const SGPropertyNode*, - const osgDB::ReaderWriter::Options* options); + const SGReaderWriterOptions* options); protected: typedef map > TexMap; TexMap texMap; @@ -163,7 +285,7 @@ protected: template Texture* TexBuilder::build(Effect* effect, const SGPropertyNode* props, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterOptions* options) { TexTuple attrs = makeTexTuple(effect, props, options, _type); typename TexMap::iterator itr = texMap.find(attrs); @@ -187,11 +309,11 @@ class WhiteTextureBuilder : public TextureBuilder { public: Texture* build(Effect* effect, const SGPropertyNode*, - const osgDB::ReaderWriter::Options* options); + const SGReaderWriterOptions* options); }; Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterOptions* options) { return StateAttributeFactory::instance()->getWhiteTexture(); } @@ -201,6 +323,25 @@ namespace TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder); } +class TransparentTextureBuilder : public TextureBuilder +{ +public: + Texture* build(Effect* effect, const SGPropertyNode*, + const SGReaderWriterOptions* options); +}; + +Texture* TransparentTextureBuilder::build(Effect* effect, const SGPropertyNode*, + const SGReaderWriterOptions* options) +{ + return StateAttributeFactory::instance()->getTransparentTexture(); +} + +namespace +{ +TextureBuilder::Registrar installTransparent("transparent", + new TransparentTextureBuilder); +} + osg::Image* make3DNoiseImage(int texSize) { osg::Image* image = new osg::Image; @@ -250,14 +391,14 @@ class NoiseBuilder : public TextureBuilder { public: Texture* build(Effect* effect, const SGPropertyNode*, - const osgDB::ReaderWriter::Options* options); + const SGReaderWriterOptions* options); protected: typedef map > NoiseMap; NoiseMap _noises; }; Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterOptions* options) { int texSize = 64; const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props, @@ -283,4 +424,453 @@ namespace TextureBuilder::Registrar installNoise("noise", new NoiseBuilder); } + + +// Image names for all sides +typedef boost::tuple 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 SGReaderWriterOptions* options); +protected: + typedef map > CubeMap; + typedef map > CrossCubeMap; + CubeMap _cubemaps; + CrossCubeMap _crossmaps; +}; + +// I use this until osg::CopyImage is fixed +// This one assumes images are the same format and sizes are correct +void copySubImage(const osg::Image* srcImage, int src_s, int src_t, int width, int height, + osg::Image* destImage, int dest_s, int dest_t) +{ + for(int row = 0; rowdata(src_s, src_t+row, 0); + unsigned char* destData = destImage->data(dest_s, dest_t+row, 0); + memcpy(destData, srcData, (width*destImage->getPixelSizeInBits())/8); + } +} + + +Texture* CubeMapBuilder::build(Effect* effect, const SGPropertyNode* props, + const SGReaderWriterOptions* options) +{ + // First check that there is a tag + const SGPropertyNode* texturesProp = getEffectPropertyChild(effect, props, "images"); + const SGPropertyNode* crossProp = getEffectPropertyChild(effect, props, "image"); + if (!texturesProp && !crossProp) { + throw BuilderException("no images defined for cube map"); + return NULL; // This is redundant + } + + // Using 6 separate images + if(texturesProp) { + + SG_LOG(SG_INPUT, SG_DEBUG, "try 6 images "); + + 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; + result = osgDB::readImageFile(_tuple.get<0>(), options); + if(result.success()) { + osg::Image* image = result.getImage(); + cubeTexture->setImage(TextureCubeMap::POSITIVE_X, image); + } + result = osgDB::readImageFile(_tuple.get<1>(), options); + if(result.success()) { + osg::Image* image = result.getImage(); + cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, image); + } + result = osgDB::readImageFile(_tuple.get<2>(), options); + if(result.success()) { + osg::Image* image = result.getImage(); + cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, image); + } + result = osgDB::readImageFile(_tuple.get<3>(), options); + if(result.success()) { + osg::Image* image = result.getImage(); + cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, image); + } + result = osgDB::readImageFile(_tuple.get<4>(), options); + if(result.success()) { + osg::Image* image = result.getImage(); + cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, image); + } + result = osgDB::readImageFile(_tuple.get<5>(), options); + if(result.success()) { + osg::Image* image = result.getImage(); + cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, image); + } + + _cubemaps[_tuple] = cubeTexture; + + return cubeTexture; + } + + + // Using 1 cross image + else if(crossProp) { + SG_LOG(SG_INPUT, SG_DEBUG, "try cross map "); + + std::string texname = crossProp->getStringValue(); + + // Try to find existing cube map + CrossCubeMap::iterator itr = _crossmaps.find(texname); + if (itr != _crossmaps.end()) + return itr->second.get(); + + osgDB::ReaderWriter::ReadResult result; + result = osgDB::readImageFile(texname, options); + if(result.success()) { + osg::Image* image = result.getImage(); + image->flipVertical(); // Seems like the image coordinates are somewhat funny, flip to get better ones + + //cubeTexture->setResizeNonPowerOfTwoHint(false); + + // Size of a single image, 4 rows and 3 columns + int width = image->s() / 3; + int height = image->t() / 4; + int depth = image->r(); + + TextureCubeMap* cubeTexture = new osg::TextureCubeMap; + + // Copy the 6 sub-images and push them + for(int n=0; n<6; n++) { + + SG_LOG(SG_INPUT, SG_DEBUG, "Copying the " << n << "th sub-images and pushing it" ); + + osg::ref_ptr subimg = new osg::Image(); + subimg->allocateImage(width, height, depth, image->getPixelFormat(), image->getDataType()); // Copy attributes + + // Choose correct image + switch(n) { + case 0: // Front + copySubImage(image, width, 0, width, height, subimg.get(), 0, 0); + cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, subimg.get()); + 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); + break; + case 1: // Left + copySubImage(image, 0, height, width, height, subimg.get(), 0, 0); + cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, subimg.get()); + cubeTexture->setWrap(osg::Texture2D::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); + break; + case 2: // Top + copySubImage(image, width, height, width, height, subimg.get(), 0, 0); + cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, subimg.get()); + 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); + break; + case 3: // Right + copySubImage(image, width*2, height, width, height, subimg.get(), 0, 0); + cubeTexture->setImage(TextureCubeMap::POSITIVE_X, subimg.get()); + 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); + break; + case 4: // Back + copySubImage(image, width, height*2, width, height, subimg.get(), 0, 0); + cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, subimg.get()); + 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); + break; + case 5: // Bottom + copySubImage(image, width, height*3, width, height, subimg.get(), 0, 0); + cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, subimg.get()); + 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); + break; + }; + + } + + _crossmaps[texname] = cubeTexture; + + return cubeTexture; + + } else { + throw BuilderException("Could not load cube cross"); + } + } + + return NULL; +} + +namespace { +TextureBuilder::Registrar installCubeMap("cubemap", new CubeMapBuilder); +} + +EffectNameValue 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 combineParams(combineParamInit); + +EffectNameValue 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 sourceParams(sourceParamInit); + +EffectNameValue 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 operandParams(opParamInit); + +TexEnvCombine* buildTexEnvCombine(Effect* effect, const SGPropertyNode* envProp, + const SGReaderWriterOptions* 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()); + } + if ((p = getEffectPropertyChild(effect, envProp, "scale-alpha"))) { + result->setScale_Alpha(p->getValue()); + } +#if 0 + if ((p = getEffectPropertyChild(effect, envProp, "constant-color"))) { + SGVec4d color = p->getValue(); + result->setConstantColor(toOsg(color)); + } else if ((p = getEffectPropertyChild(effect, envProp, + "light-direction"))) { + SGVec3d direction = p->getValue(); + 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 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 tgenModes(tgenModeInit); + +EffectNameValue tgenCoordInit[] = +{ + {"s", TexGen::S}, + {"t", TexGen::T}, + {"r", TexGen::R}, + {"q", TexGen::Q} +}; + +EffectPropertyMap 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(); + result->setPlane(coord, toOsg(plane)); + } + } + return result; +} + +bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss) +{ + SGPropertyNode* texUnit = makeChild(paramRoot, "texture"); + const Texture* tex = getStateAttribute(0, ss); + const Texture2D* texture = dynamic_cast(tex); + makeChild(texUnit, "unit")->setValue(0); + if (!tex) { + // The default shader-based technique ignores active + makeChild(texUnit, "active")->setValue(false); + 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 filter = findName(filterModes, + texture->getFilter(Texture::MIN_FILTER)); + string magFilter = findName(filterModes, + texture->getFilter(Texture::MAG_FILTER)); + 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, "filter")->setStringValue(filter); + makeChild(texUnit, "mag-filter")->setStringValue(magFilter); + makeChild(texUnit, "wrap-s")->setStringValue(wrapS); + makeChild(texUnit, "wrap-t")->setStringValue(wrapT); + makeChild(texUnit, "wrap-r")->setStringValue(wrapR); + return true; +} + }