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.
17 #include "TextureBuilder.hxx"
19 #include <osg/Texture1D>
20 #include <osg/Texture2D>
21 #include <osg/Texture3D>
22 #include <osg/TextureRectangle>
23 #include <osgDB/FileUtils>
25 #include <boost/tuple/tuple.hpp>
26 #include <boost/tuple/tuple_comparison.hpp>
28 #include <simgear/scene/util/SGSceneFeatures.hxx>
29 #include <simgear/scene/util/StateAttributeFactory.hxx>
38 // Hack to force inclusion of TextureBuilder.cxx in library
39 osg::Texture* TextureBuilder::buildFromType(Effect* effect, const string& type,
40 const SGPropertyNode*props,
41 const osgDB::ReaderWriter::Options*
44 return EffectBuilder<Texture>::buildFromType(effect, type, props, options);
47 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
48 Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
53 EffectNameValue<Texture::FilterMode> filterModes[] =
55 { "linear", Texture::LINEAR },
56 { "linear-mipmap-linear", Texture::LINEAR_MIPMAP_LINEAR},
57 { "linear-mipmap-nearest", Texture::LINEAR_MIPMAP_NEAREST},
58 { "nearest", Texture::NEAREST},
59 { "nearest-mipmap-linear", Texture::NEAREST_MIPMAP_LINEAR},
60 { "nearest-mipmap-nearest", Texture::NEAREST_MIPMAP_NEAREST}
63 EffectNameValue<Texture::WrapMode> wrapModes[] =
65 {"clamp", Texture::CLAMP},
66 {"clamp-to-border", Texture::CLAMP_TO_BORDER},
67 {"clamp-to-edge", Texture::CLAMP_TO_EDGE},
68 {"mirror", Texture::MIRROR},
69 {"repeat", Texture::REPEAT}
74 TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
75 const osgDB::ReaderWriter::Options* options,
76 const string& texType)
78 Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR;
79 findAttr(filterModes, getEffectPropertyChild(effect, props, "filter"),
81 Texture::FilterMode magFilter = Texture::LINEAR;
82 findAttr(filterModes, getEffectPropertyChild(effect, props,
85 const SGPropertyNode* pWrapS
86 = getEffectPropertyChild(effect, props, "wrap-s");
87 Texture::WrapMode sWrap = Texture::CLAMP;
88 findAttr(wrapModes, pWrapS, sWrap);
89 const SGPropertyNode* pWrapT
90 = getEffectPropertyChild(effect, props, "wrap-t");
91 Texture::WrapMode tWrap = Texture::CLAMP;
92 findAttr(wrapModes, pWrapT, tWrap);
93 const SGPropertyNode* pWrapR
94 = getEffectPropertyChild(effect, props, "wrap-r");
95 Texture::WrapMode rWrap = Texture::CLAMP;
96 findAttr(wrapModes, pWrapR, rWrap);
97 const SGPropertyNode* pImage
98 = getEffectPropertyChild(effect, props, "image");
101 imageName = pImage->getStringValue();
102 string absFileName = osgDB::findDataFile(imageName, options);
103 return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
107 void setAttrs(const TexTuple& attrs, Texture* tex,
108 const osgDB::ReaderWriter::Options* options)
110 const string& imageName = attrs.get<0>();
111 if (imageName.empty()) {
112 throw BuilderException("no image file");
114 osgDB::ReaderWriter::ReadResult result
115 = osgDB::Registry::instance()->readImage(imageName, options);
116 if (result.success()) {
117 osg::Image* image = result.getImage();
118 tex->setImage(GL_FRONT_AND_BACK, image);
121 if (s <= t && 32 <= s) {
122 SGSceneFeatures::instance()->setTextureCompression(tex);
123 } else if (t < s && 32 <= t) {
124 SGSceneFeatures::instance()->setTextureCompression(tex);
126 tex->setMaxAnisotropy(SGSceneFeatures::instance()
127 ->getTextureFilter());
129 SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
133 // texture->setDataVariance(osg::Object::STATIC);
134 tex->setFilter(Texture::MIN_FILTER, attrs.get<1>());
135 tex->setFilter(Texture::MAG_FILTER, attrs.get<2>());
136 tex->setWrap(Texture::WRAP_S, attrs.get<3>());
137 tex->setWrap(Texture::WRAP_T, attrs.get<4>());
138 tex->setWrap(Texture::WRAP_R, attrs.get<5>());
143 class TexBuilder : public TextureBuilder
146 TexBuilder(const string& texType) : _type(texType) {}
147 Texture* build(Effect* effect, const SGPropertyNode*,
148 const osgDB::ReaderWriter::Options* options);
150 typedef map<TexTuple, ref_ptr<T> > TexMap;
156 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
157 const osgDB::ReaderWriter::Options* options)
159 TexTuple attrs = makeTexTuple(effect, props, options, _type);
160 typename TexMap::iterator itr = texMap.find(attrs);
161 if (itr != texMap.end())
162 return itr->second.get();
164 setAttrs(attrs, tex, options);
165 texMap.insert(make_pair(attrs, tex));
172 TextureBuilder::Registrar install1D("1d", new TexBuilder<Texture1D>("1d"));
173 TextureBuilder::Registrar install2D("2d", new TexBuilder<Texture2D>("2d"));
174 TextureBuilder::Registrar install3D("3d", new TexBuilder<Texture3D>("3d"));
177 class WhiteTextureBuilder : public TextureBuilder
180 Texture* build(Effect* effect, const SGPropertyNode*,
181 const osgDB::ReaderWriter::Options* options);
184 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
185 const osgDB::ReaderWriter::Options* options)
187 return StateAttributeFactory::instance()->getWhiteTexture();
192 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
195 osg::Image* make3DNoiseImage(int texSize)
197 osg::Image* image = new osg::Image;
198 image->setImage(texSize, texSize, texSize,
199 4, GL_RGBA, GL_UNSIGNED_BYTE,
200 new unsigned char[4 * texSize * texSize * texSize],
201 osg::Image::USE_NEW_DELETE);
203 const int startFrequency = 4;
204 const int numOctaves = 4;
208 double inci, incj, inck;
209 int frequency = startFrequency;
213 osg::notify(osg::WARN) << "creating 3D noise texture... ";
215 for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
217 SetNoiseFrequency(frequency);
219 ni[0] = ni[1] = ni[2] = 0;
221 inci = 1.0 / (texSize / frequency);
222 for (i = 0; i < texSize; ++i, ni[0] += inci)
224 incj = 1.0 / (texSize / frequency);
225 for (j = 0; j < texSize; ++j, ni[1] += incj)
227 inck = 1.0 / (texSize / frequency);
228 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
230 *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
236 osg::notify(osg::WARN) << "DONE" << std::endl;
240 class NoiseBuilder : public TextureBuilder
243 Texture* build(Effect* effect, const SGPropertyNode*,
244 const osgDB::ReaderWriter::Options* options);
246 typedef map<int, ref_ptr<Texture3D> > NoiseMap;
250 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
251 const osgDB::ReaderWriter::Options* options)
254 const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
257 texSize = sizeProp->getValue<int>();
258 NoiseMap::iterator itr = _noises.find(texSize);
259 if (itr != _noises.end())
260 return itr->second.get();
261 Texture3D* noiseTexture = new osg::Texture3D;
262 noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
263 noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
264 noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
265 noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
266 noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
267 noiseTexture->setImage( make3DNoiseImage(texSize) );
268 _noises.insert(make_pair(texSize, noiseTexture));
274 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);