00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef HAVE_CONFIG_H
00018 # include <simgear_config.h>
00019 #endif
00020
00021 #include "TextureBuilder.hxx"
00022
00023 #include "Pass.hxx"
00024
00025 #include <osg/TexEnv>
00026 #include <osg/TexEnvCombine>
00027 #include <osg/TexGen>
00028 #include <osg/Texture1D>
00029 #include <osg/Texture2D>
00030 #include <osg/Texture3D>
00031 #include <osg/TextureRectangle>
00032 #include <osgDB/FileUtils>
00033
00034 #include <boost/lexical_cast.hpp>
00035 #include <boost/tuple/tuple.hpp>
00036 #include <boost/tuple/tuple_comparison.hpp>
00037
00038 #include <simgear/scene/util/SGSceneFeatures.hxx>
00039 #include <simgear/scene/util/StateAttributeFactory.hxx>
00040 #include <simgear/math/SGMath.hxx>
00041 #include <simgear/structure/OSGUtils.hxx>
00042
00043 #include "Noise.hxx"
00044
00045 namespace simgear
00046 {
00047 using namespace std;
00048 using namespace osg;
00049
00050 using namespace effect;
00051
00052 TexEnvCombine* buildTexEnvCombine(Effect* effect,
00053 const SGPropertyNode* envProp);
00054 TexGen* buildTexGen(Effect* Effect, const SGPropertyNode* tgenProp);
00055
00056
00057 osg::Texture* TextureBuilder::buildFromType(Effect* effect, const string& type,
00058 const SGPropertyNode*props,
00059 const osgDB::ReaderWriter::Options*
00060 options)
00061 {
00062 return EffectBuilder<Texture>::buildFromType(effect, type, props, options);
00063 }
00064
00065 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
00066 Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
00067 string> TexTuple;
00068
00069 EffectNameValue<TexEnv::Mode> texEnvModesInit[] =
00070 {
00071 {"add", TexEnv::ADD},
00072 {"blend", TexEnv::BLEND},
00073 {"decal", TexEnv::DECAL},
00074 {"modulate", TexEnv::MODULATE},
00075 {"replace", TexEnv::REPLACE}
00076 };
00077 EffectPropertyMap<TexEnv::Mode> texEnvModes(texEnvModesInit);
00078
00079 TexEnv* buildTexEnv(Effect* effect, const SGPropertyNode* prop)
00080 {
00081 const SGPropertyNode* modeProp = getEffectPropertyChild(effect, prop,
00082 "mode");
00083 const SGPropertyNode* colorProp = getEffectPropertyChild(effect, prop,
00084 "color");
00085 if (!modeProp)
00086 return 0;
00087 TexEnv::Mode mode = TexEnv::MODULATE;
00088 findAttr(texEnvModes, modeProp, mode);
00089 if (mode == TexEnv::MODULATE) {
00090 return StateAttributeFactory::instance()->getStandardTexEnv();
00091 }
00092 TexEnv* env = new TexEnv(mode);
00093 if (colorProp)
00094 env->setColor(toOsg(colorProp->getValue<SGVec4d>()));
00095 return env;
00096 }
00097
00098
00099 void TextureUnitBuilder::buildAttribute(Effect* effect, Pass* pass,
00100 const SGPropertyNode* prop,
00101 const osgDB::ReaderWriter::Options* options)
00102 {
00103 if (!isAttributeActive(effect, prop))
00104 return;
00105
00106 int unit = 0;
00107 const SGPropertyNode* pUnit = prop->getChild("unit");
00108 if (pUnit) {
00109 unit = pUnit->getValue<int>();
00110 } else {
00111 const SGPropertyNode* pName = prop->getChild("name");
00112 if (pName)
00113 try {
00114 unit = boost::lexical_cast<int>(pName->getStringValue());
00115 } catch (boost::bad_lexical_cast& lex) {
00116 SG_LOG(SG_INPUT, SG_ALERT, "can't decode name as texture unit "
00117 << lex.what());
00118 }
00119 }
00120 const SGPropertyNode* pType = getEffectPropertyChild(effect, prop, "type");
00121 string type;
00122 if (!pType)
00123 type = "2d";
00124 else
00125 type = pType->getStringValue();
00126 Texture* texture = 0;
00127 try {
00128 texture = TextureBuilder::buildFromType(effect, type, prop,
00129 options);
00130 }
00131 catch (BuilderException& e) {
00132 SG_LOG(SG_INPUT, SG_ALERT, "No image file for texture, using white ");
00133 texture = StateAttributeFactory::instance()->getWhiteTexture();
00134 }
00135 pass->setTextureAttributeAndModes(unit, texture);
00136 const SGPropertyNode* envProp = prop->getChild("environment");
00137 if (envProp) {
00138 TexEnv* env = buildTexEnv(effect, envProp);
00139 if (env)
00140 pass->setTextureAttributeAndModes(unit, env);
00141 }
00142 const SGPropertyNode* combineProp = prop->getChild("texenv-combine");
00143 TexEnvCombine* combiner = 0;
00144 if (combineProp && ((combiner = buildTexEnvCombine(effect, combineProp))))
00145 pass->setTextureAttributeAndModes(unit, combiner);
00146 const SGPropertyNode* tgenProp = prop->getChild("texgen");
00147 TexGen* tgen = 0;
00148 if (tgenProp && (tgen = buildTexGen(effect, tgenProp)))
00149 pass->setTextureAttributeAndModes(unit, tgen);
00150 }
00151
00152
00153
00154
00155 namespace
00156 {
00157 EffectNameValue<Texture::FilterMode> filterModesInit[] =
00158 {
00159 { "linear", Texture::LINEAR },
00160 { "linear-mipmap-linear", Texture::LINEAR_MIPMAP_LINEAR},
00161 { "linear-mipmap-nearest", Texture::LINEAR_MIPMAP_NEAREST},
00162 { "nearest", Texture::NEAREST},
00163 { "nearest-mipmap-linear", Texture::NEAREST_MIPMAP_LINEAR},
00164 { "nearest-mipmap-nearest", Texture::NEAREST_MIPMAP_NEAREST}
00165 };
00166 EffectPropertyMap<Texture::FilterMode> filterModes(filterModesInit);
00167
00168 EffectNameValue<Texture::WrapMode> wrapModesInit[] =
00169 {
00170 {"clamp", Texture::CLAMP},
00171 {"clamp-to-border", Texture::CLAMP_TO_BORDER},
00172 {"clamp-to-edge", Texture::CLAMP_TO_EDGE},
00173 {"mirror", Texture::MIRROR},
00174 {"repeat", Texture::REPEAT}
00175 };
00176 EffectPropertyMap<Texture::WrapMode> wrapModes(wrapModesInit);
00177
00178
00179 TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
00180 const osgDB::ReaderWriter::Options* options,
00181 const string& texType)
00182 {
00183 Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR;
00184 const SGPropertyNode* ep = 0;
00185 if ((ep = getEffectPropertyChild(effect, props, "filter")))
00186 findAttr(filterModes, ep, minFilter);
00187 Texture::FilterMode magFilter = Texture::LINEAR;
00188 if ((ep = getEffectPropertyChild(effect, props, "mag-filter")))
00189 findAttr(filterModes, ep, magFilter);
00190 const SGPropertyNode* pWrapS
00191 = getEffectPropertyChild(effect, props, "wrap-s");
00192 Texture::WrapMode sWrap = Texture::CLAMP;
00193 if (pWrapS)
00194 findAttr(wrapModes, pWrapS, sWrap);
00195 const SGPropertyNode* pWrapT
00196 = getEffectPropertyChild(effect, props, "wrap-t");
00197 Texture::WrapMode tWrap = Texture::CLAMP;
00198 if (pWrapT)
00199 findAttr(wrapModes, pWrapT, tWrap);
00200 const SGPropertyNode* pWrapR
00201 = getEffectPropertyChild(effect, props, "wrap-r");
00202 Texture::WrapMode rWrap = Texture::CLAMP;
00203 if (pWrapR)
00204 findAttr(wrapModes, pWrapR, rWrap);
00205 const SGPropertyNode* pImage
00206 = getEffectPropertyChild(effect, props, "image");
00207 string imageName;
00208 if (pImage)
00209 imageName = pImage->getStringValue();
00210 string absFileName = osgDB::findDataFile(imageName, options);
00211 return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
00212 texType);
00213 }
00214
00215 void setAttrs(const TexTuple& attrs, Texture* tex,
00216 const osgDB::ReaderWriter::Options* options)
00217 {
00218 const string& imageName = attrs.get<0>();
00219 if (imageName.empty()) {
00220 throw BuilderException("no image file");
00221 } else {
00222 osgDB::ReaderWriter::ReadResult result
00223 = osgDB::Registry::instance()->readImage(imageName, options);
00224 if (result.success()) {
00225 osg::Image* image = result.getImage();
00226 tex->setImage(GL_FRONT_AND_BACK, image);
00227 int s = image->s();
00228 int t = image->t();
00229 if (s <= t && 32 <= s) {
00230 SGSceneFeatures::instance()->setTextureCompression(tex);
00231 } else if (t < s && 32 <= t) {
00232 SGSceneFeatures::instance()->setTextureCompression(tex);
00233 }
00234 tex->setMaxAnisotropy(SGSceneFeatures::instance()
00235 ->getTextureFilter());
00236 } else {
00237 SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
00238 << imageName);
00239 }
00240 }
00241
00242 tex->setFilter(Texture::MIN_FILTER, attrs.get<1>());
00243 tex->setFilter(Texture::MAG_FILTER, attrs.get<2>());
00244 tex->setWrap(Texture::WRAP_S, attrs.get<3>());
00245 tex->setWrap(Texture::WRAP_T, attrs.get<4>());
00246 tex->setWrap(Texture::WRAP_R, attrs.get<5>());
00247 }
00248 }
00249
00250 template<typename T>
00251 class TexBuilder : public TextureBuilder
00252 {
00253 public:
00254 TexBuilder(const string& texType) : _type(texType) {}
00255 Texture* build(Effect* effect, const SGPropertyNode*,
00256 const osgDB::ReaderWriter::Options* options);
00257 protected:
00258 typedef map<TexTuple, ref_ptr<T> > TexMap;
00259 TexMap texMap;
00260 const string _type;
00261 };
00262
00263 template<typename T>
00264 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
00265 const osgDB::ReaderWriter::Options* options)
00266 {
00267 TexTuple attrs = makeTexTuple(effect, props, options, _type);
00268 typename TexMap::iterator itr = texMap.find(attrs);
00269 if (itr != texMap.end())
00270 return itr->second.get();
00271 T* tex = new T;
00272 setAttrs(attrs, tex, options);
00273 texMap.insert(make_pair(attrs, tex));
00274 return tex;
00275 }
00276
00277
00278 namespace
00279 {
00280 TextureBuilder::Registrar install1D("1d", new TexBuilder<Texture1D>("1d"));
00281 TextureBuilder::Registrar install2D("2d", new TexBuilder<Texture2D>("2d"));
00282 TextureBuilder::Registrar install3D("3d", new TexBuilder<Texture3D>("3d"));
00283 }
00284
00285 class WhiteTextureBuilder : public TextureBuilder
00286 {
00287 public:
00288 Texture* build(Effect* effect, const SGPropertyNode*,
00289 const osgDB::ReaderWriter::Options* options);
00290 };
00291
00292 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
00293 const osgDB::ReaderWriter::Options* options)
00294 {
00295 return StateAttributeFactory::instance()->getWhiteTexture();
00296 }
00297
00298 namespace
00299 {
00300 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
00301 }
00302
00303 class TransparentTextureBuilder : public TextureBuilder
00304 {
00305 public:
00306 Texture* build(Effect* effect, const SGPropertyNode*,
00307 const osgDB::ReaderWriter::Options* options);
00308 };
00309
00310 Texture* TransparentTextureBuilder::build(Effect* effect, const SGPropertyNode*,
00311 const osgDB::ReaderWriter::Options* options)
00312 {
00313 return StateAttributeFactory::instance()->getTransparentTexture();
00314 }
00315
00316 namespace
00317 {
00318 TextureBuilder::Registrar installTransparent("transparent",
00319 new TransparentTextureBuilder);
00320 }
00321
00322 osg::Image* make3DNoiseImage(int texSize)
00323 {
00324 osg::Image* image = new osg::Image;
00325 image->setImage(texSize, texSize, texSize,
00326 4, GL_RGBA, GL_UNSIGNED_BYTE,
00327 new unsigned char[4 * texSize * texSize * texSize],
00328 osg::Image::USE_NEW_DELETE);
00329
00330 const int startFrequency = 4;
00331 const int numOctaves = 4;
00332
00333 int f, i, j, k, inc;
00334 double ni[3];
00335 double inci, incj, inck;
00336 int frequency = startFrequency;
00337 GLubyte *ptr;
00338 double amp = 0.5;
00339
00340 osg::notify(osg::WARN) << "creating 3D noise texture... ";
00341
00342 for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
00343 {
00344 SetNoiseFrequency(frequency);
00345 ptr = image->data();
00346 ni[0] = ni[1] = ni[2] = 0;
00347
00348 inci = 1.0 / (texSize / frequency);
00349 for (i = 0; i < texSize; ++i, ni[0] += inci)
00350 {
00351 incj = 1.0 / (texSize / frequency);
00352 for (j = 0; j < texSize; ++j, ni[1] += incj)
00353 {
00354 inck = 1.0 / (texSize / frequency);
00355 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
00356 {
00357 *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
00358 }
00359 }
00360 }
00361 }
00362
00363 osg::notify(osg::WARN) << "DONE" << std::endl;
00364 return image;
00365 }
00366
00367 class NoiseBuilder : public TextureBuilder
00368 {
00369 public:
00370 Texture* build(Effect* effect, const SGPropertyNode*,
00371 const osgDB::ReaderWriter::Options* options);
00372 protected:
00373 typedef map<int, ref_ptr<Texture3D> > NoiseMap;
00374 NoiseMap _noises;
00375 };
00376
00377 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
00378 const osgDB::ReaderWriter::Options* options)
00379 {
00380 int texSize = 64;
00381 const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
00382 "size");
00383 if (sizeProp)
00384 texSize = sizeProp->getValue<int>();
00385 NoiseMap::iterator itr = _noises.find(texSize);
00386 if (itr != _noises.end())
00387 return itr->second.get();
00388 Texture3D* noiseTexture = new osg::Texture3D;
00389 noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
00390 noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
00391 noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
00392 noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
00393 noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
00394 noiseTexture->setImage( make3DNoiseImage(texSize) );
00395 _noises.insert(make_pair(texSize, noiseTexture));
00396 return noiseTexture;
00397 }
00398
00399 namespace
00400 {
00401 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
00402 }
00403
00404 EffectNameValue<TexEnvCombine::CombineParam> combineParamInit[] =
00405 {
00406 {"replace", TexEnvCombine::REPLACE},
00407 {"modulate", TexEnvCombine::MODULATE},
00408 {"add", TexEnvCombine::ADD},
00409 {"add-signed", TexEnvCombine::ADD_SIGNED},
00410 {"interpolate", TexEnvCombine::INTERPOLATE},
00411 {"subtract", TexEnvCombine::SUBTRACT},
00412 {"dot3-rgb", TexEnvCombine::DOT3_RGB},
00413 {"dot3-rgba", TexEnvCombine::DOT3_RGBA}
00414 };
00415
00416 EffectPropertyMap<TexEnvCombine::CombineParam> combineParams(combineParamInit);
00417
00418 EffectNameValue<TexEnvCombine::SourceParam> sourceParamInit[] =
00419 {
00420 {"constant", TexEnvCombine::CONSTANT},
00421 {"primary_color", TexEnvCombine::PRIMARY_COLOR},
00422 {"previous", TexEnvCombine::PREVIOUS},
00423 {"texture", TexEnvCombine::TEXTURE},
00424 {"texture0", TexEnvCombine::TEXTURE0},
00425 {"texture1", TexEnvCombine::TEXTURE1},
00426 {"texture2", TexEnvCombine::TEXTURE2},
00427 {"texture3", TexEnvCombine::TEXTURE3},
00428 {"texture4", TexEnvCombine::TEXTURE4},
00429 {"texture5", TexEnvCombine::TEXTURE5},
00430 {"texture6", TexEnvCombine::TEXTURE6},
00431 {"texture7", TexEnvCombine::TEXTURE7}
00432 };
00433
00434 EffectPropertyMap<TexEnvCombine::SourceParam> sourceParams(sourceParamInit);
00435
00436 EffectNameValue<TexEnvCombine::OperandParam> opParamInit[] =
00437 {
00438 {"src-color", TexEnvCombine::SRC_COLOR},
00439 {"one-minus-src-color", TexEnvCombine::ONE_MINUS_SRC_COLOR},
00440 {"src-alpha", TexEnvCombine::SRC_ALPHA},
00441 {"one-minus-src-alpha", TexEnvCombine::ONE_MINUS_SRC_ALPHA}
00442 };
00443
00444 EffectPropertyMap<TexEnvCombine::OperandParam> operandParams(opParamInit);
00445
00446 TexEnvCombine* buildTexEnvCombine(Effect* effect, const SGPropertyNode* envProp)
00447 {
00448 if (!isAttributeActive(effect, envProp))
00449 return 0;
00450 TexEnvCombine* result = new TexEnvCombine;
00451 const SGPropertyNode* p = 0;
00452 if ((p = getEffectPropertyChild(effect, envProp, "combine-rgb"))) {
00453 TexEnvCombine::CombineParam crgb = TexEnvCombine::MODULATE;
00454 findAttr(combineParams, p, crgb);
00455 result->setCombine_RGB(crgb);
00456 }
00457 if ((p = getEffectPropertyChild(effect, envProp, "combine-alpha"))) {
00458 TexEnvCombine::CombineParam calpha = TexEnvCombine::MODULATE;
00459 findAttr(combineParams, p, calpha);
00460 result->setCombine_Alpha(calpha);
00461 }
00462 if ((p = getEffectPropertyChild(effect, envProp, "source0-rgb"))) {
00463 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
00464 findAttr(sourceParams, p, source);
00465 result->setSource0_RGB(source);
00466 }
00467 if ((p = getEffectPropertyChild(effect, envProp, "source1-rgb"))) {
00468 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
00469 findAttr(sourceParams, p, source);
00470 result->setSource1_RGB(source);
00471 }
00472 if ((p = getEffectPropertyChild(effect, envProp, "source2-rgb"))) {
00473 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
00474 findAttr(sourceParams, p, source);
00475 result->setSource2_RGB(source);
00476 }
00477 if ((p = getEffectPropertyChild(effect, envProp, "source0-alpha"))) {
00478 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
00479 findAttr(sourceParams, p, source);
00480 result->setSource0_Alpha(source);
00481 }
00482 if ((p = getEffectPropertyChild(effect, envProp, "source1-alpha"))) {
00483 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
00484 findAttr(sourceParams, p, source);
00485 result->setSource1_Alpha(source);
00486 }
00487 if ((p = getEffectPropertyChild(effect, envProp, "source2-alpha"))) {
00488 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
00489 findAttr(sourceParams, p, source);
00490 result->setSource2_Alpha(source);
00491 }
00492 if ((p = getEffectPropertyChild(effect, envProp, "operand0-rgb"))) {
00493 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
00494 findAttr(operandParams, p, op);
00495 result->setOperand0_RGB(op);
00496 }
00497 if ((p = getEffectPropertyChild(effect, envProp, "operand1-rgb"))) {
00498 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
00499 findAttr(operandParams, p, op);
00500 result->setOperand1_RGB(op);
00501 }
00502 if ((p = getEffectPropertyChild(effect, envProp, "operand2-rgb"))) {
00503 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
00504 findAttr(operandParams, p, op);
00505 result->setOperand2_RGB(op);
00506 }
00507 if ((p = getEffectPropertyChild(effect, envProp, "operand0-alpha"))) {
00508 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
00509 findAttr(operandParams, p, op);
00510 result->setOperand0_Alpha(op);
00511 }
00512 if ((p = getEffectPropertyChild(effect, envProp, "operand1-alpha"))) {
00513 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
00514 findAttr(operandParams, p, op);
00515 result->setOperand1_Alpha(op);
00516 }
00517 if ((p = getEffectPropertyChild(effect, envProp, "operand2-alpha"))) {
00518 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
00519 findAttr(operandParams, p, op);
00520 result->setOperand2_Alpha(op);
00521 }
00522 if ((p = getEffectPropertyChild(effect, envProp, "scale-rgb"))) {
00523 result->setScale_RGB(p->getValue<float>());
00524 }
00525 if ((p = getEffectPropertyChild(effect, envProp, "scale-alpha"))) {
00526 result->setScale_Alpha(p->getValue<float>());
00527 }
00528 #if 0
00529 if ((p = getEffectPropertyChild(effect, envProp, "constant-color"))) {
00530 SGVec4d color = p->getValue<SGVec4d>();
00531 result->setConstantColor(toOsg(color));
00532 } else if ((p = getEffectPropertyChild(effect, envProp,
00533 "light-direction"))) {
00534 SGVec3d direction = p->getValue<SGVec3d>();
00535 result->setConstantColorAsLightDirection(toOsg(direction));
00536 }
00537 #endif
00538 const SGPropertyNode* colorNode = envProp->getChild("constant-color");
00539 if (colorNode)
00540 initFromParameters(effect, colorNode, result,
00541 &TexEnvCombine::setConstantColor, colorFields);
00542 return result;
00543 }
00544
00545 EffectNameValue<TexGen::Mode> tgenModeInit[] =
00546 {
00547 { "object-linear", TexGen::OBJECT_LINEAR},
00548 { "eye-linear", TexGen::EYE_LINEAR},
00549 { "sphere-map", TexGen::SPHERE_MAP},
00550 { "normal-map", TexGen::NORMAL_MAP},
00551 { "reflection-map", TexGen::REFLECTION_MAP}
00552 };
00553
00554 EffectPropertyMap<TexGen::Mode> tgenModes(tgenModeInit);
00555
00556 EffectNameValue<TexGen::Coord> tgenCoordInit[] =
00557 {
00558 {"s", TexGen::S},
00559 {"t", TexGen::T},
00560 {"r", TexGen::R},
00561 {"q", TexGen::Q}
00562 };
00563
00564 EffectPropertyMap<TexGen::Coord> tgenCoords(tgenCoordInit);
00565
00566 TexGen* buildTexGen(Effect* effect, const SGPropertyNode* tgenProp)
00567 {
00568 if (!isAttributeActive(effect, tgenProp))
00569 return 0;
00570 TexGen* result = new TexGen;
00571 const SGPropertyNode* p = 0;
00572 TexGen::Mode mode = TexGen::OBJECT_LINEAR;
00573 findAttr(tgenModes, getEffectPropertyChild(effect, tgenProp, "mode"), mode);
00574 result->setMode(mode);
00575 const SGPropertyNode* planesNode = tgenProp->getChild("planes");
00576 if (planesNode) {
00577 for (int i = 0; i < planesNode->nChildren(); ++i) {
00578 const SGPropertyNode* planeNode = planesNode->getChild(i);
00579 TexGen::Coord coord;
00580 findAttr(tgenCoords, planeNode->getName(), coord);
00581 const SGPropertyNode* realNode
00582 = getEffectPropertyNode(effect, planeNode);
00583 SGVec4d plane = realNode->getValue<SGVec4d>();
00584 result->setPlane(coord, toOsg(plane));
00585 }
00586 }
00587 return result;
00588 }
00589
00590 bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
00591 {
00592 SGPropertyNode* texUnit = makeChild(paramRoot, "texture");
00593 const Texture* tex = getStateAttribute<Texture>(0, ss);
00594 const Texture2D* texture = dynamic_cast<const Texture2D*>(tex);
00595 makeChild(texUnit, "unit")->setValue(0);
00596 if (!tex) {
00597
00598 makeChild(texUnit, "active")->setValue(false);
00599 return false;
00600 }
00601 const Image* image = texture->getImage();
00602 string imageName;
00603 if (image) {
00604 imageName = image->getFileName();
00605 } else {
00606 makeChild(texUnit, "active")->setValue(false);
00607 makeChild(texUnit, "type")->setValue("white");
00608 return false;
00609 }
00610 makeChild(texUnit, "active")->setValue(true);
00611 makeChild(texUnit, "type")->setValue("2d");
00612 string filter = findName(filterModes,
00613 texture->getFilter(Texture::MIN_FILTER));
00614 string magFilter = findName(filterModes,
00615 texture->getFilter(Texture::MAG_FILTER));
00616 string wrapS = findName(wrapModes, texture->getWrap(Texture::WRAP_S));
00617 string wrapT = findName(wrapModes, texture->getWrap(Texture::WRAP_T));
00618 string wrapR = findName(wrapModes, texture->getWrap(Texture::WRAP_R));
00619 makeChild(texUnit, "image")->setStringValue(imageName);
00620 makeChild(texUnit, "filter")->setStringValue(filter);
00621 makeChild(texUnit, "mag-filter")->setStringValue(magFilter);
00622 makeChild(texUnit, "wrap-s")->setStringValue(wrapS);
00623 makeChild(texUnit, "wrap-t")->setStringValue(wrapT);
00624 makeChild(texUnit, "wrap-r")->setStringValue(wrapR);
00625 return true;
00626 }
00627
00628 }