You.i Engine
YiTextureAtlas.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2020. All rights reserved.
2 #ifndef _YI_TEXTURE_ATLAS_H_
3 #define _YI_TEXTURE_ATLAS_H_
4 
5 /*****************************************************************************
6  Includes
7 ****************************************************************************/
8 #include "asset/YiAssetTexture.h"
9 #include "graphics/YiBitmap.h"
10 #include <glm/gtc/type_ptr.hpp>
11 
20 typedef struct
21 {
23  void *pReference;
24  size_t page;
25  int32_t startX;
26  int32_t startY;
27  int32_t width;
28  int32_t height;
29  float UV_S0;
30  float UV_T0;
31  float UV_S1;
32  float UV_T1;
33  uint32_t refCount;
35 
37 typedef struct
38 {
39  std::vector<YI_TEXTURE_DETAILS> Textures; /* Vector of textures */
41 
42 typedef struct
43 {
45  size_t page;
46  bool valid;
47 } YI_REGION;
48 
54 {
55 public:
57  virtual ~CYITextureAtlas();
58 
59  bool CreateNewAtlas(uint32_t unWidth, uint32_t unHeight, size_t pages);
60 
61  /* Returns a region allocation based on a requested size
62  The atlas can hold anything, this method is used if
63  you are using the regioning for packing data. */
64  YI_REGION GetRegion(int32_t width, int32_t height);
65 
66  //Set pixels and references in a region allocation based on a requested size
67  virtual void SetRegion(ssize_t ID, void *pReference, YI_REGION Region, const uint8_t *data, const int32_t stride);
68 
69  //Get the details of a texture region from the atlas. Increments the use count
70  virtual YI_TEXTURE_DETAILS *GetTexture(ssize_t ID, void *pReference);
71 
72  //Update the atlas, forces a GPU update
73  void UpdateAtlas();
74 
75  //Get the atlas bitmap image
76  const std::shared_ptr<CYIBitmap> GetBitmap() const;
77 
78  //Get the atlas node array
79  const std::vector<glm::ivec4> *GetNodes(size_t page) const;
80 
81  //Get the GPU texture for the atlas
82  virtual std::shared_ptr<CYIAssetTexture> GetGLTexture() const;
83 
84  const std::shared_ptr<CYIAssetTexture> GetTextureAsset()
85  {
86  return m_pTextureAsset;
87  }
88 
89  //Get the array of textures in the atlas
91 
92  void SetTextureFilter(CYIAssetTexture::Filtering minificationFilter, CYIAssetTexture::Filtering magnificationFilter);
93 
95 
96  void SetName(const CYIString &name);
97  const CYIString &GetName() const;
98 
104  float GetAtlasPercentageUsed() const;
105 
110  bool RenderAtlasToPng(const CYIString &filePath) const;
111 
115  void Reset();
116 
117 protected:
118  int32_t Fit(size_t page, size_t unIndex, int32_t width, int32_t height);
119  void Merge(size_t page);
120 
122  std::shared_ptr<CYIBitmap> m_Image;
123  std::vector<glm::ivec4> m_Nodes[4];
124  std::shared_ptr<CYIAssetTexture> m_pTextureAsset;
126  int32_t m_width;
127  int32_t m_height;
128  size_t m_pages;
132 };
133 
136 #endif // _YI_TEXTURE_ATLAS_H_
int32_t Fit(size_t page, size_t unIndex, int32_t width, int32_t height)
virtual ~CYITextureAtlas()
uint32_t refCount
Definition: YiTextureAtlas.h:33
Definition: YiAssetTextureBase.h:65
std::shared_ptr< CYIBitmap > m_Image
Definition: YiTextureAtlas.h:122
virtual std::shared_ptr< CYIAssetTexture > GetGLTexture() const
Definition: YiPredef.h:291
Definition: YiTextureAtlas.h:37
void * pReference
Definition: YiTextureAtlas.h:23
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:36
float UV_T0
Definition: YiTextureAtlas.h:30
Filtering
Definition: YiAssetTextureBase.h:30
Class for a texture atlas.
Definition: YiTextureAtlas.h:53
float GetAtlasPercentageUsed() const
YI_REGION GetRegion(int32_t width, int32_t height)
Definition: YiTextureAtlas.h:42
virtual YI_TEXTURE_DETAILS * GetTexture(ssize_t ID, void *pReference)
size_t m_unLastUpdatedSize
Definition: YiTextureAtlas.h:131
void SetName(const CYIString &name)
const std::shared_ptr< CYIAssetTexture > GetTextureAsset()
Definition: YiTextureAtlas.h:84
float UV_S1
Definition: YiTextureAtlas.h:31
bool CreateNewAtlas(uint32_t unWidth, uint32_t unHeight, size_t pages)
YI_RECT_REL Area
Definition: YiTextureAtlas.h:44
ssize_t ID
Definition: YiTextureAtlas.h:22
CYIString m_name
Definition: YiTextureAtlas.h:121
const std::shared_ptr< CYIBitmap > GetBitmap() const
virtual void SetRegion(ssize_t ID, void *pReference, YI_REGION Region, const uint8_t *data, const int32_t stride)
const std::vector< glm::ivec4 > * GetNodes(size_t page) const
size_t m_unPixelsUsed
Definition: YiTextureAtlas.h:130
int32_t startY
Definition: YiTextureAtlas.h:26
int32_t height
Definition: YiTextureAtlas.h:28
const CYIString & GetName() const
size_t m_pages
Definition: YiTextureAtlas.h:128
YI_LOADED_TEXTURES m_Textures
Definition: YiTextureAtlas.h:125
int32_t startX
Definition: YiTextureAtlas.h:25
bool RenderAtlasToPng(const CYIString &filePath) const
YI_LOADED_TEXTURES * GetLoadedTextures()
size_t page
Definition: YiTextureAtlas.h:45
int ssize_t
Definition: YiPredefWin32.h:131
std::vector< glm::ivec4 > m_Nodes[4]
Definition: YiTextureAtlas.h:123
int32_t m_width
Definition: YiTextureAtlas.h:126
CYIAssetTexture::TextureConfig m_textureConfiguration
Definition: YiTextureAtlas.h:129
std::shared_ptr< CYIAssetTexture > m_pTextureAsset
Definition: YiTextureAtlas.h:124
int32_t m_height
Definition: YiTextureAtlas.h:127
size_t page
Definition: YiTextureAtlas.h:24
void SetTextureFilter(CYIAssetTexture::Filtering minificationFilter, CYIAssetTexture::Filtering magnificationFilter)
float UV_S0
Definition: YiTextureAtlas.h:29
std::vector< YI_TEXTURE_DETAILS > Textures
Definition: YiTextureAtlas.h:39
bool valid
Definition: YiTextureAtlas.h:46
Definition: YiTextureAtlas.h:20
void SetTextureWrap(CYIAssetTexture::WrapMode wrapS, CYIAssetTexture::WrapMode wrapT)
int32_t width
Definition: YiTextureAtlas.h:27
float UV_T1
Definition: YiTextureAtlas.h:32
void Merge(size_t page)
WrapMode
Definition: YiAssetTextureBase.h:45