1 // Copyright (C) 2009 Tim Moore timoore@redhat.com
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # include <simgear_config.h>
21 #include "TextureBuilder.hxx"
26 #include <osg/TexEnvCombine>
28 #include <osg/Texture1D>
29 #include <osg/Texture2D>
30 #include <osg/Texture3D>
31 #include <osg/TextureRectangle>
32 #include <osgDB/FileUtils>
34 #include <boost/lexical_cast.hpp>
35 #include <boost/tuple/tuple.hpp>
36 #include <boost/tuple/tuple_comparison.hpp>
38 #include <simgear/scene/util/SGSceneFeatures.hxx>
39 #include <simgear/scene/util/StateAttributeFactory.hxx>
40 #include <simgear/math/SGMath.hxx>
41 #include <simgear/structure/OSGUtils.hxx>
50 using namespace effect;
52 TexEnvCombine* buildTexEnvCombine(Effect* effect,
53 const SGPropertyNode* envProp);
54 TexGen* buildTexGen(Effect* Effect, const SGPropertyNode* tgenProp);
56 // Hack to force inclusion of TextureBuilder.cxx in library
57 osg::Texture* TextureBuilder::buildFromType(Effect* effect, const string& type,
58 const SGPropertyNode*props,
59 const osgDB::ReaderWriter::Options*
62 return EffectBuilder<Texture>::buildFromType(effect, type, props, options);
65 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
66 Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
69 EffectNameValue<TexEnv::Mode> texEnvModesInit[] =
72 {"blend", TexEnv::BLEND},
73 {"decal", TexEnv::DECAL},
74 {"modulate", TexEnv::MODULATE},
75 {"replace", TexEnv::REPLACE}
77 EffectPropertyMap<TexEnv::Mode> texEnvModes(texEnvModesInit);
79 TexEnv* buildTexEnv(Effect* effect, const SGPropertyNode* prop)
81 const SGPropertyNode* modeProp = getEffectPropertyChild(effect, prop,
83 const SGPropertyNode* colorProp = getEffectPropertyChild(effect, prop,
87 TexEnv::Mode mode = TexEnv::MODULATE;
88 findAttr(texEnvModes, modeProp, mode);
89 if (mode == TexEnv::MODULATE) {
90 return StateAttributeFactory::instance()->getStandardTexEnv();
92 TexEnv* env = new TexEnv(mode);
94 env->setColor(toOsg(colorProp->getValue<SGVec4d>()));
99 void TextureUnitBuilder::buildAttribute(Effect* effect, Pass* pass,
100 const SGPropertyNode* prop,
101 const osgDB::ReaderWriter::Options* options)
103 if (!isAttributeActive(effect, prop))
105 // Decode the texture unit
107 const SGPropertyNode* pUnit = prop->getChild("unit");
109 unit = pUnit->getValue<int>();
111 const SGPropertyNode* pName = prop->getChild("name");
114 unit = boost::lexical_cast<int>(pName->getStringValue());
115 } catch (boost::bad_lexical_cast& lex) {
116 SG_LOG(SG_INPUT, SG_ALERT, "can't decode name as texture unit "
120 const SGPropertyNode* pType = getEffectPropertyChild(effect, prop, "type");
125 type = pType->getStringValue();
126 Texture* texture = 0;
128 texture = TextureBuilder::buildFromType(effect, type, prop,
131 catch (BuilderException& e) {
132 SG_LOG(SG_INPUT, SG_ALERT, "No image file for texture, using white ");
133 texture = StateAttributeFactory::instance()->getWhiteTexture();
135 pass->setTextureAttributeAndModes(unit, texture);
136 const SGPropertyNode* envProp = prop->getChild("environment");
138 TexEnv* env = buildTexEnv(effect, envProp);
140 pass->setTextureAttributeAndModes(unit, env);
142 const SGPropertyNode* combineProp = prop->getChild("texenv-combine");
143 TexEnvCombine* combiner = 0;
144 if (combineProp && ((combiner = buildTexEnvCombine(effect, combineProp))))
145 pass->setTextureAttributeAndModes(unit, combiner);
146 const SGPropertyNode* tgenProp = prop->getChild("texgen");
148 if (tgenProp && (tgen = buildTexGen(effect, tgenProp)))
149 pass->setTextureAttributeAndModes(unit, tgen);
152 // InstallAttributeBuilder call is in Effect.cxx to force this file to
157 EffectNameValue<Texture::FilterMode> filterModesInit[] =
159 { "linear", Texture::LINEAR },
160 { "linear-mipmap-linear", Texture::LINEAR_MIPMAP_LINEAR},
161 { "linear-mipmap-nearest", Texture::LINEAR_MIPMAP_NEAREST},
162 { "nearest", Texture::NEAREST},
163 { "nearest-mipmap-linear", Texture::NEAREST_MIPMAP_LINEAR},
164 { "nearest-mipmap-nearest", Texture::NEAREST_MIPMAP_NEAREST}
166 EffectPropertyMap<Texture::FilterMode> filterModes(filterModesInit);
168 EffectNameValue<Texture::WrapMode> wrapModesInit[] =
170 {"clamp", Texture::CLAMP},
171 {"clamp-to-border", Texture::CLAMP_TO_BORDER},
172 {"clamp-to-edge", Texture::CLAMP_TO_EDGE},
173 {"mirror", Texture::MIRROR},
174 {"repeat", Texture::REPEAT}
176 EffectPropertyMap<Texture::WrapMode> wrapModes(wrapModesInit);
179 TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
180 const osgDB::ReaderWriter::Options* options,
181 const string& texType)
183 Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR;
184 const SGPropertyNode* ep = 0;
185 if ((ep = getEffectPropertyChild(effect, props, "filter")))
186 findAttr(filterModes, ep, minFilter);
187 Texture::FilterMode magFilter = Texture::LINEAR;
188 if ((ep = getEffectPropertyChild(effect, props, "mag-filter")))
189 findAttr(filterModes, ep, magFilter);
190 const SGPropertyNode* pWrapS
191 = getEffectPropertyChild(effect, props, "wrap-s");
192 Texture::WrapMode sWrap = Texture::CLAMP;
194 findAttr(wrapModes, pWrapS, sWrap);
195 const SGPropertyNode* pWrapT
196 = getEffectPropertyChild(effect, props, "wrap-t");
197 Texture::WrapMode tWrap = Texture::CLAMP;
199 findAttr(wrapModes, pWrapT, tWrap);
200 const SGPropertyNode* pWrapR
201 = getEffectPropertyChild(effect, props, "wrap-r");
202 Texture::WrapMode rWrap = Texture::CLAMP;
204 findAttr(wrapModes, pWrapR, rWrap);
205 const SGPropertyNode* pImage
206 = getEffectPropertyChild(effect, props, "image");
209 imageName = pImage->getStringValue();
210 string absFileName = osgDB::findDataFile(imageName, options);
211 return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
215 void setAttrs(const TexTuple& attrs, Texture* tex,
216 const osgDB::ReaderWriter::Options* options)
218 const string& imageName = attrs.get<0>();
219 if (imageName.empty()) {
220 throw BuilderException("no image file");
222 osgDB::ReaderWriter::ReadResult result
223 = osgDB::Registry::instance()->readImage(imageName, options);
224 if (result.success()) {
225 osg::Image* image = result.getImage();
226 tex->setImage(GL_FRONT_AND_BACK, image);
229 if (s <= t && 32 <= s) {
230 SGSceneFeatures::instance()->setTextureCompression(tex);
231 } else if (t < s && 32 <= t) {
232 SGSceneFeatures::instance()->setTextureCompression(tex);
234 tex->setMaxAnisotropy(SGSceneFeatures::instance()
235 ->getTextureFilter());
237 SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
241 // texture->setDataVariance(osg::Object::STATIC);
242 tex->setFilter(Texture::MIN_FILTER, attrs.get<1>());
243 tex->setFilter(Texture::MAG_FILTER, attrs.get<2>());
244 tex->setWrap(Texture::WRAP_S, attrs.get<3>());
245 tex->setWrap(Texture::WRAP_T, attrs.get<4>());
246 tex->setWrap(Texture::WRAP_R, attrs.get<5>());
251 class TexBuilder : public TextureBuilder
254 TexBuilder(const string& texType) : _type(texType) {}
255 Texture* build(Effect* effect, const SGPropertyNode*,
256 const osgDB::ReaderWriter::Options* options);
258 typedef map<TexTuple, ref_ptr<T> > TexMap;
264 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
265 const osgDB::ReaderWriter::Options* options)
267 TexTuple attrs = makeTexTuple(effect, props, options, _type);
268 typename TexMap::iterator itr = texMap.find(attrs);
269 if (itr != texMap.end())
270 return itr->second.get();
272 setAttrs(attrs, tex, options);
273 texMap.insert(make_pair(attrs, tex));
280 TextureBuilder::Registrar install1D("1d", new TexBuilder<Texture1D>("1d"));
281 TextureBuilder::Registrar install2D("2d", new TexBuilder<Texture2D>("2d"));
282 TextureBuilder::Registrar install3D("3d", new TexBuilder<Texture3D>("3d"));
285 class WhiteTextureBuilder : public TextureBuilder
288 Texture* build(Effect* effect, const SGPropertyNode*,
289 const osgDB::ReaderWriter::Options* options);
292 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
293 const osgDB::ReaderWriter::Options* options)
295 return StateAttributeFactory::instance()->getWhiteTexture();
300 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
303 class TransparentTextureBuilder : public TextureBuilder
306 Texture* build(Effect* effect, const SGPropertyNode*,
307 const osgDB::ReaderWriter::Options* options);
310 Texture* TransparentTextureBuilder::build(Effect* effect, const SGPropertyNode*,
311 const osgDB::ReaderWriter::Options* options)
313 return StateAttributeFactory::instance()->getTransparentTexture();
318 TextureBuilder::Registrar installTransparent("transparent",
319 new TransparentTextureBuilder);
322 osg::Image* make3DNoiseImage(int texSize)
324 osg::Image* image = new osg::Image;
325 image->setImage(texSize, texSize, texSize,
326 4, GL_RGBA, GL_UNSIGNED_BYTE,
327 new unsigned char[4 * texSize * texSize * texSize],
328 osg::Image::USE_NEW_DELETE);
330 const int startFrequency = 4;
331 const int numOctaves = 4;
335 double inci, incj, inck;
336 int frequency = startFrequency;
340 osg::notify(osg::WARN) << "creating 3D noise texture... ";
342 for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
344 SetNoiseFrequency(frequency);
346 ni[0] = ni[1] = ni[2] = 0;
348 inci = 1.0 / (texSize / frequency);
349 for (i = 0; i < texSize; ++i, ni[0] += inci)
351 incj = 1.0 / (texSize / frequency);
352 for (j = 0; j < texSize; ++j, ni[1] += incj)
354 inck = 1.0 / (texSize / frequency);
355 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
357 *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
363 osg::notify(osg::WARN) << "DONE" << std::endl;
367 class NoiseBuilder : public TextureBuilder
370 Texture* build(Effect* effect, const SGPropertyNode*,
371 const osgDB::ReaderWriter::Options* options);
373 typedef map<int, ref_ptr<Texture3D> > NoiseMap;
377 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
378 const osgDB::ReaderWriter::Options* options)
381 const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
384 texSize = sizeProp->getValue<int>();
385 NoiseMap::iterator itr = _noises.find(texSize);
386 if (itr != _noises.end())
387 return itr->second.get();
388 Texture3D* noiseTexture = new osg::Texture3D;
389 noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
390 noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
391 noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
392 noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
393 noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
394 noiseTexture->setImage( make3DNoiseImage(texSize) );
395 _noises.insert(make_pair(texSize, noiseTexture));
401 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
404 EffectNameValue<TexEnvCombine::CombineParam> combineParamInit[] =
406 {"replace", TexEnvCombine::REPLACE},
407 {"modulate", TexEnvCombine::MODULATE},
408 {"add", TexEnvCombine::ADD},
409 {"add-signed", TexEnvCombine::ADD_SIGNED},
410 {"interpolate", TexEnvCombine::INTERPOLATE},
411 {"subtract", TexEnvCombine::SUBTRACT},
412 {"dot3-rgb", TexEnvCombine::DOT3_RGB},
413 {"dot3-rgba", TexEnvCombine::DOT3_RGBA}
416 EffectPropertyMap<TexEnvCombine::CombineParam> combineParams(combineParamInit);
418 EffectNameValue<TexEnvCombine::SourceParam> sourceParamInit[] =
420 {"constant", TexEnvCombine::CONSTANT},
421 {"primary_color", TexEnvCombine::PRIMARY_COLOR},
422 {"previous", TexEnvCombine::PREVIOUS},
423 {"texture", TexEnvCombine::TEXTURE},
424 {"texture0", TexEnvCombine::TEXTURE0},
425 {"texture1", TexEnvCombine::TEXTURE1},
426 {"texture2", TexEnvCombine::TEXTURE2},
427 {"texture3", TexEnvCombine::TEXTURE3},
428 {"texture4", TexEnvCombine::TEXTURE4},
429 {"texture5", TexEnvCombine::TEXTURE5},
430 {"texture6", TexEnvCombine::TEXTURE6},
431 {"texture7", TexEnvCombine::TEXTURE7}
434 EffectPropertyMap<TexEnvCombine::SourceParam> sourceParams(sourceParamInit);
436 EffectNameValue<TexEnvCombine::OperandParam> opParamInit[] =
438 {"src-color", TexEnvCombine::SRC_COLOR},
439 {"one-minus-src-color", TexEnvCombine::ONE_MINUS_SRC_COLOR},
440 {"src-alpha", TexEnvCombine::SRC_ALPHA},
441 {"one-minus-src-alpha", TexEnvCombine::ONE_MINUS_SRC_ALPHA}
444 EffectPropertyMap<TexEnvCombine::OperandParam> operandParams(opParamInit);
446 TexEnvCombine* buildTexEnvCombine(Effect* effect, const SGPropertyNode* envProp)
448 if (!isAttributeActive(effect, envProp))
450 TexEnvCombine* result = new TexEnvCombine;
451 const SGPropertyNode* p = 0;
452 if ((p = getEffectPropertyChild(effect, envProp, "combine-rgb"))) {
453 TexEnvCombine::CombineParam crgb = TexEnvCombine::MODULATE;
454 findAttr(combineParams, p, crgb);
455 result->setCombine_RGB(crgb);
457 if ((p = getEffectPropertyChild(effect, envProp, "combine-alpha"))) {
458 TexEnvCombine::CombineParam calpha = TexEnvCombine::MODULATE;
459 findAttr(combineParams, p, calpha);
460 result->setCombine_Alpha(calpha);
462 if ((p = getEffectPropertyChild(effect, envProp, "source0-rgb"))) {
463 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
464 findAttr(sourceParams, p, source);
465 result->setSource0_RGB(source);
467 if ((p = getEffectPropertyChild(effect, envProp, "source1-rgb"))) {
468 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
469 findAttr(sourceParams, p, source);
470 result->setSource1_RGB(source);
472 if ((p = getEffectPropertyChild(effect, envProp, "source2-rgb"))) {
473 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
474 findAttr(sourceParams, p, source);
475 result->setSource2_RGB(source);
477 if ((p = getEffectPropertyChild(effect, envProp, "source0-alpha"))) {
478 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
479 findAttr(sourceParams, p, source);
480 result->setSource0_Alpha(source);
482 if ((p = getEffectPropertyChild(effect, envProp, "source1-alpha"))) {
483 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
484 findAttr(sourceParams, p, source);
485 result->setSource1_Alpha(source);
487 if ((p = getEffectPropertyChild(effect, envProp, "source2-alpha"))) {
488 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
489 findAttr(sourceParams, p, source);
490 result->setSource2_Alpha(source);
492 if ((p = getEffectPropertyChild(effect, envProp, "operand0-rgb"))) {
493 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
494 findAttr(operandParams, p, op);
495 result->setOperand0_RGB(op);
497 if ((p = getEffectPropertyChild(effect, envProp, "operand1-rgb"))) {
498 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
499 findAttr(operandParams, p, op);
500 result->setOperand1_RGB(op);
502 if ((p = getEffectPropertyChild(effect, envProp, "operand2-rgb"))) {
503 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
504 findAttr(operandParams, p, op);
505 result->setOperand2_RGB(op);
507 if ((p = getEffectPropertyChild(effect, envProp, "operand0-alpha"))) {
508 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
509 findAttr(operandParams, p, op);
510 result->setOperand0_Alpha(op);
512 if ((p = getEffectPropertyChild(effect, envProp, "operand1-alpha"))) {
513 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
514 findAttr(operandParams, p, op);
515 result->setOperand1_Alpha(op);
517 if ((p = getEffectPropertyChild(effect, envProp, "operand2-alpha"))) {
518 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
519 findAttr(operandParams, p, op);
520 result->setOperand2_Alpha(op);
522 if ((p = getEffectPropertyChild(effect, envProp, "scale-rgb"))) {
523 result->setScale_RGB(p->getValue<float>());
525 if ((p = getEffectPropertyChild(effect, envProp, "scale-alpha"))) {
526 result->setScale_Alpha(p->getValue<float>());
529 if ((p = getEffectPropertyChild(effect, envProp, "constant-color"))) {
530 SGVec4d color = p->getValue<SGVec4d>();
531 result->setConstantColor(toOsg(color));
532 } else if ((p = getEffectPropertyChild(effect, envProp,
533 "light-direction"))) {
534 SGVec3d direction = p->getValue<SGVec3d>();
535 result->setConstantColorAsLightDirection(toOsg(direction));
538 const SGPropertyNode* colorNode = envProp->getChild("constant-color");
540 initFromParameters(effect, colorNode, result,
541 &TexEnvCombine::setConstantColor, colorFields);
545 EffectNameValue<TexGen::Mode> tgenModeInit[] =
547 { "object-linear", TexGen::OBJECT_LINEAR},
548 { "eye-linear", TexGen::EYE_LINEAR},
549 { "sphere-map", TexGen::SPHERE_MAP},
550 { "normal-map", TexGen::NORMAL_MAP},
551 { "reflection-map", TexGen::REFLECTION_MAP}
554 EffectPropertyMap<TexGen::Mode> tgenModes(tgenModeInit);
556 EffectNameValue<TexGen::Coord> tgenCoordInit[] =
564 EffectPropertyMap<TexGen::Coord> tgenCoords(tgenCoordInit);
566 TexGen* buildTexGen(Effect* effect, const SGPropertyNode* tgenProp)
568 if (!isAttributeActive(effect, tgenProp))
570 TexGen* result = new TexGen;
571 const SGPropertyNode* p = 0;
572 TexGen::Mode mode = TexGen::OBJECT_LINEAR;
573 findAttr(tgenModes, getEffectPropertyChild(effect, tgenProp, "mode"), mode);
574 result->setMode(mode);
575 const SGPropertyNode* planesNode = tgenProp->getChild("planes");
577 for (int i = 0; i < planesNode->nChildren(); ++i) {
578 const SGPropertyNode* planeNode = planesNode->getChild(i);
580 findAttr(tgenCoords, planeNode->getName(), coord);
581 const SGPropertyNode* realNode
582 = getEffectPropertyNode(effect, planeNode);
583 SGVec4d plane = realNode->getValue<SGVec4d>();
584 result->setPlane(coord, toOsg(plane));
590 bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
592 SGPropertyNode* texUnit = makeChild(paramRoot, "texture");
593 const Texture* tex = getStateAttribute<Texture>(0, ss);
594 const Texture2D* texture = dynamic_cast<const Texture2D*>(tex);
595 makeChild(texUnit, "unit")->setValue(0);
597 // The default shader-based technique ignores active
598 makeChild(texUnit, "active")->setValue(false);
601 const Image* image = texture->getImage();
604 imageName = image->getFileName();
606 makeChild(texUnit, "active")->setValue(false);
607 makeChild(texUnit, "type")->setValue("white");
610 makeChild(texUnit, "active")->setValue(true);
611 makeChild(texUnit, "type")->setValue("2d");
612 string filter = findName(filterModes,
613 texture->getFilter(Texture::MIN_FILTER));
614 string magFilter = findName(filterModes,
615 texture->getFilter(Texture::MAG_FILTER));
616 string wrapS = findName(wrapModes, texture->getWrap(Texture::WRAP_S));
617 string wrapT = findName(wrapModes, texture->getWrap(Texture::WRAP_T));
618 string wrapR = findName(wrapModes, texture->getWrap(Texture::WRAP_R));
619 makeChild(texUnit, "image")->setStringValue(imageName);
620 makeChild(texUnit, "filter")->setStringValue(filter);
621 makeChild(texUnit, "mag-filter")->setStringValue(magFilter);
622 makeChild(texUnit, "wrap-s")->setStringValue(wrapS);
623 makeChild(texUnit, "wrap-t")->setStringValue(wrapT);
624 makeChild(texUnit, "wrap-r")->setStringValue(wrapR);