]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/TextureBuilder.cxx
Effects for models
[simgear.git] / simgear / scene / material / TextureBuilder.cxx
1 // Copyright (C) 2009  Tim Moore timoore@redhat.com
2 //
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.
7 //
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.
12 //
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.
16
17 #ifdef HAVE_CONFIG_H
18 #  include <simgear_config.h>
19 #endif
20
21 #include "TextureBuilder.hxx"
22
23 #include <osg/Texture1D>
24 #include <osg/Texture2D>
25 #include <osg/Texture3D>
26 #include <osg/TextureRectangle>
27 #include <osgDB/FileUtils>
28
29 #include <boost/tuple/tuple.hpp>
30 #include <boost/tuple/tuple_comparison.hpp>
31
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>
36
37 #include "Noise.hxx"
38
39 namespace simgear
40 {
41 using namespace std;
42 using namespace osg;
43
44 using namespace effect;
45
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*
50                                             options)
51 {
52     return EffectBuilder<Texture>::buildFromType(effect, type, props, options);
53 }
54
55 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
56                      Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
57                      string> TexTuple;
58
59 namespace
60 {
61 EffectNameValue<Texture::FilterMode> filterModesInit[] =
62 {
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}
69 };
70 EffectPropertyMap<Texture::FilterMode> filterModes(filterModesInit);
71
72 EffectNameValue<Texture::WrapMode> wrapModesInit[] =
73 {
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}
79 };
80 EffectPropertyMap<Texture::WrapMode> wrapModes(wrapModesInit);
81
82
83 TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
84                       const osgDB::ReaderWriter::Options* options,
85                       const string& texType)
86 {
87     Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR;
88     findAttr(filterModes, getEffectPropertyChild(effect, props, "filter"),
89              minFilter);
90     Texture::FilterMode magFilter = Texture::LINEAR;
91     findAttr(filterModes, getEffectPropertyChild(effect, props,
92                                                  "mag-filter"),
93              magFilter);
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");
108     string imageName;
109     if (pImage)
110         imageName = pImage->getStringValue();
111     string absFileName = osgDB::findDataFile(imageName, options);
112     return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
113                     texType);
114 }
115
116 void setAttrs(const TexTuple& attrs, Texture* tex,
117               const osgDB::ReaderWriter::Options* options)
118 {
119     const string& imageName = attrs.get<0>();
120     if (imageName.empty()) {
121         throw BuilderException("no image file");
122     } else {
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);
128             int s = image->s();
129             int t = image->t();
130             if (s <= t && 32 <= s) {
131                 SGSceneFeatures::instance()->setTextureCompression(tex);
132             } else if (t < s && 32 <= t) {
133                 SGSceneFeatures::instance()->setTextureCompression(tex);
134             }
135             tex->setMaxAnisotropy(SGSceneFeatures::instance()
136                                   ->getTextureFilter());
137         } else {
138             SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
139                    << imageName);
140         }
141     }
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>());
148 }
149 }
150
151 template<typename T>
152 class TexBuilder : public TextureBuilder
153 {
154 public:
155     TexBuilder(const string& texType) : _type(texType) {}
156     Texture* build(Effect* effect, const SGPropertyNode*,
157                    const osgDB::ReaderWriter::Options* options);
158 protected:
159     typedef map<TexTuple, ref_ptr<T> > TexMap;
160     TexMap texMap;
161     const string _type;
162 };
163
164 template<typename T>
165 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
166                               const osgDB::ReaderWriter::Options* options)
167 {
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();
172     T* tex = new T;
173     setAttrs(attrs, tex, options);
174     texMap.insert(make_pair(attrs, tex));
175     return tex;
176 }
177
178
179 namespace
180 {
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"));
184 }
185
186 class WhiteTextureBuilder : public TextureBuilder
187 {
188 public:
189     Texture* build(Effect* effect, const SGPropertyNode*,
190                    const osgDB::ReaderWriter::Options* options);
191 };
192
193 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
194                                     const osgDB::ReaderWriter::Options* options)
195 {
196     return StateAttributeFactory::instance()->getWhiteTexture();
197 }
198
199 namespace
200 {
201 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
202 }
203
204 osg::Image* make3DNoiseImage(int texSize)
205 {
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);
211
212     const int startFrequency = 4;
213     const int numOctaves = 4;
214
215     int f, i, j, k, inc;
216     double ni[3];
217     double inci, incj, inck;
218     int frequency = startFrequency;
219     GLubyte *ptr;
220     double amp = 0.5;
221
222     osg::notify(osg::WARN) << "creating 3D noise texture... ";
223
224     for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
225     {
226         SetNoiseFrequency(frequency);
227         ptr = image->data();
228         ni[0] = ni[1] = ni[2] = 0;
229
230         inci = 1.0 / (texSize / frequency);
231         for (i = 0; i < texSize; ++i, ni[0] += inci)
232         {
233             incj = 1.0 / (texSize / frequency);
234             for (j = 0; j < texSize; ++j, ni[1] += incj)
235             {
236                 inck = 1.0 / (texSize / frequency);
237                 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
238                 {
239                     *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
240                 }
241             }
242         }
243     }
244
245     osg::notify(osg::WARN) << "DONE" << std::endl;
246     return image;
247 }
248
249 class NoiseBuilder : public TextureBuilder
250 {
251 public:
252     Texture* build(Effect* effect, const SGPropertyNode*,
253                    const osgDB::ReaderWriter::Options* options);
254 protected:
255     typedef map<int, ref_ptr<Texture3D> > NoiseMap;
256     NoiseMap _noises;
257 };
258
259 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
260                              const osgDB::ReaderWriter::Options* options)
261 {
262     int texSize = 64;
263     const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
264                                                             "size");
265     if (sizeProp)
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));
278     return noiseTexture;
279 }
280
281 namespace
282 {
283 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
284 }
285
286 bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
287 {
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);
292     if (!tex) {
293         makeChild(texUnit, "active")->setValue(false);
294         makeChild(texUnit, "type")->setValue("white");
295         return false;
296     }
297     const Image* image = texture->getImage();
298     string imageName;
299     if (image) {
300         imageName = image->getFileName();
301     } else {
302         makeChild(texUnit, "active")->setValue(false);
303         makeChild(texUnit, "type")->setValue("white");
304         return false;
305     }
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);
315     return true;
316 }
317
318 }