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