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>
34 #include <simgear/math/SGMath.hxx>
35 #include <simgear/structure/OSGUtils.hxx>
44 using namespace effect;
46 // Hack to force inclusion of TextureBuilder.cxx in library
47 osg::Texture* TextureBuilder::buildFromType(Effect* effect, const string& type,
48 const SGPropertyNode*props,
49 const osgDB::ReaderWriter::Options*
52 return EffectBuilder<Texture>::buildFromType(effect, type, props, options);
55 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
56 Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
61 EffectNameValue<Texture::FilterMode> filterModesInit[] =
63 { "linear", Texture::LINEAR },
64 { "linear-mipmap-linear", Texture::LINEAR_MIPMAP_LINEAR},
65 { "linear-mipmap-nearest", Texture::LINEAR_MIPMAP_NEAREST},
66 { "nearest", Texture::NEAREST},
67 { "nearest-mipmap-linear", Texture::NEAREST_MIPMAP_LINEAR},
68 { "nearest-mipmap-nearest", Texture::NEAREST_MIPMAP_NEAREST}
70 EffectPropertyMap<Texture::FilterMode> filterModes(filterModesInit);
72 EffectNameValue<Texture::WrapMode> wrapModesInit[] =
74 {"clamp", Texture::CLAMP},
75 {"clamp-to-border", Texture::CLAMP_TO_BORDER},
76 {"clamp-to-edge", Texture::CLAMP_TO_EDGE},
77 {"mirror", Texture::MIRROR},
78 {"repeat", Texture::REPEAT}
80 EffectPropertyMap<Texture::WrapMode> wrapModes(wrapModesInit);
83 TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
84 const osgDB::ReaderWriter::Options* options,
85 const string& texType)
87 Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR;
88 findAttr(filterModes, getEffectPropertyChild(effect, props, "filter"),
90 Texture::FilterMode magFilter = Texture::LINEAR;
91 findAttr(filterModes, getEffectPropertyChild(effect, props,
94 const SGPropertyNode* pWrapS
95 = getEffectPropertyChild(effect, props, "wrap-s");
96 Texture::WrapMode sWrap = Texture::CLAMP;
97 findAttr(wrapModes, pWrapS, sWrap);
98 const SGPropertyNode* pWrapT
99 = getEffectPropertyChild(effect, props, "wrap-t");
100 Texture::WrapMode tWrap = Texture::CLAMP;
101 findAttr(wrapModes, pWrapT, tWrap);
102 const SGPropertyNode* pWrapR
103 = getEffectPropertyChild(effect, props, "wrap-r");
104 Texture::WrapMode rWrap = Texture::CLAMP;
105 findAttr(wrapModes, pWrapR, rWrap);
106 const SGPropertyNode* pImage
107 = getEffectPropertyChild(effect, props, "image");
110 imageName = pImage->getStringValue();
111 string absFileName = osgDB::findDataFile(imageName, options);
112 return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
116 void setAttrs(const TexTuple& attrs, Texture* tex,
117 const osgDB::ReaderWriter::Options* options)
119 const string& imageName = attrs.get<0>();
120 if (imageName.empty()) {
121 throw BuilderException("no image file");
123 osgDB::ReaderWriter::ReadResult result
124 = osgDB::Registry::instance()->readImage(imageName, options);
125 if (result.success()) {
126 osg::Image* image = result.getImage();
127 tex->setImage(GL_FRONT_AND_BACK, image);
130 if (s <= t && 32 <= s) {
131 SGSceneFeatures::instance()->setTextureCompression(tex);
132 } else if (t < s && 32 <= t) {
133 SGSceneFeatures::instance()->setTextureCompression(tex);
135 tex->setMaxAnisotropy(SGSceneFeatures::instance()
136 ->getTextureFilter());
138 SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
142 // texture->setDataVariance(osg::Object::STATIC);
143 tex->setFilter(Texture::MIN_FILTER, attrs.get<1>());
144 tex->setFilter(Texture::MAG_FILTER, attrs.get<2>());
145 tex->setWrap(Texture::WRAP_S, attrs.get<3>());
146 tex->setWrap(Texture::WRAP_T, attrs.get<4>());
147 tex->setWrap(Texture::WRAP_R, attrs.get<5>());
152 class TexBuilder : public TextureBuilder
155 TexBuilder(const string& texType) : _type(texType) {}
156 Texture* build(Effect* effect, const SGPropertyNode*,
157 const osgDB::ReaderWriter::Options* options);
159 typedef map<TexTuple, ref_ptr<T> > TexMap;
165 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
166 const osgDB::ReaderWriter::Options* options)
168 TexTuple attrs = makeTexTuple(effect, props, options, _type);
169 typename TexMap::iterator itr = texMap.find(attrs);
170 if (itr != texMap.end())
171 return itr->second.get();
173 setAttrs(attrs, tex, options);
174 texMap.insert(make_pair(attrs, tex));
181 TextureBuilder::Registrar install1D("1d", new TexBuilder<Texture1D>("1d"));
182 TextureBuilder::Registrar install2D("2d", new TexBuilder<Texture2D>("2d"));
183 TextureBuilder::Registrar install3D("3d", new TexBuilder<Texture3D>("3d"));
186 class WhiteTextureBuilder : public TextureBuilder
189 Texture* build(Effect* effect, const SGPropertyNode*,
190 const osgDB::ReaderWriter::Options* options);
193 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
194 const osgDB::ReaderWriter::Options* options)
196 return StateAttributeFactory::instance()->getWhiteTexture();
201 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
204 osg::Image* make3DNoiseImage(int texSize)
206 osg::Image* image = new osg::Image;
207 image->setImage(texSize, texSize, texSize,
208 4, GL_RGBA, GL_UNSIGNED_BYTE,
209 new unsigned char[4 * texSize * texSize * texSize],
210 osg::Image::USE_NEW_DELETE);
212 const int startFrequency = 4;
213 const int numOctaves = 4;
217 double inci, incj, inck;
218 int frequency = startFrequency;
222 osg::notify(osg::WARN) << "creating 3D noise texture... ";
224 for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
226 SetNoiseFrequency(frequency);
228 ni[0] = ni[1] = ni[2] = 0;
230 inci = 1.0 / (texSize / frequency);
231 for (i = 0; i < texSize; ++i, ni[0] += inci)
233 incj = 1.0 / (texSize / frequency);
234 for (j = 0; j < texSize; ++j, ni[1] += incj)
236 inck = 1.0 / (texSize / frequency);
237 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
239 *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
245 osg::notify(osg::WARN) << "DONE" << std::endl;
249 class NoiseBuilder : public TextureBuilder
252 Texture* build(Effect* effect, const SGPropertyNode*,
253 const osgDB::ReaderWriter::Options* options);
255 typedef map<int, ref_ptr<Texture3D> > NoiseMap;
259 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
260 const osgDB::ReaderWriter::Options* options)
263 const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
266 texSize = sizeProp->getValue<int>();
267 NoiseMap::iterator itr = _noises.find(texSize);
268 if (itr != _noises.end())
269 return itr->second.get();
270 Texture3D* noiseTexture = new osg::Texture3D;
271 noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
272 noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
273 noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
274 noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
275 noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
276 noiseTexture->setImage( make3DNoiseImage(texSize) );
277 _noises.insert(make_pair(texSize, noiseTexture));
283 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
286 bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
288 SGPropertyNode* texUnit = makeChild(paramRoot, "texture");
289 const Texture* tex = getStateAttribute<Texture>(0, ss);
290 const Texture2D* texture = dynamic_cast<const Texture2D*>(tex);
291 makeChild(texUnit, "unit")->setValue(0);
293 makeChild(texUnit, "active")->setValue(false);
294 makeChild(texUnit, "type")->setValue("white");
297 const Image* image = texture->getImage();
300 imageName = image->getFileName();
302 makeChild(texUnit, "active")->setValue(false);
303 makeChild(texUnit, "type")->setValue("white");
306 makeChild(texUnit, "active")->setValue(true);
307 makeChild(texUnit, "type")->setValue("2d");
308 string wrapS = findName(wrapModes, texture->getWrap(Texture::WRAP_S));
309 string wrapT = findName(wrapModes, texture->getWrap(Texture::WRAP_T));
310 string wrapR = findName(wrapModes, texture->getWrap(Texture::WRAP_R));
311 makeChild(texUnit, "image")->setStringValue(imageName);
312 makeChild(texUnit, "wrap-s")->setStringValue(wrapS);
313 makeChild(texUnit, "wrap-t")->setStringValue(wrapT);
314 makeChild(texUnit, "wrap-r")->setStringValue(wrapR);