uncrustify whole font code

This commit is contained in:
Jeremie Roy 2013-05-15 15:21:23 +02:00
parent 61305ed0e4
commit c4b4b15e37
8 changed files with 1586 additions and 1367 deletions

View file

@ -6,7 +6,7 @@
#include <bgfx.h>
#include <bx/bx.h>
#include <bx/timer.h>
#include <bx/countof.h>
#include <bx/countof.h>
#include "../common/entry.h"
#include "../common/dbg.h"
#include "../common/math.h"
@ -19,12 +19,12 @@
int _main_(int /*_argc*/, char** /*_argv*/)
{
uint32_t width = 1280;
uint32_t width = 1280;
uint32_t height = 720;
uint32_t debug = BGFX_DEBUG_TEXT;
uint32_t reset = 0;
bgfx::init();
bgfx::init();
bgfx::reset(width, height);
@ -33,20 +33,21 @@ int _main_(int /*_argc*/, char** /*_argv*/)
// Set view 0 clear state.
bgfx::setViewClear(0
, BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
, 0x303030ff
, 1.0f
, 0
);
, BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
, 0x303030ff
, 1.0f
, 0
);
//init the text rendering system
FontManager* fontManager = new FontManager(512);
TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
//load some truetype files
const char* fontNames[7] = {
const char* fontNames[7] =
{
"font/droidsans.ttf",
"font/chp-fire.ttf",
"font/chp-fire.ttf",
"font/bleeding_cowboys.ttf",
"font/mias_scribblings.ttf",
"font/ruritania.ttf",
@ -54,11 +55,11 @@ int _main_(int /*_argc*/, char** /*_argv*/)
"font/five_minutes.otf"
};
const uint32_t fontCount = sizeof(fontNames)/sizeof(const char*);
const uint32_t fontCount = sizeof(fontNames) / sizeof(const char*);
TrueTypeHandle fontFiles[fontCount];
FontHandle fonts[fontCount];
for(int32_t ii = 0; ii<fontCount ; ++ii)
for (int32_t ii = 0; ii < fontCount; ++ii)
{
//instantiate a usable font
fontFiles[ii] = fontManager->loadTrueTypeFromFile(fontNames[ii]);
@ -68,43 +69,43 @@ int _main_(int /*_argc*/, char** /*_argv*/)
//You can unload the truetype files at this stage, but in that case, the set of glyph's will be limited to the set of preloaded glyph
fontManager->unloadTrueType(fontFiles[ii]);
}
TrueTypeHandle console_tt = fontManager->loadTrueTypeFromFile("font/visitor1.ttf");
//this font doesn't have any preloaded glyph's but the truetype file is loaded
//so glyph will be generated as needed
FontHandle consola_16 = fontManager->createFontByPixelSize(console_tt, 0, 10);
//create a static text buffer compatible with alpha font
//a static text buffer content cannot be modified after its first submit.
TextBufferHandle staticText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, STATIC);
//the pen position represent the top left of the box of the first line of text
textBufferManager->setPenPosition(staticText, 24.0f, 100.0f);
for(int32_t ii = 0; ii<fontCount ; ++ii)
for (int32_t ii = 0; ii < fontCount; ++ii)
{
//add some text to the buffer
textBufferManager->appendText(staticText, fonts[ii], L"The quick brown fox jumps over the lazy dog\n");
//the position of the pen is adjusted when there is an endline
}
// Now write some styled text
//setup style colors
// Now write some styled text
//setup style colors
textBufferManager->setBackgroundColor(staticText, 0x551111FF);
textBufferManager->setUnderlineColor(staticText, 0xFF2222FF);
textBufferManager->setOverlineColor(staticText, 0x2222FFFF);
textBufferManager->setStrikeThroughColor(staticText, 0x22FF22FF);
textBufferManager->setStrikeThroughColor(staticText, 0x22FF22FF);
//text + bkg
textBufferManager->setStyle(staticText, STYLE_BACKGROUND);
textBufferManager->appendText(staticText, fonts[0], L"The quick ");
//text + strike-through
textBufferManager->setStyle(staticText, STYLE_STRIKE_THROUGH);
textBufferManager->appendText(staticText, fonts[0], L"brown fox ");
//text + overline
textBufferManager->setStyle(staticText, STYLE_OVERLINE);
textBufferManager->appendText(staticText, fonts[0], L"jumps over ");
@ -112,25 +113,25 @@ int _main_(int /*_argc*/, char** /*_argv*/)
//text + underline
textBufferManager->setStyle(staticText, STYLE_UNDERLINE);
textBufferManager->appendText(staticText, fonts[0], L"the lazy ");
//text + bkg + strike-through
textBufferManager->setStyle(staticText, STYLE_BACKGROUND|STYLE_STRIKE_THROUGH);
textBufferManager->setStyle(staticText, STYLE_BACKGROUND | STYLE_STRIKE_THROUGH);
textBufferManager->appendText(staticText, fonts[0], L"dog\n");
//create a transient buffer for realtime data
TextBufferHandle transientText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, TRANSIENT);
uint32_t w = 0,h = 0;
while (!processEvents(width, height, debug, reset) )
//create a transient buffer for realtime data
TextBufferHandle transientText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, TRANSIENT);
uint32_t w = 0, h = 0;
while (!processEvents(width, height, debug, reset) )
{
if(w!=width|| h!=height)
if (w != width
|| h != height)
{
w=width;
h= height;
printf("ri: %d,%d\n",width,height);
w = width;
h = height;
printf("ri: %d,%d\n", width, height);
}
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, width, height);
@ -143,35 +144,35 @@ int _main_(int /*_argc*/, char** /*_argv*/)
const int64_t frameTime = now - last;
last = now;
const double freq = double(bx::getHPFrequency() );
const double toMs = 1000.0/freq;
const double toMs = 1000.0 / freq;
// Use debug font to print information about this example.
//bgfx::dbgTextClear();
//bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/10-font");
//bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Use the font system to display text and styled text.");
//bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
//Use transient text to display debug information
//Code below is similar to commented code above
wchar_t fpsText[64];
//swprintf(fpsText,L"Frame: % 7.3f[ms]", double(frameTime)*toMs);
swprintf(fpsText, countof(fpsText), L"Frame: % 7.3f[ms]", double(frameTime)*toMs);
swprintf(fpsText, countof(fpsText), L"Frame: % 7.3f[ms]", double(frameTime) * toMs);
textBufferManager->clearTextBuffer(transientText);
textBufferManager->setPenPosition(transientText, 20.0, 4.0f);
textBufferManager->appendText(transientText, consola_16, L"bgfx/examples/10-font\n");
textBufferManager->setPenPosition(transientText, 20.0, 4.0f);
textBufferManager->appendText(transientText, consola_16, L"bgfx/examples/10-font\n");
textBufferManager->appendText(transientText, consola_16, L"Description: Use the font system to display text and styled text.\n");
textBufferManager->appendText(transientText, consola_16, fpsText);
float at[3] = { 0, 0, 0.0f };
float eye[3] = {0, 0, -1.0f };
float view[16];
float proj[16];
mtxLookAt(view, eye, at);
//setup a top-left ortho matrix for screen space drawing
//setup a top-left ortho matrix for screen space drawing
float centering = 0.5f;
mtxOrtho(proj, centering, width+centering,height+centering, centering,-1.0f, 1.0f);
mtxOrtho(proj, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
// Set view and projection matrix for view 0.
bgfx::setViewTransform(0, view, proj);
@ -180,30 +181,29 @@ int _main_(int /*_argc*/, char** /*_argv*/)
textBufferManager->submitTextBuffer(transientText, 0);
//submit the static text
textBufferManager->submitTextBuffer(staticText, 0);
// Advance to next frame. Rendering thread will be kicked to
textBufferManager->submitTextBuffer(staticText, 0);
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
}
fontManager->unloadTrueType(console_tt);
//destroy the fonts
fontManager->destroyFont(consola_16);
for(int32_t ii = 0; ii<fontCount ; ++ii)
fontManager->destroyFont(consola_16);
for (int32_t ii = 0; ii < fontCount; ++ii)
{
fontManager->destroyFont(fonts[ii]);
}
textBufferManager->destroyTextBuffer(staticText);
textBufferManager->destroyTextBuffer(transientText);
textBufferManager->destroyTextBuffer(transientText);
delete textBufferManager;
delete fontManager;
delete fontManager;
// Shutdown bgfx.
bgfx::shutdown();
bgfx::shutdown();
return 0;
}

View file

@ -16,7 +16,7 @@
inline void mtxTranslate(float* _result, float x, float y, float z)
{
memset(_result, 0, sizeof(float)*16);
memset(_result, 0, sizeof(float) * 16);
_result[0] = _result[5] = _result[10] = _result[15] = 1.0f;
_result[12] = x;
_result[13] = y;
@ -25,7 +25,7 @@ inline void mtxTranslate(float* _result, float x, float y, float z)
inline void mtxScale(float* _result, float x, float y, float z)
{
memset(_result, 0, sizeof(float)*16);
memset(_result, 0, sizeof(float) * 16);
_result[0] = x;
_result[5] = y;
_result[10] = z;
@ -34,7 +34,7 @@ inline void mtxScale(float* _result, float x, float y, float z)
int _main_(int /*_argc*/, char** /*_argv*/)
{
uint32_t width = 1280;
uint32_t width = 1280;
uint32_t height = 720;
uint32_t debug = BGFX_DEBUG_TEXT;
uint32_t reset = 0;
@ -48,14 +48,14 @@ int _main_(int /*_argc*/, char** /*_argv*/)
// Set view 0 clear state.
bgfx::setViewClear(0
, BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
//, 0x303030ff
//, 0xffffffff
, 0x000000FF
, 1.0f
, 0
);
, BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
//, 0x303030ff
//, 0xffffffff
, 0x000000FF
, 1.0f
, 0
);
//init the text rendering system
FontManager* fontManager = new FontManager(512);
TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
@ -63,7 +63,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
//load a truetype files
/*
"font/droidsans.ttf",
"font/chp-fire.ttf",
"font/chp-fire.ttf",
"font/bleeding_cowboys.ttf",
"font/mias_scribblings.ttf",
"font/ruritania.ttf",
@ -71,29 +71,28 @@ int _main_(int /*_argc*/, char** /*_argv*/)
"font/five_minutes.otf"
*/
TrueTypeHandle times_tt = fontManager->loadTrueTypeFromFile("font/bleeding_cowboys.ttf");
//create a distance field font
FontHandle distance_font = fontManager->createFontByPixelSize(times_tt, 0, 48, FONT_TYPE_DISTANCE);
//create a scalled down version of the same font (without adding anything to the atlas)
FontHandle smaller_font = fontManager->createScaledFontToPixelSize(distance_font, 32);
//preload glyph and generate (generate bitmap's)
fontManager->preloadGlyph(distance_font, L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,\" \n");
//You can unload the truetype files at this stage, but in that case, the set of glyph's will be limited to the set of preloaded glyph
fontManager->unloadTrueType(times_tt);
TextBufferHandle staticText = textBufferManager->createTextBuffer(FONT_TYPE_DISTANCE, STATIC);
TextBufferHandle staticText = textBufferManager->createTextBuffer(FONT_TYPE_DISTANCE, STATIC);
textBufferManager->setTextColor(staticText, 0xDD0000FF);
//textBufferManager->appendText(staticText, distance_font, L"The quick brown fox jumps over the lazy dog\n");
//textBufferManager->appendText(staticText, distance_font, L"The quick brown fox jumps over the lazy dog\n");
//textBufferManager->appendText(staticText, distance_font, L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
textBufferManager->appendText(staticText, distance_font, L"BGFX ");
textBufferManager->appendText(staticText, smaller_font, L"bgfx");
int64_t timeOffset = bx::getHPCounter();
while (!processEvents(width, height, debug, reset) )
while (!processEvents(width, height, debug, reset) )
{
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, width, height);
@ -107,14 +106,14 @@ int _main_(int /*_argc*/, char** /*_argv*/)
const int64_t frameTime = now - last;
last = now;
const double freq = double(bx::getHPFrequency() );
const double toMs = 1000.0/freq;
float time = (float)( (now - timeOffset)/double(bx::getHPFrequency() ) );
const double toMs = 1000.0 / freq;
float time = (float)( (now - timeOffset) / double(bx::getHPFrequency() ) );
// Use debug font to print information about this example.
bgfx::dbgTextClear();
bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/11-fontsdf");
bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Use a single distance field font to render text of various size.");
bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime) * toMs);
float at[3] = { 0, 0, 0.0f };
float eye[3] = {0, 0, -1.0f };
@ -124,48 +123,48 @@ int _main_(int /*_argc*/, char** /*_argv*/)
mtxLookAt(view, eye, at);
float centering = 0.5f;
//setup a top-left ortho matrix for screen space drawing
mtxOrtho(proj, centering, width+centering,height+centering, centering,-1.0f, 1.0f);
mtxOrtho(proj, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
// Set view and projection matrix for view 0.
bgfx::setViewTransform(0, view, proj);
bgfx::setViewTransform(0, view, proj);
TextRectangle rect = textBufferManager->getRectangle(staticText);
float mtxA[16];
float mtxB[16];
float mtxC[16];
mtxRotateZ(mtxA, time*0.37f);
mtxTranslate(mtxB, -(rect.width*0.5f), -(rect.height*0.5f), 0);
mtxRotateZ(mtxA, time * 0.37f);
mtxTranslate(mtxB, -(rect.width * 0.5f), -(rect.height * 0.5f), 0);
mtxMul(mtxC, mtxB, mtxA);
float scale=4.1f+4.0f*sinf(time);
float scale = 4.1f + 4.0f * sinf(time);
mtxScale(mtxA, scale, scale, 1.0f);
mtxMul(mtxB, mtxC, mtxA);
mtxTranslate(mtxC, ((width)*0.5f), ((height)*0.5f), 0);
mtxTranslate(mtxC, ( (width) * 0.5f), ( (height) * 0.5f), 0);
mtxMul(mtxA, mtxB, mtxC);
// Set model matrix for rendering.
bgfx::setTransform(mtxA);
//draw your text
textBufferManager->submitTextBuffer(staticText, 0);
textBufferManager->submitTextBuffer(staticText, 0);
// Advance to next frame. Rendering thread will be kicked to
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
}
//destroy the fonts
//destroy the fonts
fontManager->destroyFont(distance_font);
fontManager->destroyFont(smaller_font);
textBufferManager->destroyTextBuffer(staticText);
fontManager->destroyFont(smaller_font);
textBufferManager->destroyTextBuffer(staticText);
delete textBufferManager;
delete fontManager;
delete fontManager;
// Shutdown bgfx.
bgfx::shutdown();
bgfx::shutdown();
return 0;
}

View file

@ -5,67 +5,74 @@
#include "cube_atlas.h"
#include <bx/bx.h>
#include <bgfx.h>
#include <bgfx.h>
#include <vector>
//********** Rectangle packer implementation ************
class RectanglePacker
{
{
public:
RectanglePacker();
RectanglePacker(uint32_t _width, uint32_t _height);
/// non constructor initialization
void init(uint32_t _width, uint32_t _height);
/// 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() { return m_usedSpace; }
/// 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();
RectanglePacker();
RectanglePacker(uint32_t _width, uint32_t _height);
/// non constructor initialization
void init(uint32_t _width, uint32_t _height);
/// 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()
{
return m_usedSpace;
}
/// 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();
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
struct Node
{
Node(int16_t _x, int16_t _y, int16_t _width) : m_x(_x), m_y(_y), m_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).
int16_t m_x;
/// The y-coordinate of the skyline level line.
int16_t m_y;
/// The line _width. The ending coordinate (inclusive) will be x+width-1.
int32_t m_width; //32bit to avoid padding
};
}
/// 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;
/// The starting x-coordinate (leftmost).
int16_t m_x;
/// The y-coordinate of the skyline level line.
int16_t m_y;
/// The line _width. The ending coordinate (inclusive) will be x+width-1.
int32_t m_width; //32bit to avoid padding
};
RectanglePacker::RectanglePacker(): m_width(0), m_height(0), m_usedSpace(0)
{
/// 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;
};
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)
{
// 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));
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) );
}
void RectanglePacker::init(uint32_t _width, uint32_t _height)
@ -78,32 +85,33 @@ void RectanglePacker::init(uint32_t _width, uint32_t _height)
m_skyline.clear();
// We want a one pixel border around the whole atlas to avoid any artifact when
// sampling texture
m_skyline.push_back(Node(1,1, _width-2));
// sampling texture
m_skyline.push_back(Node(1, 1, _width - 2) );
}
bool RectanglePacker::addRectangle(uint16_t _width, uint16_t _height, uint16_t& _outX, uint16_t& _outY)
{
int y, best_height, best_index;
int32_t best_width;
Node* node;
Node* prev;
_outX = 0;
int32_t best_width;
Node* node;
Node* prev;
_outX = 0;
_outY = 0;
uint32_t ii;
best_height = INT_MAX;
best_index = -1;
best_width = INT_MAX;
for( ii = 0; ii < m_skyline.size(); ++ii )
best_height = INT_MAX;
best_index = -1;
best_width = INT_MAX;
for (ii = 0; ii < m_skyline.size(); ++ii)
{
y = fit( ii, _width, _height );
if( y >= 0 )
y = fit(ii, _width, _height);
if (y >= 0)
{
node = &m_skyline[ii];
if( ( (y + _height) < best_height ) ||
( ((y + _height) == best_height) && (node->m_width < best_width)) )
node = &m_skyline[ii];
if ( ( (y + _height) < best_height)
|| ( ( (y + _height) == best_height)
&& (node->m_width < best_width) ) )
{
best_height = y + _height;
best_index = ii;
@ -111,116 +119,124 @@ bool RectanglePacker::addRectangle(uint16_t _width, uint16_t _height, uint16_t&
_outX = node->m_x;
_outY = y;
}
}
}
}
}
if( best_index == -1 )
{
if (best_index == -1)
{
return false;
}
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];
if (node->m_x < (prev->m_x + prev->m_width) )
{
int shrink = prev->m_x + prev->m_width - node->m_x;
node->m_x += shrink;
node->m_width -= shrink;
if (node->m_width <= 0)
{
m_skyline.erase(m_skyline.begin() + ii);
--ii;
}
else
{
break;
}
}
else
{
break;
}
}
Node newNode(_outX, _outY + _height, _width);
m_skyline.insert(m_skyline.begin() + best_index, newNode);
merge();
m_usedSpace += _width * _height;
return true;
for (ii = best_index + 1; ii < m_skyline.size(); ++ii)
{
node = &m_skyline[ii];
prev = &m_skyline[ii - 1];
if (node->m_x < (prev->m_x + prev->m_width) )
{
int shrink = prev->m_x + prev->m_width - node->m_x;
node->m_x += shrink;
node->m_width -= shrink;
if (node->m_width <= 0)
{
m_skyline.erase(m_skyline.begin() + ii);
--ii;
}
else
{
break;
}
}
else
{
break;
}
}
merge();
m_usedSpace += _width * _height;
return true;
}
float RectanglePacker::getUsageRatio()
{
uint32_t total = m_width*m_height;
if(total > 0)
{
uint32_t total = m_width * m_height;
if (total > 0)
{
return (float) m_usedSpace / (float) total;
}
else
{
return 0.0f;
}
}
void RectanglePacker::clear()
{
m_skyline.clear();
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));
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) );
}
int32_t RectanglePacker::fit(uint32_t _skylineNodeIndex, uint16_t _width, uint16_t _height)
{
int32_t width = _width;
int32_t height = _height;
const Node& baseNode = m_skyline[_skylineNodeIndex];
int32_t x = baseNode.m_x, y;
int32_t height = _height;
const Node& baseNode = m_skyline[_skylineNodeIndex];
int32_t x = baseNode.m_x, y;
int32_t _width_left = width;
int32_t i = _skylineNodeIndex;
if ( (x + width) > (int32_t)(m_width-1) )
{
return -1;
}
y = baseNode.m_y;
while( _width_left > 0 )
if ( (x + width) > (int32_t)(m_width - 1) )
{
const Node& node = m_skyline[i];
if( node.m_y > y )
{
y = node.m_y;
}
if( (y + height) > (int32_t)(m_height-1) )
{
return -1;
}
y = baseNode.m_y;
while (_width_left > 0)
{
const Node& node = m_skyline[i];
if (node.m_y > y)
{
y = node.m_y;
}
if ( (y + height) > (int32_t)(m_height - 1) )
{
return -1;
}
_width_left -= node.m_width;
}
_width_left -= node.m_width;
++i;
}
return y;
}
void RectanglePacker::merge()
{
Node* node;
Node* next;
uint32_t ii;
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];
if( node->m_y == next->m_y )
for (ii = 0; ii < m_skyline.size() - 1; ++ii)
{
node = (Node*) &m_skyline[ii];
next = (Node*) &m_skyline[ii + 1];
if (node->m_y == next->m_y)
{
node->m_width += next->m_width;
m_skyline.erase(m_skyline.begin() + ii + 1);
m_skyline.erase(m_skyline.begin() + ii + 1);
--ii;
}
}
}
}
//********** Cube Atlas implementation ************
@ -231,15 +247,18 @@ struct Atlas::PackedLayer
AtlasRegion faceRegion;
};
Atlas::Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount )
Atlas::Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount)
{
BX_CHECK(_textureSize >= 64 && _textureSize <= 4096, "suspicious texture size" );
BX_CHECK(_maxRegionsCount >= 64 && _maxRegionsCount <= 32000, "suspicious _regions count" );
BX_CHECK(_textureSize >= 64
&& _textureSize <= 4096, "suspicious texture size");
BX_CHECK(_maxRegionsCount >= 64
&& _maxRegionsCount <= 32000, "suspicious _regions count");
m_layers = new PackedLayer[24];
for(int ii=0; ii<24;++ii)
for (int ii = 0; ii < 24; ++ii)
{
m_layers[ii].packer.init(_textureSize, _textureSize);
m_layers[ii].packer.init(_textureSize, _textureSize);
}
m_usedLayers = 0;
m_usedFaces = 0;
@ -248,28 +267,29 @@ Atlas::Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount )
m_maxRegionCount = _maxRegionsCount;
m_regions = new AtlasRegion[_maxRegionsCount];
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_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC|BGFX_TEXTURE_MIP_POINT
//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;
//Uncomment this to debug atlas
//const bgfx::Memory* mem = bgfx::alloc(textureSize*textureSize * 6 * 4);
//memset(mem->data, 255, mem->size);
const bgfx::Memory* mem = NULL;
//memset(mem->data, 255, mem->size);
const bgfx::Memory* mem = NULL;
m_textureHandle = bgfx::createTextureCube(6
, _textureSize
, 1
, bgfx::TextureFormat::BGRA8
, flags
,mem
);
, _textureSize
, 1
, bgfx::TextureFormat::BGRA8
, flags
, mem
);
}
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)
{
BX_CHECK(_regionCount <= 64 && _maxRegionsCount <= 4096, "suspicious initialization");
BX_CHECK(_regionCount <= 64
&& _maxRegionsCount <= 4096, "suspicious initialization");
//layers are frozen
m_usedLayers = 24;
m_usedFaces = 6;
@ -277,27 +297,32 @@ Atlas::Atlas(uint16_t _textureSize, const uint8_t* _textureBuffer , uint16_t _re
m_textureSize = _textureSize;
m_regionCount = _regionCount;
//regions are frozen
if(_regionCount < _maxRegionsCount)
if (_regionCount < _maxRegionsCount)
{
m_maxRegionCount = _regionCount;
}
else
{
m_maxRegionCount = _maxRegionsCount;
}
m_regions = new AtlasRegion[_regionCount];
m_textureBuffer = new uint8_t[getTextureBufferSize()];
//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
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());
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() );
m_textureHandle = bgfx::createTextureCube(6
, _textureSize
, 1
, bgfx::TextureFormat::BGRA8
, flags
, bgfx::makeRef(m_textureBuffer, getTextureBufferSize())
);
, _textureSize
, 1
, bgfx::TextureFormat::BGRA8
, flags
, bgfx::makeRef(m_textureBuffer, getTextureBufferSize() )
);
}
Atlas::~Atlas()
@ -307,183 +332,198 @@ Atlas::~Atlas()
delete[] m_textureBuffer;
}
uint16_t Atlas::addRegion(uint16_t _width, uint16_t _height, const uint8_t* _bitmapBuffer, AtlasRegion::Type _type, uint16_t outline)
uint16_t Atlas::addRegion(uint16_t _width, uint16_t _height, const uint8_t* _bitmapBuffer, AtlasRegion::Type _type, uint16_t outline)
{
if (m_regionCount >= m_maxRegionCount)
{
return UINT16_MAX;
}
uint16_t x=0,y=0;
uint16_t x = 0, y = 0;
// We want each bitmap to be separated by at least one black pixel
// TODO manage mipmaps
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++;
}
if(idx >= m_usedLayers)
if (idx >= m_usedLayers)
{
//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;
}
//create new layers
for(int ii=0; ii < _type; ++ii)
{
m_layers[idx+ii].faceRegion.setMask(_type, m_usedFaces, ii);
return UINT16_MAX;
}
//create new layers
for (int ii = 0; ii < _type; ++ii)
{
m_layers[idx + ii].faceRegion.setMask(_type, m_usedFaces, ii);
}
m_usedLayers += _type;
m_usedFaces++;
//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;
}
}
AtlasRegion& region = m_regions[m_regionCount];
region.m_x = x ;
region.m_y = y ;
region.m_x = x;
region.m_y = y;
region.m_width = _width;
region.m_height = _height;
region.m_mask = m_layers[idx].faceRegion.m_mask;
updateRegion(region, _bitmapBuffer);
region.m_x += outline;
region.m_y += outline;
region.m_width -= (outline*2);
region.m_height -= (outline*2);
region.m_width -= (outline * 2);
region.m_height -= (outline * 2);
return m_regionCount++;
}
void Atlas::updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffer)
{
{
const bgfx::Memory* mem = bgfx::alloc(_region.m_width * _region.m_height * 4);
//BAD!
memset(mem->data,0, mem->size);
if(_region.getType() == AtlasRegion::TYPE_BGRA8)
{
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.m_y *m_textureSize)+_region.m_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
for(int yy = 0; yy < _region.m_height; ++yy)
for (int yy = 0; yy < _region.m_height; ++yy)
{
memcpy(outLineBuffer, inLineBuffer, _region.m_width * 4);
inLineBuffer += _region.m_width*4;
outLineBuffer += m_textureSize*4;
inLineBuffer += _region.m_width * 4;
outLineBuffer += m_textureSize * 4;
}
//update the GPU buffer
memcpy(mem->data, _bitmapBuffer, mem->size);
}else
}
else
{
uint32_t layer = _region.getComponentIndex();
//uint32_t face = _region.getFaceIndex();
const uint8_t* inLineBuffer = _bitmapBuffer;
uint8_t* outLineBuffer = (m_textureBuffer + _region.getFaceIndex() * (m_textureSize*m_textureSize*4) + (((_region.m_y *m_textureSize)+_region.m_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
for(int yy = 0; yy<_region.m_height; ++yy)
for (int yy = 0; yy < _region.m_height; ++yy)
{
for(int xx = 0; xx<_region.m_width; ++xx)
for (int xx = 0; xx < _region.m_width; ++xx)
{
outLineBuffer[(xx*4) + layer] = inLineBuffer[xx];
outLineBuffer[(xx * 4) + layer] = inLineBuffer[xx];
}
//update the GPU buffer
memcpy(mem->data + yy*_region.m_width*4, outLineBuffer, _region.m_width*4);
memcpy(mem->data + yy * _region.m_width * 4, outLineBuffer, _region.m_width * 4);
inLineBuffer += _region.m_width;
outLineBuffer += m_textureSize*4;
outLineBuffer += m_textureSize * 4;
}
}
bgfx::updateTextureCube(m_textureHandle, (uint8_t)_region.getFaceIndex(), 0, _region.m_x, _region.m_y, _region.m_width, _region.m_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);
}
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];
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 maxVal = 32767;
int16_t x0 = (int16_t)(_region.m_x * texMult)-32768;
int16_t y0 = (int16_t)(_region.m_y * texMult)-32768;
int16_t x1 = (int16_t)((_region.m_x + _region.m_width)* 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());
_vertexBuffer+=_offset;
switch(_region.getFaceIndex())
int16_t x0 = (int16_t)(_region.m_x * texMult) - 32768;
int16_t y0 = (int16_t)(_region.m_y * texMult) - 32768;
int16_t x1 = (int16_t)( (_region.m_x + _region.m_width) * 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() );
_vertexBuffer += _offset;
switch (_region.getFaceIndex() )
{
case 0: // +X
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;
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;
break;
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;
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;
break;
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;
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;
break;
case 3: // -Y
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;
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;
break;
case 4: // +Z
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;
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;
break;
case 5: // -Z
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;
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;
break;
}
}

View file

@ -15,7 +15,7 @@
/// algorithm based on C++ sources provided by Jukka Jylänki at:
/// http://clb.demon.fi/files/RectangleBinPack/
#include <bgfx.h>
#include <bgfx.h>
struct AtlasRegion
{
@ -23,115 +23,147 @@ struct AtlasRegion
{
TYPE_GRAY = 1, // 1 component
TYPE_BGRA8 = 4 // 4 components
};
};
uint16_t m_x, m_y;
uint16_t m_width, m_height;
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) ((m_mask >> 0) & 0x0000000F); }
uint32_t getFaceIndex()const { return (m_mask >> 4) & 0x0000000F; }
uint32_t getComponentIndex()const { return (m_mask >> 8) & 0x0000000F; }
void setMask(Type _type, uint32_t _faceIndex, uint32_t _componentIndex) { m_mask = (_componentIndex << 8) + (_faceIndex << 4) + (uint32_t)_type; }
Type getType() const
{
return (Type) ( (m_mask >> 0) & 0x0000000F);
}
uint32_t getFaceIndex() const
{
return (m_mask >> 4) & 0x0000000F;
}
uint32_t getComponentIndex() const
{
return (m_mask >> 8) & 0x0000000F;
}
void setMask(Type _type, uint32_t _faceIndex, uint32_t _componentIndex)
{
m_mask = (_componentIndex << 8) + (_faceIndex << 4) + (uint32_t)_type;
}
};
class Atlas
{
public:
/// 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 maxRegionCount maximum number of region allowed in the atlas
Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount = 4096);
/// 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 textureBuffer buffer of size 6*textureSize*textureSize*sizeof(uint32_t) (will be copied)
/// @param regionCount number of region in the Atlas
/// @param regionBuffer buffer containing the region (will be copied)
/// @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();
/// 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 outline = 0);
/// 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 maxRegionCount maximum number of region allowed in the atlas
Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount = 4096);
/// update a preallocated region
void updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffer);
/// 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 textureBuffer buffer of size 6*textureSize*textureSize*sizeof(uint32_t) (will be copied)
/// @param regionCount number of region in the Atlas
/// @param regionBuffer buffer containing the region (will be copied)
/// @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();
/// Pack the UV coordinates of the four corners of a region to a vertex buffer using the supplied vertex format.
/// v0 -- v3
/// | | encoded in that order: v0,v1,v2,v3
/// v1 -- v2
/// @remark the UV are four signed short normalized components.
/// @remark the x,y,z components encode cube uv coordinates. The w component encode the color channel if any.
/// @param handle handle to the region we are interested in
/// @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 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( 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
void packFaceLayerUV(uint32_t _idx, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride );
/// 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 outline = 0);
/// 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 )
{
_indexBuffer[_startIndex+0] = _startVertex+0;
_indexBuffer[_startIndex+1] = _startVertex+1;
_indexBuffer[_startIndex+2] = _startVertex+2;
_indexBuffer[_startIndex+3] = _startVertex+0;
_indexBuffer[_startIndex+4] = _startVertex+2;
_indexBuffer[_startIndex+5] = _startVertex+3;
}
/// update a preallocated region
void updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffer);
/// return the TextureHandle (cube) of the atlas
bgfx::TextureHandle getTextureHandle() const { return m_textureHandle; }
/// Pack the UV coordinates of the four corners of a region to a vertex buffer using the supplied vertex format.
/// v0 -- v3
/// | | encoded in that order: v0,v1,v2,v3
/// v1 -- v2
/// @remark the UV are four signed short normalized components.
/// @remark the x,y,z components encode cube uv coordinates. The w component encode the color channel if any.
/// @param handle handle to the region we are interested in
/// @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 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(const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride);
//retrieve a region info
const AtlasRegion& getRegion(uint16_t _handle) const { return m_regions[_handle]; }
/// retrieve the size of side of a texture in pixels
uint16_t getTextureSize(){ return m_textureSize; }
/// 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);
/// retrieve the usage ratio of the atlas
//float getUsageRatio() const { return 0.0f; }
/// 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)
{
_indexBuffer[_startIndex + 0] = _startVertex + 0;
_indexBuffer[_startIndex + 1] = _startVertex + 1;
_indexBuffer[_startIndex + 2] = _startVertex + 2;
_indexBuffer[_startIndex + 3] = _startVertex + 0;
_indexBuffer[_startIndex + 4] = _startVertex + 2;
_indexBuffer[_startIndex + 5] = _startVertex + 3;
}
/// retrieve the numbers of region in the atlas
uint16_t getRegionCount() const { return m_regionCount; }
/// return the TextureHandle (cube) of the atlas
bgfx::TextureHandle getTextureHandle() const
{
return m_textureHandle;
}
/// retrieve a pointer to the region buffer (in order to serialize it)
const AtlasRegion* getRegionBuffer() const { return m_regions; }
/// retrieve the byte size of the texture
uint32_t getTextureBufferSize() const { return 6*m_textureSize*m_textureSize*4; }
//retrieve a region info
const AtlasRegion& getRegion(uint16_t _handle) const
{
return m_regions[_handle];
}
/// retrieve the mirrored texture buffer (to serialize it)
const uint8_t* getTextureBuffer() const { return m_textureBuffer; }
/// retrieve the size of side of a texture in pixels
uint16_t getTextureSize()
{
return m_textureSize;
}
/// retrieve the usage ratio of the atlas
//float getUsageRatio() const { return 0.0f; }
/// retrieve the numbers of region in the atlas
uint16_t getRegionCount() const
{
return m_regionCount;
}
/// retrieve a pointer to the region buffer (in order to serialize it)
const AtlasRegion* getRegionBuffer() const
{
return m_regions;
}
/// retrieve the byte size of the texture
uint32_t getTextureBufferSize() const
{
return 6 * m_textureSize * m_textureSize * 4;
}
/// retrieve the mirrored texture buffer (to serialize it)
const uint8_t* getTextureBuffer() const
{
return m_textureBuffer;
}
private:
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)[1] = _y;
((uint16_t*) _vertexBuffer)[2] = _z;
((uint16_t*) _vertexBuffer)[3] = _w;
}
struct PackedLayer;
PackedLayer* m_layers;
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)[1] = _y;
( (uint16_t*) _vertexBuffer)[2] = _z;
( (uint16_t*) _vertexBuffer)[3] = _w;
}
struct PackedLayer;
PackedLayer* m_layers;
uint32_t m_usedLayers;
uint32_t m_usedFaces;
uint32_t m_usedLayers;
uint32_t m_usedFaces;
bgfx::TextureHandle m_textureHandle;
uint16_t m_textureSize;
bgfx::TextureHandle m_textureHandle;
uint16_t m_textureSize;
uint16_t m_regionCount;
uint16_t m_maxRegionCount;
AtlasRegion* m_regions;
uint8_t* m_textureBuffer;
uint16_t m_regionCount;
uint16_t m_maxRegionCount;
AtlasRegion* m_regions;
uint8_t* m_textureBuffer;
};
#endif // __CUBE_ATLAS_H__

File diff suppressed because it is too large Load diff

View file

@ -12,7 +12,7 @@
class Atlas;
enum FontType
{
FONT_TYPE_ALPHA = 0x00000100 , // L8
FONT_TYPE_ALPHA = 0x00000100, // L8
//FONT_TYPE_LCD = 0x00000200, // BGRA8
//FONT_TYPE_RGBA = 0x00000300, // BGRA8
FONT_TYPE_DISTANCE = 0x00000400, // L8
@ -21,7 +21,7 @@ enum FontType
struct FontInfo
{
//the font height in pixel
//the font height in pixel
uint16_t pixelSize;
/// Rendering type used for the font
int16_t fontType;
@ -36,7 +36,7 @@ struct FontInfo
float underline_thickness;
/// The position of the underline relatively to the baseline
float underline_position;
//scale to apply to glyph data
float scale;
};
@ -47,26 +47,26 @@ struct FontInfo
// xmin xmax
// | |
// |<-------- width -------->|
// | |
// | |
// | +-------------------------+----------------- ymax
// | | ggggggggg ggggg | ^ ^
// | | g:::::::::ggg::::g | | |
// | | g:::::::::::::::::g | | |
// | | g::::::ggggg::::::gg | | |
// | | g:::::g g:::::g | | |
// offset_x -|-------->| g:::::g g:::::g | offset_y |
// | | g:::::g g:::::g | | |
// | | g::::::g g:::::g | | |
// | | g:::::::ggggg:::::g | | |
// | | g:::::::::ggg::::g | | |
// | | g:::::::::::::::::g | | |
// | | g::::::ggggg::::::gg | | |
// | | g:::::g g:::::g | | |
// offset_x -|-------->| g:::::g g:::::g | offset_y |
// | | g:::::g g:::::g | | |
// | | g::::::g g:::::g | | |
// | | g:::::::ggggg:::::g | | |
// | | g::::::::::::::::g | | height
// | | gg::::::::::::::g | | |
// | | gg::::::::::::::g | | |
// baseline ---*---------|---- gggggggg::::::g-----*-------- |
// / | | g:::::g | |
// origin | | gggggg g:::::g | |
// | | g:::::gg gg:::::g | |
// | | g::::::ggg:::::::g | |
// | | gg:::::::::::::g | |
// | | ggg::::::ggg | |
// / | | g:::::g | |
// origin | | gggggg g:::::g | |
// | | g:::::gg gg:::::g | |
// | | g::::::ggg:::::::g | |
// | | gg:::::::::::::g | |
// | | ggg::::::ggg | |
// | | gggggg | v
// | +-------------------------+----------------- ymin
// | |
@ -75,18 +75,18 @@ struct FontInfo
/// Unicode value of a character
typedef int32_t CodePoint_t;
/// A structure that describe a glyph.
/// A structure that describe a glyph.
struct GlyphInfo
{
/// Index for faster retrieval
int32_t glyphIndex;
/// Glyph's width in pixels.
float width;
/// Glyph's height in pixels.
float height;
/// Glyph's left offset in pixels
float offset_x;
@ -98,15 +98,15 @@ struct GlyphInfo
/// For horizontal text layouts, this is the unscaled horizontal distance in pixels
/// used to increment the pen position when the glyph is drawn as part of a string of text.
float advance_x;
/// For vertical text layouts, this is the unscaled vertical distance in pixels
/// used to increment the pen position when the glyph is drawn as part of a string of text.
float advance_y;
/// region index in the atlas storing textures
uint16_t regionIndex;
///32 bits alignment
int16_t padding;
int16_t padding;
};
BGFX_HANDLE(TrueTypeHandle);
@ -115,94 +115,100 @@ BGFX_HANDLE(FontHandle);
class FontManager
{
public:
/// create the font manager using an external cube atlas (doesn't take ownership of the atlas)
FontManager(Atlas* _atlas);
/// create the font manager and create the texture cube as BGRA8 with linear filtering
FontManager(uint32_t _textureSideWidth = 512);
/// create the font manager using an external cube atlas (doesn't take ownership of the atlas)
FontManager(Atlas* _atlas);
/// create the font manager and create the texture cube as BGRA8 with linear filtering
FontManager(uint32_t _textureSideWidth = 512);
~FontManager();
~FontManager();
/// retrieve the atlas used by the font manager (e.g. to add stuff to it)
Atlas* getAtlas() { return m_atlas; }
/// load a TrueType font from a file path
/// @return invalid handle if the loading fail
TrueTypeHandle loadTrueTypeFromFile(const char* _fontPath);
/// retrieve the atlas used by the font manager (e.g. to add stuff to it)
Atlas* getAtlas()
{
return m_atlas;
}
/// load a TrueType font from a given buffer.
/// the buffer is copied and thus can be freed or reused after this call
/// @return invalid handle if the loading fail
TrueTypeHandle loadTrueTypeFromMemory(const uint8_t* _buffer, uint32_t _size);
/// load a TrueType font from a file path
/// @return invalid handle if the loading fail
TrueTypeHandle loadTrueTypeFromFile(const char* _fontPath);
/// unload a TrueType font (free font memory) but keep loaded glyphs
void unloadTrueType(TrueTypeHandle _handle);
/// return a font whose height is a fixed pixel size
FontHandle createFontByPixelSize(TrueTypeHandle _handle, uint32_t _typefaceIndex, uint32_t _pixelSize, FontType _fontType = FONT_TYPE_ALPHA);
/// load a TrueType font from a given buffer.
/// the buffer is copied and thus can be freed or reused after this call
/// @return invalid handle if the loading fail
TrueTypeHandle loadTrueTypeFromMemory(const uint8_t* _buffer, uint32_t _size);
/// return a scaled child font whose height is a fixed pixel size
FontHandle createScaledFontToPixelSize(FontHandle _baseFontHandle, uint32_t _pixelSize);
/// unload a TrueType font (free font memory) but keep loaded glyphs
void unloadTrueType(TrueTypeHandle _handle);
/// load a baked font (the set of glyph is fixed)
/// @return INVALID_HANDLE if the loading fail
FontHandle loadBakedFontFromFile(const char* _imagePath, const char* _descriptorPath);
/// return a font whose height is a fixed pixel size
FontHandle createFontByPixelSize(TrueTypeHandle _handle, uint32_t _typefaceIndex, uint32_t _pixelSize, FontType _fontType = FONT_TYPE_ALPHA);
/// load a baked font (the set of glyph is fixed)
/// @return INVALID_HANDLE if the loading fail
FontHandle loadBakedFontFromMemory(const uint8_t* _imageBuffer, uint32_t _imageSize, const uint8_t* _descriptorBuffer, uint32_t _descriptorSize);
/// return a scaled child font whose height is a fixed pixel size
FontHandle createScaledFontToPixelSize(FontHandle _baseFontHandle, uint32_t _pixelSize);
/// destroy a font (truetype or baked)
void destroyFont(FontHandle _handle);
/// load a baked font (the set of glyph is fixed)
/// @return INVALID_HANDLE if the loading fail
FontHandle loadBakedFontFromFile(const char* _imagePath, const char* _descriptorPath);
/// Preload a set of glyphs from a TrueType file
/// @return true if every glyph could be preloaded, false otherwise
/// if the Font is a baked font, this only do validation on the characters
bool preloadGlyph(FontHandle _handle, const wchar_t* _string);
/// load a baked font (the set of glyph is fixed)
/// @return INVALID_HANDLE if the loading fail
FontHandle loadBakedFontFromMemory(const uint8_t* _imageBuffer, uint32_t _imageSize, const uint8_t* _descriptorBuffer, uint32_t _descriptorSize);
/// Preload a single glyph, return true on success
bool preloadGlyph(FontHandle _handle, CodePoint_t _character);
/// destroy a font (truetype or baked)
void destroyFont(FontHandle _handle);
/// bake a font to disk (the set of preloaded glyph)
/// @return true if the baking succeed, false otherwise
bool saveBakedFont(FontHandle _handle, const char* _fontDirectory, const char* _fontName );
/// return the font descriptor of a font
/// @remark the handle is required to be valid
const FontInfo& getFontInfo(FontHandle _handle);
/// Return the rendering informations about the glyph region
/// Load the glyph from a TrueType font if possible
/// @return true if the Glyph is available
bool getGlyphInfo(FontHandle _handle, CodePoint_t _codePoint, GlyphInfo& _outInfo);
/// Preload a set of glyphs from a TrueType file
/// @return true if every glyph could be preloaded, false otherwise
/// if the Font is a baked font, this only do validation on the characters
bool preloadGlyph(FontHandle _handle, const wchar_t* _string);
GlyphInfo& getBlackGlyph(){ return m_blackGlyph; }
/// Preload a single glyph, return true on success
bool preloadGlyph(FontHandle _handle, CodePoint_t _character);
class TrueTypeFont; //public to shut off Intellisense warning
/// bake a font to disk (the set of preloaded glyph)
/// @return true if the baking succeed, false otherwise
bool saveBakedFont(FontHandle _handle, const char* _fontDirectory, const char* _fontName);
/// return the font descriptor of a font
/// @remark the handle is required to be valid
const FontInfo& getFontInfo(FontHandle _handle);
/// Return the rendering informations about the glyph region
/// Load the glyph from a TrueType font if possible
/// @return true if the Glyph is available
bool getGlyphInfo(FontHandle _handle, CodePoint_t _codePoint, GlyphInfo& _outInfo);
GlyphInfo& getBlackGlyph()
{
return m_blackGlyph;
}
class TrueTypeFont; //public to shut off Intellisense warning
private:
struct CachedFont;
struct CachedFile
{
uint8_t* buffer;
uint32_t bufferSize;
};
void init();
bool addBitmap(GlyphInfo& _glyphInfo, const uint8_t* _data);
struct CachedFont;
struct CachedFile
{
uint8_t* buffer;
uint32_t bufferSize;
};
bool m_ownAtlas;
Atlas* m_atlas;
bx::HandleAlloc m_fontHandles;
CachedFont* m_cachedFonts;
bx::HandleAlloc m_filesHandles;
CachedFile* m_cachedFiles;
GlyphInfo m_blackGlyph;
void init();
bool addBitmap(GlyphInfo& _glyphInfo, const uint8_t* _data);
//temporary buffer to raster glyph
uint8_t* m_buffer;
bool m_ownAtlas;
Atlas* m_atlas;
bx::HandleAlloc m_fontHandles;
CachedFont* m_cachedFonts;
bx::HandleAlloc m_filesHandles;
CachedFile* m_cachedFiles;
GlyphInfo m_blackGlyph;
//temporary buffer to raster glyph
uint8_t* m_buffer;
};
#endif // __FONT_MANAGER_H__

File diff suppressed because it is too large Load diff

View file

@ -9,27 +9,27 @@
#include "font_manager.h"
BGFX_HANDLE(TextBufferHandle);
/// type of vertex and index buffer to use with a TextBuffer
enum BufferType
{
STATIC,
DYNAMIC ,
DYNAMIC,
TRANSIENT
};
/// special style effect (can be combined)
enum TextStyleFlags
{
STYLE_NORMAL = 0,
STYLE_OVERLINE = 1,
STYLE_UNDERLINE = 1<<1,
STYLE_STRIKE_THROUGH = 1<<2,
STYLE_BACKGROUND = 1<<3,
STYLE_NORMAL = 0,
STYLE_OVERLINE = 1,
STYLE_UNDERLINE = 1 << 1,
STYLE_STRIKE_THROUGH = 1 << 2,
STYLE_BACKGROUND = 1 << 3,
};
struct TextRectangle
{
{
float width, height;
};
@ -37,63 +37,63 @@ class TextBuffer;
class TextBufferManager
{
public:
TextBufferManager(FontManager* _fontManager);
~TextBufferManager();
TextBufferHandle createTextBuffer(FontType _type, BufferType _bufferType);
void destroyTextBuffer(TextBufferHandle _handle);
void submitTextBuffer(TextBufferHandle _handle, uint8_t _id, int32_t _depth = 0);
void submitTextBufferMask(TextBufferHandle _handle, uint32_t _viewMask, int32_t _depth = 0);
void setStyle(TextBufferHandle _handle, uint32_t _flags = STYLE_NORMAL);
void setTextColor(TextBufferHandle _handle, uint32_t _rgba = 0x000000FF);
void setBackgroundColor(TextBufferHandle _handle, uint32_t _rgba = 0x000000FF);
TextBufferManager(FontManager* _fontManager);
~TextBufferManager();
void setOverlineColor(TextBufferHandle _handle, uint32_t _rgba = 0x000000FF);
void setUnderlineColor(TextBufferHandle _handle, uint32_t _rgba = 0x000000FF);
void setStrikeThroughColor(TextBufferHandle _handle, uint32_t _rgba = 0x000000FF);
void setPenPosition(TextBufferHandle _handle, float _x, float _y);
TextBufferHandle createTextBuffer(FontType _type, BufferType _bufferType);
void destroyTextBuffer(TextBufferHandle _handle);
void submitTextBuffer(TextBufferHandle _handle, uint8_t _id, int32_t _depth = 0);
void submitTextBufferMask(TextBufferHandle _handle, uint32_t _viewMask, int32_t _depth = 0);
/// append an ASCII/utf-8 string to the buffer using current pen position and color
void appendText(TextBufferHandle _handle, FontHandle _fontHandle, const char * _string);
void setStyle(TextBufferHandle _handle, uint32_t _flags = STYLE_NORMAL);
void setTextColor(TextBufferHandle _handle, uint32_t _rgba = 0x000000FF);
void setBackgroundColor(TextBufferHandle _handle, uint32_t _rgba = 0x000000FF);
/// append a wide char unicode string to the buffer using current pen position and color
void appendText(TextBufferHandle _handle, FontHandle _fontHandle, const wchar_t * _string);
void setOverlineColor(TextBufferHandle _handle, uint32_t _rgba = 0x000000FF);
void setUnderlineColor(TextBufferHandle _handle, uint32_t _rgba = 0x000000FF);
void setStrikeThroughColor(TextBufferHandle _handle, uint32_t _rgba = 0x000000FF);
/// Clear the text buffer and reset its state (pen/color)
void clearTextBuffer(TextBufferHandle _handle);
void setPenPosition(TextBufferHandle _handle, float _x, float _y);
TextRectangle getRectangle(TextBufferHandle _handle) const;
/// return the size of the text
//Rectangle measureText(FontHandle fontHandle, const char * _string);
//Rectangle measureText(FontHandle fontHandle, const wchar_t * _string);
/// append an ASCII/utf-8 string to the buffer using current pen position and color
void appendText(TextBufferHandle _handle, FontHandle _fontHandle, const char* _string);
/// append a wide char unicode string to the buffer using current pen position and color
void appendText(TextBufferHandle _handle, FontHandle _fontHandle, const wchar_t* _string);
/// Clear the text buffer and reset its state (pen/color)
void clearTextBuffer(TextBufferHandle _handle);
TextRectangle getRectangle(TextBufferHandle _handle) const;
/// return the size of the text
//Rectangle measureText(FontHandle fontHandle, const char * _string);
//Rectangle measureText(FontHandle fontHandle, const wchar_t * _string);
private:
struct BufferCache
{
uint16_t indexBufferHandle;
uint16_t vertexBufferHandle;
TextBuffer* textBuffer;
BufferType bufferType;
FontType fontType;
};
BufferCache* m_textBuffers;
bx::HandleAlloc m_textBufferHandles;
FontManager* m_fontManager;
bgfx::VertexDecl m_vertexDecl;
bgfx::UniformHandle u_texColor;
bgfx::UniformHandle u_inverse_gamma;
//shaders program
bgfx::ProgramHandle m_basicProgram;
bgfx::ProgramHandle m_distanceProgram;
bgfx::ProgramHandle m_distanceSubpixelProgram;
struct BufferCache
{
uint16_t indexBufferHandle;
uint16_t vertexBufferHandle;
TextBuffer* textBuffer;
BufferType bufferType;
FontType fontType;
};
float m_height;
float m_width;
BufferCache* m_textBuffers;
bx::HandleAlloc m_textBufferHandles;
FontManager* m_fontManager;
bgfx::VertexDecl m_vertexDecl;
bgfx::UniformHandle u_texColor;
bgfx::UniformHandle u_inverse_gamma;
//shaders program
bgfx::ProgramHandle m_basicProgram;
bgfx::ProgramHandle m_distanceProgram;
bgfx::ProgramHandle m_distanceSubpixelProgram;
float m_height;
float m_width;
};
#endif // __TEXT_BUFFER_MANAGER_H__