]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/TextureBuilder.cxx
2ea23c6e61ca0dde7f34a278531f4fb44bf6b183
[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 #include "TextureBuilder.hxx"
18
19 #include <osg/Texture1D>
20 #include <osg/Texture2D>
21 #include <osg/Texture3D>
22 #include <osg/TextureRectangle>
23 #include <osgDB/FileUtils>
24
25 #include <boost/tuple/tuple.hpp>
26 #include <boost/tuple/tuple_comparison.hpp>
27
28 #include <simgear/scene/util/SGSceneFeatures.hxx>
29 #include <simgear/scene/util/StateAttributeFactory.hxx>
30
31 #include "Noise.hxx"
32
33 namespace simgear
34 {
35 using namespace std;
36 using namespace osg;
37
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*
42                                             options)
43 {
44     return EffectBuilder<Texture>::buildFromType(effect, type, props, options);
45 }
46
47 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
48                      Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
49                      string> TexTuple;
50
51 namespace
52 {
53 EffectNameValue<Texture::FilterMode> filterModes[] =
54 {
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}
61 };
62
63 EffectNameValue<Texture::WrapMode> wrapModes[] =
64 {
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}
70 };
71
72
73
74 TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
75                       const osgDB::ReaderWriter::Options* options,
76                       const string& texType)
77 {
78     Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR;
79     findAttr(filterModes, getEffectPropertyChild(effect, props, "filter"),
80              minFilter);
81     Texture::FilterMode magFilter = Texture::LINEAR;
82     findAttr(filterModes, getEffectPropertyChild(effect, props,
83                                                  "mag-filter"),
84              magFilter);
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");
99     string imageName;
100     if (pImage)
101         imageName = pImage->getStringValue();
102     string absFileName = osgDB::findDataFile(imageName, options);
103     return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
104                     texType);
105 }
106
107 void setAttrs(const TexTuple& attrs, Texture* tex,
108               const osgDB::ReaderWriter::Options* options)
109 {
110     const string& imageName = attrs.get<0>();
111     if (imageName.empty()) {
112         throw BuilderException("no image file");
113     } else {
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);
119             int s = image->s();
120             int t = image->t();
121             if (s <= t && 32 <= s) {
122                 SGSceneFeatures::instance()->setTextureCompression(tex);
123             } else if (t < s && 32 <= t) {
124                 SGSceneFeatures::instance()->setTextureCompression(tex);
125             }
126             tex->setMaxAnisotropy(SGSceneFeatures::instance()
127                                   ->getTextureFilter());
128         } else {
129             SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
130                    << imageName);
131         }
132     }
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>());
139 }
140 }
141
142 template<typename T>
143 class TexBuilder : public TextureBuilder
144 {
145 public:
146     TexBuilder(const string& texType) : _type(texType) {}
147     Texture* build(Effect* effect, const SGPropertyNode*,
148                    const osgDB::ReaderWriter::Options* options);
149 protected:
150     typedef map<TexTuple, ref_ptr<T> > TexMap;
151     TexMap texMap;
152     const string _type;
153 };
154
155 template<typename T>
156 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
157                               const osgDB::ReaderWriter::Options* options)
158 {
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();
163     T* tex = new T;
164     setAttrs(attrs, tex, options);
165     texMap.insert(make_pair(attrs, tex));
166     return tex;
167 }
168
169
170 namespace
171 {
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"));
175 }
176
177 class WhiteTextureBuilder : public TextureBuilder
178 {
179 public:
180     Texture* build(Effect* effect, const SGPropertyNode*,
181                    const osgDB::ReaderWriter::Options* options);
182 };
183
184 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
185                                     const osgDB::ReaderWriter::Options* options)
186 {
187     return StateAttributeFactory::instance()->getWhiteTexture();
188 }
189
190 namespace
191 {
192 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
193 }
194
195 osg::Image* make3DNoiseImage(int texSize)
196 {
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);
202
203     const int startFrequency = 4;
204     const int numOctaves = 4;
205
206     int f, i, j, k, inc;
207     double ni[3];
208     double inci, incj, inck;
209     int frequency = startFrequency;
210     GLubyte *ptr;
211     double amp = 0.5;
212
213     osg::notify(osg::WARN) << "creating 3D noise texture... ";
214
215     for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
216     {
217         SetNoiseFrequency(frequency);
218         ptr = image->data();
219         ni[0] = ni[1] = ni[2] = 0;
220
221         inci = 1.0 / (texSize / frequency);
222         for (i = 0; i < texSize; ++i, ni[0] += inci)
223         {
224             incj = 1.0 / (texSize / frequency);
225             for (j = 0; j < texSize; ++j, ni[1] += incj)
226             {
227                 inck = 1.0 / (texSize / frequency);
228                 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
229                 {
230                     *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
231                 }
232             }
233         }
234     }
235
236     osg::notify(osg::WARN) << "DONE" << std::endl;
237     return image;
238 }
239
240 class NoiseBuilder : public TextureBuilder
241 {
242 public:
243     Texture* build(Effect* effect, const SGPropertyNode*,
244                    const osgDB::ReaderWriter::Options* options);
245 protected:
246     typedef map<int, ref_ptr<Texture3D> > NoiseMap;
247     NoiseMap _noises;
248 };
249
250 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
251                              const osgDB::ReaderWriter::Options* options)
252 {
253     int texSize = 64;
254     const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
255                                                             "size");
256     if (sizeProp)
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));
269     return noiseTexture;
270 }
271
272 namespace
273 {
274 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
275 }
276
277 }