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/model/SGReaderWriterXMLOptions.hxx>
39 #include <simgear/scene/util/SGSceneFeatures.hxx>
40 #include <simgear/scene/util/StateAttributeFactory.hxx>
41 #include <simgear/math/SGMath.hxx>
42 #include <simgear/structure/OSGUtils.hxx>
51 using namespace effect;
53 TexEnvCombine* buildTexEnvCombine(Effect* effect,
54 const SGPropertyNode* envProp,
55 const SGReaderWriterXMLOptions* options);
56 TexGen* buildTexGen(Effect* Effect, const SGPropertyNode* tgenProp);
58 // Hack to force inclusion of TextureBuilder.cxx in library
59 osg::Texture* TextureBuilder::buildFromType(Effect* effect, const string& type,
60 const SGPropertyNode*props,
61 const SGReaderWriterXMLOptions*
64 return EffectBuilder<Texture>::buildFromType(effect, type, props, options);
67 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
68 Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
71 EffectNameValue<TexEnv::Mode> texEnvModesInit[] =
74 {"blend", TexEnv::BLEND},
75 {"decal", TexEnv::DECAL},
76 {"modulate", TexEnv::MODULATE},
77 {"replace", TexEnv::REPLACE}
79 EffectPropertyMap<TexEnv::Mode> texEnvModes(texEnvModesInit);
81 TexEnv* buildTexEnv(Effect* effect, const SGPropertyNode* prop)
83 const SGPropertyNode* modeProp = getEffectPropertyChild(effect, prop,
85 const SGPropertyNode* colorProp = getEffectPropertyChild(effect, prop,
89 TexEnv::Mode mode = TexEnv::MODULATE;
90 findAttr(texEnvModes, modeProp, mode);
91 if (mode == TexEnv::MODULATE) {
92 return StateAttributeFactory::instance()->getStandardTexEnv();
94 TexEnv* env = new TexEnv(mode);
96 env->setColor(toOsg(colorProp->getValue<SGVec4d>()));
101 void TextureUnitBuilder::buildAttribute(Effect* effect, Pass* pass,
102 const SGPropertyNode* prop,
103 const SGReaderWriterXMLOptions* options)
105 if (!isAttributeActive(effect, prop))
107 // Decode the texture unit
109 const SGPropertyNode* pUnit = prop->getChild("unit");
111 unit = pUnit->getValue<int>();
113 const SGPropertyNode* pName = prop->getChild("name");
116 unit = boost::lexical_cast<int>(pName->getStringValue());
117 } catch (boost::bad_lexical_cast& lex) {
118 SG_LOG(SG_INPUT, SG_ALERT, "can't decode name as texture unit "
122 const SGPropertyNode* pType = getEffectPropertyChild(effect, prop, "type");
127 type = pType->getStringValue();
128 Texture* texture = 0;
130 texture = TextureBuilder::buildFromType(effect, type, prop,
133 catch (BuilderException& e) {
134 SG_LOG(SG_INPUT, SG_ALERT, "No image file for texture, using white ");
135 texture = StateAttributeFactory::instance()->getWhiteTexture();
137 pass->setTextureAttributeAndModes(unit, texture);
138 const SGPropertyNode* envProp = prop->getChild("environment");
140 TexEnv* env = buildTexEnv(effect, envProp);
142 pass->setTextureAttributeAndModes(unit, env);
144 const SGPropertyNode* combineProp = prop->getChild("texenv-combine");
145 TexEnvCombine* combiner = 0;
146 if (combineProp && ((combiner = buildTexEnvCombine(effect, combineProp,
148 pass->setTextureAttributeAndModes(unit, combiner);
149 const SGPropertyNode* tgenProp = prop->getChild("texgen");
151 if (tgenProp && (tgen = buildTexGen(effect, tgenProp)))
152 pass->setTextureAttributeAndModes(unit, tgen);
155 // InstallAttributeBuilder call is in Effect.cxx to force this file to
160 EffectNameValue<Texture::FilterMode> filterModesInit[] =
162 { "linear", Texture::LINEAR },
163 { "linear-mipmap-linear", Texture::LINEAR_MIPMAP_LINEAR},
164 { "linear-mipmap-nearest", Texture::LINEAR_MIPMAP_NEAREST},
165 { "nearest", Texture::NEAREST},
166 { "nearest-mipmap-linear", Texture::NEAREST_MIPMAP_LINEAR},
167 { "nearest-mipmap-nearest", Texture::NEAREST_MIPMAP_NEAREST}
169 EffectPropertyMap<Texture::FilterMode> filterModes(filterModesInit);
171 EffectNameValue<Texture::WrapMode> wrapModesInit[] =
173 {"clamp", Texture::CLAMP},
174 {"clamp-to-border", Texture::CLAMP_TO_BORDER},
175 {"clamp-to-edge", Texture::CLAMP_TO_EDGE},
176 {"mirror", Texture::MIRROR},
177 {"repeat", Texture::REPEAT}
179 EffectPropertyMap<Texture::WrapMode> wrapModes(wrapModesInit);
182 TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
183 const SGReaderWriterXMLOptions* options,
184 const string& texType)
186 Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR;
187 const SGPropertyNode* ep = 0;
188 if ((ep = getEffectPropertyChild(effect, props, "filter")))
189 findAttr(filterModes, ep, minFilter);
190 Texture::FilterMode magFilter = Texture::LINEAR;
191 if ((ep = getEffectPropertyChild(effect, props, "mag-filter")))
192 findAttr(filterModes, ep, magFilter);
193 const SGPropertyNode* pWrapS
194 = getEffectPropertyChild(effect, props, "wrap-s");
195 Texture::WrapMode sWrap = Texture::CLAMP;
197 findAttr(wrapModes, pWrapS, sWrap);
198 const SGPropertyNode* pWrapT
199 = getEffectPropertyChild(effect, props, "wrap-t");
200 Texture::WrapMode tWrap = Texture::CLAMP;
202 findAttr(wrapModes, pWrapT, tWrap);
203 const SGPropertyNode* pWrapR
204 = getEffectPropertyChild(effect, props, "wrap-r");
205 Texture::WrapMode rWrap = Texture::CLAMP;
207 findAttr(wrapModes, pWrapR, rWrap);
208 const SGPropertyNode* pImage
209 = getEffectPropertyChild(effect, props, "image");
212 imageName = pImage->getStringValue();
213 string absFileName = osgDB::findDataFile(imageName, options);
214 return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
218 void setAttrs(const TexTuple& attrs, Texture* tex,
219 const SGReaderWriterXMLOptions* options)
221 const string& imageName = attrs.get<0>();
222 if (imageName.empty()) {
223 throw BuilderException("no image file");
225 osgDB::ReaderWriter::ReadResult result
226 = osgDB::Registry::instance()->readImage(imageName, options);
227 if (result.success()) {
228 osg::Image* image = result.getImage();
229 tex->setImage(GL_FRONT_AND_BACK, image);
232 if (s <= t && 32 <= s) {
233 SGSceneFeatures::instance()->setTextureCompression(tex);
234 } else if (t < s && 32 <= t) {
235 SGSceneFeatures::instance()->setTextureCompression(tex);
237 tex->setMaxAnisotropy(SGSceneFeatures::instance()
238 ->getTextureFilter());
240 SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
244 // texture->setDataVariance(osg::Object::STATIC);
245 tex->setFilter(Texture::MIN_FILTER, attrs.get<1>());
246 tex->setFilter(Texture::MAG_FILTER, attrs.get<2>());
247 tex->setWrap(Texture::WRAP_S, attrs.get<3>());
248 tex->setWrap(Texture::WRAP_T, attrs.get<4>());
249 tex->setWrap(Texture::WRAP_R, attrs.get<5>());
254 class TexBuilder : public TextureBuilder
257 TexBuilder(const string& texType) : _type(texType) {}
258 Texture* build(Effect* effect, const SGPropertyNode*,
259 const SGReaderWriterXMLOptions* options);
261 typedef map<TexTuple, ref_ptr<T> > TexMap;
267 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
268 const SGReaderWriterXMLOptions* options)
270 TexTuple attrs = makeTexTuple(effect, props, options, _type);
271 typename TexMap::iterator itr = texMap.find(attrs);
272 if (itr != texMap.end())
273 return itr->second.get();
275 setAttrs(attrs, tex, options);
276 texMap.insert(make_pair(attrs, tex));
283 TextureBuilder::Registrar install1D("1d", new TexBuilder<Texture1D>("1d"));
284 TextureBuilder::Registrar install2D("2d", new TexBuilder<Texture2D>("2d"));
285 TextureBuilder::Registrar install3D("3d", new TexBuilder<Texture3D>("3d"));
288 class WhiteTextureBuilder : public TextureBuilder
291 Texture* build(Effect* effect, const SGPropertyNode*,
292 const SGReaderWriterXMLOptions* options);
295 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
296 const SGReaderWriterXMLOptions* options)
298 return StateAttributeFactory::instance()->getWhiteTexture();
303 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
306 class TransparentTextureBuilder : public TextureBuilder
309 Texture* build(Effect* effect, const SGPropertyNode*,
310 const SGReaderWriterXMLOptions* options);
313 Texture* TransparentTextureBuilder::build(Effect* effect, const SGPropertyNode*,
314 const SGReaderWriterXMLOptions* options)
316 return StateAttributeFactory::instance()->getTransparentTexture();
321 TextureBuilder::Registrar installTransparent("transparent",
322 new TransparentTextureBuilder);
325 osg::Image* make3DNoiseImage(int texSize)
327 osg::Image* image = new osg::Image;
328 image->setImage(texSize, texSize, texSize,
329 4, GL_RGBA, GL_UNSIGNED_BYTE,
330 new unsigned char[4 * texSize * texSize * texSize],
331 osg::Image::USE_NEW_DELETE);
333 const int startFrequency = 4;
334 const int numOctaves = 4;
338 double inci, incj, inck;
339 int frequency = startFrequency;
343 osg::notify(osg::WARN) << "creating 3D noise texture... ";
345 for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
347 SetNoiseFrequency(frequency);
349 ni[0] = ni[1] = ni[2] = 0;
351 inci = 1.0 / (texSize / frequency);
352 for (i = 0; i < texSize; ++i, ni[0] += inci)
354 incj = 1.0 / (texSize / frequency);
355 for (j = 0; j < texSize; ++j, ni[1] += incj)
357 inck = 1.0 / (texSize / frequency);
358 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
360 *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
366 osg::notify(osg::WARN) << "DONE" << std::endl;
370 class NoiseBuilder : public TextureBuilder
373 Texture* build(Effect* effect, const SGPropertyNode*,
374 const SGReaderWriterXMLOptions* options);
376 typedef map<int, ref_ptr<Texture3D> > NoiseMap;
380 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
381 const SGReaderWriterXMLOptions* options)
384 const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
387 texSize = sizeProp->getValue<int>();
388 NoiseMap::iterator itr = _noises.find(texSize);
389 if (itr != _noises.end())
390 return itr->second.get();
391 Texture3D* noiseTexture = new osg::Texture3D;
392 noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
393 noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
394 noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
395 noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
396 noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
397 noiseTexture->setImage( make3DNoiseImage(texSize) );
398 _noises.insert(make_pair(texSize, noiseTexture));
404 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
407 EffectNameValue<TexEnvCombine::CombineParam> combineParamInit[] =
409 {"replace", TexEnvCombine::REPLACE},
410 {"modulate", TexEnvCombine::MODULATE},
411 {"add", TexEnvCombine::ADD},
412 {"add-signed", TexEnvCombine::ADD_SIGNED},
413 {"interpolate", TexEnvCombine::INTERPOLATE},
414 {"subtract", TexEnvCombine::SUBTRACT},
415 {"dot3-rgb", TexEnvCombine::DOT3_RGB},
416 {"dot3-rgba", TexEnvCombine::DOT3_RGBA}
419 EffectPropertyMap<TexEnvCombine::CombineParam> combineParams(combineParamInit);
421 EffectNameValue<TexEnvCombine::SourceParam> sourceParamInit[] =
423 {"constant", TexEnvCombine::CONSTANT},
424 {"primary_color", TexEnvCombine::PRIMARY_COLOR},
425 {"previous", TexEnvCombine::PREVIOUS},
426 {"texture", TexEnvCombine::TEXTURE},
427 {"texture0", TexEnvCombine::TEXTURE0},
428 {"texture1", TexEnvCombine::TEXTURE1},
429 {"texture2", TexEnvCombine::TEXTURE2},
430 {"texture3", TexEnvCombine::TEXTURE3},
431 {"texture4", TexEnvCombine::TEXTURE4},
432 {"texture5", TexEnvCombine::TEXTURE5},
433 {"texture6", TexEnvCombine::TEXTURE6},
434 {"texture7", TexEnvCombine::TEXTURE7}
437 EffectPropertyMap<TexEnvCombine::SourceParam> sourceParams(sourceParamInit);
439 EffectNameValue<TexEnvCombine::OperandParam> opParamInit[] =
441 {"src-color", TexEnvCombine::SRC_COLOR},
442 {"one-minus-src-color", TexEnvCombine::ONE_MINUS_SRC_COLOR},
443 {"src-alpha", TexEnvCombine::SRC_ALPHA},
444 {"one-minus-src-alpha", TexEnvCombine::ONE_MINUS_SRC_ALPHA}
447 EffectPropertyMap<TexEnvCombine::OperandParam> operandParams(opParamInit);
449 TexEnvCombine* buildTexEnvCombine(Effect* effect, const SGPropertyNode* envProp,
450 const SGReaderWriterXMLOptions* options)
452 if (!isAttributeActive(effect, envProp))
454 TexEnvCombine* result = new TexEnvCombine;
455 const SGPropertyNode* p = 0;
456 if ((p = getEffectPropertyChild(effect, envProp, "combine-rgb"))) {
457 TexEnvCombine::CombineParam crgb = TexEnvCombine::MODULATE;
458 findAttr(combineParams, p, crgb);
459 result->setCombine_RGB(crgb);
461 if ((p = getEffectPropertyChild(effect, envProp, "combine-alpha"))) {
462 TexEnvCombine::CombineParam calpha = TexEnvCombine::MODULATE;
463 findAttr(combineParams, p, calpha);
464 result->setCombine_Alpha(calpha);
466 if ((p = getEffectPropertyChild(effect, envProp, "source0-rgb"))) {
467 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
468 findAttr(sourceParams, p, source);
469 result->setSource0_RGB(source);
471 if ((p = getEffectPropertyChild(effect, envProp, "source1-rgb"))) {
472 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
473 findAttr(sourceParams, p, source);
474 result->setSource1_RGB(source);
476 if ((p = getEffectPropertyChild(effect, envProp, "source2-rgb"))) {
477 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
478 findAttr(sourceParams, p, source);
479 result->setSource2_RGB(source);
481 if ((p = getEffectPropertyChild(effect, envProp, "source0-alpha"))) {
482 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
483 findAttr(sourceParams, p, source);
484 result->setSource0_Alpha(source);
486 if ((p = getEffectPropertyChild(effect, envProp, "source1-alpha"))) {
487 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
488 findAttr(sourceParams, p, source);
489 result->setSource1_Alpha(source);
491 if ((p = getEffectPropertyChild(effect, envProp, "source2-alpha"))) {
492 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
493 findAttr(sourceParams, p, source);
494 result->setSource2_Alpha(source);
496 if ((p = getEffectPropertyChild(effect, envProp, "operand0-rgb"))) {
497 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
498 findAttr(operandParams, p, op);
499 result->setOperand0_RGB(op);
501 if ((p = getEffectPropertyChild(effect, envProp, "operand1-rgb"))) {
502 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
503 findAttr(operandParams, p, op);
504 result->setOperand1_RGB(op);
506 if ((p = getEffectPropertyChild(effect, envProp, "operand2-rgb"))) {
507 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
508 findAttr(operandParams, p, op);
509 result->setOperand2_RGB(op);
511 if ((p = getEffectPropertyChild(effect, envProp, "operand0-alpha"))) {
512 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
513 findAttr(operandParams, p, op);
514 result->setOperand0_Alpha(op);
516 if ((p = getEffectPropertyChild(effect, envProp, "operand1-alpha"))) {
517 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
518 findAttr(operandParams, p, op);
519 result->setOperand1_Alpha(op);
521 if ((p = getEffectPropertyChild(effect, envProp, "operand2-alpha"))) {
522 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
523 findAttr(operandParams, p, op);
524 result->setOperand2_Alpha(op);
526 if ((p = getEffectPropertyChild(effect, envProp, "scale-rgb"))) {
527 result->setScale_RGB(p->getValue<float>());
529 if ((p = getEffectPropertyChild(effect, envProp, "scale-alpha"))) {
530 result->setScale_Alpha(p->getValue<float>());
533 if ((p = getEffectPropertyChild(effect, envProp, "constant-color"))) {
534 SGVec4d color = p->getValue<SGVec4d>();
535 result->setConstantColor(toOsg(color));
536 } else if ((p = getEffectPropertyChild(effect, envProp,
537 "light-direction"))) {
538 SGVec3d direction = p->getValue<SGVec3d>();
539 result->setConstantColorAsLightDirection(toOsg(direction));
542 const SGPropertyNode* colorNode = envProp->getChild("constant-color");
544 initFromParameters(effect, colorNode, result,
545 &TexEnvCombine::setConstantColor, colorFields,
550 EffectNameValue<TexGen::Mode> tgenModeInit[] =
552 { "object-linear", TexGen::OBJECT_LINEAR},
553 { "eye-linear", TexGen::EYE_LINEAR},
554 { "sphere-map", TexGen::SPHERE_MAP},
555 { "normal-map", TexGen::NORMAL_MAP},
556 { "reflection-map", TexGen::REFLECTION_MAP}
559 EffectPropertyMap<TexGen::Mode> tgenModes(tgenModeInit);
561 EffectNameValue<TexGen::Coord> tgenCoordInit[] =
569 EffectPropertyMap<TexGen::Coord> tgenCoords(tgenCoordInit);
571 TexGen* buildTexGen(Effect* effect, const SGPropertyNode* tgenProp)
573 if (!isAttributeActive(effect, tgenProp))
575 TexGen* result = new TexGen;
576 const SGPropertyNode* p = 0;
577 TexGen::Mode mode = TexGen::OBJECT_LINEAR;
578 findAttr(tgenModes, getEffectPropertyChild(effect, tgenProp, "mode"), mode);
579 result->setMode(mode);
580 const SGPropertyNode* planesNode = tgenProp->getChild("planes");
582 for (int i = 0; i < planesNode->nChildren(); ++i) {
583 const SGPropertyNode* planeNode = planesNode->getChild(i);
585 findAttr(tgenCoords, planeNode->getName(), coord);
586 const SGPropertyNode* realNode
587 = getEffectPropertyNode(effect, planeNode);
588 SGVec4d plane = realNode->getValue<SGVec4d>();
589 result->setPlane(coord, toOsg(plane));
595 bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
597 SGPropertyNode* texUnit = makeChild(paramRoot, "texture");
598 const Texture* tex = getStateAttribute<Texture>(0, ss);
599 const Texture2D* texture = dynamic_cast<const Texture2D*>(tex);
600 makeChild(texUnit, "unit")->setValue(0);
602 // The default shader-based technique ignores active
603 makeChild(texUnit, "active")->setValue(false);
606 const Image* image = texture->getImage();
609 imageName = image->getFileName();
611 makeChild(texUnit, "active")->setValue(false);
612 makeChild(texUnit, "type")->setValue("white");
615 makeChild(texUnit, "active")->setValue(true);
616 makeChild(texUnit, "type")->setValue("2d");
617 string filter = findName(filterModes,
618 texture->getFilter(Texture::MIN_FILTER));
619 string magFilter = findName(filterModes,
620 texture->getFilter(Texture::MAG_FILTER));
621 string wrapS = findName(wrapModes, texture->getWrap(Texture::WRAP_S));
622 string wrapT = findName(wrapModes, texture->getWrap(Texture::WRAP_T));
623 string wrapR = findName(wrapModes, texture->getWrap(Texture::WRAP_R));
624 makeChild(texUnit, "image")->setStringValue(imageName);
625 makeChild(texUnit, "filter")->setStringValue(filter);
626 makeChild(texUnit, "mag-filter")->setStringValue(magFilter);
627 makeChild(texUnit, "wrap-s")->setStringValue(wrapS);
628 makeChild(texUnit, "wrap-t")->setStringValue(wrapT);
629 makeChild(texUnit, "wrap-r")->setStringValue(wrapR);