

plotTrajectory.cpp
#include <pangolin/pangolin.h>
#include <thread>
//https://blog.csdn.net/weixin_43991178/article/details/105119610
static const std::string window_name = "HelloPangolinThreads";
void setup() {
// create a window and bind its context to the main thread
pangolin::CreateWindowAndBind(window_name, 640, 480);
// enable depth
glEnable(GL_DEPTH_TEST);
// unset the current context from the main thread
//但这个视窗实在主线程中创建的,因此在主线程调用玩后,需要使用GetBoundWindow()->RemoveCurrent()将其解绑。
pangolin::GetBoundWindow()->RemoveCurrent();
}
void run() {
// fetch the context and bind it to this thread
//首先使用BindToContext()函数将之前解绑的视窗绑定到当前线程
pangolin::BindToContext(window_name);
// we manually need to restore the properties of the context
glEnable(GL_DEPTH_TEST);
// Define Projection and initial ModelView matrix
// 创建相机观察
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) //窗口初始看向位置
);
// Create Interactive View in window
// 创建交互窗口 //交互式视图(view)用于显示上一步摄像机所“拍摄”到的内容
pangolin::Handler3D handler(s_cam); //交互相机视图句柄
pangolin::View& d_cam = pangolin::CreateDisplay()
//setBounds()函数前四个参数依次表示视图在视窗中的范围(下、上、左、右),可以采用相对坐标(0~1)以及绝对坐标(使用Attach对象)。
.SetBounds(0.0, 1.0, 0.0, 1.0, -640.0f/480.0f)
.SetHandler(&handler);
while( !pangolin::ShouldQuit() )
{
// Clear screen and activate view to render into 清楚上一张颜色和深度缓存 不重影子
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
d_cam.Activate(s_cam);
// Render OpenGL Cube 创建立方体
pangolin::glDrawColouredCube();
// Swap frames and Process Events // 运行帧循环以推进窗口事件
pangolin::FinishFrame();
}
// unset the current context from the main thread
//在线程结束时,我们需要解绑视窗。
pangolin::GetBoundWindow()->RemoveCurrent();
}
int main( int /*argc*/, char** /*argv*/ )
{
// create window and context in the main thread
setup();
// use the context in a separate rendering thread
std::thread render_loop;
render_loop = std::thread(run);
render_loop.join();
return 0;
}
CMakeLists.txt
# cmake needs this line
cmake_minimum_required(VERSION 3.1)
# Define project name
project(Pangolin_project)
#添加Pangolin画图依赖库
find_package(Pangolin REQUIRED)
include_directories(${Pangolin_INCLUDE_DIRS})
#编译可执行文件
add_executable(plotTrajectory plotTrajectory.cpp)
#连接Pangolin库
target_link_libraries(plotTrajectory ${Pangolin_LIBRARIES})
执行
# 创建编译文件夹(在Pangolin文件夹下) mkdir build && cd build # 配置编译选项 cmake .. # 开始编译代码 cmake --build . # 安装 sudo make install
浙公网安备 33010602011771号