Unravel Engine C++ Reference
Loading...
Searching...
No Matches
cube_atlas.h
Go to the documentation of this file.
1/*
2 * Copyright 2013 Jeremie Roy. All rights reserved.
3 * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
4 */
5
6#ifndef CUBE_ATLAS_H_HEADER_GUARD
7#define CUBE_ATLAS_H_HEADER_GUARD
8
17
18#include <bgfx/bgfx.h>
19
20namespace gfx
21{
23{
24 enum Type
25 {
26 TYPE_GRAY = 1, // 1 component
27 TYPE_BGRA8 = 4 // 4 components
28 };
29
30 uint16_t x, y;
31 uint16_t width, height;
32 uint32_t mask; // encode the region type, the face index and the component index in case of a gray region
33
34 Type getType() const
35 {
36 return (Type)((mask >> 0) & 0x0000000F);
37 }
38
39 uint32_t getFaceIndex() const
40 {
41 return (mask >> 4) & 0x0000000F;
42 }
43
44 uint32_t getComponentIndex() const
45 {
46 return (mask >> 8) & 0x0000000F;
47 }
48
49 void setMask(Type _type, uint32_t _faceIndex, uint32_t _componentIndex)
50 {
51 mask = (_componentIndex << 8) + (_faceIndex << 4) + (uint32_t)_type;
52 }
53};
54
55class Atlas
56{
57public:
62 Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount = 4096);
63
71 Atlas(uint16_t _textureSize,
72 const uint8_t* _textureBuffer,
73 uint16_t _regionCount,
74 const uint8_t* _regionBuffer,
75 uint16_t _maxRegionsCount = 4096);
76 ~Atlas();
77
79 uint16_t addRegion(uint16_t _width,
80 uint16_t _height,
81 const uint8_t* _bitmapBuffer,
83 uint16_t outline = 0);
84
86 void updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffer);
87
99 void packUV(uint16_t _regionHandle, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const;
100 void packUV(const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const;
101
103 void packFaceLayerUV(uint32_t _idx, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const;
104
106 bgfx::TextureHandle getTextureHandle() const
107 {
108 return m_textureHandle;
109 }
110
111 // retrieve a region info
112 const AtlasRegion& getRegion(uint16_t _handle) const
113 {
114 return m_regions[_handle];
115 }
116
118 uint16_t getTextureSize() const
119 {
120 return m_textureSize;
121 }
122
124 // float getUsageRatio() const { return 0.0f; }
125
127 uint16_t getRegionCount() const
128 {
129 return m_regionCount;
130 }
131
134 {
135 return m_regions;
136 }
137
139 uint32_t getTextureBufferSize() const
140 {
141 return 6 * m_textureSize * m_textureSize * 4;
142 }
143
145 const uint8_t* getTextureBuffer() const
146 {
147 return m_textureBuffer;
148 }
149
150private:
151 struct PackedLayer;
152 PackedLayer* m_layers;
153 AtlasRegion* m_regions;
154 uint8_t* m_textureBuffer;
155
156 uint32_t m_usedLayers;
157 uint32_t m_usedFaces;
158
159 bgfx::TextureHandle m_textureHandle;
160 uint16_t m_textureSize;
161 float m_texelSize;
162
163 uint16_t m_regionCount;
164 uint16_t m_maxRegionCount;
165};
166
167}
168#endif // CUBE_ATLAS_H_HEADER_GUARD
void updateRegion(const AtlasRegion &_region, const uint8_t *_bitmapBuffer)
update a preallocated region
uint16_t addRegion(uint16_t _width, uint16_t _height, const uint8_t *_bitmapBuffer, AtlasRegion::Type _type=AtlasRegion::TYPE_BGRA8, uint16_t outline=0)
add a region to the atlas, and copy the content of mem to the underlying texture
const AtlasRegion & getRegion(uint16_t _handle) const
Definition cube_atlas.h:112
uint32_t getTextureBufferSize() const
retrieve the byte size of the texture
Definition cube_atlas.h:139
uint16_t getRegionCount() const
retrieve the usage ratio of the atlas
Definition cube_atlas.h:127
const AtlasRegion * getRegionBuffer() const
retrieve a pointer to the region buffer (in order to serialize it)
Definition cube_atlas.h:133
void packFaceLayerUV(uint32_t _idx, uint8_t *_vertexBuffer, uint32_t _offset, uint32_t _stride) const
Same as packUV but pack a whole face of the atlas cube, mostly used for debugging and visualizing atl...
void packUV(uint16_t _regionHandle, uint8_t *_vertexBuffer, uint32_t _offset, uint32_t _stride) const
uint16_t getTextureSize() const
retrieve the size of side of a texture in pixels
Definition cube_atlas.h:118
Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount=4096)
bgfx::TextureHandle getTextureHandle() const
return the TextureHandle (cube) of the atlas
Definition cube_atlas.h:106
const uint8_t * getTextureBuffer() const
retrieve the mirrored texture buffer (to serialize it)
Definition cube_atlas.h:145
uint16_t height
Definition cube_atlas.h:31
void setMask(Type _type, uint32_t _faceIndex, uint32_t _componentIndex)
Definition cube_atlas.h:49
uint32_t getFaceIndex() const
Definition cube_atlas.h:39
Type getType() const
Definition cube_atlas.h:34
uint32_t getComponentIndex() const
Definition cube_atlas.h:44