void Atlas::updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffer) { uint32_t size = _region.width * _region.height * 4; if (0 < size) { const bgfx::Memory* mem = bgfx::alloc(size); memset(mem->data, 0, mem->size); if (_region.getType() == AtlasRegion::TYPE_BGRA8) { const uint8_t* inLineBuffer = _bitmapBuffer; uint8_t* outLineBuffer = m_textureBuffer + _region.getFaceIndex() * (m_textureSize * m_textureSize * 4) + ( ( (_region.y * m_textureSize) + _region.x) * 4); for (int yy = 0; yy < _region.height; ++yy) { memcpy(outLineBuffer, inLineBuffer, _region.width * 4); inLineBuffer += _region.width * 4; outLineBuffer += m_textureSize * 4; } memcpy(mem->data, _bitmapBuffer, mem->size); } else { uint32_t layer = _region.getComponentIndex(); const uint8_t* inLineBuffer = _bitmapBuffer; uint8_t* outLineBuffer = (m_textureBuffer + _region.getFaceIndex() * (m_textureSize * m_textureSize * 4) + ( ( (_region.y * m_textureSize) + _region.x) * 4) ); for (int yy = 0; yy < _region.height; ++yy) { for (int xx = 0; xx < _region.width; ++xx) { outLineBuffer[(xx * 4) + layer] = inLineBuffer[xx]; } memcpy(mem->data + yy * _region.width * 4, outLineBuffer, _region.width * 4); inLineBuffer += _region.width; outLineBuffer += m_textureSize * 4; } } bgfx::updateTextureCube(m_textureHandle, 0, (uint8_t)_region.getFaceIndex(), 0, _region.x, _region.y, _region.width, _region.height, mem); } }
void Atlas::packUV(const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const { int16_t x0 = (int16_t)( ( (float)_region.x * m_texelSize + m_texelOffset[0]) - float(INT16_MAX) ); int16_t y0 = (int16_t)( ( (float)_region.y * m_texelSize + m_texelOffset[1]) - float(INT16_MAX) ); int16_t x1 = (int16_t)( ( ( (float)_region.x + _region.width) * m_texelSize + m_texelOffset[0]) - float(INT16_MAX) ); int16_t y1 = (int16_t)( ( ( (float)_region.y + _region.height) * m_texelSize + m_texelOffset[1]) - float(INT16_MAX) ); int16_t ww = (int16_t)( (float(INT16_MAX) / 4.0f) * (float)_region.getComponentIndex() ); _vertexBuffer += _offset; switch (_region.getFaceIndex() ) { case 0: // +X x0 = -x0; x1 = -x1; y0 = -y0; y1 = -y1; writeUV(_vertexBuffer, INT16_MAX, y0, x0, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, INT16_MAX, y1, x0, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, INT16_MAX, y1, x1, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, INT16_MAX, y0, x1, ww); _vertexBuffer += _stride; break; case 1: // -X y0 = -y0; y1 = -y1; writeUV(_vertexBuffer, INT16_MIN, y0, x0, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, INT16_MIN, y1, x0, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, INT16_MIN, y1, x1, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, INT16_MIN, y0, x1, ww); _vertexBuffer += _stride; break; case 2: // +Y writeUV(_vertexBuffer, x0, INT16_MAX, y0, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, x0, INT16_MAX, y1, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, INT16_MAX, y1, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, INT16_MAX, y0, ww); _vertexBuffer += _stride; break; case 3: // -Y y0 = -y0; y1 = -y1; writeUV(_vertexBuffer, x0, INT16_MIN, y0, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, x0, INT16_MIN, y1, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, INT16_MIN, y1, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, INT16_MIN, y0, ww); _vertexBuffer += _stride; break; case 4: // +Z y0 = -y0; y1 = -y1; writeUV(_vertexBuffer, x0, y0, INT16_MAX, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, x0, y1, INT16_MAX, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, y1, INT16_MAX, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, y0, INT16_MAX, ww); _vertexBuffer += _stride; break; case 5: // -Z x0 = -x0; x1 = -x1; y0 = -y0; y1 = -y1; writeUV(_vertexBuffer, x0, y0, INT16_MIN, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, x0, y1, INT16_MIN, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, y1, INT16_MIN, ww); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, y0, INT16_MIN, ww); _vertexBuffer += _stride; break; } }