使用 pangolin显示字符串文本信息

8 篇文章 0 订阅
1 篇文章 0 订阅

pangolin::GlText 显示字符串文本信息

1、运行效果

在这里插入图片描述

在这里插入图片描述

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();

    // Restore modelview / project matrices
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();

    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();

}



int main(  )
{
    //生成一个gui界面,定义大小
    pangolin::CreateWindowAndBind(win_name_, 640, 480, {{"default_font", "./fonts/heiti.ttf"}, {"default_font_size", "18"}});
    
    // create a window and bind its context to the main thread
    //pangolin::CreateWindowAndBind(win_name_, win_width_, win_height_, {{"default_font", "./fonts/heiti.ttf"}, {"default_font_size", "18"}});

    // 3D mouse handler requires depth testing to be enabled
    //进行深度测试,保证某视角下像素只有一种颜色,不混杂
    glEnable(GL_DEPTH_TEST);

    //fetch the context and bind it to this thread
    //pangolin::BindToContext(win_name_);

    // Issue specific OpenGl we might need
    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() )
    {
    
        //set 背景白色
        glClearColor(1.0, 1.0, 1.0, 1.0);
    
        //清除颜色缓冲和深度缓冲
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        d_cam.Activate(s_cam); //方案1:有效。 LMG: 该语句对于显示立方体有用

        //绘制立方体
        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;
    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Adunn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值