Pangolin使用笔记
pangolin::CreateWindowAndBind(std::string window_title, int w = 640, int h = 480, const Params& params = Params())
初始化这个图形展示窗口
geEable(GL_DEPTH_TEST)
启用深度渲染,当需要显示3D模型时需要打开,根据目标的远近自动隐藏被遮挡的模型
glEnable(GL_BLEND);
表示窗口使用颜色混合模式,让物体显示半透明效
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
设置颜色RGBA混合因子,前面参数表示源因子,后面参数表示目标因子
GL_ZERO:表示使用0.0作为权重因子
GL_ONE : 表示使用1.0作为权重因子
GL_SRC_ALPHA :表示使用源颜色的alpha值来作为权重因子
GL_DST_ALPHA :表示使用目标颜色的alpha值来作为权重因子
GL_ONE_MINUS_SRC_ALPHA: 表示用1.0-源颜色的alpha的值来作为权重因子
GL_ONE_MINUS_DST_ALPHA: 表示用1.0-目标颜色的alpha的值来作为权重因子
GL_SRC_COLOR>:表示用源颜色的四个分量作为权重因子
在画图的过程中如果程序glClearColor();glColor3f()则后者为源颜色,前者的颜色为目标颜色以上的GL_SRC_ALPHA和GL_ONE_MINUS_SRC_ALPHA是最常用的混合模式
ProjectionMatrix()
设置相机内参,以及能看到的最大最小距离
如ProjectionMatrix(1024, 768, 500, 500, 512, 389, 0.1, 1000)
表示相机分辨率,焦距,相机光心,最远最大距离
ModelViewLookAt()
设置观看视角,pangolin::ModelViewLookAt(0, -0.1, -1.8, 0, 0, 0, 0.0, -1.0, 0.0));
的意思是在世界坐标(0,-0.1,-1.8)出观看坐标原点(0,0,0)并设置Y轴向上
glClear()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//将缓存清除为原先设定值
以下为完整代码
#include <pangolin/pangolin.h>
#include <Eigen/Core>
#include <iostream>
using namespace std;
using namespace Eigen;
string trajectory_file = "./data/trajectory.txt";
void DrawTrajectory(vector<Isometry3d, Eigen::aligned_allocator<Isometry3d>>);
int main(int argc, char **argv)
{
vector<Isometry3d, Eigen::aligned_allocator<Isometry3d>> poses;
ifstream fin(trajectory_file);
if (!fin)
{
cout << "cannot find trajectory file at " << trajectory_file << endl;
return 1;
}
while (!fin.eof())
{
double time, tx, ty, tz, qx, qy, qz, qw;
fin >> time >> tx >> ty >> tz >> qx >> qy >> qz >> qw;
Isometry3d Twr(Quaterniond(qw, qx, qy, qz));
Twr.pretranslate(Vector3d(tx, ty, tz));
poses.push_back(Twr);
}
cout << "read total " << poses.size() << " pose entries" << endl;
// draw trajectory in pangolin
DrawTrajectory(poses);
return 0;
}
void DrawTrajectory(vector<Isometry3d, Eigen::aligned_allocator<Isometry3d>> poses)
{
// create pangolin window and plot the trajectory
pangolin::CreateWindowAndBind("Trajectory Viewer", 1024, 768);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);//设置混合模式
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);//设置源权重因子,目标权重因子
pangolin::OpenGlRenderState s_cam(
pangolin::ProjectionMatrix(1024, 768, 500, 500, 512, 389, 0.1, 1000),//设置相机内参
pangolin::ModelViewLookAt(0, -0.1, -1.8, 0, 0, 0, 0.0, -1.0, 0.0));
pangolin::View &d_cam = pangolin::CreateDisplay()
.SetBounds(0.0, 1.0, 0.0, 1.0, -1024.0f / 768.0f)
.SetHandler(new pangolin::Handler3D(s_cam));
while (pangolin::ShouldQuit() == false)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //将缓存清除为原先设定值
d_cam.Activate(s_cam);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); //设置背景颜色为白色,这里时目标颜色
glLineWidth(2);
for (size_t i = 0; i < poses.size(); i++)
{
// 画每个位姿的三个坐标轴
Vector3d Ow = poses[i].translation();
Vector3d Xw = poses[i] * (0.1 * Vector3d(1, 0, 0));
Vector3d Yw = poses[i] * (0.1 * Vector3d(0, 1, 0));
Vector3d Zw = poses[i] * (0.1 * Vector3d(0, 0, 1));
glBegin(GL_LINES); //开始画线
glColor3f(1.0, 0.0, 0.0); //设置颜色 RGB
glVertex3d(Ow[0], Ow[1], Ow[2]);//设置线的顶点坐标
glVertex3d(Xw[0], Xw[1], Xw[2]);
glColor3f(0.0, 1.0, 0.0);
glVertex3d(Ow[0], Ow[1], Ow[2]);
glVertex3d(Yw[0], Yw[1], Yw[2]);
glColor3f(0.0, 0.0, 1.0);
glVertex3d(Ow[0], Ow[1], Ow[2]);
glVertex3d(Zw[0], Zw[1], Zw[2]);
glEnd(); //画线结束
}
// 画出连线
for (size_t i = 0; i < poses.size(); i++)
{
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
auto p1 = poses[i], p2 = poses[i + 1];
glVertex3d(p1.translation()[0], p1.translation()[1], p1.translation()[2]);
glVertex3d(p2.translation()[0], p2.translation()[1], p2.translation()[2]);
glEnd();
}
pangolin::FinishFrame();//交换帧和处理事件
usleep(5000); // sleep 5 ms
}
}
文章引用于
https://github.com/gaoxiang12/slambook2/blob/master/ch3/examples/plotTrajectory.cpp
http://blog.chinaunix.net/uid-20622737-id-2850251.html