Visual Computing Library
Loading...
Searching...
No Matches
cube_atlas.h
1/*
2 * Copyright 2013 Jeremie Roy. All rights reserved.
3 * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
4 */
5
6#ifndef VCL_BGFX_TEXT_FONT_CUBE_ATLAS_H
7#define VCL_BGFX_TEXT_FONT_CUBE_ATLAS_H
8
17
18#include <bgfx/bgfx.h>
19
20namespace bgfx {
21
22struct AtlasRegion
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:
61 Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount = 4096);
62
69 Atlas(uint16_t _textureSize, const uint8_t* _textureBuffer, uint16_t _regionCount, const uint8_t* _regionBuffer, uint16_t _maxRegionsCount = 4096);
70 ~Atlas();
71
73 uint16_t addRegion(uint16_t _width, uint16_t _height, const uint8_t* _bitmapBuffer, AtlasRegion::Type _type = AtlasRegion::TYPE_BGRA8, uint16_t outline = 0);
74
76 void updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffer);
77
88 void packUV(uint16_t _regionHandle, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const;
89 void packUV(const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const;
90
92 void packFaceLayerUV(uint32_t _idx, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const;
93
95 bgfx::TextureHandle getTextureHandle() const
96 {
97 return m_textureHandle;
98 }
99
100 //retrieve a region info
101 const AtlasRegion& getRegion(uint16_t _handle) const
102 {
103 return m_regions[_handle];
104 }
105
107 uint16_t getTextureSize() const
108 {
109 return m_textureSize;
110 }
111
113 //float getUsageRatio() const { return 0.0f; }
114
116 uint16_t getRegionCount() const
117 {
118 return m_regionCount;
119 }
120
122 const AtlasRegion* getRegionBuffer() const
123 {
124 return m_regions;
125 }
126
128 uint32_t getTextureBufferSize() const
129 {
130 return 6 * m_textureSize * m_textureSize * 4;
131 }
132
134 const uint8_t* getTextureBuffer() const
135 {
136 return m_textureBuffer;
137 }
138
139private:
140 struct PackedLayer;
141 PackedLayer* m_layers;
142 AtlasRegion* m_regions;
143 uint8_t* m_textureBuffer;
144
145 uint32_t m_usedLayers;
146 uint32_t m_usedFaces;
147
148 bgfx::TextureHandle m_textureHandle;
149 uint16_t m_textureSize;
150 float m_texelSize;
151
152 uint16_t m_regionCount;
153 uint16_t m_maxRegionCount;
154};
155
156} // namespace bgfx
157
158#endif // VCL_BGFX_TEXT_FONT_CUBE_ATLAS_H