Turning text strings into 3D surface meshes (or 2D outline contours) with TextMesher.
Overview
This tutorial converts the strings "Easy3D" and "Makes 3D Easy!" — in two different fonts — into real geometry that can be rendered, processed, or exported like any other mesh. Unlike Tutorial_309_TextRendering, which draws text as 2D image overlays, here the letters become actual triangles in a SurfaceMesh. A compile-time switch (#if 1 / #else) selects between the extruded 3D meshes and the flat 2D glyph contours drawn as colored lines.
The technique
TrueType fonts describe each glyph as closed 2D contours made of line segments and quadratic Bezier curves. To mesh a string, the mesher (1) scales and places each glyph's contours at the requested position and font size, (2) flattens the curves into polylines (the "quality" constructor parameter controls how finely), (3) triangulates the outlines — letters like "O" or "a" have holes, so this is polygon-with-holes triangulation, typically done via constrained Delaunay methods — and (4) extrudes the filled 2D region along the Z axis by the given height to obtain a solid 3D letter with front, back, and side walls. With collision_free = true, overlapping characters are united so the result has no self-intersections. Contour orientation matters: outer boundaries and hole boundaries wind in opposite directions, which is exactly what the alternative branch below visualizes (clockwise vs counter-clockwise contours in different colors).
Easy3D API
- TextMesher: constructed from a .ttf font file; set_font() switches fonts; generate() has three overloads — new SurfaceMesh, append to an existing mesh, or append 2D contours (std::vector<std::vector<Polygon2>>, one contour list per character).
- SurfaceMesh, Viewer, Renderer: the generated mesh is added as a model; Renderer::update() re-syncs the portable geometry snapshots after geometry was appended post-load.
- LinesDrawable, Polygon2: used in the contour branch to draw the outlines as thick colored lines (CONE impostors) with per-vertex colors.
- Camera::set_view_direction()/set_up_vector(): face the text straight-on (it lies in the XY plane and is extruded along the Z axis).
Running it
A window shows the two extruded text lines viewed from the front; navigate with the usual viewer controls to see the thickness of the letters. Flip the #if to #else (0) and rebuild to inspect the raw 2D contours instead: red = clockwise, green = counter-clockwise.
27#include <easy3d/core/surface_mesh.h>
28#include <easy3d/viewer/viewer.h>
29#include <easy3d/renderer/camera.h>
30#include <easy3d/renderer/renderer.h>
31#include <easy3d/renderer/drawable_lines.h>
32#include <easy3d/algo/text_mesher.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
88int main(
int argc,
char **argv) {
93 Viewer viewer(EXAMPLE_TITLE);
96 const std::string font_file = resource::directory() +
"/fonts/en_Earth-Normal.ttf";
103 auto mesh = mesher.generate(
"Easy3D", 0, 0, 48, 15,
true);
105 viewer.add_model(mesh);
109 mesher.set_font(resource::directory() +
"/fonts/en_Roboto-Regular.ttf");
111 mesher.generate(mesh.get(),
"Makes 3D Easy!", 350, 0, 25, 15,
true);
117 std::vector<std::vector<Polygon2>> contours;
118 mesher.set_font(font_file);
120 mesher.generate(
"Easy3D", 0, -60, 48, contours,
true);
121 mesher.set_font(resource::directory() +
"/fonts/en_Roboto-Regular.ttf");
123 mesher.generate(
"Makes 3D Easy!", 350, -60, 25, contours,
true);
128 std::vector<vec3> points, colors;
129 std::vector<unsigned int> indices;
131 for (
auto &cha : contours) {
132 for (
auto &con : cha) {
133 const vec3 c = con.is_clockwise() ?
vec3(1, 0, 0) :
vec3(0, 1, 0);
134 for (
int i = 0; i < con.size(); ++i) {
135 points.push_back(
vec3(con[i], 0.0f));
138 indices.push_back(offset + i);
139 indices.push_back(offset + (i + 1) % con.size());
141 offset += con.size();
155 viewer.add_drawable(std::shared_ptr<LinesDrawable>(d));
159 viewer.camera()->set_view_direction(
vec3(0, 0, -1));
160 viewer.camera()->set_up_vector(
vec3(0, 1, 0));
163 if (
auto *model = viewer.current_model())
164 model->renderer()->update();
void update_color_buffer(const std::vector< vec3 > &colors, bool dynamic=false)
Creates/Updates the color buffer.
Definition drawable.cpp:301
void update_vertex_buffer(const std::vector< vec3 > &vertices, bool dynamic=false)
Creates/Updates the vertex buffer.
Definition drawable.cpp:284
void update_element_buffer(const std::vector< unsigned int > &elements)
Updates the element buffer.
Definition drawable.cpp:322
The drawable for rendering a set of line segments, e.g., edges of a mesh, vector fields.
Definition drawable_lines.h:40
void set_impostor_type(ImposterType t)
Sets the type of the line imposter.
Definition drawable_lines.h:72
void set_line_width(float width)
Sets the line width in logical pixels (DPI-independent).
Definition drawable_lines.cpp:42
void set_property_coloring(Location color_location, const std::string &color_name="")
Constructs a scheme for rendering a drawable with per-element color.
Definition state.cpp:100
@ VERTEX
Property defined on vertices.
Definition state.h:69
Generate a 3D surface mesh from a text string.
Definition text_mesher.h:43
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
- See also
- TextMesher, SurfaceMesh, LinesDrawable, Polygon2, Tutorial_309_TextRendering