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 findAttr(filterModes, getEffectPropertyChild(effect, props, "filter"),
186 Texture::FilterMode magFilter = Texture::LINEAR;
187 findAttr(filterModes, getEffectPropertyChild(effect, props,
190 const SGPropertyNode* pWrapS
191 = getEffectPropertyChild(effect, props, "wrap-s");
192 Texture::WrapMode sWrap = Texture::CLAMP;
193 findAttr(wrapModes, pWrapS, sWrap);
194 const SGPropertyNode* pWrapT
195 = getEffectPropertyChild(effect, props, "wrap-t");
196 Texture::WrapMode tWrap = Texture::CLAMP;
197 findAttr(wrapModes, pWrapT, tWrap);
198 const SGPropertyNode* pWrapR
199 = getEffectPropertyChild(effect, props, "wrap-r");
200 Texture::WrapMode rWrap = Texture::CLAMP;
201 findAttr(wrapModes, pWrapR, rWrap);
202 const SGPropertyNode* pImage
203 = getEffectPropertyChild(effect, props, "image");
206 imageName = pImage->getStringValue();
207 string absFileName = osgDB::findDataFile(imageName, options);
208 return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
212 void setAttrs(const TexTuple& attrs, Texture* tex,
213 const osgDB::ReaderWriter::Options* options)
215 const string& imageName = attrs.get<0>();
216 if (imageName.empty()) {
217 throw BuilderException("no image file");
219 osgDB::ReaderWriter::ReadResult result
220 = osgDB::Registry::instance()->readImage(imageName, options);
221 if (result.success()) {
222 osg::Image* image = result.getImage();
223 tex->setImage(GL_FRONT_AND_BACK, image);
226 if (s <= t && 32 <= s) {
227 SGSceneFeatures::instance()->setTextureCompression(tex);
228 } else if (t < s && 32 <= t) {
229 SGSceneFeatures::instance()->setTextureCompression(tex);
231 tex->setMaxAnisotropy(SGSceneFeatures::instance()
232 ->getTextureFilter());
234 SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
238 // texture->setDataVariance(osg::Object::STATIC);
239 tex->setFilter(Texture::MIN_FILTER, attrs.get<1>());
240 tex->setFilter(Texture::MAG_FILTER, attrs.get<2>());
241 tex->setWrap(Texture::WRAP_S, attrs.get<3>());
242 tex->setWrap(Texture::WRAP_T, attrs.get<4>());
243 tex->setWrap(Texture::WRAP_R, attrs.get<5>());
248 class TexBuilder : public TextureBuilder
251 TexBuilder(const string& texType) : _type(texType) {}
252 Texture* build(Effect* effect, const SGPropertyNode*,
253 const osgDB::ReaderWriter::Options* options);
255 typedef map<TexTuple, ref_ptr<T> > TexMap;
261 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
262 const osgDB::ReaderWriter::Options* options)
264 TexTuple attrs = makeTexTuple(effect, props, options, _type);
265 typename TexMap::iterator itr = texMap.find(attrs);
266 if (itr != texMap.end())
267 return itr->second.get();
269 setAttrs(attrs, tex, options);
270 texMap.insert(make_pair(attrs, tex));
277 TextureBuilder::Registrar install1D("1d", new TexBuilder<Texture1D>("1d"));
278 TextureBuilder::Registrar install2D("2d", new TexBuilder<Texture2D>("2d"));
279 TextureBuilder::Registrar install3D("3d", new TexBuilder<Texture3D>("3d"));
282 class WhiteTextureBuilder : public TextureBuilder
285 Texture* build(Effect* effect, const SGPropertyNode*,
286 const osgDB::ReaderWriter::Options* options);
289 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
290 const osgDB::ReaderWriter::Options* options)
292 return StateAttributeFactory::instance()->getWhiteTexture();
297 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
300 osg::Image* make3DNoiseImage(int texSize)
302 osg::Image* image = new osg::Image;
303 image->setImage(texSize, texSize, texSize,
304 4, GL_RGBA, GL_UNSIGNED_BYTE,
305 new unsigned char[4 * texSize * texSize * texSize],
306 osg::Image::USE_NEW_DELETE);
308 const int startFrequency = 4;
309 const int numOctaves = 4;
313 double inci, incj, inck;
314 int frequency = startFrequency;
318 osg::notify(osg::WARN) << "creating 3D noise texture... ";
320 for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
322 SetNoiseFrequency(frequency);
324 ni[0] = ni[1] = ni[2] = 0;
326 inci = 1.0 / (texSize / frequency);
327 for (i = 0; i < texSize; ++i, ni[0] += inci)
329 incj = 1.0 / (texSize / frequency);
330 for (j = 0; j < texSize; ++j, ni[1] += incj)
332 inck = 1.0 / (texSize / frequency);
333 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
335 *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
341 osg::notify(osg::WARN) << "DONE" << std::endl;
345 class NoiseBuilder : public TextureBuilder
348 Texture* build(Effect* effect, const SGPropertyNode*,
349 const osgDB::ReaderWriter::Options* options);
351 typedef map<int, ref_ptr<Texture3D> > NoiseMap;
355 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
356 const osgDB::ReaderWriter::Options* options)
359 const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
362 texSize = sizeProp->getValue<int>();
363 NoiseMap::iterator itr = _noises.find(texSize);
364 if (itr != _noises.end())
365 return itr->second.get();
366 Texture3D* noiseTexture = new osg::Texture3D;
367 noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
368 noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
369 noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
370 noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
371 noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
372 noiseTexture->setImage( make3DNoiseImage(texSize) );
373 _noises.insert(make_pair(texSize, noiseTexture));
379 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
382 EffectNameValue<TexEnvCombine::CombineParam> combineParamInit[] =
384 {"replace", TexEnvCombine::REPLACE},
385 {"modulate", TexEnvCombine::MODULATE},
386 {"add", TexEnvCombine::ADD},
387 {"add-signed", TexEnvCombine::ADD_SIGNED},
388 {"interpolate", TexEnvCombine::INTERPOLATE},
389 {"subtract", TexEnvCombine::SUBTRACT},
390 {"dot3-rgb", TexEnvCombine::DOT3_RGB},
391 {"dot3-rgba", TexEnvCombine::DOT3_RGBA}
394 EffectPropertyMap<TexEnvCombine::CombineParam> combineParams(combineParamInit);
396 EffectNameValue<TexEnvCombine::SourceParam> sourceParamInit[] =
398 {"constant", TexEnvCombine::CONSTANT},
399 {"primary_color", TexEnvCombine::PRIMARY_COLOR},
400 {"previous", TexEnvCombine::PREVIOUS},
401 {"texture", TexEnvCombine::TEXTURE},
402 {"texture0", TexEnvCombine::TEXTURE0},
403 {"texture1", TexEnvCombine::TEXTURE1},
404 {"texture2", TexEnvCombine::TEXTURE2},
405 {"texture3", TexEnvCombine::TEXTURE3},
406 {"texture4", TexEnvCombine::TEXTURE4},
407 {"texture5", TexEnvCombine::TEXTURE5},
408 {"texture6", TexEnvCombine::TEXTURE6},
409 {"texture7", TexEnvCombine::TEXTURE7}
412 EffectPropertyMap<TexEnvCombine::SourceParam> sourceParams(sourceParamInit);
414 EffectNameValue<TexEnvCombine::OperandParam> opParamInit[] =
416 {"src_color", TexEnvCombine::SRC_COLOR},
417 {"one_minus_src_color", TexEnvCombine::ONE_MINUS_SRC_COLOR},
418 {"src_alpha", TexEnvCombine::SRC_ALPHA},
419 {"one_minus_src_alpha", TexEnvCombine::ONE_MINUS_SRC_ALPHA}
422 EffectPropertyMap<TexEnvCombine::OperandParam> operandParams(opParamInit);
424 TexEnvCombine* buildTexEnvCombine(Effect* effect, const SGPropertyNode* envProp)
426 if (!isAttributeActive(effect, envProp))
428 TexEnvCombine* result = new TexEnvCombine;
429 const SGPropertyNode* p = 0;
430 if ((p = getEffectPropertyChild(effect, envProp, "combine-rgb"))) {
431 TexEnvCombine::CombineParam crgb = TexEnvCombine::MODULATE;
432 findAttr(combineParams, p, crgb);
433 result->setCombine_RGB(crgb);
435 if ((p = getEffectPropertyChild(effect, envProp, "combine-alpha"))) {
436 TexEnvCombine::CombineParam calpha = TexEnvCombine::MODULATE;
437 findAttr(combineParams, p, calpha);
438 result->setCombine_RGB(calpha);
440 if ((p = getEffectPropertyChild(effect, envProp, "source0-rgb"))) {
441 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
442 findAttr(sourceParams, p, source);
443 result->setSource0_RGB(source);
445 if ((p = getEffectPropertyChild(effect, envProp, "source1-rgb"))) {
446 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
447 findAttr(sourceParams, p, source);
448 result->setSource1_RGB(source);
450 if ((p = getEffectPropertyChild(effect, envProp, "source2-rgb"))) {
451 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
452 findAttr(sourceParams, p, source);
453 result->setSource2_RGB(source);
455 if ((p = getEffectPropertyChild(effect, envProp, "source0-alpha"))) {
456 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
457 findAttr(sourceParams, p, source);
458 result->setSource0_Alpha(source);
460 if ((p = getEffectPropertyChild(effect, envProp, "source1-alpha"))) {
461 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
462 findAttr(sourceParams, p, source);
463 result->setSource1_Alpha(source);
465 if ((p = getEffectPropertyChild(effect, envProp, "source2-alpha"))) {
466 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
467 findAttr(sourceParams, p, source);
468 result->setSource2_Alpha(source);
470 if ((p = getEffectPropertyChild(effect, envProp, "operand0-rgb"))) {
471 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
472 findAttr(operandParams, p, op);
473 result->setOperand0_RGB(op);
475 if ((p = getEffectPropertyChild(effect, envProp, "operand1-rgb"))) {
476 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
477 findAttr(operandParams, p, op);
478 result->setOperand1_RGB(op);
480 if ((p = getEffectPropertyChild(effect, envProp, "operand2-rgb"))) {
481 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
482 findAttr(operandParams, p, op);
483 result->setOperand2_RGB(op);
485 if ((p = getEffectPropertyChild(effect, envProp, "operand0-alpha"))) {
486 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
487 findAttr(operandParams, p, op);
488 result->setOperand0_Alpha(op);
490 if ((p = getEffectPropertyChild(effect, envProp, "operand1-alpha"))) {
491 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
492 findAttr(operandParams, p, op);
493 result->setOperand1_Alpha(op);
495 if ((p = getEffectPropertyChild(effect, envProp, "operand2-alpha"))) {
496 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
497 findAttr(operandParams, p, op);
498 result->setOperand2_Alpha(op);
500 if ((p = getEffectPropertyChild(effect, envProp, "scale-rgb"))) {
501 result->setScale_RGB(p->getValue<float>());
503 if ((p = getEffectPropertyChild(effect, envProp, "scale-alpha"))) {
504 result->setScale_Alpha(p->getValue<float>());
507 if ((p = getEffectPropertyChild(effect, envProp, "constant-color"))) {
508 SGVec4d color = p->getValue<SGVec4d>();
509 result->setConstantColor(toOsg(color));
510 } else if ((p = getEffectPropertyChild(effect, envProp,
511 "light-direction"))) {
512 SGVec3d direction = p->getValue<SGVec3d>();
513 result->setConstantColorAsLightDirection(toOsg(direction));
516 const SGPropertyNode* colorNode = envProp->getChild("constant-color");
518 initFromParameters(effect, colorNode, result,
519 &TexEnvCombine::setConstantColor, colorFields);
523 EffectNameValue<TexGen::Mode> tgenModeInit[] =
525 { "object-linear", TexGen::OBJECT_LINEAR},
526 { "eye-linear", TexGen::EYE_LINEAR},
527 { "sphere-map", TexGen::SPHERE_MAP},
528 { "normal-map", TexGen::NORMAL_MAP},
529 { "reflection-map", TexGen::REFLECTION_MAP}
532 EffectPropertyMap<TexGen::Mode> tgenModes(tgenModeInit);
534 EffectNameValue<TexGen::Coord> tgenCoordInit[] =
542 EffectPropertyMap<TexGen::Coord> tgenCoords(tgenCoordInit);
544 TexGen* buildTexGen(Effect* effect, const SGPropertyNode* tgenProp)
546 if (!isAttributeActive(effect, tgenProp))
548 TexGen* result = new TexGen;
549 const SGPropertyNode* p = 0;
550 TexGen::Mode mode = TexGen::OBJECT_LINEAR;
551 if (findAttr(tgenModes, getEffectPropertyChild(effect, tgenProp, "mode"),
553 result->setMode(mode);
554 const SGPropertyNode* planesNode = tgenProp->getChild("planes");
556 for (int i = 0; i < planesNode->nChildren(); ++i) {
557 const SGPropertyNode* planeNode = planesNode->getChild(i);
559 if (!findAttr(tgenCoords, planeNode->getName(), coord)) {
560 SG_LOG(SG_INPUT, SG_ALERT, "Unknown TexGen plane "
561 << planeNode->getName());
563 const SGPropertyNode* realNode
564 = getEffectPropertyNode(effect, planeNode);
565 SGVec4d plane = realNode->getValue<SGVec4d>();
566 result->setPlane(coord, toOsg(plane));
573 bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
575 SGPropertyNode* texUnit = makeChild(paramRoot, "texture");
576 const Texture* tex = getStateAttribute<Texture>(0, ss);
577 const Texture2D* texture = dynamic_cast<const Texture2D*>(tex);
578 makeChild(texUnit, "unit")->setValue(0);
580 makeChild(texUnit, "active")->setValue(false);
581 makeChild(texUnit, "type")->setValue("white");
584 const Image* image = texture->getImage();
587 imageName = image->getFileName();
589 makeChild(texUnit, "active")->setValue(false);
590 makeChild(texUnit, "type")->setValue("white");
593 makeChild(texUnit, "active")->setValue(true);
594 makeChild(texUnit, "type")->setValue("2d");
595 string wrapS = findName(wrapModes, texture->getWrap(Texture::WRAP_S));
596 string wrapT = findName(wrapModes, texture->getWrap(Texture::WRAP_T));
597 string wrapR = findName(wrapModes, texture->getWrap(Texture::WRAP_R));
598 makeChild(texUnit, "image")->setStringValue(imageName);
599 makeChild(texUnit, "wrap-s")->setStringValue(wrapS);
600 makeChild(texUnit, "wrap-t")->setStringValue(wrapT);
601 makeChild(texUnit, "wrap-r")->setStringValue(wrapR);