Label rendering a bit better

This commit is contained in:
2023-12-14 23:29:54 -06:00
parent 9eee482883
commit 2074cc9211
13 changed files with 201 additions and 61 deletions

View File

@ -38,9 +38,20 @@ struct RenderPassContext &ctx
) {
if(this->components.empty()) return {};
data.projection = ctx.camera->getProjection();
data.view = ctx.camera->getItem()->getWorldTransform();
data.model = this->getItem()->getWorldTransform();
glm::mat4 projection;
glm::mat4 view;
// data.projection = ctx.camera->getProjection();
// data.view = ctx.camera->getItem()->getWorldTransform();
// data.model = this->getItem()->getWorldTransform();
data.projection = glm::ortho(
0.0f, 1280.0f,
720.0f, 0.0f,
0.0f, 1.0f
);
data.view = glm::mat4(1.0f);
data.model = glm::mat4(1.0f);
this->passes.clear();
this->textureBindings.clear();
@ -57,7 +68,6 @@ struct RenderPassContext &ctx
}
flushPass();
std::cout << "Passes: " << passes.size() << "\n";
return passes;
}
@ -65,11 +75,14 @@ void UICanvas::addQuad(
const glm::vec4 quad,
const glm::vec4 uvs,
const struct Color color,
const enum UIShaderQuadStyle style,
const std::shared_ptr<Texture> text
) {
float_t fTexture;
glm::vec4 styleData;
styleData[0] = (float_t)style;
if(text == nullptr) {
fTexture = -1;
styleData[1] = -1;
} else {
shadertexturebinding_t texture;
auto bindingIt = textureBindings.find(text);
@ -84,14 +97,14 @@ void UICanvas::addQuad(
} else {
texture = bindingIt->second;
}
fTexture = (float_t)texture;
styleData[1] = (float_t)texture;
}
data.quads[quadCount] = {
quad,
uvs,
color,
fTexture
styleData
};
quadCount++;
if(quadCount == UI_SHADER_QUAD_COUNT) flushPass();

View File

@ -33,6 +33,11 @@ namespace Dawn {
protected:
virtual void onInit() override;
virtual void onDispose() override;
/**
* Flushes all pending quads to the render pass. This doesn't actually
* render anything, it just flushes the data buffer to a new pass.
*/
void flushPass();
public:
@ -42,11 +47,21 @@ namespace Dawn {
struct RenderPassContext &ctx
) override;
/**
* Adds a quad to the canvas and performs a flush if necessary.
*
* @param quad The quad to add.
* @param uvs The UVs to use for the quad.
* @param color The color to use for the quad.
* @param style Style that the quad should be rendered in.
* @param texture The texture to use for the quad, can be null.
*/
void addQuad(
const glm::vec4 quad,
const glm::vec4 uvs,
const struct Color color,
const std::shared_ptr<Texture> texture
const enum UIShaderQuadStyle style,
const std::shared_ptr<Texture> texture = nullptr
);
};
}

View File

@ -10,6 +10,7 @@ namespace Dawn {
struct TrueTypeCharacter {
glm::vec2 advance;
glm::vec2 size;
glm::vec2 offset;
glm::vec4 quad;
};
}

View File

@ -29,8 +29,8 @@ void TrueTypeTexture::setFace(const FT_Face face) {
// Set the texture size
texture->setSize(
fontSize * 26,
fontSize * 26,
fontSize * 24,
fontSize * 24,
TextureFormat::R,
TextureDataFormat::UNSIGNED_BYTE
);
@ -75,13 +75,17 @@ void TrueTypeTexture::setFace(const FT_Face face) {
// Determine the texture position
if(textureX + face->glyph->bitmap.width >= texture->getWidth()) {
textureX = 0;
textureY += rowHeight + 1;// Tiny gap between rows
textureY += rowHeight + 2;// Tiny gap between rows
rowHeight = face->glyph->bitmap.rows;
} else {
rowHeight = Math::max<int32_t>(rowHeight, face->glyph->bitmap.rows);
}
// Set the quad positions
info.offset = glm::vec2(
face->glyph->bitmap_left,
-face->glyph->bitmap_top
);
info.quad = glm::vec4(
textureX,
textureY,
@ -119,7 +123,7 @@ void TrueTypeTexture::setFace(const FT_Face face) {
}
// Increment textureX
textureX += face->glyph->bitmap.width + 1;// I add a tiny gap between chars
textureX += face->glyph->bitmap.width + 2;// I add a tiny gap between chars
}
this->texture->buffer(buffer);

View File

@ -11,8 +11,27 @@
namespace Dawn {
class UIComponent {
protected:
/**
* Virtual method overridden by the UIComponent to get the quads for the
* component.
*
* @param t The translation of this component already applied.
* @param ctx The canvas to add the quads to.
*/
virtual void getSelfQuads(const glm::vec2 t, UICanvas &ctx) = 0;
/**
* Virtual method overridden by the UIComponent to get the children of
* this component.
*/
virtual std::vector<std::shared_ptr<UIComponent>> getChildren();
/**
* Method called by the UICanvas to get the quads for this component.
*
* @param parent The parent translation to apply to the component.
* @param ctx The canvas to add the quads to.
*/
void getQuads(const glm::vec2 parent, UICanvas &ctx);
public:

View File

@ -9,9 +9,8 @@ using namespace Dawn;
void UILabel::getSelfQuads(const glm::vec2 t, UICanvas &ctx) {
std::vector<struct UIShaderQuad> quads;
if(this->texture == nullptr) return;
if(this->texture == nullptr || this->text.empty()) return;
const std::wstring text = L"Hello World";
glm::vec2 position = t;
glm::vec4 quad;
@ -19,19 +18,37 @@ void UILabel::getSelfQuads(const glm::vec2 t, UICanvas &ctx) {
auto info = texture->getCharacterData(c);
ctx.addQuad(
{
position.x,
position.y,
position.x + info.size.x,
position.y + info.size.y
position.x + info.offset.x,
position.y + info.offset.y,
position.x + info.size.x + info.offset.x,
position.y + info.size.y + info.offset.y
},
{
info.quad.x,
info.quad.y,
info.quad.z,
info.quad.w
},
info.quad,
COLOR_WHITE,
UIShaderQuadStyle::FONT,
texture->texture
);
position += info.advance;
}
}
std::shared_ptr<TrueTypeTexture> UILabel::getFont() {
return this->texture;
}
std::wstring UILabel::getText() {
return this->text;
}
void UILabel::setFont(std::shared_ptr<TrueTypeTexture> texture) {
this->texture = texture;
}
void UILabel::setText(const std::wstring &text) {
this->text = text;
}

View File

@ -11,11 +11,38 @@ namespace Dawn {
class UILabel final : public UIComponent {
private:
std::shared_ptr<TrueTypeTexture> texture = nullptr;
std::wstring text = L"Hello World";
protected:
void getSelfQuads(const glm::vec2 t, UICanvas &ctx) override;
public:
/**
* Returns the font used for this label.
*
* @return The font used for this label.
*/
std::shared_ptr<TrueTypeTexture> getFont();
/**
* Returns the text used for this label.
*
* @return The text used for this label.
*/
std::wstring getText();
/**
* Sets the font to use for this label.
*
* @param texture TrueType texture to use for this label.
*/
void setFont(std::shared_ptr<TrueTypeTexture> texture);
/**
* Sets the text to use for this label.
*
* @param text The text to use for this label.
*/
void setText(const std::wstring &text);
};
}

View File

@ -13,6 +13,7 @@ void UIRectangle::getSelfQuads(const glm::vec2 t, UICanvas &ctx) {
glm::vec4(t, t + size),
uv,
color,
nullptr
UIShaderQuadStyle::TEXTURED,
texture
);
}

View File

@ -105,7 +105,7 @@ namespace Dawn {
* @return Rounded number.
*/
template<typename T>
static T round(float_t n) {
static T round(const float_t n) {
return (T)roundf(n);
}
@ -115,8 +115,41 @@ namespace Dawn {
* @return Rounded number.
*/
template<typename T>
static T floor(float_t n) {
static T floor(const float_t n) {
return (T)floorf(n);
}
/**
* Get the square root of a number.
*
* @param n Number to get the square root of.
* @return float_t
*/
static float_t sqrt(const float_t n) {
return sqrtf(n);
}
template<typename T>
static T ceil(const float_t n) {
return (T)ceilf(n);
}
/**
* Returns the next power of two for the given number.
*
* @param n Number to get the next power of two for.
* @return The next power of two.
*/
template<typename T>
static T nextPowerOfTwo(T n) {
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
};
}