2013-05-15 09:07:04 -04:00
|
|
|
/*
|
2013-05-15 22:42:39 -04:00
|
|
|
* Copyright 2013 Jeremie Roy. All rights reserved.
|
|
|
|
* License: http://www.opensource.org/licenses/BSD-2-Clause
|
|
|
|
*/
|
2013-05-15 09:07:04 -04:00
|
|
|
|
2013-05-19 01:12:40 -04:00
|
|
|
#include "common.h"
|
2013-05-15 09:21:23 -04:00
|
|
|
#include <bgfx.h>
|
2013-05-15 22:42:39 -04:00
|
|
|
|
|
|
|
#include <limits.h> // INT_MAX
|
|
|
|
#include <memory.h> // memset
|
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
#include <vector>
|
2013-04-23 17:14:32 -04:00
|
|
|
|
2013-05-15 22:42:39 -04:00
|
|
|
#include "cube_atlas.h"
|
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
class RectanglePacker
|
2013-05-15 09:21:23 -04:00
|
|
|
{
|
2013-04-22 16:42:11 -04:00
|
|
|
public:
|
2013-05-15 22:42:39 -04:00
|
|
|
RectanglePacker();
|
|
|
|
RectanglePacker(uint32_t _width, uint32_t _height);
|
2013-04-22 16:42:11 -04:00
|
|
|
|
2013-05-15 22:42:39 -04:00
|
|
|
/// non constructor initialization
|
|
|
|
void init(uint32_t _width, uint32_t _height);
|
2013-04-22 16:42:11 -04:00
|
|
|
|
2013-05-15 22:42:39 -04:00
|
|
|
/// find a suitable position for the given rectangle
|
|
|
|
/// @return true if the rectangle can be added, false otherwise
|
|
|
|
bool addRectangle(uint16_t _width, uint16_t _height, uint16_t& _outX, uint16_t& _outY);
|
|
|
|
|
|
|
|
/// return the used surface in squared unit
|
|
|
|
uint32_t getUsedSurface()
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-15 22:42:39 -04:00
|
|
|
return m_usedSpace;
|
2013-05-15 09:21:23 -04:00
|
|
|
}
|
|
|
|
|
2013-05-15 22:42:39 -04:00
|
|
|
/// return the total available surface in squared unit
|
|
|
|
uint32_t getTotalSurface()
|
|
|
|
{
|
|
|
|
return m_width * m_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// return the usage ratio of the available surface [0:1]
|
|
|
|
float getUsageRatio();
|
|
|
|
|
|
|
|
/// reset to initial state
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
private:
|
|
|
|
int32_t fit(uint32_t _skylineNodeIndex, uint16_t _width, uint16_t _height);
|
|
|
|
/// Merges all skyline nodes that are at the same level.
|
|
|
|
void merge();
|
|
|
|
|
|
|
|
struct Node
|
|
|
|
{
|
2013-05-17 17:47:08 -04:00
|
|
|
Node(int16_t _x, int16_t _y, int16_t _width) : x(_x), y(_y), width(_width)
|
2013-05-15 22:42:39 -04:00
|
|
|
{
|
|
|
|
}
|
2013-04-22 16:42:11 -04:00
|
|
|
|
2013-05-15 22:42:39 -04:00
|
|
|
/// The starting x-coordinate (leftmost).
|
2013-05-17 17:47:08 -04:00
|
|
|
int16_t x;
|
2013-05-15 22:42:39 -04:00
|
|
|
/// The y-coordinate of the skyline level line.
|
2013-05-17 17:47:08 -04:00
|
|
|
int16_t y;
|
2013-05-15 22:42:39 -04:00
|
|
|
/// The line _width. The ending coordinate (inclusive) will be x+width-1.
|
2013-05-17 17:47:08 -04:00
|
|
|
int32_t width; // 32bit to avoid padding
|
2013-05-15 22:42:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/// width (in pixels) of the underlying texture
|
|
|
|
uint32_t m_width;
|
|
|
|
/// height (in pixels) of the underlying texture
|
|
|
|
uint32_t m_height;
|
|
|
|
/// Surface used in squared pixel
|
|
|
|
uint32_t m_usedSpace;
|
|
|
|
/// node of the skyline algorithm
|
|
|
|
std::vector<Node> m_skyline;
|
2013-05-15 09:21:23 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
RectanglePacker::RectanglePacker() : m_width(0), m_height(0), m_usedSpace(0)
|
|
|
|
{
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
|
|
|
|
2013-05-15 09:21:23 -04:00
|
|
|
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
|
|
|
|
// sampling texture
|
|
|
|
m_skyline.push_back(Node(1, 1, _width - 2) );
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
|
|
|
|
2013-04-23 14:36:07 -04:00
|
|
|
void RectanglePacker::init(uint32_t _width, uint32_t _height)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-04-23 17:14:32 -04:00
|
|
|
BX_CHECK(_width > 2, "_width must be > 2");
|
|
|
|
BX_CHECK(_height > 2, "_height must be > 2");
|
2013-04-23 14:36:07 -04:00
|
|
|
m_width = _width;
|
|
|
|
m_height = _height;
|
2013-04-22 16:42:11 -04:00
|
|
|
m_usedSpace = 0;
|
|
|
|
|
|
|
|
m_skyline.clear();
|
|
|
|
// We want a one pixel border around the whole atlas to avoid any artifact when
|
2013-05-15 09:21:23 -04:00
|
|
|
// sampling texture
|
|
|
|
m_skyline.push_back(Node(1, 1, _width - 2) );
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
|
|
|
|
2013-04-23 14:36:07 -04:00
|
|
|
bool RectanglePacker::addRectangle(uint16_t _width, uint16_t _height, uint16_t& _outX, uint16_t& _outY)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
|
|
|
int y, best_height, best_index;
|
2013-05-15 09:21:23 -04:00
|
|
|
int32_t best_width;
|
|
|
|
Node* node;
|
|
|
|
Node* prev;
|
|
|
|
_outX = 0;
|
2013-04-23 14:36:07 -04:00
|
|
|
_outY = 0;
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-04-23 16:48:34 -04:00
|
|
|
uint32_t ii;
|
2013-04-22 16:42:11 -04:00
|
|
|
|
2013-05-15 09:21:23 -04:00
|
|
|
best_height = INT_MAX;
|
|
|
|
best_index = -1;
|
|
|
|
best_width = INT_MAX;
|
|
|
|
for (ii = 0; ii < m_skyline.size(); ++ii)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-15 09:21:23 -04:00
|
|
|
y = fit(ii, _width, _height);
|
|
|
|
if (y >= 0)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-15 09:21:23 -04:00
|
|
|
node = &m_skyline[ii];
|
|
|
|
if ( ( (y + _height) < best_height)
|
2013-05-15 22:42:39 -04:00
|
|
|
|| ( ( (y + _height) == best_height)
|
2013-05-17 17:47:08 -04:00
|
|
|
&& (node->width < best_width) ) )
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-04-23 14:36:07 -04:00
|
|
|
best_height = y + _height;
|
2013-04-23 16:48:34 -04:00
|
|
|
best_index = ii;
|
2013-05-17 17:47:08 -04:00
|
|
|
best_width = node->width;
|
|
|
|
_outX = node->x;
|
2013-04-23 14:36:07 -04:00
|
|
|
_outY = y;
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
2013-05-15 09:21:23 -04:00
|
|
|
}
|
|
|
|
}
|
2013-04-22 16:42:11 -04:00
|
|
|
|
2013-05-15 09:21:23 -04:00
|
|
|
if (best_index == -1)
|
|
|
|
{
|
2013-04-22 16:42:11 -04:00
|
|
|
return false;
|
2013-05-15 09:21:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Node newNode(_outX, _outY + _height, _width);
|
|
|
|
m_skyline.insert(m_skyline.begin() + best_index, newNode);
|
|
|
|
|
|
|
|
for (ii = best_index + 1; ii < m_skyline.size(); ++ii)
|
|
|
|
{
|
|
|
|
node = &m_skyline[ii];
|
|
|
|
prev = &m_skyline[ii - 1];
|
2013-05-17 17:47:08 -04:00
|
|
|
if (node->x < (prev->x + prev->width) )
|
2013-05-15 09:21:23 -04:00
|
|
|
{
|
2013-05-17 17:47:08 -04:00
|
|
|
int shrink = prev->x + prev->width - node->x;
|
|
|
|
node->x += shrink;
|
|
|
|
node->width -= shrink;
|
|
|
|
if (node->width <= 0)
|
2013-05-15 09:21:23 -04:00
|
|
|
{
|
|
|
|
m_skyline.erase(m_skyline.begin() + ii);
|
|
|
|
--ii;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
merge();
|
|
|
|
m_usedSpace += _width * _height;
|
|
|
|
return true;
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
float RectanglePacker::getUsageRatio()
|
2013-05-15 09:21:23 -04:00
|
|
|
{
|
|
|
|
uint32_t total = m_width * m_height;
|
|
|
|
if (total > 0)
|
|
|
|
{
|
2013-04-22 16:42:11 -04:00
|
|
|
return (float) m_usedSpace / (float) total;
|
2013-05-15 09:21:23 -04:00
|
|
|
}
|
2013-04-22 16:42:11 -04:00
|
|
|
else
|
2013-05-15 09:21:23 -04:00
|
|
|
{
|
2013-04-22 16:42:11 -04:00
|
|
|
return 0.0f;
|
2013-05-15 09:21:23 -04:00
|
|
|
}
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void RectanglePacker::clear()
|
|
|
|
{
|
|
|
|
m_skyline.clear();
|
2013-05-15 09:21:23 -04:00
|
|
|
m_usedSpace = 0;
|
|
|
|
|
|
|
|
// We want a one pixel border around the whole atlas to avoid any artefact when
|
|
|
|
// sampling texture
|
|
|
|
m_skyline.push_back(Node(1, 1, m_width - 2) );
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
|
|
|
|
2013-04-23 14:36:07 -04:00
|
|
|
int32_t RectanglePacker::fit(uint32_t _skylineNodeIndex, uint16_t _width, uint16_t _height)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
|
|
|
int32_t width = _width;
|
2013-05-15 09:21:23 -04:00
|
|
|
int32_t height = _height;
|
|
|
|
|
|
|
|
const Node& baseNode = m_skyline[_skylineNodeIndex];
|
|
|
|
|
2013-05-17 17:47:08 -04:00
|
|
|
int32_t x = baseNode.x, y;
|
2013-04-23 14:36:07 -04:00
|
|
|
int32_t _width_left = width;
|
|
|
|
int32_t i = _skylineNodeIndex;
|
2013-04-22 16:42:11 -04:00
|
|
|
|
2013-05-15 09:21:23 -04:00
|
|
|
if ( (x + width) > (int32_t)(m_width - 1) )
|
|
|
|
{
|
2013-04-22 16:42:11 -04:00
|
|
|
return -1;
|
2013-05-15 09:21:23 -04:00
|
|
|
}
|
|
|
|
|
2013-05-17 17:47:08 -04:00
|
|
|
y = baseNode.y;
|
2013-05-15 09:21:23 -04:00
|
|
|
while (_width_left > 0)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-15 09:21:23 -04:00
|
|
|
const Node& node = m_skyline[i];
|
2013-05-17 17:47:08 -04:00
|
|
|
if (node.y > y)
|
2013-05-15 09:21:23 -04:00
|
|
|
{
|
2013-05-17 17:47:08 -04:00
|
|
|
y = node.y;
|
2013-05-15 09:21:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( (y + height) > (int32_t)(m_height - 1) )
|
|
|
|
{
|
2013-04-22 16:42:11 -04:00
|
|
|
return -1;
|
2013-05-15 09:21:23 -04:00
|
|
|
}
|
|
|
|
|
2013-05-17 17:47:08 -04:00
|
|
|
_width_left -= node.width;
|
2013-04-22 16:42:11 -04:00
|
|
|
++i;
|
|
|
|
}
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
return y;
|
|
|
|
}
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
void RectanglePacker::merge()
|
|
|
|
{
|
|
|
|
Node* node;
|
2013-05-15 09:21:23 -04:00
|
|
|
Node* next;
|
|
|
|
uint32_t ii;
|
|
|
|
|
|
|
|
for (ii = 0; ii < m_skyline.size() - 1; ++ii)
|
|
|
|
{
|
|
|
|
node = (Node*) &m_skyline[ii];
|
|
|
|
next = (Node*) &m_skyline[ii + 1];
|
2013-05-17 17:47:08 -04:00
|
|
|
if (node->y == next->y)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-17 17:47:08 -04:00
|
|
|
node->width += next->width;
|
2013-05-15 09:21:23 -04:00
|
|
|
m_skyline.erase(m_skyline.begin() + ii + 1);
|
2013-04-23 16:48:34 -04:00
|
|
|
--ii;
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
2013-05-15 09:21:23 -04:00
|
|
|
}
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Atlas::PackedLayer
|
|
|
|
{
|
|
|
|
RectanglePacker packer;
|
|
|
|
AtlasRegion faceRegion;
|
|
|
|
};
|
|
|
|
|
2013-05-15 09:21:23 -04:00
|
|
|
Atlas::Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-15 09:21:23 -04:00
|
|
|
BX_CHECK(_textureSize >= 64
|
2013-05-15 22:42:39 -04:00
|
|
|
&& _textureSize <= 4096, "suspicious texture size");
|
2013-05-15 09:21:23 -04:00
|
|
|
BX_CHECK(_maxRegionsCount >= 64
|
2013-05-15 22:42:39 -04:00
|
|
|
&& _maxRegionsCount <= 32000, "suspicious _regions count");
|
2013-04-22 16:42:11 -04:00
|
|
|
m_layers = new PackedLayer[24];
|
2013-05-15 09:21:23 -04:00
|
|
|
for (int ii = 0; ii < 24; ++ii)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-15 09:21:23 -04:00
|
|
|
m_layers[ii].packer.init(_textureSize, _textureSize);
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
m_usedLayers = 0;
|
|
|
|
m_usedFaces = 0;
|
|
|
|
|
2013-04-23 14:36:07 -04:00
|
|
|
m_textureSize = _textureSize;
|
2013-04-22 16:42:11 -04:00
|
|
|
m_regionCount = 0;
|
2013-04-23 14:36:07 -04:00
|
|
|
m_maxRegionCount = _maxRegionsCount;
|
|
|
|
m_regions = new AtlasRegion[_maxRegionsCount];
|
|
|
|
m_textureBuffer = new uint8_t[ _textureSize * _textureSize * 6 * 4 ];
|
2013-05-15 09:21:23 -04:00
|
|
|
memset(m_textureBuffer, 0, _textureSize * _textureSize * 6 * 4);
|
2013-04-22 16:42:11 -04:00
|
|
|
//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_U_CLAMP|BGFX_TEXTURE_V_CLAMP
|
2013-05-15 09:21:23 -04:00
|
|
|
uint32_t flags = 0; // BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC|BGFX_TEXTURE_MIP_POINT;
|
2013-04-22 16:42:11 -04:00
|
|
|
|
|
|
|
//Uncomment this to debug atlas
|
|
|
|
//const bgfx::Memory* mem = bgfx::alloc(textureSize*textureSize * 6 * 4);
|
2013-05-15 09:21:23 -04:00
|
|
|
//memset(mem->data, 255, mem->size);
|
|
|
|
const bgfx::Memory* mem = NULL;
|
2013-04-22 16:42:11 -04:00
|
|
|
m_textureHandle = bgfx::createTextureCube(6
|
2013-05-15 22:42:39 -04:00
|
|
|
, _textureSize
|
|
|
|
, 1
|
|
|
|
, bgfx::TextureFormat::BGRA8
|
|
|
|
, flags
|
|
|
|
, mem
|
|
|
|
);
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
|
|
|
|
2013-05-15 09:21:23 -04:00
|
|
|
Atlas::Atlas(uint16_t _textureSize, const uint8_t* _textureBuffer, uint16_t _regionCount, const uint8_t* _regionBuffer, uint16_t _maxRegionsCount)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-15 09:21:23 -04:00
|
|
|
BX_CHECK(_regionCount <= 64
|
2013-05-15 22:42:39 -04:00
|
|
|
&& _maxRegionsCount <= 4096, "suspicious initialization");
|
2013-04-22 16:42:11 -04:00
|
|
|
//layers are frozen
|
|
|
|
m_usedLayers = 24;
|
|
|
|
m_usedFaces = 6;
|
|
|
|
|
2013-04-23 14:36:07 -04:00
|
|
|
m_textureSize = _textureSize;
|
|
|
|
m_regionCount = _regionCount;
|
2013-04-22 16:42:11 -04:00
|
|
|
//regions are frozen
|
2013-05-15 09:21:23 -04:00
|
|
|
if (_regionCount < _maxRegionsCount)
|
|
|
|
{
|
2013-04-23 17:14:32 -04:00
|
|
|
m_maxRegionCount = _regionCount;
|
2013-05-15 09:21:23 -04:00
|
|
|
}
|
2013-04-23 17:14:32 -04:00
|
|
|
else
|
2013-05-15 09:21:23 -04:00
|
|
|
{
|
2013-04-23 17:14:32 -04:00
|
|
|
m_maxRegionCount = _maxRegionsCount;
|
2013-05-15 09:21:23 -04:00
|
|
|
}
|
|
|
|
|
2013-04-23 14:36:07 -04:00
|
|
|
m_regions = new AtlasRegion[_regionCount];
|
2013-04-22 16:42:11 -04:00
|
|
|
m_textureBuffer = new uint8_t[getTextureBufferSize()];
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
//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_U_CLAMP|BGFX_TEXTURE_V_CLAMP
|
2013-05-15 09:21:23 -04:00
|
|
|
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_textureBuffer, _textureBuffer, getTextureBufferSize() );
|
2013-04-22 16:42:11 -04:00
|
|
|
|
|
|
|
m_textureHandle = bgfx::createTextureCube(6
|
2013-05-15 22:42:39 -04:00
|
|
|
, _textureSize
|
|
|
|
, 1
|
|
|
|
, bgfx::TextureFormat::BGRA8
|
|
|
|
, flags
|
|
|
|
, bgfx::makeRef(m_textureBuffer, getTextureBufferSize() )
|
|
|
|
);
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Atlas::~Atlas()
|
|
|
|
{
|
|
|
|
delete[] m_layers;
|
|
|
|
delete[] m_regions;
|
|
|
|
delete[] m_textureBuffer;
|
|
|
|
}
|
|
|
|
|
2013-05-15 09:21:23 -04:00
|
|
|
uint16_t Atlas::addRegion(uint16_t _width, uint16_t _height, const uint8_t* _bitmapBuffer, AtlasRegion::Type _type, uint16_t outline)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
|
|
|
if (m_regionCount >= m_maxRegionCount)
|
|
|
|
{
|
|
|
|
return UINT16_MAX;
|
|
|
|
}
|
2013-05-15 09:21:23 -04:00
|
|
|
|
|
|
|
uint16_t x = 0, y = 0;
|
2013-04-22 16:42:11 -04:00
|
|
|
// We want each bitmap to be separated by at least one black pixel
|
|
|
|
// TODO manage mipmaps
|
|
|
|
uint32_t idx = 0;
|
2013-05-15 09:21:23 -04:00
|
|
|
while (idx < m_usedLayers)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-15 09:21:23 -04:00
|
|
|
if (m_layers[idx].faceRegion.getType() == _type)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-15 09:21:23 -04:00
|
|
|
if (m_layers[idx].packer.addRectangle(_width + 1, _height + 1, x, y) )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
2013-05-15 09:21:23 -04:00
|
|
|
if (idx >= m_usedLayers)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
|
|
|
//do we have still room to add layers ?
|
2013-05-15 09:21:23 -04:00
|
|
|
if ( (idx + _type) > 24
|
2013-05-15 22:42:39 -04:00
|
|
|
|| m_usedFaces >= 6)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-15 09:21:23 -04:00
|
|
|
return UINT16_MAX;
|
|
|
|
}
|
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
//create new layers
|
2013-05-15 09:21:23 -04:00
|
|
|
for (int ii = 0; ii < _type; ++ii)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-17 17:47:08 -04:00
|
|
|
m_layers[idx + ii].faceRegion.x = 0;
|
|
|
|
m_layers[idx + ii].faceRegion.y = 0;
|
|
|
|
m_layers[idx + ii].faceRegion.width = m_textureSize;
|
|
|
|
m_layers[idx + ii].faceRegion.height = m_textureSize;
|
2013-05-15 09:21:23 -04:00
|
|
|
m_layers[idx + ii].faceRegion.setMask(_type, m_usedFaces, ii);
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-04-23 14:36:07 -04:00
|
|
|
m_usedLayers += _type;
|
2013-04-22 16:42:11 -04:00
|
|
|
m_usedFaces++;
|
|
|
|
|
|
|
|
//add it to the created layer
|
2013-05-15 09:21:23 -04:00
|
|
|
if (!m_layers[idx].packer.addRectangle(_width + 1, _height + 1, x, y) )
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
|
|
|
return UINT16_MAX;
|
|
|
|
}
|
|
|
|
}
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
AtlasRegion& region = m_regions[m_regionCount];
|
2013-05-17 17:47:08 -04:00
|
|
|
region.x = x;
|
|
|
|
region.y = y;
|
|
|
|
region.width = _width;
|
|
|
|
region.height = _height;
|
|
|
|
region.mask = m_layers[idx].faceRegion.mask;
|
2013-04-22 16:42:11 -04:00
|
|
|
|
2013-04-23 14:36:07 -04:00
|
|
|
updateRegion(region, _bitmapBuffer);
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-05-17 17:47:08 -04:00
|
|
|
region.x += outline;
|
|
|
|
region.y += outline;
|
|
|
|
region.width -= (outline * 2);
|
|
|
|
region.height -= (outline * 2);
|
2013-05-08 13:45:58 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
return m_regionCount++;
|
|
|
|
}
|
|
|
|
|
2013-04-23 14:36:07 -04:00
|
|
|
void Atlas::updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffer)
|
2013-05-15 09:21:23 -04:00
|
|
|
{
|
2013-05-17 17:47:08 -04:00
|
|
|
const bgfx::Memory* mem = bgfx::alloc(_region.width * _region.height * 4);
|
2013-04-22 16:42:11 -04:00
|
|
|
//BAD!
|
2013-05-15 09:21:23 -04:00
|
|
|
memset(mem->data, 0, mem->size);
|
|
|
|
if (_region.getType() == AtlasRegion::TYPE_BGRA8)
|
|
|
|
{
|
2013-04-23 14:36:07 -04:00
|
|
|
const uint8_t* inLineBuffer = _bitmapBuffer;
|
2013-05-17 17:47:08 -04:00
|
|
|
uint8_t* outLineBuffer = m_textureBuffer + _region.getFaceIndex() * (m_textureSize * m_textureSize * 4) + ( ( (_region.y * m_textureSize) + _region.x) * 4);
|
2013-04-22 16:42:11 -04:00
|
|
|
|
|
|
|
//update the cpu buffer
|
2013-05-17 17:47:08 -04:00
|
|
|
for (int yy = 0; yy < _region.height; ++yy)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-17 17:47:08 -04:00
|
|
|
memcpy(outLineBuffer, inLineBuffer, _region.width * 4);
|
|
|
|
inLineBuffer += _region.width * 4;
|
2013-05-15 09:21:23 -04:00
|
|
|
outLineBuffer += m_textureSize * 4;
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
//update the GPU buffer
|
2013-04-23 14:36:07 -04:00
|
|
|
memcpy(mem->data, _bitmapBuffer, mem->size);
|
2013-05-15 09:21:23 -04:00
|
|
|
}
|
|
|
|
else
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-04-23 14:36:07 -04:00
|
|
|
uint32_t layer = _region.getComponentIndex();
|
2013-04-23 17:14:32 -04:00
|
|
|
//uint32_t face = _region.getFaceIndex();
|
2013-04-23 14:36:07 -04:00
|
|
|
const uint8_t* inLineBuffer = _bitmapBuffer;
|
2013-05-17 17:47:08 -04:00
|
|
|
uint8_t* outLineBuffer = (m_textureBuffer + _region.getFaceIndex() * (m_textureSize * m_textureSize * 4) + ( ( (_region.y * m_textureSize) + _region.x) * 4) );
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
//update the cpu buffer
|
2013-05-17 17:47:08 -04:00
|
|
|
for (int yy = 0; yy < _region.height; ++yy)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-17 17:47:08 -04:00
|
|
|
for (int xx = 0; xx < _region.width; ++xx)
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-15 09:21:23 -04:00
|
|
|
outLineBuffer[(xx * 4) + layer] = inLineBuffer[xx];
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
//update the GPU buffer
|
2013-05-17 17:47:08 -04:00
|
|
|
memcpy(mem->data + yy * _region.width * 4, outLineBuffer, _region.width * 4);
|
|
|
|
inLineBuffer += _region.width;
|
2013-05-15 09:21:23 -04:00
|
|
|
outLineBuffer += m_textureSize * 4;
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
|
|
|
}
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-05-17 17:47:08 -04:00
|
|
|
bgfx::updateTextureCube(m_textureHandle, (uint8_t)_region.getFaceIndex(), 0, _region.x, _region.y, _region.width, _region.height, mem);
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
|
|
|
|
2013-05-30 01:47:19 -04:00
|
|
|
void Atlas::packFaceLayerUV(uint32_t _idx, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-04-23 14:36:07 -04:00
|
|
|
packUV(m_layers[_idx].faceRegion, _vertexBuffer, _offset, _stride);
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
|
|
|
|
2013-05-30 01:47:19 -04:00
|
|
|
void Atlas::packUV(uint16_t _regionHandle, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-30 01:47:19 -04:00
|
|
|
const AtlasRegion& region = m_regions[_regionHandle];
|
2013-04-23 14:36:07 -04:00
|
|
|
packUV(region, _vertexBuffer, _offset, _stride);
|
2013-04-22 16:42:11 -04:00
|
|
|
}
|
|
|
|
|
2013-05-30 01:47:19 -04:00
|
|
|
void Atlas::packUV(const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-30 01:47:19 -04:00
|
|
|
static const int16_t minVal = INT16_MIN;
|
|
|
|
static const int16_t maxVal = INT16_MAX;
|
2013-05-17 18:11:54 -04:00
|
|
|
float texMult = (float)(maxVal - minVal) / ( (float)(m_textureSize) );
|
|
|
|
|
2013-05-30 01:47:19 -04:00
|
|
|
int16_t x0 = (int16_t)( ((float)_region.x * texMult) - float(INT16_MAX) );
|
|
|
|
int16_t y0 = (int16_t)( ((float)_region.y * texMult) - float(INT16_MAX) );
|
|
|
|
int16_t x1 = (int16_t)( (((float)_region.x + _region.width) * texMult) - float(INT16_MAX) );
|
|
|
|
int16_t y1 = (int16_t)( (((float)_region.y + _region.height) * texMult) - float(INT16_MAX) );
|
|
|
|
int16_t w = (int16_t) ( (float(INT16_MAX) / 4.0f) * (float) _region.getComponentIndex() );
|
2013-05-15 09:21:23 -04:00
|
|
|
|
|
|
|
_vertexBuffer += _offset;
|
|
|
|
switch (_region.getFaceIndex() )
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
|
|
|
case 0: // +X
|
2013-05-15 09:21:23 -04:00
|
|
|
x0 = -x0;
|
|
|
|
x1 = -x1;
|
|
|
|
y0 = -y0;
|
|
|
|
y1 = -y1;
|
|
|
|
writeUV(_vertexBuffer, maxVal, y0, x0, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, maxVal, y1, x0, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, maxVal, y1, x1, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, maxVal, y0, x1, w); _vertexBuffer += _stride;
|
2013-04-22 16:42:11 -04:00
|
|
|
break;
|
2013-05-15 09:21:23 -04:00
|
|
|
|
|
|
|
case 1: // -X
|
|
|
|
y0 = -y0;
|
|
|
|
y1 = -y1;
|
|
|
|
writeUV(_vertexBuffer, minVal, y0, x0, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, minVal, y1, x0, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, minVal, y1, x1, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, minVal, y0, x1, w); _vertexBuffer += _stride;
|
2013-04-22 16:42:11 -04:00
|
|
|
break;
|
2013-05-15 09:21:23 -04:00
|
|
|
|
|
|
|
case 2: // +Y
|
|
|
|
writeUV(_vertexBuffer, x0, maxVal, y0, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, x0, maxVal, y1, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, x1, maxVal, y1, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, x1, maxVal, y0, w); _vertexBuffer += _stride;
|
2013-04-22 16:42:11 -04:00
|
|
|
break;
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
case 3: // -Y
|
2013-05-15 09:21:23 -04:00
|
|
|
y0 = -y0;
|
|
|
|
y1 = -y1;
|
|
|
|
writeUV(_vertexBuffer, x0, minVal, y0, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, x0, minVal, y1, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, x1, minVal, y1, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, x1, minVal, y0, w); _vertexBuffer += _stride;
|
2013-04-22 16:42:11 -04:00
|
|
|
break;
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
case 4: // +Z
|
2013-05-15 09:21:23 -04:00
|
|
|
y0 = -y0;
|
|
|
|
y1 = -y1;
|
|
|
|
writeUV(_vertexBuffer, x0, y0, maxVal, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, x0, y1, maxVal, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, x1, y1, maxVal, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, x1, y0, maxVal, w); _vertexBuffer += _stride;
|
2013-04-22 16:42:11 -04:00
|
|
|
break;
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
case 5: // -Z
|
2013-05-15 09:21:23 -04:00
|
|
|
x0 = -x0;
|
|
|
|
x1 = -x1;
|
|
|
|
y0 = -y0;
|
|
|
|
y1 = -y1;
|
|
|
|
writeUV(_vertexBuffer, x0, y0, minVal, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, x0, y1, minVal, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, x1, y1, minVal, w); _vertexBuffer += _stride;
|
|
|
|
writeUV(_vertexBuffer, x1, y0, minVal, w); _vertexBuffer += _stride;
|
2013-04-22 16:42:11 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|