效果:
代码:
#include <iostream>
#include <pangolin/pangolin.h>
// pangolin 绘制文字demo
using namespace std;
int main() {
//创建一个窗口
pangolin::CreateWindowAndBind("PangolinShowText", 640, 480);
// 定义字体
pangolin::GlFont * text_font = new pangolin::GlFont("./DejaVuSans.ttf", 25.0);
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, 320, 0.2, 100),
pangolin::ModelViewLookAt(-2, 2, -2, 0, 0, 0, pangolin::AxisY)
);
pangolin::Handler3D handler(s_cam);
pangolin::View & d_cam = pangolin::CreateDisplay()
.SetBounds(0.0, 1.0, 0.0, 1.0)
.SetHandler(&handler);
string text = "Hello,Pangolin!";
while( ! pangolin::ShouldQuit()){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
d_cam.Activate(s_cam);
// 在对应位置画字体
glColor3f(1.0,0.0,0.0);
text_font->Text(text.c_str()).Draw(3, 1, 1);
// ---
auto gltext_label_global_ = text_font->Text(text);
auto cur_width = d_cam.v.w;
auto cur_height = d_cam.v.h;
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(5, gltext_label_global_.Height(), 1.);//
glColor3ub(127, 127, 127);
gltext_label_global_.Draw();
// Restore modelview / project matrices
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
// ---
pangolin::glDrawColouredCube();
pangolin::FinishFrame();
}
return 0;
}
参考
博客
高博开源代码