一般的ply文件是可以用OpenGL直接读取的,但是ply点云文件尝试之后却不太顺利。
于是转而查如何读取ply点云文件,查到的结果是安装pcl。除了不小心装错位数外,用它来读取和显示ply文件倒是蛮方便的。但是在后面想实现光照模型的时候没有在pcl里找到合适的函数,总想着用OpenGL调节模型更方便。
于是最后将ply文件转换为txt文件。其中保存了ply文件里的xyz坐标和rgba坐标。
如何看ply文件里有什么信息呢?打开方式选写字板看一下它的header就好啦
以下为这个环节的代码:
运行前提是先去安装pcl的教程,推荐一份 http://gyshgx868.github.io/2018/03/06/PointCloud/pcl-install/
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace pcl;
using namespace pcl::io;
using namespace pcl::console;
using namespace std;
int main()
{
pcl::PointCloud::Ptr cloud(new pcl::PointCloud());
if (pcl::io::loadPLYFile("F:\\3D human model.ply", *cloud) == -1) //* load the file
{
PCL_ERROR("Couldn't read the file \n");
system("PAUSE");
return (-1);
}
ofstream zos("F:\\shixiala.txt"); //包含rgba信息的txt点云
for (size_t i = 0; i < cloud->points.size(); i++)
zos << cloud->points[i].x << " " << cloud->points[i].y
<< " " << cloud->points[i].z << " " << int(cloud->points[i].r) << " " << int(cloud->points[i].g)
<< " " << int(cloud->points[i].b) <<" " << int(cloud->points[i].a) << endl;
cout << "done!" << endl;
cin.get();
system("PAUSE");
return (0);
}
2.OpenGL读取txt点云文件
读取txt点云的教程网上蛮多的,主要遇到的难点是显示有颜色信息的点云文件。
这里实现的功能有:
每个点可以是不同的颜色(如果你文件里的点的rgb值不同的话)
键盘上下左右移动模型
鼠标滚轮放大缩小模型
鼠标旋转模型
有两个灯的开关
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define GLUT_WHEEL_UP 3 //定义滚轮操作
#define GLUT_WHEEL_DOWN 4
typedef struct tagPoint3D
{
GLfloat x;
GLfloat y;
GLfloat z;
}Point3D;
typedef struct color
{
GLfloat r;
GLfloat g;
GLfloat b;
}rgb;
const float PI = 3.141592543f;
vector vec_pts;
vector vec_cols;
GLfloat eyeZ;
GLfloat centerX, centerY, centerZ;
GLfloat fWidth, fHeight;
GLfloat halfWidth, halfHeight;
GLfloat translateX, translateY;
GLfloat rotX, rotY;
GLfloat downPtX, downPtY;
bool leftBtnDown;
GLfloat tempEyeZ;
GLuint arb;
GLuint pt3DNum;
bool readFile2()
{
string filePath = "F:\\3dhumanmodelRGB.txt";
ifstream in(filePath.c_str());
if (!in)
{
printf("读取文件失败!");
return false;
}
char buf[80];
float a, b, c, r, g, bb;
float minX = 0.0f, maxX = 0.0f, minY = 0.0f, maxY = 0.0f, minZ = 0.0f, maxZ = 0.0f;
int index = 0;
while (!in.eof())
{
in.getline(buf, 80, '\n');
sscanf(buf, "%f %f %f %f %f %f", &a, &b, &c, &r, &g, &bb);
Point3D pt;
pt.x = a;
pt.y = b;
pt.z = c;
rgb col;
col.r = r;
col.g = g;
col.b = bb;
if(eyeZ maxX)
maxX = pt.x;
if (pt.y < minY)
minY = pt.y;
if (pt.y > maxY)
maxY = pt.y;
minZ += pt.z;
vec_pts.push_back(pt);
vec_cols.push_back(col);
index = 0;
}
in.close();
centerX = (maxX + minX)/2;
centerY = (maxY + minY)/2;
centerZ = minZ/vec_pts.size();
halfWidth = fabs(maxX) > fabs(minX) ? fabs(maxX) : fabs(minX);
halfHeight = fabs(maxY) > fabs(minY) ? fabs(maxY) : fabs(minY);
pt3DNum = vec_pts.size();
Point3D* pData = new Point3D[pt3DNum];
for (int i=0; i
例子:
读取了个人的模型