]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/TextureBuilder.cxx
restore some part of the code to prevent an untwanted segmentationf fault.
[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
35 #include <simgear/math/SGMath.hxx>
36
37 #include "Noise.hxx"
38
39 namespace simgear
40 {
41 using namespace std;
42 using namespace osg;
43
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*
48                                             options)
49 {
50     return EffectBuilder<Texture>::buildFromType(effect, type, props, options);
51 }
52
53 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
54                      Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
55                      string> TexTuple;
56
57 namespace
58 {
59 EffectNameValue<Texture::FilterMode> filterModes[] =
60 {
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}
67 };
68
69 EffectNameValue<Texture::WrapMode> wrapModes[] =
70 {
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}
76 };
77
78
79
80 TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
81                       const osgDB::ReaderWriter::Options* options,
82                       const string& texType)
83 {
84     Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR;
85     findAttr(filterModes, getEffectPropertyChild(effect, props, "filter"),
86              minFilter);
87     Texture::FilterMode magFilter = Texture::LINEAR;
88     findAttr(filterModes, getEffectPropertyChild(effect, props,
89                                                  "mag-filter"),
90              magFilter);
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");
105     string imageName;
106     if (pImage)
107         imageName = pImage->getStringValue();
108     string absFileName = osgDB::findDataFile(imageName, options);
109     return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
110                     texType);
111 }
112
113 void setAttrs(const TexTuple& attrs, Texture* tex,
114               const osgDB::ReaderWriter::Options* options)
115 {
116     const string& imageName = attrs.get<0>();
117     if (imageName.empty()) {
118         throw BuilderException("no image file");
119     } else {
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);
125             int s = image->s();
126             int t = image->t();
127             if (s <= t && 32 <= s) {
128                 SGSceneFeatures::instance()->setTextureCompression(tex);
129             } else if (t < s && 32 <= t) {
130                 SGSceneFeatures::instance()->setTextureCompression(tex);
131             }
132             tex->setMaxAnisotropy(SGSceneFeatures::instance()
133                                   ->getTextureFilter());
134         } else {
135             SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
136                    << imageName);
137         }
138     }
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>());
145 }
146 }
147
148 template<typename T>
149 class TexBuilder : public TextureBuilder
150 {
151 public:
152     TexBuilder(const string& texType) : _type(texType) {}
153     Texture* build(Effect* effect, const SGPropertyNode*,
154                    const osgDB::ReaderWriter::Options* options);
155 protected:
156     typedef map<TexTuple, ref_ptr<T> > TexMap;
157     TexMap texMap;
158     const string _type;
159 };
160
161 template<typename T>
162 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
163                               const osgDB::ReaderWriter::Options* options)
164 {
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();
169     T* tex = new T;
170     setAttrs(attrs, tex, options);
171     texMap.insert(make_pair(attrs, tex));
172     return tex;
173 }
174
175
176 namespace
177 {
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"));
181 }
182
183 class WhiteTextureBuilder : public TextureBuilder
184 {
185 public:
186     Texture* build(Effect* effect, const SGPropertyNode*,
187                    const osgDB::ReaderWriter::Options* options);
188 };
189
190 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
191                                     const osgDB::ReaderWriter::Options* options)
192 {
193     return StateAttributeFactory::instance()->getWhiteTexture();
194 }
195
196 namespace
197 {
198 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
199 }
200
201 osg::Image* make3DNoiseImage(int texSize)
202 {
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);
208
209     const int startFrequency = 4;
210     const int numOctaves = 4;
211
212     int f, i, j, k, inc;
213     double ni[3];
214     double inci, incj, inck;
215     int frequency = startFrequency;
216     GLubyte *ptr;
217     double amp = 0.5;
218
219     osg::notify(osg::WARN) << "creating 3D noise texture... ";
220
221     for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
222     {
223         SetNoiseFrequency(frequency);
224         ptr = image->data();
225         ni[0] = ni[1] = ni[2] = 0;
226
227         inci = 1.0 / (texSize / frequency);
228         for (i = 0; i < texSize; ++i, ni[0] += inci)
229         {
230             incj = 1.0 / (texSize / frequency);
231             for (j = 0; j < texSize; ++j, ni[1] += incj)
232             {
233                 inck = 1.0 / (texSize / frequency);
234                 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
235                 {
236                     *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
237                 }
238             }
239         }
240     }
241
242     osg::notify(osg::WARN) << "DONE" << std::endl;
243     return image;
244 }
245
246 class NoiseBuilder : public TextureBuilder
247 {
248 public:
249     Texture* build(Effect* effect, const SGPropertyNode*,
250                    const osgDB::ReaderWriter::Options* options);
251 protected:
252     typedef map<int, ref_ptr<Texture3D> > NoiseMap;
253     NoiseMap _noises;
254 };
255
256 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
257                              const osgDB::ReaderWriter::Options* options)
258 {
259     int texSize = 64;
260     const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
261                                                             "size");
262     if (sizeProp)
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));
275     return noiseTexture;
276 }
277
278 namespace
279 {
280 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
281 }
282
283 }