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