]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/model.cxx
model paging patch from Till Busch
[simgear.git] / simgear / scene / model / model.cxx
1 // model.cxx - manage a 3D aircraft model.
2 // Written by David Megginson, started 2002.
3 //
4 // This file is in the Public Domain, and comes with no warranty.
5
6 #ifdef HAVE_CONFIG_H
7 #include <simgear_config.h>
8 #endif
9
10 #include <osg/ref_ptr>
11 #include <osgDB/ReadFile>
12 #include <osgDB/SharedStateManager>
13
14 #include <simgear/scene/util/SGSceneFeatures.hxx>
15
16 #include <simgear/structure/exception.hxx>
17 #include <simgear/props/props.hxx>
18 #include <simgear/props/props_io.hxx>
19 #include <simgear/props/condition.hxx>
20
21 #include "model.hxx"
22
23 SG_USING_STD(vector);
24
25 osg::Texture2D*
26 SGLoadTexture2D(bool staticTexture, const std::string& path,
27                 const osgDB::ReaderWriter::Options* options,
28                 bool wrapu, bool wrapv, int)
29 {
30   osg::Image* image;
31   if (options)
32     image = osgDB::readImageFile(path, options);
33   else
34     image = osgDB::readImageFile(path);
35   osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
36   texture->setImage(image);
37   if (staticTexture)
38     texture->setDataVariance(osg::Object::STATIC);
39   if (wrapu)
40     texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
41   else
42     texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
43   if (wrapv)
44     texture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
45   else
46     texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
47
48   if (image) {
49     int s = image->s();
50     int t = image->t();
51
52     if (s <= t && 32 <= s) {
53       SGSceneFeatures::instance()->setTextureCompression(texture.get());
54     } else if (t < s && 32 <= t) {
55       SGSceneFeatures::instance()->setTextureCompression(texture.get());
56     }
57   }
58
59   // Make sure the texture is shared if we already have the same texture
60   // somewhere ...
61   {
62     osg::ref_ptr<osg::Node> tmpNode = new osg::Node;
63     osg::StateSet* stateSet = tmpNode->getOrCreateStateSet();
64     stateSet->setTextureAttribute(0, texture.get());
65
66     // OSGFIXME: don't forget that mutex here
67     osgDB::Registry* registry = osgDB::Registry::instance();
68     registry->getSharedStateManager()->share(tmpNode.get(), 0);
69
70     // should be the same, but be paranoid ...
71     stateSet = tmpNode->getStateSet();
72     osg::StateAttribute* stateAttr;
73     stateAttr = stateSet->getTextureAttribute(0, osg::StateAttribute::TEXTURE);
74     osg::Texture2D* texture2D = dynamic_cast<osg::Texture2D*>(stateAttr);
75     if (texture2D)
76       texture = texture2D;
77   }
78
79   return texture.release();
80 }
81
82 // end of model.cxx