update cube_atlas to bgfx naming style
This commit is contained in:
parent
1f5db2d7db
commit
6b8b95acd8
@ -12,13 +12,13 @@ class RectanglePacker
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RectanglePacker();
|
RectanglePacker();
|
||||||
RectanglePacker(uint32_t width, uint32_t height);
|
RectanglePacker(uint32_t _width, uint32_t _height);
|
||||||
|
|
||||||
/// non constructor initialization
|
/// non constructor initialization
|
||||||
void init(uint32_t width, uint32_t height);
|
void init(uint32_t _width, uint32_t _height);
|
||||||
/// find a suitable position for the given rectangle
|
/// find a suitable position for the given rectangle
|
||||||
/// @return true if the rectangle can be added, false otherwise
|
/// @return true if the rectangle can be added, false otherwise
|
||||||
bool addRectangle(uint16_t width, uint16_t height, uint16_t& outX, uint16_t& outY );
|
bool addRectangle(uint16_t _width, uint16_t _height, uint16_t& _outX, uint16_t& _outY );
|
||||||
/// return the used surface in squared unit
|
/// return the used surface in squared unit
|
||||||
uint32_t getUsedSurface() { return m_usedSpace; }
|
uint32_t getUsedSurface() { return m_usedSpace; }
|
||||||
/// return the total available surface in squared unit
|
/// return the total available surface in squared unit
|
||||||
@ -29,25 +29,25 @@ public:
|
|||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int32_t fit(uint32_t skylineNodeIndex, uint16_t width, uint16_t height);
|
int32_t fit(uint32_t _skylineNodeIndex, uint16_t _width, uint16_t _height);
|
||||||
/// Merges all skyline nodes that are at the same level.
|
/// Merges all skyline nodes that are at the same level.
|
||||||
void merge();
|
void merge();
|
||||||
|
|
||||||
struct Node
|
struct Node
|
||||||
{
|
{
|
||||||
Node(int16_t _x, int16_t _y, int16_t _width):x(_x), y(_y), width(_width) {}
|
Node(int16_t _x, int16_t _y, int16_t _width):m_x(_x), m_y(_y), m_width(_width) {}
|
||||||
|
|
||||||
/// The starting x-coordinate (leftmost).
|
/// The starting x-coordinate (leftmost).
|
||||||
int16_t x;
|
int16_t m_x;
|
||||||
/// The y-coordinate of the skyline level line.
|
/// The y-coordinate of the skyline level line.
|
||||||
int16_t y;
|
int16_t m_y;
|
||||||
/// The line width. The ending coordinate (inclusive) will be x+width-1.
|
/// The line _width. The ending coordinate (inclusive) will be x+width-1.
|
||||||
int32_t width; //32bit to avoid padding
|
int32_t m_width; //32bit to avoid padding
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Width (in pixels) of the underlying texture
|
/// width (in pixels) of the underlying texture
|
||||||
uint32_t m_width;
|
uint32_t m_width;
|
||||||
/// Height (in pixels) of the underlying texture
|
/// height (in pixels) of the underlying texture
|
||||||
uint32_t m_height;
|
uint32_t m_height;
|
||||||
/// Surface used in squared pixel
|
/// Surface used in squared pixel
|
||||||
uint32_t m_usedSpace;
|
uint32_t m_usedSpace;
|
||||||
@ -59,35 +59,35 @@ RectanglePacker::RectanglePacker(): m_width(0), m_height(0), m_usedSpace(0)
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
RectanglePacker::RectanglePacker(uint32_t width, uint32_t height):m_width(width), m_height(height), m_usedSpace(0)
|
RectanglePacker::RectanglePacker(uint32_t _width, uint32_t _height):m_width(_width), m_height(_height), m_usedSpace(0)
|
||||||
{
|
{
|
||||||
// We want a one pixel border around the whole atlas to avoid any artefact when
|
// We want a one pixel border around the whole atlas to avoid any artefact when
|
||||||
// sampling texture
|
// sampling texture
|
||||||
m_skyline.push_back(Node(1,1, width-2));
|
m_skyline.push_back(Node(1,1, _width-2));
|
||||||
}
|
}
|
||||||
|
|
||||||
void RectanglePacker::init(uint32_t width, uint32_t height)
|
void RectanglePacker::init(uint32_t _width, uint32_t _height)
|
||||||
{
|
{
|
||||||
assert(width > 2);
|
assert(_width > 2);
|
||||||
assert(height > 2);
|
assert(_height > 2);
|
||||||
m_width = width;
|
m_width = _width;
|
||||||
m_height = height;
|
m_height = _height;
|
||||||
m_usedSpace = 0;
|
m_usedSpace = 0;
|
||||||
|
|
||||||
m_skyline.clear();
|
m_skyline.clear();
|
||||||
// We want a one pixel border around the whole atlas to avoid any artifact when
|
// We want a one pixel border around the whole atlas to avoid any artifact when
|
||||||
// sampling texture
|
// sampling texture
|
||||||
m_skyline.push_back(Node(1,1, width-2));
|
m_skyline.push_back(Node(1,1, _width-2));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RectanglePacker::addRectangle(uint16_t width, uint16_t height, uint16_t& outX, uint16_t& outY)
|
bool RectanglePacker::addRectangle(uint16_t _width, uint16_t _height, uint16_t& _outX, uint16_t& _outY)
|
||||||
{
|
{
|
||||||
int y, best_height, best_index;
|
int y, best_height, best_index;
|
||||||
int32_t best_width;
|
int32_t best_width;
|
||||||
Node* node;
|
Node* node;
|
||||||
Node* prev;
|
Node* prev;
|
||||||
outX = 0;
|
_outX = 0;
|
||||||
outY = 0;
|
_outY = 0;
|
||||||
|
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
@ -96,18 +96,18 @@ bool RectanglePacker::addRectangle(uint16_t width, uint16_t height, uint16_t& ou
|
|||||||
best_width = INT_MAX;
|
best_width = INT_MAX;
|
||||||
for( i = 0; i < m_skyline.size(); ++i )
|
for( i = 0; i < m_skyline.size(); ++i )
|
||||||
{
|
{
|
||||||
y = fit( i, width, height );
|
y = fit( i, _width, _height );
|
||||||
if( y >= 0 )
|
if( y >= 0 )
|
||||||
{
|
{
|
||||||
node = &m_skyline[i];
|
node = &m_skyline[i];
|
||||||
if( ( (y + height) < best_height ) ||
|
if( ( (y + _height) < best_height ) ||
|
||||||
( ((y + height) == best_height) && (node->width < best_width)) )
|
( ((y + _height) == best_height) && (node->m_width < best_width)) )
|
||||||
{
|
{
|
||||||
best_height = y + height;
|
best_height = y + _height;
|
||||||
best_index = i;
|
best_index = i;
|
||||||
best_width = node->width;
|
best_width = node->m_width;
|
||||||
outX = node->x;
|
_outX = node->m_x;
|
||||||
outY = y;
|
_outY = y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -117,19 +117,19 @@ bool RectanglePacker::addRectangle(uint16_t width, uint16_t height, uint16_t& ou
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node newNode(outX,outY + height, width);
|
Node newNode(_outX, _outY + _height, _width);
|
||||||
m_skyline.insert(m_skyline.begin() + best_index, newNode);
|
m_skyline.insert(m_skyline.begin() + best_index, newNode);
|
||||||
|
|
||||||
for(i = best_index+1; i < m_skyline.size(); ++i)
|
for(i = best_index+1; i < m_skyline.size(); ++i)
|
||||||
{
|
{
|
||||||
node = &m_skyline[i];
|
node = &m_skyline[i];
|
||||||
prev = &m_skyline[i-1];
|
prev = &m_skyline[i-1];
|
||||||
if (node->x < (prev->x + prev->width) )
|
if (node->m_x < (prev->m_x + prev->m_width) )
|
||||||
{
|
{
|
||||||
int shrink = prev->x + prev->width - node->x;
|
int shrink = prev->m_x + prev->m_width - node->m_x;
|
||||||
node->x += shrink;
|
node->m_x += shrink;
|
||||||
node->width -= shrink;
|
node->m_width -= shrink;
|
||||||
if (node->width <= 0)
|
if (node->m_width <= 0)
|
||||||
{
|
{
|
||||||
m_skyline.erase(m_skyline.begin() + i);
|
m_skyline.erase(m_skyline.begin() + i);
|
||||||
--i;
|
--i;
|
||||||
@ -146,7 +146,7 @@ bool RectanglePacker::addRectangle(uint16_t width, uint16_t height, uint16_t& ou
|
|||||||
}
|
}
|
||||||
|
|
||||||
merge();
|
merge();
|
||||||
m_usedSpace += width * height;
|
m_usedSpace += _width * _height;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,34 +169,34 @@ void RectanglePacker::clear()
|
|||||||
m_skyline.push_back(Node(1,1, m_width-2));
|
m_skyline.push_back(Node(1,1, m_width-2));
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t RectanglePacker::fit(uint32_t skylineNodeIndex, uint16_t _width, uint16_t _height)
|
int32_t RectanglePacker::fit(uint32_t _skylineNodeIndex, uint16_t _width, uint16_t _height)
|
||||||
{
|
{
|
||||||
int32_t width = _width;
|
int32_t width = _width;
|
||||||
int32_t height = _height;
|
int32_t height = _height;
|
||||||
|
|
||||||
const Node& baseNode = m_skyline[skylineNodeIndex];
|
const Node& baseNode = m_skyline[_skylineNodeIndex];
|
||||||
|
|
||||||
int32_t x = baseNode.x, y;
|
int32_t x = baseNode.m_x, y;
|
||||||
int32_t width_left = width;
|
int32_t _width_left = width;
|
||||||
int32_t i = skylineNodeIndex;
|
int32_t i = _skylineNodeIndex;
|
||||||
|
|
||||||
if ( (x + width) > (int32_t)(m_width-1) )
|
if ( (x + width) > (int32_t)(m_width-1) )
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
y = baseNode.y;
|
y = baseNode.m_y;
|
||||||
while( width_left > 0 )
|
while( _width_left > 0 )
|
||||||
{
|
{
|
||||||
const Node& node = m_skyline[i];
|
const Node& node = m_skyline[i];
|
||||||
if( node.y > y )
|
if( node.m_y > y )
|
||||||
{
|
{
|
||||||
y = node.y;
|
y = node.m_y;
|
||||||
}
|
}
|
||||||
if( (y + height) > (int32_t)(m_height-1) )
|
if( (y + height) > (int32_t)(m_height-1) )
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
width_left -= node.width;
|
_width_left -= node.m_width;
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
return y;
|
return y;
|
||||||
@ -212,9 +212,9 @@ void RectanglePacker::merge()
|
|||||||
{
|
{
|
||||||
node = (Node *) &m_skyline[i];
|
node = (Node *) &m_skyline[i];
|
||||||
next = (Node *) &m_skyline[i+1];
|
next = (Node *) &m_skyline[i+1];
|
||||||
if( node->y == next->y )
|
if( node->m_y == next->m_y )
|
||||||
{
|
{
|
||||||
node->width += next->width;
|
node->m_width += next->m_width;
|
||||||
m_skyline.erase(m_skyline.begin() + i + 1);
|
m_skyline.erase(m_skyline.begin() + i + 1);
|
||||||
--i;
|
--i;
|
||||||
}
|
}
|
||||||
@ -229,24 +229,24 @@ struct Atlas::PackedLayer
|
|||||||
AtlasRegion faceRegion;
|
AtlasRegion faceRegion;
|
||||||
};
|
};
|
||||||
|
|
||||||
Atlas::Atlas(uint16_t textureSize, uint16_t maxRegionsCount )
|
Atlas::Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount )
|
||||||
{
|
{
|
||||||
assert(textureSize >= 64 && textureSize <= 4096 && "suspicious texture size" );
|
assert(_textureSize >= 64 && _textureSize <= 4096 && "suspicious texture size" );
|
||||||
assert(maxRegionsCount >= 64 && maxRegionsCount <= 32000 && "suspicious regions count" );
|
assert(_maxRegionsCount >= 64 && _maxRegionsCount <= 32000 && "suspicious _regions count" );
|
||||||
m_layers = new PackedLayer[24];
|
m_layers = new PackedLayer[24];
|
||||||
for(int i=0; i<24;++i)
|
for(int i=0; i<24;++i)
|
||||||
{
|
{
|
||||||
m_layers[i].packer.init(textureSize, textureSize);
|
m_layers[i].packer.init(_textureSize, _textureSize);
|
||||||
}
|
}
|
||||||
m_usedLayers = 0;
|
m_usedLayers = 0;
|
||||||
m_usedFaces = 0;
|
m_usedFaces = 0;
|
||||||
|
|
||||||
m_textureSize = textureSize;
|
m_textureSize = _textureSize;
|
||||||
m_regionCount = 0;
|
m_regionCount = 0;
|
||||||
m_maxRegionCount = maxRegionsCount;
|
m_maxRegionCount = _maxRegionsCount;
|
||||||
m_regions = new AtlasRegion[maxRegionsCount];
|
m_regions = new AtlasRegion[_maxRegionsCount];
|
||||||
m_textureBuffer = new uint8_t[ textureSize * textureSize * 6 * 4 ];
|
m_textureBuffer = new uint8_t[ _textureSize * _textureSize * 6 * 4 ];
|
||||||
memset(m_textureBuffer, 0, textureSize * textureSize * 6 * 4);
|
memset(m_textureBuffer, 0, _textureSize * _textureSize * 6 * 4);
|
||||||
//BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT;
|
//BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT;
|
||||||
//BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC|BGFX_TEXTURE_MIP_POINT
|
//BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC|BGFX_TEXTURE_MIP_POINT
|
||||||
//BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP
|
//BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP
|
||||||
@ -257,7 +257,7 @@ Atlas::Atlas(uint16_t textureSize, uint16_t maxRegionsCount )
|
|||||||
//memset(mem->data, 255, mem->size);
|
//memset(mem->data, 255, mem->size);
|
||||||
const bgfx::Memory* mem = NULL;
|
const bgfx::Memory* mem = NULL;
|
||||||
m_textureHandle = bgfx::createTextureCube(6
|
m_textureHandle = bgfx::createTextureCube(6
|
||||||
, textureSize
|
, _textureSize
|
||||||
, 1
|
, 1
|
||||||
, bgfx::TextureFormat::BGRA8
|
, bgfx::TextureFormat::BGRA8
|
||||||
, flags
|
, flags
|
||||||
@ -265,29 +265,29 @@ Atlas::Atlas(uint16_t textureSize, uint16_t maxRegionsCount )
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Atlas::Atlas(uint16_t textureSize, const uint8_t* textureBuffer , uint16_t regionCount, const uint8_t* regionBuffer, uint16_t maxRegionsCount)
|
Atlas::Atlas(uint16_t _textureSize, const uint8_t* _textureBuffer , uint16_t _regionCount, const uint8_t* _regionBuffer, uint16_t _maxRegionsCount)
|
||||||
{
|
{
|
||||||
assert(regionCount <= 64 && maxRegionsCount <= 4096);
|
assert(_regionCount <= 64 && _maxRegionsCount <= 4096);
|
||||||
//layers are frozen
|
//layers are frozen
|
||||||
m_usedLayers = 24;
|
m_usedLayers = 24;
|
||||||
m_usedFaces = 6;
|
m_usedFaces = 6;
|
||||||
|
|
||||||
m_textureSize = textureSize;
|
m_textureSize = _textureSize;
|
||||||
m_regionCount = regionCount;
|
m_regionCount = _regionCount;
|
||||||
//regions are frozen
|
//regions are frozen
|
||||||
m_maxRegionCount = regionCount;
|
m_maxRegionCount = _regionCount;
|
||||||
m_regions = new AtlasRegion[regionCount];
|
m_regions = new AtlasRegion[_regionCount];
|
||||||
m_textureBuffer = new uint8_t[getTextureBufferSize()];
|
m_textureBuffer = new uint8_t[getTextureBufferSize()];
|
||||||
|
|
||||||
//BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT;
|
//BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT;
|
||||||
//BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC|BGFX_TEXTURE_MIP_POINT
|
//BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC|BGFX_TEXTURE_MIP_POINT
|
||||||
//BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP
|
//BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP
|
||||||
uint32_t flags = 0;//BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC|BGFX_TEXTURE_MIP_POINT;
|
uint32_t flags = 0;//BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC|BGFX_TEXTURE_MIP_POINT;
|
||||||
memcpy(m_regions, regionBuffer, regionCount * sizeof(AtlasRegion));
|
memcpy(m_regions, _regionBuffer, _regionCount * sizeof(AtlasRegion));
|
||||||
memcpy(m_textureBuffer, textureBuffer, getTextureBufferSize());
|
memcpy(m_textureBuffer, _textureBuffer, getTextureBufferSize());
|
||||||
|
|
||||||
m_textureHandle = bgfx::createTextureCube(6
|
m_textureHandle = bgfx::createTextureCube(6
|
||||||
, textureSize
|
, _textureSize
|
||||||
, 1
|
, 1
|
||||||
, bgfx::TextureFormat::BGRA8
|
, bgfx::TextureFormat::BGRA8
|
||||||
, flags
|
, flags
|
||||||
@ -302,7 +302,7 @@ Atlas::~Atlas()
|
|||||||
delete[] m_textureBuffer;
|
delete[] m_textureBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Atlas::addRegion(uint16_t width, uint16_t height, const uint8_t* bitmapBuffer, AtlasRegion::Type type)
|
uint16_t Atlas::addRegion(uint16_t _width, uint16_t _height, const uint8_t* _bitmapBuffer, AtlasRegion::Type _type)
|
||||||
{
|
{
|
||||||
if (m_regionCount >= m_maxRegionCount)
|
if (m_regionCount >= m_maxRegionCount)
|
||||||
{
|
{
|
||||||
@ -315,9 +315,9 @@ uint16_t Atlas::addRegion(uint16_t width, uint16_t height, const uint8_t* bitmap
|
|||||||
uint32_t idx = 0;
|
uint32_t idx = 0;
|
||||||
while(idx<m_usedLayers)
|
while(idx<m_usedLayers)
|
||||||
{
|
{
|
||||||
if(m_layers[idx].faceRegion.getType() == type)
|
if(m_layers[idx].faceRegion.getType() == _type)
|
||||||
{
|
{
|
||||||
if(m_layers[idx].packer.addRectangle(width+1,height+1,x,y)) break;
|
if(m_layers[idx].packer.addRectangle(_width+1,_height+1,x,y)) break;
|
||||||
}
|
}
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
@ -325,154 +325,154 @@ uint16_t Atlas::addRegion(uint16_t width, uint16_t height, const uint8_t* bitmap
|
|||||||
if(idx >= m_usedLayers)
|
if(idx >= m_usedLayers)
|
||||||
{
|
{
|
||||||
//do we have still room to add layers ?
|
//do we have still room to add layers ?
|
||||||
if( (idx + type) > 24 || m_usedFaces>=6)
|
if( (idx + _type) > 24 || m_usedFaces>=6)
|
||||||
{
|
{
|
||||||
return UINT16_MAX;
|
return UINT16_MAX;
|
||||||
}
|
}
|
||||||
//create new layers
|
//create new layers
|
||||||
for(int i=0; i < type;++i)
|
for(int i=0; i < _type;++i)
|
||||||
{
|
{
|
||||||
m_layers[idx+i].faceRegion.setMask(type, m_usedFaces, i);
|
m_layers[idx+i].faceRegion.setMask(_type, m_usedFaces, i);
|
||||||
}
|
}
|
||||||
m_usedLayers += type;
|
m_usedLayers += _type;
|
||||||
m_usedFaces++;
|
m_usedFaces++;
|
||||||
|
|
||||||
|
|
||||||
//add it to the created layer
|
//add it to the created layer
|
||||||
if(!m_layers[idx].packer.addRectangle(width+1,height+1,x,y))
|
if(!m_layers[idx].packer.addRectangle(_width+1, _height+1, x, y))
|
||||||
{
|
{
|
||||||
return UINT16_MAX;
|
return UINT16_MAX;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AtlasRegion& region = m_regions[m_regionCount];
|
AtlasRegion& region = m_regions[m_regionCount];
|
||||||
region.x = x;
|
region.m_x = x;
|
||||||
region.y = y;
|
region.m_y = y;
|
||||||
region.width = width;
|
region.m_width = _width;
|
||||||
region.height = height;
|
region.m_height = _height;
|
||||||
region.mask = m_layers[idx].faceRegion.mask;
|
region.m_mask = m_layers[idx].faceRegion.m_mask;
|
||||||
|
|
||||||
updateRegion(region, bitmapBuffer);
|
updateRegion(region, _bitmapBuffer);
|
||||||
return m_regionCount++;
|
return m_regionCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Atlas::updateRegion(const AtlasRegion& region, const uint8_t* bitmapBuffer)
|
void Atlas::updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffer)
|
||||||
{
|
{
|
||||||
const bgfx::Memory* mem = bgfx::alloc(region.width * region.height * 4);
|
const bgfx::Memory* mem = bgfx::alloc(_region.m_width * _region.m_height * 4);
|
||||||
//BAD!
|
//BAD!
|
||||||
memset(mem->data,0, mem->size);
|
memset(mem->data,0, mem->size);
|
||||||
if(region.getType() == AtlasRegion::TYPE_BGRA8)
|
if(_region.getType() == AtlasRegion::TYPE_BGRA8)
|
||||||
{
|
{
|
||||||
const uint8_t* inLineBuffer = bitmapBuffer;
|
const uint8_t* inLineBuffer = _bitmapBuffer;
|
||||||
uint8_t* outLineBuffer = m_textureBuffer + region.getFaceIndex() * (m_textureSize*m_textureSize*4) + (((region.y *m_textureSize)+region.x)*4);
|
uint8_t* outLineBuffer = m_textureBuffer + _region.getFaceIndex() * (m_textureSize*m_textureSize*4) + (((_region.m_y *m_textureSize)+_region.m_x)*4);
|
||||||
|
|
||||||
//update the cpu buffer
|
//update the cpu buffer
|
||||||
for(int y = 0; y < region.height; ++y)
|
for(int y = 0; y < _region.m_height; ++y)
|
||||||
{
|
{
|
||||||
memcpy(outLineBuffer, inLineBuffer, region.width * 4);
|
memcpy(outLineBuffer, inLineBuffer, _region.m_width * 4);
|
||||||
inLineBuffer += region.width*4;
|
inLineBuffer += _region.m_width*4;
|
||||||
outLineBuffer += m_textureSize*4;
|
outLineBuffer += m_textureSize*4;
|
||||||
}
|
}
|
||||||
//update the GPU buffer
|
//update the GPU buffer
|
||||||
memcpy(mem->data, bitmapBuffer, mem->size);
|
memcpy(mem->data, _bitmapBuffer, mem->size);
|
||||||
}else
|
}else
|
||||||
{
|
{
|
||||||
uint32_t layer = region.getComponentIndex();
|
uint32_t layer = _region.getComponentIndex();
|
||||||
uint32_t face = region.getFaceIndex();
|
uint32_t face = _region.getFaceIndex();
|
||||||
const uint8_t* inLineBuffer = bitmapBuffer;
|
const uint8_t* inLineBuffer = _bitmapBuffer;
|
||||||
uint8_t* outLineBuffer = (m_textureBuffer + region.getFaceIndex() * (m_textureSize*m_textureSize*4) + (((region.y *m_textureSize)+region.x)*4));
|
uint8_t* outLineBuffer = (m_textureBuffer + _region.getFaceIndex() * (m_textureSize*m_textureSize*4) + (((_region.m_y *m_textureSize)+_region.m_x)*4));
|
||||||
|
|
||||||
//update the cpu buffer
|
//update the cpu buffer
|
||||||
for(int y = 0; y<region.height; ++y)
|
for(int y = 0; y<_region.m_height; ++y)
|
||||||
{
|
{
|
||||||
for(int x = 0; x<region.width; ++x)
|
for(int x = 0; x<_region.m_width; ++x)
|
||||||
{
|
{
|
||||||
outLineBuffer[(x*4) + layer] = inLineBuffer[x];
|
outLineBuffer[(x*4) + layer] = inLineBuffer[x];
|
||||||
}
|
}
|
||||||
//update the GPU buffer
|
//update the GPU buffer
|
||||||
memcpy(mem->data + y*region.width*4, outLineBuffer, region.width*4);
|
memcpy(mem->data + y*_region.m_width*4, outLineBuffer, _region.m_width*4);
|
||||||
inLineBuffer += region.width;
|
inLineBuffer += _region.m_width;
|
||||||
outLineBuffer += m_textureSize*4;
|
outLineBuffer += m_textureSize*4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bgfx::updateTextureCube(m_textureHandle, (uint8_t)region.getFaceIndex(), 0, region.x, region.y, region.width, region.height, mem);
|
bgfx::updateTextureCube(m_textureHandle, (uint8_t)_region.getFaceIndex(), 0, _region.m_x, _region.m_y, _region.m_width, _region.m_height, mem);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Atlas::packFaceLayerUV(uint32_t idx, uint8_t* vertexBuffer, uint32_t offset, uint32_t stride )
|
void Atlas::packFaceLayerUV(uint32_t _idx, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride )
|
||||||
{
|
{
|
||||||
packUV(m_layers[idx].faceRegion, vertexBuffer, offset, stride);
|
packUV(m_layers[_idx].faceRegion, _vertexBuffer, _offset, _stride);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Atlas::packUV( uint16_t handle, uint8_t* vertexBuffer, uint32_t offset, uint32_t stride )
|
void Atlas::packUV( uint16_t handle, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride )
|
||||||
{
|
{
|
||||||
const AtlasRegion& region = m_regions[handle];
|
const AtlasRegion& region = m_regions[handle];
|
||||||
packUV(region, vertexBuffer, offset, stride);
|
packUV(region, _vertexBuffer, _offset, _stride);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Atlas::packUV( const AtlasRegion& region, uint8_t* vertexBuffer, uint32_t offset, uint32_t stride )
|
void Atlas::packUV( const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride )
|
||||||
{
|
{
|
||||||
float texMult = 65535.0f / ((float)(m_textureSize));
|
float texMult = 65535.0f / ((float)(m_textureSize));
|
||||||
static const int16_t minVal = -32768;
|
static const int16_t minVal = -32768;
|
||||||
static const int16_t maxVal = 32767;
|
static const int16_t maxVal = 32767;
|
||||||
|
|
||||||
int16_t x0 = (int16_t)(region.x * texMult)-32768;
|
int16_t x0 = (int16_t)(_region.m_x * texMult)-32768;
|
||||||
int16_t y0 = (int16_t)(region.y * texMult)-32768;
|
int16_t y0 = (int16_t)(_region.m_y * texMult)-32768;
|
||||||
int16_t x1 = (int16_t)((region.x + region.width)* texMult)-32768;
|
int16_t x1 = (int16_t)((_region.m_x + _region.m_width)* texMult)-32768;
|
||||||
int16_t y1 = (int16_t)((region.y + region.height)* texMult)-32768;
|
int16_t y1 = (int16_t)((_region.m_y + _region.m_height)* texMult)-32768;
|
||||||
int16_t w = (int16_t) ((32767.0f/4.0f) * region.getComponentIndex());
|
int16_t w = (int16_t) ((32767.0f/4.0f) * _region.getComponentIndex());
|
||||||
|
|
||||||
vertexBuffer+=offset;
|
_vertexBuffer+=_offset;
|
||||||
switch(region.getFaceIndex())
|
switch(_region.getFaceIndex())
|
||||||
{
|
{
|
||||||
case 0: // +X
|
case 0: // +X
|
||||||
x0= -x0;
|
x0= -x0;
|
||||||
x1= -x1;
|
x1= -x1;
|
||||||
y0= -y0;
|
y0= -y0;
|
||||||
y1= -y1;
|
y1= -y1;
|
||||||
writeUV(vertexBuffer, maxVal, y0, x0, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, maxVal, y0, x0, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, maxVal, y1, x0, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, maxVal, y1, x0, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, maxVal, y1, x1, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, maxVal, y1, x1, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, maxVal, y0, x1, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, maxVal, y0, x1, w); _vertexBuffer+=_stride;
|
||||||
break;
|
break;
|
||||||
case 1: // -X
|
case 1: // -X
|
||||||
y0= -y0;
|
y0= -y0;
|
||||||
y1= -y1;
|
y1= -y1;
|
||||||
writeUV(vertexBuffer, minVal, y0, x0, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, minVal, y0, x0, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, minVal, y1, x0, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, minVal, y1, x0, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, minVal, y1, x1, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, minVal, y1, x1, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, minVal, y0, x1, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, minVal, y0, x1, w); _vertexBuffer+=_stride;
|
||||||
break;
|
break;
|
||||||
case 2: // +Y
|
case 2: // +Y
|
||||||
writeUV(vertexBuffer, x0, maxVal, y0, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x0, maxVal, y0, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, x0, maxVal, y1, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x0, maxVal, y1, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, x1, maxVal, y1, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x1, maxVal, y1, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, x1, maxVal, y0, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x1, maxVal, y0, w); _vertexBuffer+=_stride;
|
||||||
break;
|
break;
|
||||||
case 3: // -Y
|
case 3: // -Y
|
||||||
y0= -y0;
|
y0= -y0;
|
||||||
y1= -y1;
|
y1= -y1;
|
||||||
writeUV(vertexBuffer, x0, minVal, y0, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x0, minVal, y0, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, x0, minVal, y1, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x0, minVal, y1, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, x1, minVal, y1, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x1, minVal, y1, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, x1, minVal, y0, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x1, minVal, y0, w); _vertexBuffer+=_stride;
|
||||||
break;
|
break;
|
||||||
case 4: // +Z
|
case 4: // +Z
|
||||||
y0= -y0;
|
y0= -y0;
|
||||||
y1= -y1;
|
y1= -y1;
|
||||||
writeUV(vertexBuffer, x0, y0, maxVal, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x0, y0, maxVal, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, x0, y1, maxVal, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x0, y1, maxVal, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, x1, y1, maxVal, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x1, y1, maxVal, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, x1, y0, maxVal, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x1, y0, maxVal, w); _vertexBuffer+=_stride;
|
||||||
break;
|
break;
|
||||||
case 5: // -Z
|
case 5: // -Z
|
||||||
x0= -x0;
|
x0= -x0;
|
||||||
x1= -x1;
|
x1= -x1;
|
||||||
y0= -y0;
|
y0= -y0;
|
||||||
y1= -y1;
|
y1= -y1;
|
||||||
writeUV(vertexBuffer, x0, y0, minVal, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x0, y0, minVal, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, x0, y1, minVal, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x0, y1, minVal, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, x1, y1, minVal, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x1, y1, minVal, w); _vertexBuffer+=_stride;
|
||||||
writeUV(vertexBuffer, x1, y0, minVal, w); vertexBuffer+=stride;
|
writeUV(_vertexBuffer, x1, y0, minVal, w); _vertexBuffer+=_stride;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,14 +22,14 @@ struct AtlasRegion
|
|||||||
TYPE_BGRA8 = 4 // 4 components
|
TYPE_BGRA8 = 4 // 4 components
|
||||||
};
|
};
|
||||||
|
|
||||||
uint16_t x, y;
|
uint16_t m_x, m_y;
|
||||||
uint16_t width, height;
|
uint16_t m_width, m_height;
|
||||||
uint32_t mask; //encode the region type, the face index and the component index in case of a gray region
|
uint32_t m_mask; //encode the region type, the face index and the component index in case of a gray region
|
||||||
|
|
||||||
Type getType()const { return (Type) ((mask >> 0) & 0x0000000F); }
|
Type getType()const { return (Type) ((m_mask >> 0) & 0x0000000F); }
|
||||||
uint32_t getFaceIndex()const { return (mask >> 4) & 0x0000000F; }
|
uint32_t getFaceIndex()const { return (m_mask >> 4) & 0x0000000F; }
|
||||||
uint32_t getComponentIndex()const { return (mask >> 8) & 0x0000000F; }
|
uint32_t getComponentIndex()const { return (m_mask >> 8) & 0x0000000F; }
|
||||||
void setMask(Type type, uint32_t faceIndex, uint32_t componentIndex) { mask = (componentIndex << 8) + (faceIndex << 4) + (uint32_t)type; }
|
void setMask(Type _type, uint32_t _faceIndex, uint32_t _componentIndex) { m_mask = (_componentIndex << 8) + (_faceIndex << 4) + (uint32_t)_type; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class Atlas
|
class Atlas
|
||||||
@ -38,7 +38,7 @@ public:
|
|||||||
/// create an empty dynamic atlas (region can be updated and added)
|
/// create an empty dynamic atlas (region can be updated and added)
|
||||||
/// @param textureSize an atlas creates a texture cube of 6 faces with size equal to (textureSize*textureSize * sizeof(RGBA))
|
/// @param textureSize an atlas creates a texture cube of 6 faces with size equal to (textureSize*textureSize * sizeof(RGBA))
|
||||||
/// @param maxRegionCount maximum number of region allowed in the atlas
|
/// @param maxRegionCount maximum number of region allowed in the atlas
|
||||||
Atlas(uint16_t textureSize, uint16_t _maxRegionsCount = 4096);
|
Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount = 4096);
|
||||||
|
|
||||||
/// initialize a static atlas with serialized data (region can be updated but not added)
|
/// initialize a static atlas with serialized data (region can be updated but not added)
|
||||||
/// @param textureSize an atlas creates a texture cube of 6 faces with size equal to (textureSize*textureSize * sizeof(RGBA))
|
/// @param textureSize an atlas creates a texture cube of 6 faces with size equal to (textureSize*textureSize * sizeof(RGBA))
|
||||||
@ -46,14 +46,14 @@ public:
|
|||||||
/// @param regionCount number of region in the Atlas
|
/// @param regionCount number of region in the Atlas
|
||||||
/// @param regionBuffer buffer containing the region (will be copied)
|
/// @param regionBuffer buffer containing the region (will be copied)
|
||||||
/// @param maxRegionCount maximum number of region allowed in the atlas
|
/// @param maxRegionCount maximum number of region allowed in the atlas
|
||||||
Atlas(uint16_t textureSize, const uint8_t * textureBuffer, uint16_t regionCount, const uint8_t* regionBuffer, uint16_t maxRegionsCount = 4096);
|
Atlas(uint16_t _textureSize, const uint8_t * _textureBuffer, uint16_t _regionCount, const uint8_t* _regionBuffer, uint16_t _maxRegionsCount = 4096);
|
||||||
~Atlas();
|
~Atlas();
|
||||||
|
|
||||||
/// add a region to the atlas, and copy the content of mem to the underlying texture
|
/// add a region to the atlas, and copy the content of mem to the underlying texture
|
||||||
uint16_t addRegion(uint16_t width, uint16_t height, const uint8_t* bitmapBuffer, AtlasRegion::Type type = AtlasRegion::TYPE_BGRA8);
|
uint16_t addRegion(uint16_t _width, uint16_t _height, const uint8_t* _bitmapBuffer, AtlasRegion::Type _type = AtlasRegion::TYPE_BGRA8);
|
||||||
|
|
||||||
/// update a preallocated region
|
/// update a preallocated region
|
||||||
void updateRegion(const AtlasRegion& region, const uint8_t* bitmapBuffer);
|
void updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffer);
|
||||||
|
|
||||||
/// Pack the UV coordinates of the four corners of a region to a vertex buffer using the supplied vertex format.
|
/// Pack the UV coordinates of the four corners of a region to a vertex buffer using the supplied vertex format.
|
||||||
/// v0 -- v3
|
/// v0 -- v3
|
||||||
@ -65,28 +65,28 @@ public:
|
|||||||
/// @param vertexBuffer address of the first vertex we want to update. Must be valid up to vertexBuffer + offset + 3*stride + 4*sizeof(int16_t), which means the buffer must contains at least 4 vertex includind the first.
|
/// @param vertexBuffer address of the first vertex we want to update. Must be valid up to vertexBuffer + offset + 3*stride + 4*sizeof(int16_t), which means the buffer must contains at least 4 vertex includind the first.
|
||||||
/// @param offset byte offset to the first uv coordinate of the vertex in the buffer
|
/// @param offset byte offset to the first uv coordinate of the vertex in the buffer
|
||||||
/// @param stride stride between tho UV coordinates, usually size of a Vertex.
|
/// @param stride stride between tho UV coordinates, usually size of a Vertex.
|
||||||
void packUV( uint16_t regionHandle, uint8_t* vertexBuffer, uint32_t offset, uint32_t stride );
|
void packUV( uint16_t _regionHandle, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride );
|
||||||
void packUV( const AtlasRegion& region, uint8_t* vertexBuffer, uint32_t offset, uint32_t stride );
|
void packUV( const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride );
|
||||||
|
|
||||||
/// Same as packUV but pack a whole face of the atlas cube, mostly used for debugging and visualizing atlas
|
/// Same as packUV but pack a whole face of the atlas cube, mostly used for debugging and visualizing atlas
|
||||||
void packFaceLayerUV(uint32_t idx, uint8_t* vertexBuffer, uint32_t offset, uint32_t stride );
|
void packFaceLayerUV(uint32_t _idx, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride );
|
||||||
|
|
||||||
/// Pack the vertex index of the region as 2 quad into an index buffer
|
/// Pack the vertex index of the region as 2 quad into an index buffer
|
||||||
void packIndex(uint16_t* indexBuffer, uint32_t startIndex, uint32_t startVertex )
|
void packIndex(uint16_t* _indexBuffer, uint32_t _startIndex, uint32_t _startVertex )
|
||||||
{
|
{
|
||||||
indexBuffer[startIndex+0] = startVertex+0;
|
_indexBuffer[_startIndex+0] = _startVertex+0;
|
||||||
indexBuffer[startIndex+1] = startVertex+1;
|
_indexBuffer[_startIndex+1] = _startVertex+1;
|
||||||
indexBuffer[startIndex+2] = startVertex+2;
|
_indexBuffer[_startIndex+2] = _startVertex+2;
|
||||||
indexBuffer[startIndex+3] = startVertex+0;
|
_indexBuffer[_startIndex+3] = _startVertex+0;
|
||||||
indexBuffer[startIndex+4] = startVertex+2;
|
_indexBuffer[_startIndex+4] = _startVertex+2;
|
||||||
indexBuffer[startIndex+5] = startVertex+3;
|
_indexBuffer[_startIndex+5] = _startVertex+3;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// return the TextureHandle (cube) of the atlas
|
/// return the TextureHandle (cube) of the atlas
|
||||||
bgfx::TextureHandle getTextureHandle() const { return m_textureHandle; }
|
bgfx::TextureHandle getTextureHandle() const { return m_textureHandle; }
|
||||||
|
|
||||||
//retrieve a region info
|
//retrieve a region info
|
||||||
const AtlasRegion& getRegion(uint16_t handle) const { return m_regions[handle]; }
|
const AtlasRegion& getRegion(uint16_t _handle) const { return m_regions[_handle]; }
|
||||||
|
|
||||||
/// retrieve the size of side of a texture in pixels
|
/// retrieve the size of side of a texture in pixels
|
||||||
uint16_t getTextureSize(){ return m_textureSize; }
|
uint16_t getTextureSize(){ return m_textureSize; }
|
||||||
@ -108,12 +108,12 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void writeUV( uint8_t* vertexBuffer, int16_t x, int16_t y, int16_t z, int16_t w)
|
void writeUV( uint8_t* _vertexBuffer, int16_t _x, int16_t _y, int16_t _z, int16_t _w)
|
||||||
{
|
{
|
||||||
((uint16_t*) vertexBuffer)[0] = x;
|
((uint16_t*) _vertexBuffer)[0] = _x;
|
||||||
((uint16_t*) vertexBuffer)[1] = y;
|
((uint16_t*) _vertexBuffer)[1] = _y;
|
||||||
((uint16_t*) vertexBuffer)[2] = z;
|
((uint16_t*) _vertexBuffer)[2] = _z;
|
||||||
((uint16_t*) vertexBuffer)[3] = w;
|
((uint16_t*) _vertexBuffer)[3] = _w;
|
||||||
}
|
}
|
||||||
struct PackedLayer;
|
struct PackedLayer;
|
||||||
PackedLayer* m_layers;
|
PackedLayer* m_layers;
|
||||||
|
Loading…
Reference in New Issue
Block a user