Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_310_TextMesher

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

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.

1
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>
35
86using namespace easy3d;
87
88int main(int argc, char **argv) {
89 // initialize Easy3D.
90 initialize();
91
92 // Create an Easy3D viewer.
93 Viewer viewer(EXAMPLE_TITLE);
94
95 // Create an instance of the mesher by specifying a font file.
96 const std::string font_file = resource::directory() + "/fonts/en_Earth-Normal.ttf";
97 TextMesher mesher(font_file);
98
99#if 1 // extract and visualize the mesh model of the text
100 // Generate a surface mesh for "Easy3D".
101 // Arguments: text, start position (x, y) in the text plane, font size, extrusion height
102 // along Z, and collision_free (unite overlapping characters into one clean solid).
103 auto mesh = mesher.generate("Easy3D", 0, 0, 48, 15, true);
104 if (mesh)
105 viewer.add_model(mesh); // Add the mesh to the viewer.
106 // Generate a surface mesh for "Makes 3D Easy!".
107 // Switch to a second font, then APPEND the second string to the same mesh (note the
108 // mesh-first overload of generate), offset in x so the two lines sit side by side.
109 mesher.set_font(resource::directory() + "/fonts/en_Roboto-Regular.ttf");
110 if (mesh)
111 mesher.generate(mesh.get(), "Makes 3D Easy!", 350, 0, 25, 15, true);
112
113#else // extract and visualize the 2D contours of the text
114 // Alternative view: no triangulation/extrusion — just the raw glyph outlines.
115 // One entry per character; each character may consist of several closed contours
116 // (e.g. the letter "B" has an outer boundary plus two holes).
117 std::vector<std::vector<Polygon2>> contours;
118 mesher.set_font(font_file);
119 // Generate the contours for "Easy3D".
120 mesher.generate("Easy3D", 0, -60, 48, contours, true);
121 mesher.set_font(resource::directory() + "/fonts/en_Roboto-Regular.ttf");
122 // Generate the contours for "Makes 3D Easy!".
123 mesher.generate("Makes 3D Easy!", 350, -60, 25, contours, true);
124
125 // Prepare data for the rendering drawable.
126 // Flatten all contours into one point list plus line-segment indices. The winding
127 // direction distinguishes outer boundaries from holes: color them differently.
128 std::vector<vec3> points, colors;
129 std::vector<unsigned int> indices;
130 int offset = 0;
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)); // 2D contour points live in the z = 0 plane
136 colors.push_back(c);
137 // Each segment connects point i to point i+1, wrapping around to close the loop.
138 indices.push_back(offset + i);
139 indices.push_back(offset + (i + 1) % con.size());
140 }
141 offset += con.size();
142 }
143 }
144 // Create the rendering drawable and copy data into its portable geometry snapshot.
146 d->update_vertex_buffer(points);
147 d->update_color_buffer(colors);
148 d->update_element_buffer(indices);
149 // CONE impostors render thick, smoothly shaded lines instead of 1-pixel GL lines.
150 d->set_impostor_type(LinesDrawable::CONE);
151 d->set_line_width(3);
152 // Use the per-vertex colors updated above (winding-direction coloring).
154 // Add the drawable to the viewer.
155 viewer.add_drawable(std::shared_ptr<LinesDrawable>(d));
156#endif
157
158 // We always want to look at the front of the meshed text.
159 viewer.camera()->set_view_direction(vec3(0, 0, -1));
160 viewer.camera()->set_up_vector(vec3(0, 1, 0));
161
162 // The mesher appended geometry after attaching the model; refresh its portable geometry snapshot and fit both strings.
163 if (auto *model = viewer.current_model())
164 model->renderer()->update();
165
166 return viewer.run();
167}
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