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 TexGen* buildTexGen(Effect* Effect, const SGPropertyNode* tgenProp);
57 // Hack to force inclusion of TextureBuilder.cxx in library
58 osg::Texture* TextureBuilder::buildFromType(Effect* effect, const string& type,
59 const SGPropertyNode*props,
60 const SGReaderWriterXMLOptions*
63 return EffectBuilder<Texture>::buildFromType(effect, type, props, options);
66 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
67 Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
70 EffectNameValue<TexEnv::Mode> texEnvModesInit[] =
73 {"blend", TexEnv::BLEND},
74 {"decal", TexEnv::DECAL},
75 {"modulate", TexEnv::MODULATE},
76 {"replace", TexEnv::REPLACE}
78 EffectPropertyMap<TexEnv::Mode> texEnvModes(texEnvModesInit);
80 TexEnv* buildTexEnv(Effect* effect, const SGPropertyNode* prop)
82 const SGPropertyNode* modeProp = getEffectPropertyChild(effect, prop,
84 const SGPropertyNode* colorProp = getEffectPropertyChild(effect, prop,
88 TexEnv::Mode mode = TexEnv::MODULATE;
89 findAttr(texEnvModes, modeProp, mode);
90 if (mode == TexEnv::MODULATE) {
91 return StateAttributeFactory::instance()->getStandardTexEnv();
93 TexEnv* env = new TexEnv(mode);
95 env->setColor(toOsg(colorProp->getValue<SGVec4d>()));
100 void TextureUnitBuilder::buildAttribute(Effect* effect, Pass* pass,
101 const SGPropertyNode* prop,
102 const SGReaderWriterXMLOptions* options)
104 if (!isAttributeActive(effect, prop))
106 // Decode the texture unit
108 const SGPropertyNode* pUnit = prop->getChild("unit");
110 unit = pUnit->getValue<int>();
112 const SGPropertyNode* pName = prop->getChild("name");
115 unit = boost::lexical_cast<int>(pName->getStringValue());
116 } catch (boost::bad_lexical_cast& lex) {
117 SG_LOG(SG_INPUT, SG_ALERT, "can't decode name as texture unit "
121 const SGPropertyNode* pType = getEffectPropertyChild(effect, prop, "type");
126 type = pType->getStringValue();
127 Texture* texture = 0;
129 texture = TextureBuilder::buildFromType(effect, type, prop,
132 catch (BuilderException& e) {
133 SG_LOG(SG_INPUT, SG_ALERT, "No image file for texture, using white ");
134 texture = StateAttributeFactory::instance()->getWhiteTexture();
136 pass->setTextureAttributeAndModes(unit, texture);
137 const SGPropertyNode* envProp = prop->getChild("environment");
139 TexEnv* env = buildTexEnv(effect, envProp);
141 pass->setTextureAttributeAndModes(unit, env);
143 const SGPropertyNode* combineProp = prop->getChild("texenv-combine");
144 TexEnvCombine* combiner = 0;
145 if (combineProp && ((combiner = buildTexEnvCombine(effect, combineProp))))
146 pass->setTextureAttributeAndModes(unit, combiner);
147 const SGPropertyNode* tgenProp = prop->getChild("texgen");
149 if (tgenProp && (tgen = buildTexGen(effect, tgenProp)))
150 pass->setTextureAttributeAndModes(unit, tgen);
153 // InstallAttributeBuilder call is in Effect.cxx to force this file to
158 EffectNameValue<Texture::FilterMode> filterModesInit[] =
160 { "linear", Texture::LINEAR },
161 { "linear-mipmap-linear", Texture::LINEAR_MIPMAP_LINEAR},
162 { "linear-mipmap-nearest", Texture::LINEAR_MIPMAP_NEAREST},
163 { "nearest", Texture::NEAREST},
164 { "nearest-mipmap-linear", Texture::NEAREST_MIPMAP_LINEAR},
165 { "nearest-mipmap-nearest", Texture::NEAREST_MIPMAP_NEAREST}
167 EffectPropertyMap<Texture::FilterMode> filterModes(filterModesInit);
169 EffectNameValue<Texture::WrapMode> wrapModesInit[] =
171 {"clamp", Texture::CLAMP},
172 {"clamp-to-border", Texture::CLAMP_TO_BORDER},
173 {"clamp-to-edge", Texture::CLAMP_TO_EDGE},
174 {"mirror", Texture::MIRROR},
175 {"repeat", Texture::REPEAT}
177 EffectPropertyMap<Texture::WrapMode> wrapModes(wrapModesInit);
180 TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
181 const SGReaderWriterXMLOptions* options,
182 const string& texType)
184 Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR;
185 const SGPropertyNode* ep = 0;
186 if ((ep = getEffectPropertyChild(effect, props, "filter")))
187 findAttr(filterModes, ep, minFilter);
188 Texture::FilterMode magFilter = Texture::LINEAR;
189 if ((ep = getEffectPropertyChild(effect, props, "mag-filter")))
190 findAttr(filterModes, ep, magFilter);
191 const SGPropertyNode* pWrapS
192 = getEffectPropertyChild(effect, props, "wrap-s");
193 Texture::WrapMode sWrap = Texture::CLAMP;
195 findAttr(wrapModes, pWrapS, sWrap);
196 const SGPropertyNode* pWrapT
197 = getEffectPropertyChild(effect, props, "wrap-t");
198 Texture::WrapMode tWrap = Texture::CLAMP;
200 findAttr(wrapModes, pWrapT, tWrap);
201 const SGPropertyNode* pWrapR
202 = getEffectPropertyChild(effect, props, "wrap-r");
203 Texture::WrapMode rWrap = Texture::CLAMP;
205 findAttr(wrapModes, pWrapR, rWrap);
206 const SGPropertyNode* pImage
207 = getEffectPropertyChild(effect, props, "image");
210 imageName = pImage->getStringValue();
211 string absFileName = osgDB::findDataFile(imageName, options);
212 return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
216 void setAttrs(const TexTuple& attrs, Texture* tex,
217 const SGReaderWriterXMLOptions* options)
219 const string& imageName = attrs.get<0>();
220 if (imageName.empty()) {
221 throw BuilderException("no image file");
223 osgDB::ReaderWriter::ReadResult result
224 = osgDB::Registry::instance()->readImage(imageName, options);
225 if (result.success()) {
226 osg::Image* image = result.getImage();
227 tex->setImage(GL_FRONT_AND_BACK, image);
230 if (s <= t && 32 <= s) {
231 SGSceneFeatures::instance()->setTextureCompression(tex);
232 } else if (t < s && 32 <= t) {
233 SGSceneFeatures::instance()->setTextureCompression(tex);
235 tex->setMaxAnisotropy(SGSceneFeatures::instance()
236 ->getTextureFilter());
238 SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
242 // texture->setDataVariance(osg::Object::STATIC);
243 tex->setFilter(Texture::MIN_FILTER, attrs.get<1>());
244 tex->setFilter(Texture::MAG_FILTER, attrs.get<2>());
245 tex->setWrap(Texture::WRAP_S, attrs.get<3>());
246 tex->setWrap(Texture::WRAP_T, attrs.get<4>());
247 tex->setWrap(Texture::WRAP_R, attrs.get<5>());
252 class TexBuilder : public TextureBuilder
255 TexBuilder(const string& texType) : _type(texType) {}
256 Texture* build(Effect* effect, const SGPropertyNode*,
257 const SGReaderWriterXMLOptions* options);
259 typedef map<TexTuple, ref_ptr<T> > TexMap;
265 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
266 const SGReaderWriterXMLOptions* options)
268 TexTuple attrs = makeTexTuple(effect, props, options, _type);
269 typename TexMap::iterator itr = texMap.find(attrs);
270 if (itr != texMap.end())
271 return itr->second.get();
273 setAttrs(attrs, tex, options);
274 texMap.insert(make_pair(attrs, tex));
281 TextureBuilder::Registrar install1D("1d", new TexBuilder<Texture1D>("1d"));
282 TextureBuilder::Registrar install2D("2d", new TexBuilder<Texture2D>("2d"));
283 TextureBuilder::Registrar install3D("3d", new TexBuilder<Texture3D>("3d"));
286 class WhiteTextureBuilder : public TextureBuilder
289 Texture* build(Effect* effect, const SGPropertyNode*,
290 const SGReaderWriterXMLOptions* options);
293 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
294 const SGReaderWriterXMLOptions* options)
296 return StateAttributeFactory::instance()->getWhiteTexture();
301 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
304 class TransparentTextureBuilder : public TextureBuilder
307 Texture* build(Effect* effect, const SGPropertyNode*,
308 const SGReaderWriterXMLOptions* options);
311 Texture* TransparentTextureBuilder::build(Effect* effect, const SGPropertyNode*,
312 const SGReaderWriterXMLOptions* options)
314 return StateAttributeFactory::instance()->getTransparentTexture();
319 TextureBuilder::Registrar installTransparent("transparent",
320 new TransparentTextureBuilder);
323 osg::Image* make3DNoiseImage(int texSize)
325 osg::Image* image = new osg::Image;
326 image->setImage(texSize, texSize, texSize,
327 4, GL_RGBA, GL_UNSIGNED_BYTE,
328 new unsigned char[4 * texSize * texSize * texSize],
329 osg::Image::USE_NEW_DELETE);
331 const int startFrequency = 4;
332 const int numOctaves = 4;
336 double inci, incj, inck;
337 int frequency = startFrequency;
341 osg::notify(osg::WARN) << "creating 3D noise texture... ";
343 for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
345 SetNoiseFrequency(frequency);
347 ni[0] = ni[1] = ni[2] = 0;
349 inci = 1.0 / (texSize / frequency);
350 for (i = 0; i < texSize; ++i, ni[0] += inci)
352 incj = 1.0 / (texSize / frequency);
353 for (j = 0; j < texSize; ++j, ni[1] += incj)
355 inck = 1.0 / (texSize / frequency);
356 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
358 *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
364 osg::notify(osg::WARN) << "DONE" << std::endl;
368 class NoiseBuilder : public TextureBuilder
371 Texture* build(Effect* effect, const SGPropertyNode*,
372 const SGReaderWriterXMLOptions* options);
374 typedef map<int, ref_ptr<Texture3D> > NoiseMap;
378 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
379 const SGReaderWriterXMLOptions* options)
382 const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
385 texSize = sizeProp->getValue<int>();
386 NoiseMap::iterator itr = _noises.find(texSize);
387 if (itr != _noises.end())
388 return itr->second.get();
389 Texture3D* noiseTexture = new osg::Texture3D;
390 noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
391 noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
392 noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
393 noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
394 noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
395 noiseTexture->setImage( make3DNoiseImage(texSize) );
396 _noises.insert(make_pair(texSize, noiseTexture));
402 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
405 EffectNameValue<TexEnvCombine::CombineParam> combineParamInit[] =
407 {"replace", TexEnvCombine::REPLACE},
408 {"modulate", TexEnvCombine::MODULATE},
409 {"add", TexEnvCombine::ADD},
410 {"add-signed", TexEnvCombine::ADD_SIGNED},
411 {"interpolate", TexEnvCombine::INTERPOLATE},
412 {"subtract", TexEnvCombine::SUBTRACT},
413 {"dot3-rgb", TexEnvCombine::DOT3_RGB},
414 {"dot3-rgba", TexEnvCombine::DOT3_RGBA}
417 EffectPropertyMap<TexEnvCombine::CombineParam> combineParams(combineParamInit);
419 EffectNameValue<TexEnvCombine::SourceParam> sourceParamInit[] =
421 {"constant", TexEnvCombine::CONSTANT},
422 {"primary_color", TexEnvCombine::PRIMARY_COLOR},
423 {"previous", TexEnvCombine::PREVIOUS},
424 {"texture", TexEnvCombine::TEXTURE},
425 {"texture0", TexEnvCombine::TEXTURE0},
426 {"texture1", TexEnvCombine::TEXTURE1},
427 {"texture2", TexEnvCombine::TEXTURE2},
428 {"texture3", TexEnvCombine::TEXTURE3},
429 {"texture4", TexEnvCombine::TEXTURE4},
430 {"texture5", TexEnvCombine::TEXTURE5},
431 {"texture6", TexEnvCombine::TEXTURE6},
432 {"texture7", TexEnvCombine::TEXTURE7}
435 EffectPropertyMap<TexEnvCombine::SourceParam> sourceParams(sourceParamInit);
437 EffectNameValue<TexEnvCombine::OperandParam> opParamInit[] =
439 {"src-color", TexEnvCombine::SRC_COLOR},
440 {"one-minus-src-color", TexEnvCombine::ONE_MINUS_SRC_COLOR},
441 {"src-alpha", TexEnvCombine::SRC_ALPHA},
442 {"one-minus-src-alpha", TexEnvCombine::ONE_MINUS_SRC_ALPHA}
445 EffectPropertyMap<TexEnvCombine::OperandParam> operandParams(opParamInit);
447 TexEnvCombine* buildTexEnvCombine(Effect* effect, const SGPropertyNode* envProp)
449 if (!isAttributeActive(effect, envProp))
451 TexEnvCombine* result = new TexEnvCombine;
452 const SGPropertyNode* p = 0;
453 if ((p = getEffectPropertyChild(effect, envProp, "combine-rgb"))) {
454 TexEnvCombine::CombineParam crgb = TexEnvCombine::MODULATE;
455 findAttr(combineParams, p, crgb);
456 result->setCombine_RGB(crgb);
458 if ((p = getEffectPropertyChild(effect, envProp, "combine-alpha"))) {
459 TexEnvCombine::CombineParam calpha = TexEnvCombine::MODULATE;
460 findAttr(combineParams, p, calpha);
461 result->setCombine_Alpha(calpha);
463 if ((p = getEffectPropertyChild(effect, envProp, "source0-rgb"))) {
464 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
465 findAttr(sourceParams, p, source);
466 result->setSource0_RGB(source);
468 if ((p = getEffectPropertyChild(effect, envProp, "source1-rgb"))) {
469 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
470 findAttr(sourceParams, p, source);
471 result->setSource1_RGB(source);
473 if ((p = getEffectPropertyChild(effect, envProp, "source2-rgb"))) {
474 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
475 findAttr(sourceParams, p, source);
476 result->setSource2_RGB(source);
478 if ((p = getEffectPropertyChild(effect, envProp, "source0-alpha"))) {
479 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
480 findAttr(sourceParams, p, source);
481 result->setSource0_Alpha(source);
483 if ((p = getEffectPropertyChild(effect, envProp, "source1-alpha"))) {
484 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
485 findAttr(sourceParams, p, source);
486 result->setSource1_Alpha(source);
488 if ((p = getEffectPropertyChild(effect, envProp, "source2-alpha"))) {
489 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
490 findAttr(sourceParams, p, source);
491 result->setSource2_Alpha(source);
493 if ((p = getEffectPropertyChild(effect, envProp, "operand0-rgb"))) {
494 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
495 findAttr(operandParams, p, op);
496 result->setOperand0_RGB(op);
498 if ((p = getEffectPropertyChild(effect, envProp, "operand1-rgb"))) {
499 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
500 findAttr(operandParams, p, op);
501 result->setOperand1_RGB(op);
503 if ((p = getEffectPropertyChild(effect, envProp, "operand2-rgb"))) {
504 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
505 findAttr(operandParams, p, op);
506 result->setOperand2_RGB(op);
508 if ((p = getEffectPropertyChild(effect, envProp, "operand0-alpha"))) {
509 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
510 findAttr(operandParams, p, op);
511 result->setOperand0_Alpha(op);
513 if ((p = getEffectPropertyChild(effect, envProp, "operand1-alpha"))) {
514 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
515 findAttr(operandParams, p, op);
516 result->setOperand1_Alpha(op);
518 if ((p = getEffectPropertyChild(effect, envProp, "operand2-alpha"))) {
519 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
520 findAttr(operandParams, p, op);
521 result->setOperand2_Alpha(op);
523 if ((p = getEffectPropertyChild(effect, envProp, "scale-rgb"))) {
524 result->setScale_RGB(p->getValue<float>());
526 if ((p = getEffectPropertyChild(effect, envProp, "scale-alpha"))) {
527 result->setScale_Alpha(p->getValue<float>());
530 if ((p = getEffectPropertyChild(effect, envProp, "constant-color"))) {
531 SGVec4d color = p->getValue<SGVec4d>();
532 result->setConstantColor(toOsg(color));
533 } else if ((p = getEffectPropertyChild(effect, envProp,
534 "light-direction"))) {
535 SGVec3d direction = p->getValue<SGVec3d>();
536 result->setConstantColorAsLightDirection(toOsg(direction));
539 const SGPropertyNode* colorNode = envProp->getChild("constant-color");
541 initFromParameters(effect, colorNode, result,
542 &TexEnvCombine::setConstantColor, colorFields);
546 EffectNameValue<TexGen::Mode> tgenModeInit[] =
548 { "object-linear", TexGen::OBJECT_LINEAR},
549 { "eye-linear", TexGen::EYE_LINEAR},
550 { "sphere-map", TexGen::SPHERE_MAP},
551 { "normal-map", TexGen::NORMAL_MAP},
552 { "reflection-map", TexGen::REFLECTION_MAP}
555 EffectPropertyMap<TexGen::Mode> tgenModes(tgenModeInit);
557 EffectNameValue<TexGen::Coord> tgenCoordInit[] =
565 EffectPropertyMap<TexGen::Coord> tgenCoords(tgenCoordInit);
567 TexGen* buildTexGen(Effect* effect, const SGPropertyNode* tgenProp)
569 if (!isAttributeActive(effect, tgenProp))
571 TexGen* result = new TexGen;
572 const SGPropertyNode* p = 0;
573 TexGen::Mode mode = TexGen::OBJECT_LINEAR;
574 findAttr(tgenModes, getEffectPropertyChild(effect, tgenProp, "mode"), mode);
575 result->setMode(mode);
576 const SGPropertyNode* planesNode = tgenProp->getChild("planes");
578 for (int i = 0; i < planesNode->nChildren(); ++i) {
579 const SGPropertyNode* planeNode = planesNode->getChild(i);
581 findAttr(tgenCoords, planeNode->getName(), coord);
582 const SGPropertyNode* realNode
583 = getEffectPropertyNode(effect, planeNode);
584 SGVec4d plane = realNode->getValue<SGVec4d>();
585 result->setPlane(coord, toOsg(plane));
591 bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
593 SGPropertyNode* texUnit = makeChild(paramRoot, "texture");
594 const Texture* tex = getStateAttribute<Texture>(0, ss);
595 const Texture2D* texture = dynamic_cast<const Texture2D*>(tex);
596 makeChild(texUnit, "unit")->setValue(0);
598 // The default shader-based technique ignores active
599 makeChild(texUnit, "active")->setValue(false);
602 const Image* image = texture->getImage();
605 imageName = image->getFileName();
607 makeChild(texUnit, "active")->setValue(false);
608 makeChild(texUnit, "type")->setValue("white");
611 makeChild(texUnit, "active")->setValue(true);
612 makeChild(texUnit, "type")->setValue("2d");
613 string filter = findName(filterModes,
614 texture->getFilter(Texture::MIN_FILTER));
615 string magFilter = findName(filterModes,
616 texture->getFilter(Texture::MAG_FILTER));
617 string wrapS = findName(wrapModes, texture->getWrap(Texture::WRAP_S));
618 string wrapT = findName(wrapModes, texture->getWrap(Texture::WRAP_T));
619 string wrapR = findName(wrapModes, texture->getWrap(Texture::WRAP_R));
620 makeChild(texUnit, "image")->setStringValue(imageName);
621 makeChild(texUnit, "filter")->setStringValue(filter);
622 makeChild(texUnit, "mag-filter")->setStringValue(magFilter);
623 makeChild(texUnit, "wrap-s")->setStringValue(wrapS);
624 makeChild(texUnit, "wrap-t")->setStringValue(wrapT);
625 makeChild(texUnit, "wrap-r")->setStringValue(wrapR);