Easy3D 2.5.3
Tutorial_309_TextRendering

The source file containing the main() function:

1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#include "viewer.h"
28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
30
31
32using namespace easy3d;
33
34// This example shows how to render strings using Easy3D.
35
36int main(int argc, char **argv) {
37 // initialize Easy3D.
38 initialize();
39
40 const std::string file_name = resource::directory() + "/data/sphere.obj";
41
42 TextRendering viewer(EXAMPLE_TITLE);
43 viewer.add_model(file_name);
44
45 // run the viewer
46 return viewer.run();
47}
48
Definition: collider.cpp:182
void initialize(bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition: initializer.cpp:35

The header file of the viewer class:

1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_TUTORIAL_TEXT_RENDERING_H
28#define EASY3D_TUTORIAL_TEXT_RENDERING_H
29
30#include <easy3d/viewer/viewer.h>
31
32namespace easy3d {
33 class TextRenderer;
34}
35
36class TextRendering : public easy3d::Viewer
37{
38public:
39 explicit TextRendering(const std::string& title);
40 ~TextRendering() override;
41
42protected:
43 void draw() const override;
44 void init() override;
45 bool key_press_event(int key, int modifiers) override;
46
47private:
48 easy3d::TextRenderer* texter_;
49 std::vector< easy3d::vec3> colors_;
50 float font_size_delta_;
51 float line_spacing_;
52 int alignment_;
53 bool upper_left_;
54};
55
56
57#endif // EASY3D_TUTORIAL_TEXT_RENDERING_H
TextRenderer enables quick and easy string rendering in OpenGL applications.
Definition: text_renderer.h:53
The built-in Easy3D viewer.
Definition: viewer.h:61

The source file of the viewer class:

1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#include "viewer.h"
28
29#include <easy3d/renderer/text_renderer.h>
30#include <easy3d/util/file_system.h>
31#include <easy3d/util/resource.h>
32#include <easy3d/core/random.h>
33
34
35using namespace easy3d;
36
37
38TextRendering::TextRendering(const std::string &title)
39 : Viewer(title)
40 , texter_(nullptr)
41 , font_size_delta_(0.0f)
42 , line_spacing_(0.0f)
43 , alignment_(TextRenderer::ALIGN_CENTER)
44 , upper_left_(true)
45{
46 usage_string_ =
47 "----------------------- Text Rendering usage ------------------------ \n"
48 "Press '+'/'-' to increase/decrease font size \n"
49 "Press '<'/'>' to increase/decrease character spacing \n"
50 "Press 'up'/'down' to increase/decrease line spacing \n"
51 "Press 'l'/'c'/'r' to left/center/right align the multi-line text \n"
52 "Press 'o' to switch the origin between 'upper left' and 'bottom left' \n"
53 "Press 'space' to enable/disable kerning \n"
54 "--------------------------------------------------------------------- \n";
55}
56
57
58TextRendering::~TextRendering() {
59 delete texter_;
60
61 // Not needed: it will be called in the destructor of the base class
62 //Viewer::cleanup();
63}
64
65
66void TextRendering::init() {
67 Viewer::init();
68
69 texter_ = new TextRenderer(dpi_scaling());
70
71 std::vector<std::string> files;
72 file_system::get_directory_entries(resource::directory() + "/fonts/", files, false);
73 for (const auto& file : files) {
74 if (file_system::extension(file) == "ttf") {
75 texter_->add_font(resource::directory() + "/fonts/" + file);
76 colors_.push_back(random_color(true));
77 }
78 }
79
80#if 0
81 const auto& names = texter_->font_names();
82 std::cout << "available fonts: " << std::endl;
83 for (std::size_t i =0; i< names.size(); ++i)
84 std::cout << "\tfont " << i << ": " << names[i] << std::endl;
85#endif
86}
87
88
89bool TextRendering::key_press_event(int key, int modifiers) {
90 if (key == KEY_MINUS) {
91 font_size_delta_ = std::max(font_size_delta_ - 1.0f, -20.0f);
92 update();
93 return true;
94 }
95 else if (key == KEY_EQUAL) {
96 font_size_delta_ = std::min(font_size_delta_ + 1.0f, 250.0f);
97 update();
98 return true;
99 }
100
101 else if (key == KEY_COMMA) {
102 const float spacing = texter_->character_spacing();
103 texter_->set_character_spacing(std::max(spacing - 0.5f, 0.0f));
104 update();
105 return true;
106 }
107 else if (key == KEY_PERIOD) {
108 const float spacing = texter_->character_spacing();
109 texter_->set_character_spacing(std::min(spacing + 0.5f, 50.0f));
110 update();
111 return true;
112 }
113
114 else if (key == KEY_DOWN) {
115 line_spacing_ = std::max(line_spacing_ - 0.1f, -1.0f);
116 update();
117 return true;
118 }
119 else if (key == KEY_UP) {
120 line_spacing_ = std::min(line_spacing_ + 0.1f, 2.0f);
121 update();
122 return true;
123 }
124
125 else if (key == KEY_L) {
126 alignment_ = TextRenderer::ALIGN_LEFT;
127 update();
128 return true;
129 }
130
131 else if (key == KEY_C) {
132 alignment_ = TextRenderer::ALIGN_CENTER;
133 update();
134 return true;
135 }
136
137 else if (key == KEY_R) {
138 alignment_ = TextRenderer::ALIGN_RIGHT;
139 update();
140 return true;
141 }
142
143 else if (key == KEY_O) {
144 upper_left_ = !upper_left_;
145 update();
146 return true;
147 }
148
149 else if (key == KEY_SPACE) {
150 const bool kerning = texter_->kerning();
151 texter_->set_kerning(!kerning);
152 update();
153 return true;
154 }
155
156 else
157 return Viewer::key_press_event(key, modifiers);
158}
159
160
161void TextRendering::draw() const {
162 Viewer::draw();
163
164 if (!texter_ || texter_->num_fonts() == 0)
165 return;
166
167 const float font_size = 28.0f + font_size_delta_;
168 float x = 50.0f;
169 float y = 80.0f;
170
171 const auto num_fonts = texter_->num_fonts();
172 const float font_height = texter_->font_height(font_size);
173
174 texter_->draw(
175 "This example shows how to render strings with Easy3D"
176 "\n'+'/'-': increase/decrease font size"
177 "\n'<'/'>': increase/decrease character spacing"
178 "\n'up'/'down': increase/decrease line spacing"
179 "\n'l'/'c'/'r': left/center/right align the multi-line text"
180 "\n'o': switch the origin between 'upper left' and 'bottom left'"
181 "\n'space': enable/disable kerning",
182 x * dpi_scaling(), y * dpi_scaling(), font_size, TextRenderer::Align(alignment_), 0, vec3(0, 0, 0),
183 line_spacing_, upper_left_);
184
185 // the new Y position to start; add extra space
186 y += (font_height * 1.5f + line_spacing_) * 5;
187
188 float next_x = 0.0f;
189 for (int i = 0; i < num_fonts; ++i) {
190 if (i % 2 == 0) {
191 next_x = texter_->draw(std::to_string(i) + " - Easy3D makes 3D easy! ", x * dpi_scaling(),
192 y * dpi_scaling(), font_size, i, colors_[i], upper_left_);
193 } else {
194 texter_->draw(std::to_string(i) + " - I Love Easy3D!", next_x * dpi_scaling(), y * dpi_scaling(), font_size,
195 i, colors_[i], upper_left_);
196 y += font_height * 1.5f;
197 }
198 }
199}
Align
Horizontal alignment.
Definition: text_renderer.h:108
vec3 random_color(bool allow_dark=false)
Generates a random color. The parameter allow_dark controls if too dark colors are allowed.
Definition: random.h:49