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>
27 #include <osg/Texture1D>
28 #include <osg/Texture2D>
29 #include <osg/Texture3D>
30 #include <osg/TextureRectangle>
31 #include <osgDB/FileUtils>
33 #include <boost/lexical_cast.hpp>
34 #include <boost/tuple/tuple.hpp>
35 #include <boost/tuple/tuple_comparison.hpp>
37 #include <simgear/scene/util/SGSceneFeatures.hxx>
38 #include <simgear/scene/util/StateAttributeFactory.hxx>
39 #include <simgear/math/SGMath.hxx>
40 #include <simgear/structure/OSGUtils.hxx>
49 using namespace effect;
51 // Hack to force inclusion of TextureBuilder.cxx in library
52 osg::Texture* TextureBuilder::buildFromType(Effect* effect, const string& type,
53 const SGPropertyNode*props,
54 const osgDB::ReaderWriter::Options*
57 return EffectBuilder<Texture>::buildFromType(effect, type, props, options);
60 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
61 Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
64 EffectNameValue<TexEnv::Mode> texEnvModesInit[] =
67 {"blend", TexEnv::BLEND},
68 {"decal", TexEnv::DECAL},
69 {"modulate", TexEnv::MODULATE},
70 {"replace", TexEnv::REPLACE}
72 EffectPropertyMap<TexEnv::Mode> texEnvModes(texEnvModesInit);
74 TexEnv* buildTexEnv(Effect* effect, const SGPropertyNode* prop)
76 const SGPropertyNode* modeProp = getEffectPropertyChild(effect, prop,
78 const SGPropertyNode* colorProp = getEffectPropertyChild(effect, prop,
82 TexEnv::Mode mode = TexEnv::MODULATE;
83 findAttr(texEnvModes, modeProp, mode);
84 if (mode == TexEnv::MODULATE) {
85 return StateAttributeFactory::instance()->getStandardTexEnv();
87 TexEnv* env = new TexEnv(mode);
89 env->setColor(toOsg(colorProp->getValue<SGVec4d>()));
94 void TextureUnitBuilder::buildAttribute(Effect* effect, Pass* pass,
95 const SGPropertyNode* prop,
96 const osgDB::ReaderWriter::Options* options)
98 if (!isAttributeActive(effect, prop))
100 // Decode the texture unit
102 const SGPropertyNode* pUnit = prop->getChild("unit");
104 unit = pUnit->getValue<int>();
106 const SGPropertyNode* pName = prop->getChild("name");
109 unit = boost::lexical_cast<int>(pName->getStringValue());
110 } catch (boost::bad_lexical_cast& lex) {
111 SG_LOG(SG_INPUT, SG_ALERT, "can't decode name as texture unit "
115 const SGPropertyNode* pType = getEffectPropertyChild(effect, prop, "type");
120 type = pType->getStringValue();
121 Texture* texture = 0;
123 texture = TextureBuilder::buildFromType(effect, type, prop,
126 catch (BuilderException& e) {
127 SG_LOG(SG_INPUT, SG_ALERT, "No image file for texture, using white ");
128 texture = StateAttributeFactory::instance()->getWhiteTexture();
130 pass->setTextureAttributeAndModes(unit, texture);
131 const SGPropertyNode* envProp = prop->getChild("environment");
133 TexEnv* env = buildTexEnv(effect, envProp);
135 pass->setTextureAttributeAndModes(unit, env);
139 // InstallAttributeBuilder call is in Effect.cxx to force this file to
144 EffectNameValue<Texture::FilterMode> filterModesInit[] =
146 { "linear", Texture::LINEAR },
147 { "linear-mipmap-linear", Texture::LINEAR_MIPMAP_LINEAR},
148 { "linear-mipmap-nearest", Texture::LINEAR_MIPMAP_NEAREST},
149 { "nearest", Texture::NEAREST},
150 { "nearest-mipmap-linear", Texture::NEAREST_MIPMAP_LINEAR},
151 { "nearest-mipmap-nearest", Texture::NEAREST_MIPMAP_NEAREST}
153 EffectPropertyMap<Texture::FilterMode> filterModes(filterModesInit);
155 EffectNameValue<Texture::WrapMode> wrapModesInit[] =
157 {"clamp", Texture::CLAMP},
158 {"clamp-to-border", Texture::CLAMP_TO_BORDER},
159 {"clamp-to-edge", Texture::CLAMP_TO_EDGE},
160 {"mirror", Texture::MIRROR},
161 {"repeat", Texture::REPEAT}
163 EffectPropertyMap<Texture::WrapMode> wrapModes(wrapModesInit);
166 TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
167 const osgDB::ReaderWriter::Options* options,
168 const string& texType)
170 Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR;
171 findAttr(filterModes, getEffectPropertyChild(effect, props, "filter"),
173 Texture::FilterMode magFilter = Texture::LINEAR;
174 findAttr(filterModes, getEffectPropertyChild(effect, props,
177 const SGPropertyNode* pWrapS
178 = getEffectPropertyChild(effect, props, "wrap-s");
179 Texture::WrapMode sWrap = Texture::CLAMP;
180 findAttr(wrapModes, pWrapS, sWrap);
181 const SGPropertyNode* pWrapT
182 = getEffectPropertyChild(effect, props, "wrap-t");
183 Texture::WrapMode tWrap = Texture::CLAMP;
184 findAttr(wrapModes, pWrapT, tWrap);
185 const SGPropertyNode* pWrapR
186 = getEffectPropertyChild(effect, props, "wrap-r");
187 Texture::WrapMode rWrap = Texture::CLAMP;
188 findAttr(wrapModes, pWrapR, rWrap);
189 const SGPropertyNode* pImage
190 = getEffectPropertyChild(effect, props, "image");
193 imageName = pImage->getStringValue();
194 string absFileName = osgDB::findDataFile(imageName, options);
195 return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
199 void setAttrs(const TexTuple& attrs, Texture* tex,
200 const osgDB::ReaderWriter::Options* options)
202 const string& imageName = attrs.get<0>();
203 if (imageName.empty()) {
204 throw BuilderException("no image file");
206 osgDB::ReaderWriter::ReadResult result
207 = osgDB::Registry::instance()->readImage(imageName, options);
208 if (result.success()) {
209 osg::Image* image = result.getImage();
210 tex->setImage(GL_FRONT_AND_BACK, image);
213 if (s <= t && 32 <= s) {
214 SGSceneFeatures::instance()->setTextureCompression(tex);
215 } else if (t < s && 32 <= t) {
216 SGSceneFeatures::instance()->setTextureCompression(tex);
218 tex->setMaxAnisotropy(SGSceneFeatures::instance()
219 ->getTextureFilter());
221 SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
225 // texture->setDataVariance(osg::Object::STATIC);
226 tex->setFilter(Texture::MIN_FILTER, attrs.get<1>());
227 tex->setFilter(Texture::MAG_FILTER, attrs.get<2>());
228 tex->setWrap(Texture::WRAP_S, attrs.get<3>());
229 tex->setWrap(Texture::WRAP_T, attrs.get<4>());
230 tex->setWrap(Texture::WRAP_R, attrs.get<5>());
235 class TexBuilder : public TextureBuilder
238 TexBuilder(const string& texType) : _type(texType) {}
239 Texture* build(Effect* effect, const SGPropertyNode*,
240 const osgDB::ReaderWriter::Options* options);
242 typedef map<TexTuple, ref_ptr<T> > TexMap;
248 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
249 const osgDB::ReaderWriter::Options* options)
251 TexTuple attrs = makeTexTuple(effect, props, options, _type);
252 typename TexMap::iterator itr = texMap.find(attrs);
253 if (itr != texMap.end())
254 return itr->second.get();
256 setAttrs(attrs, tex, options);
257 texMap.insert(make_pair(attrs, tex));
264 TextureBuilder::Registrar install1D("1d", new TexBuilder<Texture1D>("1d"));
265 TextureBuilder::Registrar install2D("2d", new TexBuilder<Texture2D>("2d"));
266 TextureBuilder::Registrar install3D("3d", new TexBuilder<Texture3D>("3d"));
269 class WhiteTextureBuilder : public TextureBuilder
272 Texture* build(Effect* effect, const SGPropertyNode*,
273 const osgDB::ReaderWriter::Options* options);
276 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
277 const osgDB::ReaderWriter::Options* options)
279 return StateAttributeFactory::instance()->getWhiteTexture();
284 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
287 osg::Image* make3DNoiseImage(int texSize)
289 osg::Image* image = new osg::Image;
290 image->setImage(texSize, texSize, texSize,
291 4, GL_RGBA, GL_UNSIGNED_BYTE,
292 new unsigned char[4 * texSize * texSize * texSize],
293 osg::Image::USE_NEW_DELETE);
295 const int startFrequency = 4;
296 const int numOctaves = 4;
300 double inci, incj, inck;
301 int frequency = startFrequency;
305 osg::notify(osg::WARN) << "creating 3D noise texture... ";
307 for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
309 SetNoiseFrequency(frequency);
311 ni[0] = ni[1] = ni[2] = 0;
313 inci = 1.0 / (texSize / frequency);
314 for (i = 0; i < texSize; ++i, ni[0] += inci)
316 incj = 1.0 / (texSize / frequency);
317 for (j = 0; j < texSize; ++j, ni[1] += incj)
319 inck = 1.0 / (texSize / frequency);
320 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
322 *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
328 osg::notify(osg::WARN) << "DONE" << std::endl;
332 class NoiseBuilder : public TextureBuilder
335 Texture* build(Effect* effect, const SGPropertyNode*,
336 const osgDB::ReaderWriter::Options* options);
338 typedef map<int, ref_ptr<Texture3D> > NoiseMap;
342 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
343 const osgDB::ReaderWriter::Options* options)
346 const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
349 texSize = sizeProp->getValue<int>();
350 NoiseMap::iterator itr = _noises.find(texSize);
351 if (itr != _noises.end())
352 return itr->second.get();
353 Texture3D* noiseTexture = new osg::Texture3D;
354 noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
355 noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
356 noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
357 noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
358 noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
359 noiseTexture->setImage( make3DNoiseImage(texSize) );
360 _noises.insert(make_pair(texSize, noiseTexture));
366 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
369 bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
371 SGPropertyNode* texUnit = makeChild(paramRoot, "texture");
372 const Texture* tex = getStateAttribute<Texture>(0, ss);
373 const Texture2D* texture = dynamic_cast<const Texture2D*>(tex);
374 makeChild(texUnit, "unit")->setValue(0);
376 makeChild(texUnit, "active")->setValue(false);
377 makeChild(texUnit, "type")->setValue("white");
380 const Image* image = texture->getImage();
383 imageName = image->getFileName();
385 makeChild(texUnit, "active")->setValue(false);
386 makeChild(texUnit, "type")->setValue("white");
389 makeChild(texUnit, "active")->setValue(true);
390 makeChild(texUnit, "type")->setValue("2d");
391 string wrapS = findName(wrapModes, texture->getWrap(Texture::WRAP_S));
392 string wrapT = findName(wrapModes, texture->getWrap(Texture::WRAP_T));
393 string wrapR = findName(wrapModes, texture->getWrap(Texture::WRAP_R));
394 makeChild(texUnit, "image")->setStringValue(imageName);
395 makeChild(texUnit, "wrap-s")->setStringValue(wrapS);
396 makeChild(texUnit, "wrap-t")->setStringValue(wrapT);
397 makeChild(texUnit, "wrap-r")->setStringValue(wrapR);