55 constexpr std::size_t text_vertex_stride = (2 + 2 + 4) *
sizeof(
float);
61 m_vertex_array = std::make_unique<gl::vertex_array>(text_vertex_attributes);
64 m_vertex_buffer = std::make_unique<gl::vertex_buffer>();
109 if (m_direction != direction)
111 m_direction = direction;
118 if (m_content_u8 != content)
120 m_content_u8 = content;
132 void text::transformed()
136 for (std::size_t i = 0; i < 8; ++i)
144 void text::update_content()
147 if (!m_font || m_content_u32.empty())
150 m_local_bounds = {{0, 0, 0}, {0, 0, 0}};
156 auto vbo_min_size = m_content_u32.length() * text_vertex_stride * 6;
157 if (m_vertex_data.size() < vbo_min_size)
159 m_vertex_data.resize(vbo_min_size);
162 std::uint32_t visible_character_count =
static_cast<std::uint32_t
>(m_content_u32.length());
167 const auto& font_texture_dimensions = font_texture->get_image_view()->get_image()->get_dimensions();
173 m_local_bounds.
min = {std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity(), 0.0f};
174 m_local_bounds.
max = {-std::numeric_limits<float>::infinity(), -std::numeric_limits<float>::infinity(), 0.0f};
177 char32_t previous_code = 0;
178 float*
v =
reinterpret_cast<float*
>(m_vertex_data.data());
179 for (char32_t code: m_content_u32)
184 pen_position.
x() += m_font->
get_kerning(previous_code, code).
x();
194 positions[1] = {positions[0].
x(), positions[0].
y() - glyph->
metrics.
height};
195 positions[2] = {positions[0].
x() + glyph->
metrics.
width, positions[1].
y()};
196 positions[3] = {positions[2].
x(), positions[0].
y()};
197 positions[4] = positions[0];
198 positions[5] = positions[2];
202 uvs[0] = {
static_cast<float>(glyph->
position.
x()),
static_cast<float>(glyph->
position.
y())};
205 uvs[3] = {uvs[2].
x(), uvs[0].
y()};
209 for (
int i = 0;
i < 6; ++
i)
216 uvs[
i].
x() = uvs[
i].
x() /
static_cast<float>(font_texture_dimensions[0]);
217 uvs[
i].
y() = uvs[
i].
y() /
static_cast<float>(font_texture_dimensions[1]);
221 for (
int i = 0;
i < 6; ++
i)
223 *(
v++) = positions[i].
x();
224 *(
v++) = positions[i].
y();
237 for (
int i = 0;
i < 4; ++
i)
240 for (
int j = 0;
j < 2; ++
j)
242 m_local_bounds.
min[
j] = std::min<float>(m_local_bounds.
min[j],
position[j]);
243 m_local_bounds.
max[
j] = std::max<float>(m_local_bounds.
max[j],
position[j]);
249 --visible_character_count;
255 pen_position.
x() = 0.0f;
256 pen_position.
y() -= font_metrics.
linegap;
260 previous_code = code;
264 vbo_min_size = visible_character_count * text_vertex_stride * 6;
267 if (vbo_min_size > m_vertex_buffer->size())
269 m_vertex_buffer->resize(vbo_min_size, {m_vertex_data.data(), vbo_min_size});
273 m_vertex_buffer->write({m_vertex_data.data(), vbo_min_size});
283 void text::update_color()
285 std::byte*
v = m_vertex_data.data();
288 v += (2 + 2) *
sizeof(
float);
293 std::memcpy(v, m_color.
data(),
sizeof(
float) * 4);
295 v += text_vertex_stride;
299 m_vertex_buffer->write({m_vertex_data.data(), m_render_op.
vertex_count * text_vertex_stride});
constexpr const view_frustum_type & get_view_frustum() const noexcept
Returns the camera's view frustum.
constexpr std::uint32_t get_layer_mask() const noexcept
Returns the layer mask of the object.
constexpr const vector_type & get_translation() const noexcept
Returns the translation of the object.
constexpr const transform_type & get_transform() const noexcept
Returns the transform of the object.
void set_content(const std::string &content)
Sets the text content.
void render(render::context &ctx) const override
Adds render operations to a render context.
void set_direction(type::text_direction direction)
Sets the direction of the text.
void set_color(const math::fvec4 &color)
Sets the text color.
void refresh()
Manually updates the text object if its font has been updated or altered in any way.
void set_material(std::shared_ptr< render::material > material)
Sets the text material.
text()
Constructs a text object.
void set_font(const type::bitmap_font *font)
Sets the text font.
Raster font in which glyphs are stored as arrays of pixels.
const std::shared_ptr< gl::texture_2d > & get_texture() const noexcept
Returns a pointer to the glyph corresponding to a UTF-32 character code, or nullptr if no such glyph ...
const bitmap_glyph * get_glyph(char32_t code) const
Returns a pointer to the glyph corresponding to a UTF-32 character code, or nullptr if no such glyph ...
const math::fvec2 & get_kerning(char32_t first, char32_t second) const
Returns the kerning offset for a pair of glyphs.
const font_metrics & get_font_metrics() const
Returns metrics describing the font.
@ triangle_list
Separate triangle primitives.
constexpr vector< T, N > round(const vector< T, N > &x)
Performs a element-wise round operation.
@ color
Vertex color (vec4)
@ uv
Vertex UV texture coordinates (vec2)
@ position
Vertex position (vec3)
std::u32string u32(const std::string &u8)
Converts a UTF-8 string to UTF-32.
text_direction
Text writing direction.
constexpr T distance(const vector_type &point) const noexcept
Calculates the signed distance from the hyperplane to a point.
vector_type min
Minimum extent of the hyperrectangle.
vector_type max
Maximum extent of the hyperrectangle.
void extend(const vector_type &point) noexcept
Extends the hyperrectangle to include a point.
constexpr vector_type corner(std::size_t index) const noexcept
Returns the nth corner of the hyperrectangle.
constexpr const plane_type & near() const noexcept
Returns the near clipping plane.
constexpr element_type & x() noexcept
Returns a reference to the first element.
constexpr element_type & y() noexcept
Returns a reference to the second element.
constexpr element_type * data() noexcept
Returns a pointer to the element array.
static constexpr vector infinity() noexcept
Returns a vector of infinities, where every element is equal to infinity.
const scene::camera * camera
Pointer to the camera.
std::vector< const operation * > operations
Render operations generated by visible objects.
std::uint32_t vertex_count
std::uint32_t instance_count
std::size_t vertex_offset
std::size_t vertex_stride
std::uint32_t first_instance
gl::primitive_topology primitive_topology
std::uint32_t first_vertex
const gl::vertex_buffer * vertex_buffer
std::shared_ptr< render::material > material
const gl::vertex_array * vertex_array
Single glyph in a bitmap font.
glyph_metrics metrics
Metrics describing the glyph.
math::uvec2 position
Position of the packed glyph bitmap within the font bitmap.
Metrics describing properties of a font.
float linegap
Distance that must be placed between two lines of text.
float height
Vertical extent of the glyph.
float width
Horizontal extent of the glyph.
math::fvec2 horizontal_bearing
Offset from the pen position to the glyph's top-left edge, in horizontal layouts.
float horizontal_advance
Distance to move the pen position after the glyph has been rendered, in horizontal layouts.