Shinya Kitaoka 810553
#pragma once
Shinya Kitaoka 810553
Toshihiro Shimizu 890ddd
#ifndef DRAWABLEMESHIMAGE_H
Toshihiro Shimizu 890ddd
#define DRAWABLEMESHIMAGE_H
Toshihiro Shimizu 890ddd
Shinya Kitaoka e5734a
#include <memory></memory>
Shinya Kitaoka e5734a
Toshihiro Shimizu 890ddd
// TnzExt includes
Toshihiro Shimizu 890ddd
#include "meshtexturizer.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tgldisplaylistsmanager.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TNZEXT_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***************************************************************************************
Toshihiro Shimizu 890ddd
//    DrawableMeshImage definition
Toshihiro Shimizu 890ddd
//***************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! DrawableTextureData is a MeshTexturizer::TextureData wrapper that
Toshihiro Shimizu 890ddd
struct DrawableTextureData {
Shinya Kitaoka 120a6e
  MeshTexturizer::TextureData *m_textureData;  //!< The wrapped texture data
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  DrawableTextureData() {}
Shinya Kitaoka 120a6e
  ~DrawableTextureData();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  friend class TTexturesStorage;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int m_texId;      //!< The texture Id in MeshTexturizer
Shinya Kitaoka 120a6e
  int m_dlSpaceId;  //!< Object's display lists id
Shinya Kitaoka 120a6e
  int m_objIdx;     //!< Object index in the container
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  // Not copyable
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  DrawableTextureData(const DrawableTextureData &);
Shinya Kitaoka 120a6e
  DrawableTextureData &operator=(const DrawableTextureData &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka e5734a
typedef std::shared_ptr<drawabletexturedata> DrawableTextureDataP;</drawabletexturedata>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***************************************************************************************
Toshihiro Shimizu 890ddd
//    TexturesStorage declaration
Toshihiro Shimizu 890ddd
//***************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Shinya Kitaoka 120a6e
  \brief    TTexturesStorage is the class responsible for the storage of
Shinya Kitaoka 120a6e
  textures associated with mesh rendering.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  \details  This class deals with textures storage and meshes compilation for
Shinya Kitaoka 120a6e
  drawing purposes.
Shinya Kitaoka 120a6e
            An OpenGL texture is typically split into several tiles before being
Shinya Kitaoka 120a6e
  rendered. A texturized mesh is then
Shinya Kitaoka 120a6e
            compiled against a texture in order to decide which mesh faces must
Shinya Kitaoka 120a6e
  be rendered with a given texture
Toshihiro Shimizu 890ddd
            tile.
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
            The TTextureStorage class uses a QCache to store texture objects up
Shinya Kitaoka 120a6e
  to a maximum memory size, plus all
Shinya Kitaoka 120a6e
            the already compiled mesh objects against the stored textures.
Shinya Kitaoka 120a6e
  Whenever a texture loading procedure would
Shinya Kitaoka 120a6e
            exceed the maximum textures size, the least accessed stored textures
Shinya Kitaoka 120a6e
  are automatically removed from the
Toshihiro Shimizu 890ddd
            storage to make space for the newly assigned one.
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
            Textures can be \a stored only if the OpenGL context they are loaded
Shinya Kitaoka 120a6e
  on has a valid known display lists
Shinya Kitaoka 120a6e
            proxy submitted in the TGLDisplayListsManager. In case the
Shinya Kitaoka 120a6e
  OpenGL context does not have a proxy, the
Shinya Kitaoka 120a6e
            loaded texture is a temporary, and will be deleted as soon as the
Shinya Kitaoka 120a6e
  returned TexturObject is destroyed
Shinya Kitaoka 120a6e
            (so, make sure the associated context is still current at that
Shinya Kitaoka 120a6e
  point).
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  \note     TTexturesStorage is thread-safe.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  \sa       The TGLDisplayListsManager class.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI TTexturesStorage : private TGLDisplayListsManager::Observer {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  static TTexturesStorage *instance();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*!
Shinya Kitaoka 120a6e
\brief    Stores the specified raster to a group of OpenGL textures, returning a
Shinya Kitaoka 120a6e
reference
Shinya Kitaoka 120a6e
        pointer ensuring the texture survival during its lifetime.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
\warning  This class may keep a copy of the specified texture only for a \b
Shinya Kitaoka 120a6e
limited
Shinya Kitaoka 120a6e
        amount of time, \a if the current context has an associated display
Shinya Kitaoka 120a6e
lists
Shinya Kitaoka 120a6e
        space proxy. Users must always be ready to \a reload the texture in case
Shinya Kitaoka 120a6e
        it was not found.
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  DrawableTextureDataP loadTexture(const std::string &textureId,
Shinya Kitaoka 120a6e
                                   const TRaster32P &ras,
Shinya Kitaoka 120a6e
                                   const TRectD &geometry);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //! Releases the texture associated with the specified texture id.
Shinya Kitaoka 120a6e
  void unloadTexture(const std::string &textureId);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //! Returns the texture data associated to the specified string identifier.
Shinya Kitaoka 120a6e
  DrawableTextureDataP getTextureData(const std::string &textureId);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  TTexturesStorage();
Shinya Kitaoka 120a6e
  ~TTexturesStorage();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void onDisplayListDestroyed(int dlSpaceId) override;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // DRAWABLEMESHIMAGE_H