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"
23 #include <osg/Texture1D>
24 #include <osg/Texture2D>
25 #include <osg/Texture3D>
26 #include <osg/TextureRectangle>
27 #include <osgDB/FileUtils>
29 #include <boost/tuple/tuple.hpp>
30 #include <boost/tuple/tuple_comparison.hpp>
32 #include <simgear/scene/util/SGSceneFeatures.hxx>
33 #include <simgear/scene/util/StateAttributeFactory.hxx>
35 #include <simgear/math/SGMath.hxx>
44 // Hack to force inclusion of TextureBuilder.cxx in library
45 osg::Texture* TextureBuilder::buildFromType(Effect* effect, const string& type,
46 const SGPropertyNode*props,
47 const osgDB::ReaderWriter::Options*
50 return EffectBuilder<Texture>::buildFromType(effect, type, props, options);
53 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
54 Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
59 EffectNameValue<Texture::FilterMode> filterModes[] =
61 { "linear", Texture::LINEAR },
62 { "linear-mipmap-linear", Texture::LINEAR_MIPMAP_LINEAR},
63 { "linear-mipmap-nearest", Texture::LINEAR_MIPMAP_NEAREST},
64 { "nearest", Texture::NEAREST},
65 { "nearest-mipmap-linear", Texture::NEAREST_MIPMAP_LINEAR},
66 { "nearest-mipmap-nearest", Texture::NEAREST_MIPMAP_NEAREST}
69 EffectNameValue<Texture::WrapMode> wrapModes[] =
71 {"clamp", Texture::CLAMP},
72 {"clamp-to-border", Texture::CLAMP_TO_BORDER},
73 {"clamp-to-edge", Texture::CLAMP_TO_EDGE},
74 {"mirror", Texture::MIRROR},
75 {"repeat", Texture::REPEAT}
80 TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
81 const osgDB::ReaderWriter::Options* options,
82 const string& texType)
84 Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR;
85 findAttr(filterModes, getEffectPropertyChild(effect, props, "filter"),
87 Texture::FilterMode magFilter = Texture::LINEAR;
88 findAttr(filterModes, getEffectPropertyChild(effect, props,
91 const SGPropertyNode* pWrapS
92 = getEffectPropertyChild(effect, props, "wrap-s");
93 Texture::WrapMode sWrap = Texture::CLAMP;
94 findAttr(wrapModes, pWrapS, sWrap);
95 const SGPropertyNode* pWrapT
96 = getEffectPropertyChild(effect, props, "wrap-t");
97 Texture::WrapMode tWrap = Texture::CLAMP;
98 findAttr(wrapModes, pWrapT, tWrap);
99 const SGPropertyNode* pWrapR
100 = getEffectPropertyChild(effect, props, "wrap-r");
101 Texture::WrapMode rWrap = Texture::CLAMP;
102 findAttr(wrapModes, pWrapR, rWrap);
103 const SGPropertyNode* pImage
104 = getEffectPropertyChild(effect, props, "image");
107 imageName = pImage->getStringValue();
108 string absFileName = osgDB::findDataFile(imageName, options);
109 return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
113 void setAttrs(const TexTuple& attrs, Texture* tex,
114 const osgDB::ReaderWriter::Options* options)
116 const string& imageName = attrs.get<0>();
117 if (imageName.empty()) {
118 throw BuilderException("no image file");
120 osgDB::ReaderWriter::ReadResult result
121 = osgDB::Registry::instance()->readImage(imageName, options);
122 if (result.success()) {
123 osg::Image* image = result.getImage();
124 tex->setImage(GL_FRONT_AND_BACK, image);
127 if (s <= t && 32 <= s) {
128 SGSceneFeatures::instance()->setTextureCompression(tex);
129 } else if (t < s && 32 <= t) {
130 SGSceneFeatures::instance()->setTextureCompression(tex);
132 tex->setMaxAnisotropy(SGSceneFeatures::instance()
133 ->getTextureFilter());
135 SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
139 // texture->setDataVariance(osg::Object::STATIC);
140 tex->setFilter(Texture::MIN_FILTER, attrs.get<1>());
141 tex->setFilter(Texture::MAG_FILTER, attrs.get<2>());
142 tex->setWrap(Texture::WRAP_S, attrs.get<3>());
143 tex->setWrap(Texture::WRAP_T, attrs.get<4>());
144 tex->setWrap(Texture::WRAP_R, attrs.get<5>());
149 class TexBuilder : public TextureBuilder
152 TexBuilder(const string& texType) : _type(texType) {}
153 Texture* build(Effect* effect, const SGPropertyNode*,
154 const osgDB::ReaderWriter::Options* options);
156 typedef map<TexTuple, ref_ptr<T> > TexMap;
162 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
163 const osgDB::ReaderWriter::Options* options)
165 TexTuple attrs = makeTexTuple(effect, props, options, _type);
166 typename TexMap::iterator itr = texMap.find(attrs);
167 if (itr != texMap.end())
168 return itr->second.get();
170 setAttrs(attrs, tex, options);
171 texMap.insert(make_pair(attrs, tex));
178 TextureBuilder::Registrar install1D("1d", new TexBuilder<Texture1D>("1d"));
179 TextureBuilder::Registrar install2D("2d", new TexBuilder<Texture2D>("2d"));
180 TextureBuilder::Registrar install3D("3d", new TexBuilder<Texture3D>("3d"));
183 class WhiteTextureBuilder : public TextureBuilder
186 Texture* build(Effect* effect, const SGPropertyNode*,
187 const osgDB::ReaderWriter::Options* options);
190 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
191 const osgDB::ReaderWriter::Options* options)
193 return StateAttributeFactory::instance()->getWhiteTexture();
198 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
201 osg::Image* make3DNoiseImage(int texSize)
203 osg::Image* image = new osg::Image;
204 image->setImage(texSize, texSize, texSize,
205 4, GL_RGBA, GL_UNSIGNED_BYTE,
206 new unsigned char[4 * texSize * texSize * texSize],
207 osg::Image::USE_NEW_DELETE);
209 const int startFrequency = 4;
210 const int numOctaves = 4;
214 double inci, incj, inck;
215 int frequency = startFrequency;
219 osg::notify(osg::WARN) << "creating 3D noise texture... ";
221 for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
223 SetNoiseFrequency(frequency);
225 ni[0] = ni[1] = ni[2] = 0;
227 inci = 1.0 / (texSize / frequency);
228 for (i = 0; i < texSize; ++i, ni[0] += inci)
230 incj = 1.0 / (texSize / frequency);
231 for (j = 0; j < texSize; ++j, ni[1] += incj)
233 inck = 1.0 / (texSize / frequency);
234 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
236 *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
242 osg::notify(osg::WARN) << "DONE" << std::endl;
246 class NoiseBuilder : public TextureBuilder
249 Texture* build(Effect* effect, const SGPropertyNode*,
250 const osgDB::ReaderWriter::Options* options);
252 typedef map<int, ref_ptr<Texture3D> > NoiseMap;
256 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
257 const osgDB::ReaderWriter::Options* options)
260 const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
263 texSize = sizeProp->getValue<int>();
264 NoiseMap::iterator itr = _noises.find(texSize);
265 if (itr != _noises.end())
266 return itr->second.get();
267 Texture3D* noiseTexture = new osg::Texture3D;
268 noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
269 noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
270 noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
271 noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
272 noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
273 noiseTexture->setImage( make3DNoiseImage(texSize) );
274 _noises.insert(make_pair(texSize, noiseTexture));
280 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);