1 #pragma once
2 
3 #ifndef DRAWABLEMESHIMAGE_H
4 #define DRAWABLEMESHIMAGE_H
5 
6 #include <memory>
7 
8 // TnzExt includes
9 #include "meshtexturizer.h"
10 
11 // TnzCore includes
12 #include "tgldisplaylistsmanager.h"
13 
14 #undef DVAPI
15 #undef DVVAR
16 #ifdef TNZEXT_EXPORTS
17 #define DVAPI DV_EXPORT_API
18 #define DVVAR DV_EXPORT_VAR
19 #else
20 #define DVAPI DV_IMPORT_API
21 #define DVVAR DV_IMPORT_VAR
22 #endif
23 
24 //***************************************************************************************
25 //    DrawableMeshImage definition
26 //***************************************************************************************
27 
28 //! DrawableTextureData is a MeshTexturizer::TextureData wrapper that
29 struct DrawableTextureData {
30   MeshTexturizer::TextureData *m_textureData;  //!< The wrapped texture data
31 
32 public:
DrawableTextureDataDrawableTextureData33   DrawableTextureData() {}
34   ~DrawableTextureData();
35 
36 private:
37   friend class TTexturesStorage;
38 
39   int m_texId;      //!< The texture Id in MeshTexturizer
40   int m_dlSpaceId;  //!< Object's display lists id
41   int m_objIdx;     //!< Object index in the container
42 
43 private:
44   // Not copyable
45 
46   DrawableTextureData(const DrawableTextureData &);
47   DrawableTextureData &operator=(const DrawableTextureData &);
48 };
49 
50 typedef std::shared_ptr<DrawableTextureData> DrawableTextureDataP;
51 
52 //***************************************************************************************
53 //    TexturesStorage declaration
54 //***************************************************************************************
55 
56 /*!
57   \brief    TTexturesStorage is the class responsible for the storage of
58   textures associated with mesh rendering.
59 
60   \details  This class deals with textures storage and meshes compilation for
61   drawing purposes.
62             An OpenGL texture is typically split into several tiles before being
63   rendered. A texturized mesh is then
64             compiled against a texture in order to decide which mesh faces must
65   be rendered with a given texture
66             tile.
67 
68             The TTextureStorage class uses a QCache to store texture objects up
69   to a maximum memory size, plus all
70             the already compiled mesh objects against the stored textures.
71   Whenever a texture loading procedure would
72             exceed the maximum textures size, the least accessed stored textures
73   are automatically removed from the
74             storage to make space for the newly assigned one.
75 
76             Textures can be \a stored only if the OpenGL context they are loaded
77   on has a valid known <I>display lists
78             proxy</I> submitted in the TGLDisplayListsManager. In case the
79   OpenGL context does not have a proxy, the
80             loaded texture is a temporary, and will be deleted as soon as the
81   returned TexturObject is destroyed
82             (so, make sure the associated context is still current at that
83   point).
84 
85   \note     TTexturesStorage is thread-safe.
86 
87   \sa       The TGLDisplayListsManager class.
88 */
89 
90 class DVAPI TTexturesStorage : private TGLDisplayListsManager::Observer {
91 public:
92   static TTexturesStorage *instance();
93 
94   /*!
95 \brief    Stores the specified raster to a group of OpenGL textures, returning a
96 reference
97         pointer ensuring the texture survival during its lifetime.
98 
99 \warning  This class may keep a copy of the specified texture only for a \b
100 limited
101         amount of time, \a if the current context has an associated display
102 lists
103         space proxy. Users must always be ready to \a reload the texture in case
104         it was not found.
105 */
106   DrawableTextureDataP loadTexture(const std::string &textureId,
107                                    const TRaster32P &ras,
108                                    const TRectD &geometry);
109 
110   //! Releases the texture associated with the specified texture id.
111   void unloadTexture(const std::string &textureId);
112 
113   //! Returns the texture data associated to the specified string identifier.
114   DrawableTextureDataP getTextureData(const std::string &textureId);
115 
116 private:
117   TTexturesStorage();
118   ~TTexturesStorage();
119 
120   void onDisplayListDestroyed(int dlSpaceId) override;
121 };
122 
123 #endif  // DRAWABLEMESHIMAGE_H
124