最近在研究3D显示,找到一款在winform上展示3D点云的控件,并且实现了点线面的展示,及光照渲染纹理贴图等功能,如下面几张图所展示。
一些基础知识可以在LearnOpenTK - OpenTK 这个网站上学习到。
我这边使用的是openTK3.3.3版本,展示点云很好实现,把读到的点云数据放进去即可展示。如下图展示点云。
我们需要把点连成面,有很多算法可以实现,但我拿到的图为一张tif格式16位的深度图,如下图
快速获取此图的深度信息
IntPtr pointer = model.ImgDepth.GetImagePointer1(out string type, out width, out height);
length = width * height;
short[] pointZCloud = new short[length];
Marshal.Copy(pointer, pointZCloud, 0, length);
point3Ds = new Vector3[length];
normals = new Vector3[length];
zGoodList = new List<float>();
for (int i = 0; i < length; i++)
{
int row = i / width;
int col = i % width;
byte[] bytes = BitConverter.GetBytes(pointZCloud[i]);
ushort z = BitConverter.ToUInt16(bytes, 0);
float tempZ = z * model.ScaleZ +model.OffsetZ;
point3Ds[i] = new Vector3(row * model.ScaleY, col * model.ScaleX, tempZ);
if (z > 0)
{
zGoodList.Add(tempZ);
}
}
我们很容易知道哪三个点可以连成一个面,如某个点为(row,col),和它组成三角形的另外两个点为(row+1,col),(row,col+1),如下图组成一个个三角形,即可形成面
图中存在一个空的点,可通过阈值分割过滤掉,然后如何保证某个点必然存在后面两个和它构成三角形的点,用一个2x2的结构元腐蚀阈值分割出的区域,得到剩下所有的点必然每个点可以构成三角形,这种得到三角形的算法效率会非常高。
model.ImgDepth.Threshold((double)1, 65535).ErosionRectangle1(2, 2).GetRegionPoints(out HTuple rows1, out HTuple columns1);
long[] rows1Int = rows1.LArr;
long[] columns1Int = columns1.LArr;
int triLength = rows1Int.Length;
indices_Triangle = new uint[triLength * 6];
for (int i = 0; i < triLength; i++)
{
uint a1 = (uint)(rows1Int[i] * width + columns1Int[i]);
uint a2 = a1 + 1;
uint a3 = a1 + (uint)width;
uint a4 = a3 + 1;
indices_Triangle[i * 6] = a1;
indices_Triangle[i * 6 + 1] = a2;
indices_Triangle[i * 6 + 2] = a3;
indices_Triangle[i * 6 + 3] = a2;
indices_Triangle[i * 6 + 4] = a3;
indices_Triangle[i * 6 + 5] = a4;
}
不同高度渲染颜色从红色到蓝色
objColors = new float[length * 3];
for (int i = 0; i < length; i++)
{
if (point3Ds[i].Z > zMid)
{
objColors[i * 3] = (point3Ds[i].Z - zMid) / zHalfHigh;
objColors[i * 3 + 1] = 1.0f - objColors[i * 3];
objColors[i * 3 + 2] = 0;
}
else
{
objColors[i * 3] = 0;
objColors[i * 3 + 1] = 1.0f - (zMid - point3Ds[i].Z) / zHalfHigh;
objColors[i * 3 + 2] = (zMid - point3Ds[i].Z) / zHalfHigh;
}
}
然后还有光照,无光照和有光照效果差异很大,处理光照核心在于得到每个点的法向量,用上面的三角形的两条边构成的向量叉乘,及得到这个点的法向量,输入给着色器处理得到光照的渲染效果,如下面两张图对比
叉乘如下代码
Vector3 a1 = new Vector3(point3Ds[i].X, point3Ds[i].Y, point3Ds[i].Z);
Vector3 a2 = new Vector3(point3Ds[i + 1].X, point3Ds[i + 1].Y, point3Ds[i + 1].Z);
Vector3 a3 = new Vector3(point3Ds[i + width].X, point3Ds[i + width].Y, point3Ds[i + width].Z);
Vector3 b1 = a3 - a1;
Vector3 b2 = a2 - a1;
normals[i] = new Vector3(b1.Y * b2.Z - b2.Y * b1.Z, b1.Z * b2.X - b1.X * b2.Z, b1.X * b2.Y - b1.Y * b2.X);