Easy3D 2.6.1
Loading...
Searching...
No Matches
Tutorial_309_TextRendering/main.cpp

This example shows how to render strings using Easy3D.

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 TutorialTextRendering : public easy3d::Viewer
37{
38public:
39 explicit TutorialTextRendering(const std::string& title);
40 ~TutorialTextRendering() 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:52
The built-in Easy3D viewer.
Definition viewer.h:63
Definition collider.cpp:182

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// \cond
38
39TutorialTextRendering::TutorialTextRendering(const std::string &title)
40 : Viewer(title)
41 , texter_(nullptr)
42 , font_size_delta_(0.0f)
43 , line_spacing_(0.0f)
44 , alignment_(TextRenderer::ALIGN_CENTER)
45 , upper_left_(true)
46{
47 manual_ =
48 "----------------------- Text Rendering usage ------------------------ \n"
49 "Press '+'/'-' to increase/decrease font size \n"
50 "Press '<'/'>' to increase/decrease character spacing \n"
51 "Press 'up'/'down' to increase/decrease line spacing \n"
52 "Press 'l'/'c'/'r' to left/center/right align the multi-line text \n"
53 "Press 'o' to switch the origin between 'upper left' and 'bottom left' \n"
54 "Press 'space' to enable/disable kerning \n"
55 "--------------------------------------------------------------------- \n";
56}
57
58
59TutorialTextRendering::~TutorialTextRendering() {
60 delete texter_;
61
62 // Not needed: it will be called in the destructor of the base class
63 //Viewer::cleanup();
64}
65
66
67void TutorialTextRendering::init() {
68 Viewer::init();
69
70 texter_ = new TextRenderer(dpi_scaling());
71
72 std::vector<std::string> files;
74 for (const auto& file : files) {
75 if (file_system::extension(file) == "ttf") {
76 texter_->add_font(resource::directory() + "/fonts/" + file);
77 colors_.push_back(random_color(true));
78 }
79 }
80
81#if 0
82 const auto& names = texter_->font_names();
83 std::cout << "available fonts: " << std::endl;
84 for (std::size_t i =0; i< names.size(); ++i)
85 std::cout << "\tfont " << i << ": " << names[i] << std::endl;
86#endif
87}
88
89
90bool TutorialTextRendering::key_press_event(int key, int modifiers) {
91 if (key == KEY_MINUS) {
92 font_size_delta_ = std::max(font_size_delta_ - 1.0f, -20.0f);
93 update();
94 return true;
95 }
96 else if (key == KEY_EQUAL) {
97 font_size_delta_ = std::min(font_size_delta_ + 1.0f, 250.0f);
98 update();
99 return true;
100 }
101
102 else if (key == KEY_COMMA) {
103 const float spacing = texter_->character_spacing();
104 texter_->set_character_spacing(std::max(spacing - 0.5f, 0.0f));
105 update();
106 return true;
107 }
108 else if (key == KEY_PERIOD) {
109 const float spacing = texter_->character_spacing();
110 texter_->set_character_spacing(std::min(spacing + 0.5f, 50.0f));
111 update();
112 return true;
113 }
114
115 else if (key == KEY_DOWN) {
116 line_spacing_ = std::max(line_spacing_ - 0.1f, -1.0f);
117 update();
118 return true;
119 }
120 else if (key == KEY_UP) {
121 line_spacing_ = std::min(line_spacing_ + 0.1f, 2.0f);
122 update();
123 return true;
124 }
125
126 else if (key == KEY_L) {
127 alignment_ = TextRenderer::ALIGN_LEFT;
128 update();
129 return true;
130 }
131
132 else if (key == KEY_C) {
133 alignment_ = TextRenderer::ALIGN_CENTER;
134 update();
135 return true;
136 }
137
138 else if (key == KEY_R) {
139 alignment_ = TextRenderer::ALIGN_RIGHT;
140 update();
141 return true;
142 }
143
144 else if (key == KEY_O) {
145 upper_left_ = !upper_left_;
146 update();
147 return true;
148 }
149
150 else if (key == KEY_SPACE) {
151 const bool kerning = texter_->kerning();
152 texter_->set_kerning(!kerning);
153 update();
154 return true;
155 }
156
157 else
158 return Viewer::key_press_event(key, modifiers);
159}
160
161
162void TutorialTextRendering::draw() const {
163 Viewer::draw();
164
165 if (!texter_ || texter_->num_fonts() == 0)
166 return;
167
168 const float font_size = 28.0f + font_size_delta_;
169 float x = 50.0f;
170 float y = 80.0f;
171
172 const auto num_fonts = texter_->num_fonts();
173 const float font_height = texter_->font_height(font_size);
174
175 auto rect = texter_->draw(
176 "This example shows how to render strings with Easy3D\n"
177 "'+'/'-': increase/decrease font size\n"
178 "'<'/'>': increase/decrease character spacing\n"
179 "'up'/'down': increase/decrease line spacing\n"
180 "'l'/'c'/'r': left/center/right align the multi-line text\n"
181 "'o': switch the origin between 'upper left' and 'bottom left'\n"
182 "'space': enable/disable kerning",
183 x * dpi_scaling(), y * dpi_scaling(), font_size, TextRenderer::Align(alignment_), 0, vec3(0, 0, 0),
184 line_spacing_, upper_left_);
185
186 // the new Y position to start; add extra space
187 y += rect.height();
188
189 float next_x = 0.0f;
190 for (int i = 0; i < num_fonts; ++i) {
191 if (i % 2 == 0) {
192 next_x = texter_->draw(std::to_string(i) + " - Easy3D makes 3D easy! ", x * dpi_scaling(),
193 y * dpi_scaling(), font_size, i, colors_[i], upper_left_);
194 } else {
195 texter_->draw(std::to_string(i) + " - I Love Easy3D!", next_x * dpi_scaling(), y * dpi_scaling(), font_size,
196 i, colors_[i], upper_left_);
197 y += font_height * 1.5f;
198 }
199 }
200}
201
202// \endcond
Align
Horizontal alignment.
Definition text_renderer.h:111
@ ALIGN_CENTER
Align text to the center.
Definition text_renderer.h:114
@ ALIGN_LEFT
Align text to the left.
Definition text_renderer.h:112
@ ALIGN_RIGHT
Align text to the right.
Definition text_renderer.h:113
void get_directory_entries(const std::string &path, std::vector< std::string > &entries, bool recursive)
Queries the entries of a directory (including subdirectories and files).
std::string extension(const std::string &path, bool lower=true)
Query the file extension without dot (e.g., /a/b/c.Ext => Ext).
std::string directory()
Returns the resource directory (containing color maps, shaders, textures, fonts, etc....
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
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
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
41
42using namespace easy3d;
43
44
45int main(int argc, char **argv) {
46 // initialize Easy3D.
47 initialize();
48
49 const std::string file_name = resource::directory() + "/data/sphere.obj";
50
51 TutorialTextRendering viewer(EXAMPLE_TITLE);
52 viewer.add_model(file_name);
53
54 // run the viewer
55 return viewer.run();
56}
57
void initialize(bool info_to_stdout, bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition initializer.cpp:39