Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef MESHTEXTURIZER_H
Toshihiro Shimizu 890ddd
#define MESHTEXTURIZER_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "traster.h"
Toshihiro Shimizu 890ddd
#include "tgl.h" // OpenGL includes
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// STL includes
Toshihiro Shimizu 890ddd
#include <vector></vector>
Toshihiro Shimizu 890ddd
#include <deque></deque>
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
//    MeshTexturizer  declaration
Toshihiro Shimizu 890ddd
//******************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  \brief    The MeshTexturizer class acts as a management wrapper to OpenGL texturing
Toshihiro Shimizu 890ddd
            capabilities, in particular concerning texture virtualization.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  \details  The first step in OpenGL texturing requires copying a texture into the video ram
Toshihiro Shimizu 890ddd
            (\a VRAM). Now, OpenGL documentation specifies that, although there is no limit
Toshihiro Shimizu 890ddd
            to the \a number of allowed textures, there \a are limits to the \b size of said
Toshihiro Shimizu 890ddd
            textures, which can be tested by asking OpenGL whether it can accomodate specific
Toshihiro Shimizu 890ddd
            texture requests or not.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
            Technically, the MeshTexturizer class acts as a virtualization manager that can be
Toshihiro Shimizu 890ddd
            used to allow texturization with arbitrary texture sizes. The manager accepts raster
Toshihiro Shimizu 890ddd
            images that are cut up into tiles and bound to VRAM.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
            Please, observe that the number of texture objects is typically not an issue, as
Toshihiro Shimizu 890ddd
            OpenGL is able to swap texture objects between RAM and VRAM, depending on which
Toshihiro Shimizu 890ddd
            textures have been used most frequently. In other words, since MeshTexturizer ensures
Toshihiro Shimizu 890ddd
            the granularity required by OpenGL, textures can be used until the process runs out of
Toshihiro Shimizu 890ddd
            memory - of course, swapping can be a source of slowdowns, though.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  \remark   Subdivision into tiles is designed to work for both premultiplied \a and
Toshihiro Shimizu 890ddd
            non-premultiplied textures.
Toshihiro Shimizu 890ddd
  \remark   MeshTexturizer instances are thread-safe.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI MeshTexturizer
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	struct TextureData;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	enum PremultMode //! Premultiplication specifier for texture binding.
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		NONPREMULTIPLIED, //!< Texture is not premultiplied.
Toshihiro Shimizu 890ddd
		PREMULTIPLIED,	//!< Texture is premultiplied.
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	MeshTexturizer();
Toshihiro Shimizu 890ddd
	~MeshTexturizer();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*! \details  The bindTexture() function is used to specify a 32-bit fullcolor
Toshihiro Shimizu 890ddd
                raster as a texture to be used for mesh rendering. The supplied
Toshihiro Shimizu 890ddd
                geometry rect defines its position with respect to compiled meshes.
Toshihiro Shimizu 890ddd
      \remark   The premultiplication specifier is used to tell the texturizer how
Toshihiro Shimizu 890ddd
                to add a proper transparent border surrounding texture tiles.
Toshihiro Shimizu 890ddd
                This border is essential to draw transparent pixels beyond a texture
Toshihiro Shimizu 890ddd
                tile's content.
Toshihiro Shimizu 890ddd
      \returns  The internal id used to reference to the input raster.                        */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int bindTexture(const TRaster32P &ras, const TRectD &geometry,   //!< Moves a \a copy of the supplied raster image to VRAM,
Toshihiro Shimizu 890ddd
					PremultMode premultiplyMode = NONPREMULTIPLIED); //!  and assigns an identification number to it.                \param ras  Texture to be copied to VRAM.  \param geometry  The texture's geometry in the world - with respect to compiled meshes.  \param premultiplyMode  Specified whether ras is to be intended premultiplied or not.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*! \warning  The pointer returned by getTextureData() is owned by this class -
Toshihiro Shimizu 890ddd
                it must not be deleted.                                                       */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TextureData *getTextureData(int textureId); //!< Retrieves the texture data built from a previously
Toshihiro Shimizu 890ddd
												//!  bound texture image.                                       \param textureId  Identifier of the texture to be retrieved  \returns See summary.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*! \details  The texture identifier specified to rebindTexture() is retained,
Toshihiro Shimizu 890ddd
                and the effect is that of an unbindTexture() - bindTexture() combo.
Toshihiro Shimizu 890ddd
      \note     Pre-compiled mesh data against the old texture can be retained \b if
Toshihiro Shimizu 890ddd
                the newly supplied texture size and geometry are the \b same as the old one.  */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void rebindTexture(int textureId,
Toshihiro Shimizu 890ddd
					   const TRaster32P &ras, const TRectD &geometry,   //!< Rebinds the image bound to the specified texture
Toshihiro Shimizu 890ddd
					   PremultMode premultiplyMode = NONPREMULTIPLIED); //!  id to a new image.
Toshihiro Shimizu 890ddd
	void unbindTexture(int textureId);									//!< Deletes the texture associated to the passed id.           \param textureId  Identifier of the texture to be unbound.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	class Imp;
Toshihiro Shimizu 890ddd
	Imp *m_imp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	// Not copyable
Toshihiro Shimizu 890ddd
	MeshTexturizer(const MeshTexturizer &);
Toshihiro Shimizu 890ddd
	MeshTexturizer &operator=(const MeshTexturizer &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//******************************************************************************************
Toshihiro Shimizu 890ddd
//    MeshTexturizer:::CompiledData  definition
Toshihiro Shimizu 890ddd
//******************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  \brief    The MeshTexturizer-owned data about a texture.
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
  \details  The purpose of MeshTexturizer is that of storing in VRAM a set of textures
Toshihiro Shimizu 890ddd
            cut up into tiles. This struture holds the data about the end-product of
Toshihiro Shimizu 890ddd
            texture loading into a MeshTexturizer instance.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
struct MeshTexturizer::TextureData {
Toshihiro Shimizu 890ddd
	struct TileData //!  Data structure for a texture tile.
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
		GLuint m_textureId;	//!< OpenGL texture identifier.
Toshihiro Shimizu 890ddd
		TRectD m_tileGeometry; //!< The tile's world geometry.
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TRectD m_geom;					   //!< The original texture's world geometry.
Toshihiro Shimizu 890ddd
	std::vector<tiledata> m_tileDatas; //!< The texture tiles the original texture was split into.</tiledata>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TextureData() {}
Toshihiro Shimizu 890ddd
	TextureData(const TRectD &geom) : m_geom(geom) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~TextureData()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		int t, tilesCount = m_tileDatas.size();
Toshihiro Shimizu 890ddd
		for (t = 0; t < tilesCount; ++t)
Toshihiro Shimizu 890ddd
			glDeleteTextures(1, &m_tileDatas[t].m_textureId);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	// Not copyable
Toshihiro Shimizu 890ddd
	TextureData(const TextureData &);
Toshihiro Shimizu 890ddd
	TextureData &operator=(const TextureData &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif // MESHTEXTURIZER_H