28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
36int main(
int argc,
char **argv) {
40 const std::string file_name = resource::directory() +
"/data/sphere.obj";
42 TextRendering viewer(EXAMPLE_TITLE);
43 viewer.add_model(file_name);
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
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>
38TextRendering::TextRendering(
const std::string &title)
41 , font_size_delta_(0.0f)
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";
58TextRendering::~TextRendering() {
66void TextRendering::init() {
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);
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;
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);
95 else if (key == KEY_EQUAL) {
96 font_size_delta_ = std::min(font_size_delta_ + 1.0f, 250.0f);
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));
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));
114 else if (key == KEY_DOWN) {
115 line_spacing_ = std::max(line_spacing_ - 0.1f, -1.0f);
119 else if (key == KEY_UP) {
120 line_spacing_ = std::min(line_spacing_ + 0.1f, 2.0f);
125 else if (key == KEY_L) {
126 alignment_ = TextRenderer::ALIGN_LEFT;
131 else if (key == KEY_C) {
132 alignment_ = TextRenderer::ALIGN_CENTER;
137 else if (key == KEY_R) {
138 alignment_ = TextRenderer::ALIGN_RIGHT;
143 else if (key == KEY_O) {
144 upper_left_ = !upper_left_;
149 else if (key == KEY_SPACE) {
150 const bool kerning = texter_->kerning();
151 texter_->set_kerning(!kerning);
157 return Viewer::key_press_event(key, modifiers);
161void TextRendering::draw()
const {
164 if (!texter_ || texter_->num_fonts() == 0)
167 const float font_size = 28.0f + font_size_delta_;
171 const auto num_fonts = texter_->num_fonts();
172 const float font_height = texter_->font_height(font_size);
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",
183 line_spacing_, upper_left_);
186 y += (font_height * 1.5f + line_spacing_) * 5;
189 for (
int i = 0; i < num_fonts; ++i) {
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_);
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;
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