pangolin::GlText 显示字符串文本信息
1、运行效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/e1f13e888b754fc3a963bc6ea57c1d7c.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/b38f0e2c4382420cac3d353216932ac8.png)
2、实现代码
#include <pangolin/pangolin.h>
#include <pangolin/display/display.h>
#include <pangolin/gl/opengl_render_state.h>
#include <pangolin/display/default_font.h>
#include <pangolin/display/view.h>
const std::string win_name_ = "Data_Collection.UI";
const std::string d_cam3d_name_ = "Cam 3D";
void DrawText(pangolin::GlText& gltext, const float& cur_width, const float& cur_height,
const float& x_pos, const float& y_pos, const bool highlight)
{
std::cout << __FUNCTION__ << "()[" << __LINE__ << "] cur_width = " << cur_width;
std::cout << ", cur_height = " << cur_height << ", x_pos = " << x_pos << ", y_pos = " << y_pos << std::endl;
auto& text_view = pangolin::Display(d_cam3d_name_);
GLint view[4];
glGetIntegerv(GL_VIEWPORT, view);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, cur_width, 0, cur_height, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(x_pos, y_pos, 1.0);
if (highlight) glColor3ub(255, 0, 0);
else glColor3ub(0, 0, 0);
gltext.Draw();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
int main( )
{
pangolin::CreateWindowAndBind(win_name_, 640, 480, {{"default_font", "./fonts/heiti.ttf"}, {"default_font_size", "18"}});
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
pangolin::OpenGlRenderState s_cam(
pangolin::ProjectionMatrix(640,480,420,420,320,240,0.2,100),
pangolin::ModelViewLookAt(-2,2,-2, 0,0,0, pangolin::AxisY));
pangolin::Handler3D handler(s_cam);
pangolin::View& d_cam = pangolin::Display(d_cam3d_name_)
.SetBounds(0.0, 1.0, 0.0, 1.0, -640.0f/480.0f)
.SetHandler(&handler);
int index = 0;
while( !pangolin::ShouldQuit() )
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
d_cam.Activate(s_cam);
pangolin::glDrawColouredCube();
glLineWidth(3);
glBegin ( GL_LINES );
glColor3f ( 0.8f, 0.f, 0.f );
glVertex3f( -1, -1, -1 );
glVertex3f( 0, -1, -1 );
glColor3f( 0.f, 0.8f, 0.f);
glVertex3f( -1, -1, -1 );
glVertex3f( -1, 0, -1 );
glColor3f( 0.2f, 0.2f, 1.f);
glVertex3f( -1, -1, -1 );
glVertex3f( -1, -1, 0 );
glEnd();
auto& text_view = pangolin::Display(d_cam3d_name_);
const auto cur_width = text_view.v.w;
const auto cur_height = text_view.v.h;
pangolin::GlText gl_text;
index++;
if(index < 100)
{
gl_text = pangolin::default_font().Text("只有一种颜色2.");
DrawText(gl_text, cur_width, cur_height, cur_width - 200, cur_height - 300, false);
}
else if(index < 200)
{
gl_text = pangolin::default_font().Text("改了一下文本显示2.");
DrawText(gl_text, cur_width, cur_height, cur_width - 200, cur_height - 300, true);
}
else
{
index = 0;
}
pangolin::FinishFrame();
}
return 0;
}